Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

time series stream, removing am/pm on x axis and 24 hour format

I'm trying to plot a time series with second precision, format HH:mm:ss (24 hour/day), as stated in moment js docs.

The problem is that the format I'm declaring is not being respected, I've tried several combinations from chartsjs docs and none of them worked.

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/[email protected]/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>

I think I'm almost there, I bet I'm just missing a little detail.
JSFIDDLE: https://jsfiddle.net/ue1x8079/

like image 904
Hula Hula Avatar asked Apr 12 '19 07:04

Hula Hula


1 Answers

The Chart.js docs you linked to specify this structure for formatting time axes labels:

time: {
    displayFormats: {
        quarter: 'MMM YYYY'
    }
}

so plugging in your format that becomes:

time: {
    displayFormats: {
        second: this.timeFormat
    }
}

You were right, just a tiny detail! (updated Fiddle).

like image 144
sheilak Avatar answered Sep 30 '22 18:09

sheilak