如何在chartjs中绘制多个时间序列,其中每个时间序列具有不同的时间 [英] how to plot multiple time series in chartjs where each time series has different times

查看:83
本文介绍了如何在chartjs中绘制多个时间序列,其中每个时间序列具有不同的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有两个时间序列:

I have two time series for example:

s1:
  2017-01-06 18:39:30    100
  2017-01-07 18:39:28    101

s2:
2017-01-07 18:00:00     90
2017-01-08 18:00:00    105

我想将它们绘制在Chartjs图表中,但是似乎Chartjs只需要一个x轴数组(或Chartjs术语中的标签)。

I want to plot these in a Chartjs chart, however it seems that Chartjs only takes one x-axis array (or label in Chartjs terminology).

所以我的问题是绘制这两种图形的最佳方法是什么?

我的方法是编写一个函数(使用python,尽管该语言对该部分并不重要),该函数遍历两个时间序列并创建3个新数组,其格式显然是Chartjs根据第一个示例所需要的格式: https://www.sitepoint.com/introduction-chart -js-2-0-six​​-examples /

My approach was to write a function (in python, although the language doesn't really matter for this part) that iterates through both time series and creates 3 new arrays which is in the format apparently Chartjs needs based off the 1st example here: https://www.sitepoint.com/introduction-chart-js-2-0-six-examples/

算法(使用sudo代码)如下:

The algorithm (in sudo code) goes like:

    # inputs are initial time series s1 and s2
    y1 = [] # to hold new s1 data values
    y2 = [] # to hold new s2 data values
    x  = [] # to hold times

    # iterate through longest series s1 or s2
    if s1[idx].time > s2[idx].time
      x.append(s1[idx].time)
      y1.append(s1[idx].data)
      # y2 appends the linear interpolation of 
      # of closest y2 points

    if (s1[idx].time < s2[idx].time)
      x.append(s2[idx].time)
      # opposite of above. ie. swap y1<->y2, s1->s2

    else # they have the same time
      x.append(s1[idx].time)
      y1.append(s1[idx].data)
      y2.append(s2[idx].data) 

还有其他一些条件检查,以检查何时数据用完了较短的序列,但这是主要逻辑。之后,我有3个数组,现在可以通过一个时间/标签数组/ x轴和两个数据数组添加到图表js。但是,这似乎比我认为这种用例的普遍程度要复杂得多。

There are a couple other conditional checks for when data runs out of the shorter series but that is the main logic. After which I have 3 arrays that I can now add to chart js via one time/label array/x-axis and two data arrays. However this seems WAY more complicated than it should be considering how common I assume this use case is. Any help or advice is greatly appreciated.

推荐答案

在ChartJS中,标签是类别笛卡尔坐标轴。由于您在代码中提到了线性插值,因此我假设像 2017-01-06 18:39:30 这样的字符串不是类别,它们表示x-的数值轴。因此,我们需要通知ChartJS x轴上的字符串实际上是时间。我们在缩放选项中执行此操作。

In ChartJS, label is a Category Cartesian Axis. Since you mentioned linear interpolation in your code, I assume the strings like 2017-01-06 18:39:30 are not categories, they represent the numeric values of the x-axis. So we need to inform ChartJS that the strings in the x axis are actually time. We do this in the scale options.

var s1 = {
  label: 's1',
  borderColor: 'blue',
  data: [
    { x: '2017-01-06 18:39:30', y: 100 },
    { x: '2017-01-07 18:39:28', y: 101 },
  ]
};

var s2 = {
  label: 's2',
  borderColor: 'red',
  data: [
    { x: '2017-01-07 18:00:00', y: 90 },
    { x: '2017-01-08 18:00:00', y: 105 },
  ]
};

var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
  type: 'line',
  data: { datasets: [s1, s2] },
  options: {
    scales: {
      xAxes: [{
        type: 'time'
      }]
    }
  }
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
<canvas id="myChart"></canvas>

您可以在 Chart.js文档中找到更多信息。

You can find more information in Chart.js documentation.

这篇关于如何在chartjs中绘制多个时间序列,其中每个时间序列具有不同的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆