Chart.js - 组合条形图和折线图 - 删除线数据集的堆叠,但仍保留条形数据集的堆叠

问题描述 投票:0回答:1

我有一个堆积条形图和两个折线图。第二个折线图堆叠在第一个折线图的顶部,但我不希望出现这种行为。

我尝试过显式地将堆栈模式设置为 false ,但此选项没有被执行。

            ...
            type: 'line',
            label: 'lineChart1',
            data: lineData1,
            borderWidth: 1,
            fill: false,
            options: {
              legend: {
                display: false
              },
              scales: {
                    xAxes: [{
                        stacked: false,
                    }],
                    yAxes: [{
                        stacked: false
                    }]
                }
            }
        }
        ...
javascript charts chart.js2
1个回答
15
投票

以下示例配置了包含 2 个堆叠条形图和 2 个非堆叠线的图表。我认为您在折线图上堆叠的原因是因为您没有在条形图中设置

stacked
选项来为它们提供堆栈 id。

var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
      type: 'line',
      label: 'Dataset 1',
      borderColor: window.chartColors.green,
      borderWidth: 2,
      fill: false,
      data: [5, 3, 4, 10, 8, 9, 2]
    }, {
      type: 'line',
      label: 'Dataset 2',
      borderColor: window.chartColors.orange,
      borderWidth: 2,
      fill: false,
      data: [8, 5, 2, 8, 7, 2, 6]
    }, {
      type: 'bar',
      label: 'Dataset 3',
      backgroundColor: window.chartColors.red,
      stack: 'Stack 0',
      data: [2, 4, 1, 3, 7, 3, 6],
      borderColor: 'white',
      borderWidth: 2
    }, {
      type: 'bar',
      label: 'Dataset 4',
      backgroundColor: window.chartColors.blue,
      stack: 'Stack 0',
      data: [7, 2, 4, 5, 6, 4, 2]
    }]
  },
  options: {
    responsive: true,
    title: {
      display: true,
      text: 'Chart.js Stacked Bar and Unstacked Line Combo Chart'
    },
    tooltips: {
      mode: 'index',
      intersect: true
    },
    scales: {
      xAxes: [{
        stacked: true,
      }]
    }
  }
});

这是一个 codepen 示例,展示了它的样子。

© www.soinside.com 2019 - 2024. All rights reserved.