如何为一个X轴chart.js绘制多个值

时间:2019-04-01 09:47:57

标签: chart.js

我已经在一个Web应用程序中实现了Charts JS库,但遇到了问题。现在,我需要将多个y值添加到一个x轴点。但是没有解决。

var ctx = document.getElementById(“ myChart”)。getContext(“ 2d”); 新图表(ctx,config);

var config = {
  type: 'line',
  data: {
    labels: [['01', '11' ,'2018'],
    ['01', '11' ,'2018'],
    ['01', '11' ,'2018'],
    ['01','11','2018'], '02','02','02','02','02','05','05','05','05','06','06','06','06','07','07','07','07','07','07','07','07','08','08','08','08','08','08','08','08','09','09','09','09',['20','02','2019'],['20','02','2019'],'21','21','21','21',['06','03'],['06','03'] ,'','','',''  ,
    ],
    datasets: [{
      label: "My First dataset",
      data: [35.48  ,35.50  ,35.5   ,35.49  ,35.49  ,35.50  ,35.50  ,35.49  ,35.45  ,35.38  ,35.42  ,35.4   ,35.49  ,35.50  ,35.49  ,35.49  ,35.49  ,35.49  ,35.50  ,35.50  ,35.48  ,35.47  ,35.48  ,35.47  ,35.49  ,35.50  ,35.50  ,35.49  ,35.48  ,35.47  ,35.48  ,35.47  ,35.50  ,35.49  ,35.49  ,35.50  ,37     ,37     ,38     ,39     ,40     ,51     ,28     ,29 ],
    }]
  },
  options: {
    scales: {
      xAxes:[{
       ticks: {
   autoSkip: false,
  }
}],
    },
  }
};

var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, config);
<canvas id="myChart"></canvas>

1 个答案:

答案 0 :(得分:0)

您可以添加多个数据集,这些数据集将在图形上绘制一条单独的线。

Here是您的示例。

function newDate() {
  return moment().add(days, 'd');
}

var config = {
  type: 'line',
  data: {
    labels: [
      ["01", "11", "2018"],
      ['01', '11', '2018'],
      ['01', '11', '2018'],
      ['01', '11', '2018'], , '02', '02', '02', '02', '05', '05', '05', '05', '06', '06', '06', '06', '07', '07', '07', '07', '07', '07', '07', '07', '08', '08', '08', '08', '08', '08', '08', '08', '09', '09', '09', '09', ['20', '02', '2019'],
      ['20', '02', '2019'], '21', '21', '21', '21', ['06', '03'],
      ['06', '03'], '', '', '', '', '', '', '', '', '', '', '', '', '', ''
    ],
    datasets: [{
        label: "My First dataset",
        data: [35.48, 35.50, 35.5, 35.49, 35.49, 35.50, 35.50, 35.49, 35.45, 35.38, 35.42, 35.4, 35.49, 35.50, 35.49, 35.49, 35.49, 35.49, 35.50, 35.50, 35.48, 35.47, 35.48, 35.47, 35.49, 35.50, 35.50, 35.49, 35.48, 35.47, 35.48, 35.47, 35.50, 35.49, 35.49, 35.50, 37, 37, 38, 39, 40, 51, 28, 29],
      },
      {
        label: "My second dataset",
        data: [35.48, 35.50, 35.5, 35.49, 35.49, 35.50, 35.50, 35.49, 35.45, 35.38, 35.42, 35.4, 35.49, 35.50, 35.49, 35.49, 35.49, 35.49, 35.50, 35.50, 35.48, 35.47, 35.48, 35.47, 35.49, 35.50, 35.50, 35.49, 35.48, 35.47, 35.48, 35.47, 35.50, 35.49, 35.49, 35.50, 37, 37, 38, 39, 40, 51, 28, 29].map(a => a + 20),
      }
    ]
  },
  options: {
    scales: {
      xAxes: [{
        ticks: {
          autoSkip: false
        }

      }],
    },
  }
};

var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<canvas id="myChart"></canvas>