【问题标题】:Chart.js v2 - combined stacked bar chart and 2 unstacked linesChart.js v2 - 组合堆叠条形图和 2 条未堆叠线
【发布时间】:2016-11-06 15:10:16
【问题描述】:

Chart.js v2.x 能否使用一个 y 轴生成包含 2 条未堆叠线的组合堆叠条形图?如果有,怎么做?

小提琴下方有 2 个 y 轴。如果右 y 轴和左轴一样具有最大值 6000,则下降线会渲染得更好。

jsfiddle example.

以下是代码供参考。

        // set default no fill beneath the line
        Chart.defaults.global.elements.line.fill = false;


        // stacked bar with 2 unstacked lines - nope
        var barChartData = {
          labels: ['2016', '2017', '2018', '2019'],
          datasets: [{
            type: 'bar',
            label: 'a',
            yAxisID: "y-axis-0",
            backgroundColor: "rgba(217,83,79,0.75)",
            data: [1000, 2000, 4000, 5000]
          }, {
            type: 'bar',
            label: 'b',
            yAxisID: "y-axis-0",
            backgroundColor: "rgba(92,184,92,0.75)",
            data: [500, 600, 700, 800]
          }, {
            type: 'line',
            label: 'c',
            yAxisID: "y-axis-0",
            backgroundColor: "rgba(51,51,51,0.5)",
            data: [1500, 2600, 4700, 5800]
          }, {
            type: 'line',
            label: 'd',
            yAxisID: "y-axis-1",
            backgroundColor: "rgba(151,187,205,0.5)",
            data: [5000, 3000, 1000, 0]
          }]
        };


        var ctx = document.getElementById("chart").getContext("2d");
        // allocate and initialize a chart
        var ch = new Chart(ctx, {
          type: 'bar',
          data: barChartData,
          options: {
            title: {
              display: true,
              text: "Chart.js Bar Chart - Stacked"
            },
            tooltips: {
              mode: 'label'
            },
            responsive: true,
            scales: {
              xAxes: [{
                stacked: true
              }],
              yAxes: [{
                stacked: true,
                position: "left",
                id: "y-axis-0",
              }, {
                stacked: false,
                position: "right",
                id: "y-axis-1",
              }]
            }
          }
        });

【问题讨论】:

    标签: javascript charts chart.js chart.js2


    【解决方案1】:

    事实证明,我只需要修改一两个,以便 2 y 轴使用相同的比例。请参阅此jsfiddle。现在图表渲染得更好了。就我而言,关键是将ticks 节点和这两个属性添加到yAxes 对象中:

    ticks: {
       beginAtZero: true,
       suggestedMax: 5800
    }
    

    当然,suggestedMax 值不会被硬编码。此外,由于现在 2 个比例相同,因此我使用此属性设置 display: false 隐藏了正确的比例。

    正如小提琴所示,我们现在在堆叠条形图上有 2 条未堆叠的线,渲染得很好。

    【讨论】:

      【解决方案2】:

      将此用作图表的数据:

      var barData = {
                      labels: ['test1','test2'],
                      datasets: [
                          {
                              label: 'stack1',
                              backgroundColor: 'yellow',
                              borderColor: 'white',
                              borderWidth: 1,
                              stack: '1',
                              data: [5,4,3,2,1]
                            },
                            {
                              label: 'stack2',
                              backgroundColor: 'blue',
                              borderColor: 'white',
                              borderWidth: 1,
                              stack: '2',
                              data: [1,2,3,4,5]
                            }
                      ]
                  };
      

      这是渲染数据的组件:

      <Bar data={barData} options={{scales: { xAxes: [{stacked:true}} }], yAxes:[{stacked:true}}] } }}}} />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-29
        • 2022-12-13
        • 2017-11-01
        • 2021-02-25
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多