我正试图实现类似的图表。例如,3月份通过卡和现金收取的出租车运输费(现金40元,卡支付38元)。我需要用主色和主色的浅色版本来显示这个条形图。我在这里有两个问题什么样的图形可能符合我的需求? 我如何能用两种不同色调的同一种颜色(深蓝色和浅蓝色)制作一个条形图?
预期的结果。
我已经用下面的代码试过了,我确定数据集不包括我前面解释的卡和现金选项。
var barChartData = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: 'Train',
backgroundColor: "rgba(168, 90, 50,1)",
data: [50, 40, 23, 45, 67, 78, 23]
}, {
label: 'Bus',
backgroundColor: "rgba(50, 168, 80,1)",
data: [50, 40, 78, 23, 23, 45, 67]
}, {
label: 'Taxi',
backgroundColor: "rgba(83, 95, 219,1)",
data: [50, 67, 78, 23, 40, 23, 0]
}]
};
var ctx = document.getElementById("canvas").getContext("2d");
var myBar = new Chart(ctx, {
type: 'bar',
data: barChartData,
options: {
title: {
display: true,
text: "Transport Mode"
},
tooltips: {
mode: 'single',
callbacks: {
label: function(tooltipItem, data) {
var text = data.datasets[tooltipItem.datasetIndex].label;
var value = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
var total = 0;
var label = '';
for (var i = 0; i < data.datasets.length; i++) {
total += data.datasets[i].data[tooltipItem.index];
}
if (tooltipItem.datasetIndex != data.datasets.length - 1) {
label += text + " : $" + value.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
} else {
label += text + " : $" + value.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'), "Total : $" + total;
}
return label;
}
}
},
responsive: true,
scales: {
xAxes: [{
gridLines: { color: "rgba(0, 0, 0, 0)" }
}],
yAxes: [{
}]
}
}
});
谢谢您。
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: 'Train',
backgroundColor: "rgba(168, 90, 50,1)",
data: [50, 40, 23, 45, 67, 78, 23],
stack: 1
}, {
label: 'Bus',
backgroundColor: "rgba(50, 168, 80,1)",
data: [50, 40, 78, 23, 23, 45, 67],
stack: 3
}, {
label: 'Taxi',
backgroundColor: "rgba(83, 95, 219,1)",
data: [50, 67, 78, 23, 40, 23, 0],
stack: 2
},
{
label: 'Taxi cash',
backgroundColor: "rgba(83, 55, 219,1)",
data: [25, 10, 12, 20, 10, 12, 5],
stack: 2
}]
};
var options = {
title: {
display: true,
text: "Transport Mode"
},
tooltips: {
mode: 'single',
callbacks: {
label: function(tooltipItem, data) {
var text = data.datasets[tooltipItem.datasetIndex].label;
var value = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
var total = 0;
var label = '';
for (var i = 0; i < data.datasets.length; i++) {
total += data.datasets[i].data[tooltipItem.index];
}
if (tooltipItem.datasetIndex != data.datasets.length - 1) {
label += text + " : $" + value.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
} else {
label += text + " : $" + value.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'), "Total : $" + total;
}
return label;
}
}
},
responsive: true,
scales: {
xAxes: [{
gridLines: { color: "rgba(0, 0, 0, 0)" },
stacked: true
}],
yAxes: [{
}]
}
};
var ctx = document.getElementById("canvas").getContext("2d");
var myBar = new Chart(ctx, {
type: 'bar',
options: options,
data: data
});
body {
background: #1D1F20;
padding: 16px;
}
canvas {
border: 1px dotted red;
}
.chart-container {
position: relative;
margin: auto;
height: 80vh;
width: 80vw;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<div class="chart-container">
<canvas id="canvas"></canvas>
</div>
看成堆叠条形图 例子
scales: {
yAxes: [{
stacked: true
}]
}
您配置的选项是正确的。它将是一个条形图,因为你为每种运输方式提供了多个数据集,chartjs将把它视为一个堆叠的条形图。
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: 'Train',
backgroundColor: "rgba(168, 90, 50,1)",
data: [50, 40, 23, 45, 67, 78, 23]
}, {
label: 'Bus',
backgroundColor: "rgba(50, 168, 80,1)",
data: [50, 40, 78, 23, 23, 45, 67]
}, {
label: 'Taxi',
backgroundColor: "rgba(83, 95, 219,1)",
data: [50, 67, 78, 23, 40, 23, 0]
}]
};
var options = {
title: {
display: true,
text: "Transport Mode"
},
tooltips: {
mode: 'single',
callbacks: {
label: function(tooltipItem, data) {
var text = data.datasets[tooltipItem.datasetIndex].label;
var value = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
var total = 0;
var label = '';
for (var i = 0; i < data.datasets.length; i++) {
total += data.datasets[i].data[tooltipItem.index];
}
if (tooltipItem.datasetIndex != data.datasets.length - 1) {
label += text + " : $" + value.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
} else {
label += text + " : $" + value.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'), "Total : $" + total;
}
return label;
}
}
},
responsive: true,
scales: {
xAxes: [{
gridLines: { color: "rgba(0, 0, 0, 0)" }
}],
yAxes: [{
}]
}
};
var ctx = document.getElementById("canvas").getContext("2d");
var myBar = new Chart(ctx, {
type: 'bar',
options: options,
data: data
});
body {
background: #1D1F20;
padding: 16px;
}
canvas {
border: 1px dotted red;
}
.chart-container {
position: relative;
margin: auto;
height: 80vh;
width: 80vw;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<div class="chart-container">
<canvas id="canvas"></canvas>
</div>