chart.js 中的堆叠条形图在堆栈中具有不同的值

use*_*842 2 javascript charts ng2-charts

我想创建一个这样的堆栈图:

注意:每个堆栈具有不同的值

在此处输入图片说明

图表 js 可以吗?如果是,我的数据应该是什么样的?

我试过这个:

var data = {
    labels: ["2018", "2019"], //Want "value 1,2,3 for 2018 and value 4,5,6 for 2019"
    datasets: [
        {
            label: "Harpo",
            fillColor: "blue",
            data: []  // What should i use here 
        },
        {
            label: "Chico",
            fillColor: "red",
            data: []  // What should i use here
        }
    ]
};
Run Code Online (Sandbox Code Playgroud)

但这不是我想要的。

任何帮助,将不胜感激。

Whi*_*Hat 5

为了有一堆 3 条,
你需要 3 datasets

要具有不同的颜色,请使用数组 for backgroundColor

请参阅以下工作片段...

$(document).ready(function() {
  new Chart(document.getElementById("myChart").getContext("2d"), {
    type: "bar",
    data: {
      labels: ["2018", "2019"],
      datasets: [
        {
            backgroundColor: ["#673AB7", "#90A4AE"],
            fillColor: "#000000",
            data: [1, 4]
        },
        {
            backgroundColor: ["#E1BEE7", "#0D47A1"],
            data: [2, 5]
        },
        {
            backgroundColor: ["#BA68C8", "#455A64"],
            data: [3, 6]
        }
      ]
    },
    options: {
      legend: {
        display: false
      },
      scales: {
        xAxes:[{
          stacked: true
        }],
        yAxes:[{
          stacked: true
        }],
      }
    }
  });
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<canvas id="myChart"></canvas>
Run Code Online (Sandbox Code Playgroud)