作者:企鹅田先生 | 来源:互联网 | 2022-10-29 20:48
我正在尝试以秒精度绘制时间序列,格式为HH:mm:ss(24小时/天),如一下js docs中所述。
问题是我声明的格式没有得到遵守,我尝试了Chartsjs文档中的几种组合,但没有一种有效。
Vue.component('line-chart', {
extends: VueChartJs.Line,
mixins: [VueChartJs.mixins.reactiveProp],
props: ['chartData', 'timeFormat'],
data() {
return {
options: {
animation: false,
scales: {
xAxes: [{
type: 'time',
distribution: 'series',
time: {
format: this.timeFormat
}
}],
},
}
}
},
mounted () {
this.renderChart(this.chartData, this.options);
}
})
var vm = new Vue({
el: '.app',
data() {
return {
chart: {},
timeFormat: 'HH:mm:ss',
timeout: null,
len_data: 20
}
},
created () {
this.change_data();
},
methods: {
change_data: function() {
this.chart = this.fillData();
this.timeout = setTimeout(this.change_data, 1000);
},
fillData () {
return {
labels: this.get_labels(),
datasets: [{
fill: false, // line
pointRadius: 2, // line
borderWidth: 2, // line
borderColor: "rgba(25, 25, 125, 1)",
data: this.get_nums(0),
label: "Points"
}]
}
},
get_labels() {
return Array.from({length: this.len_data}, (v, k) => this.newDateString(this.len_data-k));
},
get_nums(data_idx) {
if(typeof this.chart.datasets !== 'undefined') {
this.chart.datasets[data_idx].data.shift(); // removing the first item
this.chart.datasets[data_idx].data.push(this.getRandomInt()); // adding a new one
return this.chart.datasets[data_idx].data;
}
return Array.from({length: this.len_data}, (v, k) => this.getRandomInt());
},
getRandomInt () {
return Math.floor(Math.random() * (50 - 5 + 1)) + 5
},
newDateString(seconds) {
return moment().subtract(seconds, 's').format(this.timeFormat);
}
}
})
{{chart.datasets[0].data}}
我想我快要到了,我敢打赌我只是缺少了一些细节。
JSFIDDLE:https://jsfiddle.net/ue1x8079/