我正在使用ChartJS
插件创建图表以显示业务的目标与销售分析。销售同时在实体店和在线进行。
下面是我想要创建的所需图表。
但是,在ChartJS画布Javascript下面,仅正确地堆叠了中间的条形,并且移位了第一个+最后一个条形对齐。
此外,只有最后一个小节在适当的小节中显示数字,而其余数字位于错误的位置。
如果我必须隐藏任何特定条形上的数字,该如何隐藏?说,我想在画布中隐藏所有目标编号。
var data = {
labels: ["Paris", "London", "Mumbai"],
datasets: [{
label: "Target",
backgroundColor: 'rgba(0,0,0,0.5)',
data: [40, 40, 40],
xAxisID: "bar-x-axis1"
}, {
label: "Store",
backgroundColor: 'blue',
data: [15, 25, 15],
xAxisID: "bar-x-axis2"
}, {
label: "Online",
backgroundColor: 'red',
data: [20, 30, 35],
xAxisID: "bar-x-axis2"
}]
};
var options = {
scales: {
xAxes: [{
stacked: true,
id: "bar-x-axis2",
categoryPercentage: 0.4,
barPercentage: 0.9
},
{
stacked: true,
display: false,
id: "bar-x-axis1",
type: 'category',
categoryPercentage: 0.5,
barPercentage: 0.9,
gridLines: {
offsetGridLines: true
}
}],
yAxes: [{
stacked: false,
ticks: {
beginAtZero: true
}
}]
},
plugins: {
datalabels: {
color: '#ffffff'
}
},
tooltips: {
mode: 'label',
callbacks: {
label: function (tooltipItem, data) {
var type = data.datasets[tooltipItem.datasetIndex].label;
var valor = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
var total = 0;
for (var i = 0; i < data.datasets.length; i++)
total += data.datasets[i].data[tooltipItem.index];
if (tooltipItem.datasetIndex != data.datasets.length - 1) {
return type + " : " + valor.toFixed(0).replace(/(\d)(?=(\d{3})+\.)/g, '1,');
} else {
return [type + " : " + valor.toFixed(0).replace(/(\d)(?=(\d{3})+\.)/g, '1,')];
}
}
}
}
};
var ctx = document.getElementById("canvas").getContext("2d");
var myBarChart = new Chart(ctx, {
type: 'bar',
data: data,
options: options
});
答案 0 :(得分:1)
This大致看起来像您想要的:
var data = {
labels: ["Paris", "London", "Mumbai"],
datasets: [{
label: "Store",
backgroundColor: 'blue',
data: [15, 20, 25],
xAxisID: "bar-x-axis2",
stack: "background"
}, {
label: "Online",
backgroundColor: 'red',
data: [30, 25, 15],
xAxisID: "bar-x-axis2",
stack: "background"
}, {
label: "Target",
backgroundColor: 'rgba(0,0,0,0.5)',
data: [30, 40, 50],
xAxisID: "bar-x-axis1",
fill: false
}]
};
var options = {
scales: {
xAxes: [{
id: "bar-x-axis2",
stacked: true,
categoryPercentage: 0.5,
barPercentage: 0.5,
}, {
display: false,
id: "bar-x-axis1",
type: 'category',
categoryPercentage: 0.4,
barPercentage: 1,
gridLines: {
offsetGridLines: true
},
stacked: true
}],
yAxes: [{
max: 100,
min: 0,
stacked: true
}]
}
};
var ctx = document.getElementById("canvas").getContext("2d");
var myBarChart = new Chart(ctx, {
type: 'bar',
data: data,
options: options
});