how to plot multiple time series in chartjs where each time series has different times

后端 未结 2 1441
名媛妹妹
名媛妹妹 2021-02-13 03:43

I have two time series for example:

s1:
  2017-01-06 18:39:30    100
  2017-01-07 18:39:28    101

and

s2:
2017-01-07 18:00:00           


        
2条回答
  •  故里飘歌
    2021-02-13 04:27

    In ChartJS, label is a Category Cartesian Axis. Since you mentioned linear interpolation in your code, I assume the strings like 2017-01-06 18:39:30 are not categories, they represent the numeric values of the x-axis. So we need to inform ChartJS that the strings in the x axis are actually time. We do this in the scale options.

    var s1 = {
      label: 's1',
      borderColor: 'blue',
      data: [
        { x: '2017-01-06 18:39:30', y: 100 },
        { x: '2017-01-07 18:39:28', y: 101 },
      ]
    };
    
    var s2 = {
      label: 's2',
      borderColor: 'red',
      data: [
        { x: '2017-01-07 18:00:00', y: 90 },
        { x: '2017-01-08 18:00:00', y: 105 },
      ]
    };
    
    var ctx = document.getElementById('myChart').getContext('2d');
    var chart = new Chart(ctx, {
      type: 'line',
      data: { datasets: [s1, s2] },
      options: {
        scales: {
          xAxes: [{
            type: 'time'
          }]
        }
      }
    });
    
    
    

    You can find more information in Chart.js documentation.

提交回复
热议问题