javascript 如何在chartjs中绘制多个时间序列,其中每个时间序列都有不同的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48143233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to plot multiple time series in chartjs where each time series has different times
提问by achyrd
I have two time series for example:
例如,我有两个时间序列:
s1:
2017-01-06 18:39:30 100
2017-01-07 18:39:28 101
and
和
s2:
2017-01-07 18:00:00 90
2017-01-08 18:00:00 105
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).
我想将这些绘制在 Chartjs 图表中,但是 Chartjs 似乎只采用一个 x 轴数组(或 Chartjs 术语中的标签)。
So my question is what is the best way to plot both of these?
所以我的问题是绘制这两者的最佳方法是什么?
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/
我的方法是编写一个函数(在 python 中,尽管这部分语言并不重要),它遍历两个时间序列并创建 3 个新数组,这些数组的格式显然是 Chartjs 需要的基于第一个示例的格式:https ://www.sitepoint.com/introduction-chart-js-2-0-six-examples/
The algorithm (in sudo code) goes like:
算法(在 sudo 代码中)如下:
# 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)
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.
还有一些其他条件检查何时数据用完较短的系列,但这是主要逻辑。之后我有 3 个数组,我现在可以通过一个时间/标签数组/x 轴和两个数据数组将它们添加到图表 js。然而,这似乎比考虑到我假设这个用例有多普遍更复杂。非常感谢任何帮助或建议。
回答by Rodrigo5244
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:30are 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.
在 ChartJS 中, label 是Category Cartesian Axis。由于您在代码中提到了线性插值,因此我假设这些字符串2017-01-06 18:39:30不是类别,它们代表 x 轴的数值。所以我们需要通知ChartJS x 轴上的字符串实际上是时间。我们在比例选项中执行此操作。
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>
You can find more information in Chart.js documentation.
您可以在Chart.js 文档 中找到更多信息。
回答by Jeremy Myers
You can have data of the form [{x:"value", y:"value"}] when your graph is of type scatter.
当您的图表类型为scatter时,您可以拥有 [{x:"value", y:"value"}] 形式的数据。
So to make your graph work, do this.
因此,要使您的图表正常工作,请执行此操作。
var canvas = document.getElementById("graph");
var s1 = [{x:"2017-01-06 18:39:30",y:"100"},{x:"2017-01-07 18:39:28",y:"101"}];
var s2 = [{x:"2017-01-07 18:00:00",y:"90"},{x:"2017-01-08 18:00:00",y:"105"}];
var graphParams = {
type:"scatter",
data:{
datasets: [{
label:"Series 1",
data:s1,
borderColor:"red",
backgroundColor:"transparent",
},
{
label:"Series 2",
data:s2,
borderColor:"blue",
backgroundColor:"transparent",
}],
},
options:{
maintainAspectRatio:false,
responsive:false,
scales:{
xAxes:[{
type:"time",
distribution: "series",
}],
}
}
}
ctx = new Chart(canvas, graphParams);
<canvas id="graph" height="500" width="700"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.bundle.min.js"></script>

