在条形图上尝试将今年与去年进行比较,数据集“今年”和“前一年”的标签没有显示。当并排比较多个数据集时,这个东西特别方便,因此任何帮助都是非常感谢的。顺便说一下,我的chartjs版本是2.1.4
var chartdata = {
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
// labels: month,
datasets: [
{
label: 'this year',
backgroundColor: '#26B99A',
data: sold1
},
{
label: 'previous year',
backgroundColor: '#03586A',
data: sold2
}
]
}
};
发布于 2018-09-18 08:36:59
将xAxis的autoSkip设置为false:
scales: {
xAxes: [{
beginAtZero: true,
ticks: {
autoSkip: false
}
}]
}
发布于 2017-11-19 07:17:34
检查你的sold1
和sold2
。
console.log(sold1);
console.log(sold2);
例如,这是有效的:
var chartdata = {
{
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
// labels: month,
datasets: [
{
label: 'this year',
backgroundColor: '#26B99A',
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
},
{
label: 'previous year',
backgroundColor: '#03586A',
data: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
}
]
}
}
};
var ctx = document.getElementById('chartContainer').getContext('2d');
new Chart(ctx, chartdata);
JSFiddle https://jsfiddle.net/1davgzmh/1/
发布于 2019-03-12 12:54:05
这对我很有效。试一下你自己
var chartdata = {
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
datasets: [
{
label: 'this year',
backgroundColor: '#26B99A',
data: sold1
},
{
label: 'previous year',
backgroundColor: '#03586A',
data: sold2
}
]
},
"options": {
"legend": {"position": "bottom"},
"scales": {
"xAxes": [
{
"beginAtZero": true,
"ticks": {
"autoSkip": false
}
}
]
}
}
};
https://stackoverflow.com/questions/47374198
复制