How to use two Y axes in Chart.js v2?

前端 未结 1 1974
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 23:41

I am trying to create a line chart with two datasets, each with its own Y scale / axis (one to the left, one to the right of the graph) using Chart.js.

This is my co

相关标签:
1条回答
  • 2020-12-12 23:57

    For ChartJs 2.x only a couple changes need to be made (it looks like you have tried to combine 2.x options with the multi-axes options from my fork?),

    • The yAxes field needs to be in a scales object
    • the yAxis is referenced by id not name.
    • For the scale steps/size you just need to wrap these options in a ticks object.
    • No need forscalePositionLeft this is covered by position

    Example:

    var canvas = document.getElementById('chart');
    new Chart(canvas, {
      type: 'line',
      data: {
        labels: ['1', '2', '3', '4', '5'],
        datasets: [{
          label: 'A',
          yAxisID: 'A',
          data: [100, 96, 84, 76, 69]
        }, {
          label: 'B',
          yAxisID: 'B',
          data: [1, 1, 1, 1, 0]
        }]
      },
      options: {
        scales: {
          yAxes: [{
            id: 'A',
            type: 'linear',
            position: 'left',
          }, {
            id: 'B',
            type: 'linear',
            position: 'right',
            ticks: {
              max: 1,
              min: 0
            }
          }]
        }
      }
    });
    

    fiddle example

    0 讨论(0)
提交回复
热议问题