带有多个y轴的Angular Chart.js堆叠条形图

时间:2020-09-30 09:09:53

标签: javascript angular chart.js ng2-charts

我正在使用ng2-chart库(基于ChartJS),并且试图创建具有多个y轴的堆叠条形图。这是我到目前为止的内容:

enter image description here

我希望左侧的堆积条沿左侧的y轴,而右侧的堆积条沿右侧的y轴。

但是如您所见,图表运行不正常。橙色数据跟随右侧的y轴,蓝色数据跟随左侧的y轴。

我该如何做到?

这是我的代码:

// CHART LABELS
chartLabels: string[] = [
    'Commissions Earned',
    'Lots Traded'
  ];

// CHART OPTIONS
chartOptions = {
    responsive: true,
    responsiveAnimationDuration: 0,
    scales: {
      yAxes: [
        {
          id: 'y-axis-0',
          position: 'left',
          scaleLabel: {
            display: true,
            labelString: 'probability'
          }
        },
        {
          id: 'y-axis-1',
          position: 'right',
        }
      ]
    }
  };

// CHART DATA
this.chartData = [
      {
        // Data is for Commissions, followed by Lots traded
        data: variableAccounts,
        label: 'Fixed/Variable Accounts',
        stack: 'a',
        yAxisID: 'y-axis-0'
      },
      {
        // Data is for Commissions, followed by Lots traded
        data: ecnAccounts,
        label: 'ECN Premier Accounts',
        stack: 'a',
        yAxisID: 'y-axis-1'
      },
    ];

0 个答案:

没有答案