【问题标题】:How to use two Y axes in Chart.js v2?如何在 Chart.js v2 中使用两个 Y 轴?
【发布时间】:2016-10-31 07:41:43
【问题描述】:

我正在尝试使用 Chart.js 创建一个包含两个数据集的折线图,每个数据集都有自己的 Y 刻度/轴(一个在图表的左侧,一个在图表的右侧)。

这是我的代码 (jsfiddle):

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

但是,第二个轴不可见,并且第二个数据集仍然与第一个数据集完全一样(0 到 100,而不是 0 到 1)。我需要改变什么?

【问题讨论】:

    标签: javascript browser graph chart.js


    【解决方案1】:

    对于 ChartJs 2.x,只需要进行几处更改(看起来您已尝试将 2.x 选项与我 fork 中的多轴选项结合起来?),

    • yAxes 字段需要在 scales 对象中
    • yAxis 由 id 而不是名称引用。
    • 对于缩放步长/大小,您只需将这些选项包装在 ticks 对象中。
    • 不需要scalePositionLeft position

    例子:

    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

    【讨论】:

    • 当我打开你的小提琴时,两个轴都在那里,但是第二个数据集(B)仍然没有缩放到右轴(B),为什么不呢?
    • 对不起,我的错字应该是yAxisID 而不是yAxesID
    • @Bcktr 我确定您可以将类型更改为条形。 (或者你是在同一张图中显示一条条和一条线吗?)目前不在电脑附近,但我觉得应该可以。
    • 你知道如何根据数据自动设置刻度(最小/最大)吗?它适用于左轴,默认右轴为0-1
    • Chart.js 3.5 起不再有效,并且被列为 3.0 的重大更改的一部分。请参阅下面的更新答案
    【解决方案2】:

    从 3.5 开始,接受的答案不再有效,原因被列为 3.X 的重大更改的一部分(请参阅 3.x Migration Guide

    下面的更新代码将 scales 属性从 scales: {yScales: [...]} 更改为 scales: {[id]: {[options]}} ,还添加了 fill: true,(在 3.X 中从默认更改为 true)和 tension: 0.4(示例之前提供的确实有平滑的曲线,并且似乎是一个无证的“破坏性”变化)

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

    【讨论】:

    • 很好的答案。 @juanjo-martinez
    【解决方案3】:

    此解决方案正在响应中。

    // "chart.js": "^2.9.4"
    // "react-chartjs-2": "^2.11.1"
    
    
    const lineChartData = {
      data: {
        labels: ['1', '2', '3', '4', '5', '6'],
        datasets: [
          {
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            fill: false,
            backgroundColor: 'rgb(255, 99, 132)',
            borderColor: 'rgba(255, 99, 132, 0.2)',
          },
          {
            label: '# of Votes',
            data: [19, 9, 2, 1, 1, 21],
            fill: false,
            backgroundColor: 'rgb(122, 99, 255)',
            borderColor: 'rgba(102, 99, 255, 0.2)',
          }
        ],
      },
      options: {
        scales: {
          yAxes: [
            {
              type: 'linear',
              display: true,
              position: 'left',
              ticks: {
                beginAtZero: true,
              },
            },
            {
              type: 'linear',
              display: true,
              position: 'left',
              ticks: {
                beginAtZero: true,
              },
            },
          ],
        },
      }
    }
    
    
    <Line 
       data={lineChartData.data} 
       options={lineChartData.options} 
       height={30} 
       width={80} 
       options={{ maintainAspectRatio: true }} 
    />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      相关资源
      最近更新 更多