gpt4 book ai didi

charts - 使用 chart.js 的垂直堆积条形图

转载 作者:行者123 更新时间:2023-12-04 22:18:49 26 4
gpt4 key购买 nike

来自 chart.js 的优秀免费图书馆.我正在将我的图表从 Google 图表转移到 chart.js,因为我可以离线使用它们,而且它似乎对窗口大小的变化更敏感。此外,我意识到我在中国的观众无法看到我的谷歌图表,因为谷歌服务在中国被屏蔽了。

我一直在阅读有关 stacked vertical bar charts 的文档,但我不知道如何制作这样的图表。在我看到的堆积条形图的所有示例中,每个条形的项目数都相同。

我可以只制作两个垂直堆叠的数据集吗?这是因为右侧栏的项目比左侧栏多。还是我需要n数据集,是 n项目的数量,有更多项目的酒吧?

enter image description here

代码

我想为每个(堆叠的)条分组一个数据集,但我不能。

var ctx = document.getElementById("barChart").getContext('2d');

var labels = ["standing costs", "running costs"];
var dataset = [
{
type: 'bar',
label: ["cost1", "cost2", "cost3", "cost4"],
data: [1, 2, 1, 3],
stack: "standing costs",
backgroundColor: [
'navy',
'blue',
'aqua',
'teal'
]
},
{
type: 'bar',
label: ["cost5", "cost6", "cost7", "cost8"],
data: [5, 1, 3, 0],
stack: "running costs",
backgroundColor: [
'green',
'lime',
'yellow',
'white'
]
}
];

var options = {
scales: {
xAxes: [{
stacked: true
}],
yAxes: [{
stacked: true
}]
}
};

var content = {
type: 'bar',
data: {
labels: labels,
datasets: dataset
},
options
};

new Chart(ctx, content);
@import url("https://cdnjs.cloudflare.com/ajax/libs/colors/1.0/colors.min.css");
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.bundle.min.js"></script>
<canvas id="barChart"></canvas>

最佳答案

快速解决方案:

垂直堆积条形图

var chart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Standing costs', 'Running costs'], // responsible for how many bars are gonna show on the chart
// create 12 datasets, since we have 12 items
// data[0] = labels[0] (data for first bar - 'Standing costs') | data[1] = labels[1] (data for second bar - 'Running costs')
// put 0, if there is no data for the particular bar
datasets: [{
label: 'Washing and cleaning',
data: [0, 8],
backgroundColor: '#22aa99'
}, {
label: 'Traffic tickets',
data: [0, 2],
backgroundColor: '#994499'
}, {
label: 'Tolls',
data: [0, 1],
backgroundColor: '#316395'
}, {
label: 'Parking',
data: [5, 2],
backgroundColor: '#b82e2e'
}, {
label: 'Car tax',
data: [0, 1],
backgroundColor: '#66aa00'
}, {
label: 'Repairs and improvements',
data: [0, 2],
backgroundColor: '#dd4477'
}, {
label: 'Maintenance',
data: [6, 1],
backgroundColor: '#0099c6'
}, {
label: 'Inspection',
data: [0, 2],
backgroundColor: '#990099'
}, {
label: 'Loan interest',
data: [0, 3],
backgroundColor: '#109618'
}, {
label: 'Depreciation of the vehicle',
data: [0, 2],
backgroundColor: '#109618'
}, {
label: 'Fuel',
data: [0, 1],
backgroundColor: '#dc3912'
}, {
label: 'Insurance and Breakdown cover',
data: [4, 0],
backgroundColor: '#3366cc'
}]
},
options: {
responsive: false,
legend: {
position: 'right' // place legend on the right side of chart
},
scales: {
xAxes: [{
stacked: true // this should be set to make the bars stacked
}],
yAxes: [{
stacked: true // this also..
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx" width="700"></canvas>


为没有给出解释而道歉。

关于charts - 使用 chart.js 的垂直堆积条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45122306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com