我试图创建一个包含时间戳和值的200个点的折线图。我的理解是我需要将标签和数据分成2个不同的数组,以便与Chart.js 2.0(最新版本)一起使用。
完整代码:
var chartData = [],
labelData = [],
chartData2 = [];
_.each(historyData, function (row) {
chartData.push({
x: moment.unix(row[0]/1000).format('MMM DD HH:mm'),
y: row[1]
});
labelData.push(moment.unix(row[0]/1000).format('MMM DD HH:mm'));
chartData2.push(row[1]);
});
console.log(chartData);
console.log(labelData);
console.log(chartData2);
new Chart(self.$splitChart, {
type: 'line',
data: {
labels: labelData,
datasets: [{
label: 'EPC',
data: chartData2,
lineTension: 0,
backgroundColor: 'rgba(55,166,119,.2)',
borderColor: 'rgb(55,166,119)',
pointRadius: 0,
pointHitRadius: 10
}]
},
options: {
legend: {
display: false
},
scales: {
xAxes: [{
type: 'linear',
position: 'bottom'
}]
}
}
});
示例labelData:
[" 11月13日13:42"," 11月13日14:02"," 11月13日14:22"," 11月13日14 :42"," 11月 13 15:02"," 11月13日15:22"," 11月13日15:42"," 11月13日16:02",&#34 11月13日 16:22"," 11月13日16:42"," 11月13日17:03"," 11月13日17:22"," 11月13日 17:42"," 11月13日18:02"," 11月13日18:22"," 11月13日18:42"," 11月13日 19:02"," Nov 13 19:22"," Nov 13 19:42"," Nov 13 20:02"," 11月13日 20:22"," 11月13日20:42"," 11月13日21:02"," 11月13日21:22"," 11月13日 21:42"," 11月13日22:02"," 11月13日22:22"]
示例chartData2:
[" 0.005236"," 0.005088"," 0.005067"," 0.005046"," 0.005189& #34 ;, " 0.005164"," 0.005134"," 0.005373"," 0.005331"," 0.005371", " 0.005480"," 0.005420"," 0.005520"," 0.004876"," 0.004822", " 0.004770"," 0.004397"," 0.004346"," 0.004295"," 0.004302", " 0.004259"," 0.004217"," 0.003969"," 0.003935"," 0.003897", " 0.004048"," 0.004027"]
不幸的是,我没有获得一条线,x轴只是数字(不是格式化的时间戳或与时间戳的任何关系)。我已经尝试过确保数据是浮点数(不是字符串)。仍然没有。
答案 0 :(得分:0)
发现问题是由xAxes选项中定义type: 'linear'
引起的。删除它修复了问题,我的数据显示正常。