Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart JS - Use time for xAxes

i added this code

scales: 
{
        xAxes: 
        [{
            type: 'time',
            time: 
            {
                unit: 'month',
                displayFormats: { month: 'MM' },
                max: '2017-10-09 18:43:53',
                min: '2017-10-02 18:43:53'
            }
        }]
},

to the options but it does not work. Any ideas what i'm making wrong?

FIDDLE -> https://jsfiddle.net/o473y9pw/

like image 734
Peter Avatar asked Oct 08 '17 02:10

Peter


People also ask

Can I use chart js offline?

Is it possible to use chart. js for a desktop/mobile application using html that connects through an esp8266 access point or does it need a wifi connection? @marionboynton, CanvasJS is a standalone library that can work offline without any internet connection.

Is chart js easy to learn?

It's easy to get started with Chart. js. All that's required is the script included in your page along with a single <canvas> node to render the chart. In this example, we create a bar chart for a single dataset and render that in our page.

Is chart js free to use?

js is a free, open-source JavaScript library for data visualization, which supports eight chart types: bar, line, area, pie (doughnut), bubble, radar, polar, and scatter.


1 Answers

Since you wish to use time for x-axis, your labels array should be an array of date/time string (labels array is correspondent to x-axis).

You would also need to set the parser property (to parse the date/time correctly), and x-axis' ticks source to data (to properly generate x-axis ticks).

UPDATE

If you only have a min and max date then, you can create a nice little plugin to populate the labels (date) array dynamically, as such :

plugins: [{
   beforeInit: function(chart) {
      var time = chart.options.scales.xAxes[0].time, // 'time' object reference
         // difference (in days) between min and max date
         timeDiff = moment(time.max).diff(moment(time.min), 'd');
      // populate 'labels' array
      // (create a date string for each date between min and max, inclusive)
      for (i = 0; i <= timeDiff; i++) {
         var _label = moment(time.min).add(i, 'd').format('YYYY-MM-DD HH:mm:ss');
         chart.data.labels.push(_label);
      }
   }
}]

note: moment.js is used to make calculations easier.

ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ
( for demonstration purposes, I've changed the time unit to day )

$(document).ready(function() {

   new Chart(document.getElementById("chartBox"), {
      type: 'line',
      data: {
         datasets: [{
            data: [12, 19, 3, 5, 2, 3, 32, 15],
            label: "",
            borderWidth: 2,
            borderColor: "#3e95cd",
            fill: false,
            pointRadius: 0
         }]
      },
      options: {
         scales: {
            xAxes: [{
               type: 'time',
               time: {
                  parser: 'YYYY-MM-DD HH:mm:ss',
                  unit: 'day',
                  displayFormats: {
                     day: 'ddd'
                  },
                  min: '2017-10-02 18:43:53',
                  max: '2017-10-09 18:43:53'
               },
               ticks: {
                  source: 'data'
               }
            }]
         },
         legend: {
            display: false
         },
         animation: {
            duration: 0,
         },
         hover: {
            animationDuration: 0,
         },
         responsiveAnimationDuration: 0
      },
      plugins: [{
         beforeInit: function(chart) {
            var time = chart.options.scales.xAxes[0].time, // 'time' object reference
               timeDiff = moment(time.max).diff(moment(time.min), 'd'); // difference (in days) between min and max date
            // populate 'labels' array
            // (create a date string for each date between min and max, inclusive)
            for (i = 0; i <= timeDiff; i++) {
               var _label = moment(time.min).add(i, 'd').format('YYYY-MM-DD HH:mm:ss');
               chart.data.labels.push(_label);
            }
         }
      }]
   });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@latest/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.min.js"></script>

<canvas id="chartBox"></canvas>
like image 55
ɢʀᴜɴᴛ Avatar answered Oct 05 '22 17:10

ɢʀᴜɴᴛ