Currently, I am attempting to create a time series plot with second precision in the format of HH:mm:ss (24 hours per day), as indicated in the documentation provided by moment js.
However, I have encountered an issue where the format I specify is not being correctly applied. Despite referencing various combinations from the chartsjs documentation, none of them seem to work.
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);
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.6/vue.min.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3d5d6c68ec0cbc2d1d7c9d0e3918d958d96">[email protected]</a>/dist/vue-chartjs.full.min.js"></script>
<div class="app">
<line-chart :chart-data="chart" :time-format="timeFormat" width="400" height="200"></line-chart>
<br>{{chart.datasets[0].data}}
</div>
Almost there, just missing a small detail. Could use some guidance.
Check out the JSFIDDLE for reference: https://jsfiddle.net/ue1x8079/