简体   繁体   English

在 ECharts 的 X 轴上使用 'time' 类型进行时间序列图

[英]Using 'time' type on X axis of ECharts for timeseries graphing

I'm using ECharts 4.0.4 ( http://echarts.baidu.com/ ) to graph some sensor data with timestamps on the X axis.我正在使用 ECharts 4.0.4 ( http://echarts.baidu.com/ ) 在 X 轴上绘制一些带有时间戳的传感器数据。

Have tried with legacy series data and datasets (new on v4), but 'time' axis type won't work properly.已尝试使用旧系列数据和数据集(v4 上的新功能),但“时间”轴类型无法正常工作。 With 'category' it works fine:使用“类别”可以正常工作:

 var myChart = echarts.init(document.getElementById('main')); var option = { legend: {}, tooltip: { trigger: 'axis', }, dataset: { source: { timestamp: ['2018-04-10T20:40:33Z', '2018-04-10T20:40:53Z', '2018-04-10T20:41:03Z'], sensor1: [1, 2, 4], sensor2: [5, 3, 2] } }, xAxis: { type: 'category' }, yAxis: { }, series: [ { type: 'line'}, { type: 'line'} ], }; myChart.setOption(option);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.0.4/echarts.min.js"></script> <div id="main" style="width: 500px;height:400px;"></div>

With 'time' it doesn't:有了“时间”,它不会:

 var myChart = echarts.init(document.getElementById('main')); var option = { legend: {}, tooltip: { trigger: 'axis', }, dataset: { source: { timestamp: ['2018-04-10T20:40:33Z', '2018-04-10T20:40:53Z', '2018-04-10T20:41:03Z'], sensor1: [1, 2, 4], sensor2: [5, 3, 2] } }, xAxis: { type: 'time' }, yAxis: { }, series: [ { type: 'line'}, { type: 'line'} ], }; myChart.setOption(option);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.0.4/echarts.min.js"></script> <div id="main" style="width: 500px;height:400px;"></div>

I even tried using dimensions (which features the type for each series):我什至尝试使用尺寸(每个系列的类型):

 var myChart = echarts.init(document.getElementById('main')); var option = { legend: {}, tooltip: { trigger: 'axis', }, dataset: { source: [ ['2018-04-10T20:40:33Z', 1, 5], ['2018-04-10T20:40:53Z', 2, 3], ['2018-04-10T20:41:03Z', 4, 2] ] }, xAxis: { type: 'time' }, yAxis: { }, series: { type: 'line', dimensions: [ {name: 'timestamp', type: 'time'}, {name: 'sensor1', type: 'float'}, {name: 'sensor2', type: 'float'} ] }, }; myChart.setOption(option);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.0.4/echarts.min.js"></script> <div id="main" style="width: 500px;height:400px;"></div>

No good, it shows just one series (and with broken tooltips).不好,它只显示一个系列(并且带有损坏的工具提示)。 And by using dimensions, the layout of my data needs to be inverted, which is not good, as getting data from a JSON endpoint would be better in the previous way.并且通过使用维度,我的数据的布局需要反转,这并不好,因为从 JSON 端点获取数据会更好地采用以前的方式。

The time axis example on ECharts demo page uses a diferent data format for a datapoint (for a single series): ECharts 演示页面上的时间轴示例对数据点使用不同的数据格式(对于单个系列):

point = {
  name: 'Sun Jul 23 2000 00:00:00 GMT-0300 (-03)',
  value: [
    '2000/7/23',   // X data (timestamp)
    100            // Y data
  ]
}

Is this the only way to have the time axis working?这是让时间轴工作的唯一方法吗? I'm very confused on how to use this.我对如何使用它感到非常困惑。 What's the correct way to use the time axis with multiples series?在倍数系列中使用时间轴的正确方法是什么?

The correct data format is this正确的数据格式是这样的

[
  ['2018-04-10T20:40:33Z', 1, 5],
  ['2018-04-10T20:40:53Z', 2, 3],
  ['2018-04-10T20:41:03Z', 4, 2]
]

dataset:{
  source:data,
  dimensions: ['timestamp', 'sensor1', 'sensor2'],
}

and the the series should be这个系列应该是

series: [{
        name: 'sensor1',
        type: 'line',
        encode: {
          x: 'timestamp',
          y: 'sensor1' // refer sensor 1 value 
        }
    },{
        name: 'sensor2',
        type: 'line',
        encode: {
          x: 'timestamp',
          y: 'sensor2'
        }
    }]

Thank you valia for the correct answer!感谢瓦利亚的正确答案! As I was also looking for a solution to this problem - I thought it would be cool to have everything together in running example - that's why I added this answer.因为我也在寻找这个问题的解决方案 - 我认为在运行示例中将所有内容放在一起会很酷 - 这就是我添加这个答案的原因。

 var myChart = echarts.init(document.getElementById('main')); var data = [ ['2018-04-10T20:40:33Z', 1, 5], ['2018-04-10T20:40:53Z', 2, 3], ['2018-04-10T20:41:03Z', 4, 2], ['2018-04-10T20:44:03Z', 5, 1], ['2018-04-10T20:45:03Z', 6, 0] ]; var option = { legend: {}, tooltip: { trigger: 'axis', }, dataset: { source:data, dimensions: ['timestamp', 'sensor1', 'sensor2'], }, xAxis: { type: 'time' }, yAxis: { }, series: [ { name: 'sensor1', type: 'line', encode: { x: 'timestamp', y: 'sensor1' // refer sensor 1 value } },{ name: 'sensor2', type: 'line', encode: { x: 'timestamp', y: 'sensor2' } }] }; myChart.setOption(option);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.0.4/echarts.min.js"></script> <div id="main" style="width: 500px;height:400px;"></div>

According to documentation https://ecomfe.github.io/echarts-doc/public/en/option.html#series-line.data .根据文档https://ecomfe.github.io/echarts-doc/public/en/option.html#series-line.data May be you should try to use native Date可能你应该尝试使用原生日期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM