简体   繁体   English

chart.js中堆积的条形图,堆栈中具有不同的值

[英]Stacked bar charts in chart.js with different values in stacks

I want to create a stack graph something like this: 我想创建一个像这样的堆栈图:

Note: Each stack has different value 注意:每个堆栈具有不同的值

在此处输入图片说明

Is it possible with chart js ? 图表js是否可能? If yes what my data should look like ? 如果是,我的数据应该是什么样?

I have tried this: 我已经试过了:

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
        }
    ]
};

But this is not what i want. 但这不是我想要的。

Any help would be appreciated. 任何帮助,将不胜感激。

in order to have a stack of 3 bars, 为了有3条杆的堆叠,
you will need 3 datasets . 您将需要3个datasets

to have different colors, use an array for backgroundColor . 要具有不同的颜色,请为backgroundColor使用数组。

see following working snippet... 请参阅以下工作片段...

 $(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 }], } } }); }); 
 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM