我正在运行以下语法,它会显示我应该的组合图表。但是,我遇到的问题是数组值包含$ amount并显示在Y轴的左侧,数组values1包含%amount并且始终位于底部,因为$' s是总是高于%'边
我的想法是可以在Y轴的左侧显示$ amount,在Y轴的右侧显示%数量,并将行数据映射到%&#s;并且具有条形数据映射到$'
这是我目前的语法:
var labelsarr = ['George 11', 'George 12', 'Fred 11', 'Fred 12', 'Sage 11', 'Sage 12'];
var ctx = document.getElementById('canvas').getContext('2d');
var chart = new Chart(ctx, {
type: 'bar',
data: {
labels: labelsarr,
datasets: [
{
type: 'line',
fill: false,
label: 'Line Data',
backgroundColor: 'rgba(255,0,0,1)',
borderColor: 'rgba(255,0,0,1)',
data: values1
},
{
label: 'Bar Data',
backgroundColor: 'rgba(0, 129, 214, 0.8)',
data: values
}
]
},
options: {
tooltips: {
callbacks: {
label: function (t, d) {
if (t.datasetIndex === 0) {
var xLabel = d.datasets[t.datasetIndex].label;
var yLabel = t.yLabel + '%';
return xLabel + ': ' + yLabel;
} else if (t.datasetIndex === 1) {
var xLabel = d.datasets[t.datasetIndex].label;
var yLabel = t.yLabel >= 1000 ? '$' + t.yLabel.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') : '$' + t.yLabel;
return xLabel + ': ' + yLabel;
}
}
}
},
legend: {
display: false,
position: 'top',
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function (value, index, values) {
if (parseInt(value) >= 1000) {
return '$' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
} else {
return '$' + value;
}
}
}
}]
}
},
plugins: [{
beforeDraw: function (chart) {
var labels = chart.data.labels;
labels.forEach(function (e, i) {
var bar = chart.data.datasets[1]._meta[0].data[i]._model;
var dataPoint = e.split(/\s/)[1];
if (dataPoint === '16')
bar.backgroundColor = 'blue';
else if (dataPoint === '17')
bar.backgroundColor = 'green';
});
}
}]
});
答案 0 :(得分:2)
<强>ꜰɪʀꜱᴛ强>
为您的第一个数据集设置 yAxisID: 'y-axis-1'
,如下所示:
...
datasets: [{
type: 'line',
fill: false,
label: 'Line Data',
backgroundColor: 'rgba(255,0,0,1)',
borderColor: 'rgba(255,0,0,1)',
data: [3, 4, 10, 5, 8, 7],
yAxisID: 'y-axis-1' //<- set this
}, {
...
}]
...
这是将数据集映射到右y轴
<强>ꜱᴇᴄᴏɴᴅ强>
将另一个y轴(对于百分比金额)添加到 yAxes
数组中,如下所示:
...
scales: {
yAxes: [{
...
}, {
id: 'y-axis-1',
position: 'right',
ticks: {
beginAtZero: true,
callback: function(value, index, values) {
return value + '%';
}
}
}]
}
...
ᴡᴏʀᴋɪɴɢᴡᴏʀᴋɪɴɢxᴀᴍᴘʟᴇᴀᴍᴘʟᴇ
var labelsarr = ["George 11", "George 12", "Fred 11", "Fred 12", "Sage 11", "Sage 12"];
var ctx = document.getElementById('canvas').getContext('2d');
var chart = new Chart(ctx, {
type: 'bar',
data: {
labels: labelsarr,
datasets: [{
type: 'line',
fill: false,
label: 'Line Data',
backgroundColor: 'rgba(255,0,0,1)',
borderColor: 'rgba(255,0,0,1)',
data: [3, 4, 10, 5, 8, 7],
yAxisID: 'y-axis-1'
}, {
label: 'Bar Data',
backgroundColor: 'rgba(0, 129, 214, 0.8)',
data: [12, 19, 3, 5, 2, 3]
}]
},
options: {
tooltips: {
callbacks: {
label: function(t, d) {
if (t.datasetIndex === 0) {
var xLabel = d.datasets[t.datasetIndex].label;
var yLabel = t.yLabel + '%';
return xLabel + ': ' + yLabel;
} else if (t.datasetIndex === 1) {
var xLabel = d.datasets[t.datasetIndex].label;
var yLabel = t.yLabel >= 1000 ? '$' + t.yLabel.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : '$' + t.yLabel;
return xLabel + ': ' + yLabel;
}
}
}
},
legend: {
display: false,
position: 'top',
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function(value, index, values) {
if (parseInt(value) >= 1000) {
return '$' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else {
return '$' + value;
}
}
}
}, {
id: 'y-axis-1',
position: 'right',
ticks: {
beginAtZero: true,
callback: function(value, index, values) {
return value + '%';
}
}
}]
}
},
plugins: [{
beforeDraw: function(chart) {
var labels = chart.data.labels;
labels.forEach(function(e, i) {
var bar = chart.data.datasets[1]._meta[0].data[i]._model;
var dataPoint = e.split(/\s/)[1];
if (dataPoint === '16')
bar.backgroundColor = 'blue';
else if (dataPoint === '17')
bar.backgroundColor = 'green';
});
}
}]
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="canvas"></canvas>