admin管理员组文章数量:1332344
Is it possible to change the dataset labels that show up in the tooltip for a bubble chart.js chart?
As it stands right now, the dataset is based off the x,y,r
values, but I'd like to inject some additional content, so that instead of reading (5,55,27.5)
it reads something like: (Day:5, Total:55)
. I'd like to leave off the radius of 27.5
in the tooltip.
Is it possible to change the dataset labels that show up in the tooltip for a bubble chart.js chart?
As it stands right now, the dataset is based off the x,y,r
values, but I'd like to inject some additional content, so that instead of reading (5,55,27.5)
it reads something like: (Day:5, Total:55)
. I'd like to leave off the radius of 27.5
in the tooltip.
2 Answers
Reset to default 7Yes! It is possible.
To achieve that, you can use the following tooltip's label callback function :
tooltips: {
callbacks: {
label: function(t, d) {
return d.datasets[t.datasetIndex].label +
': (Day:' + t.xLabel + ', Total:' + t.yLabel + ')';
}
}
}
ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ
var chart = new Chart(ctx, {
type: 'bubble',
data: {
datasets: [{
label: 'Bubble',
data: [{
x: 5,
y: 55,
r: 27.5
}],
backgroundColor: 'rgba(0, 119, 290, 0.6)'
}]
},
options: {
tooltips: {
callbacks: {
label: function(t, d) {
return d.datasets[t.datasetIndex].label +
': (Day:' + t.xLabel + ', Total:' + t.yLabel + ')';
}
}
}
}
});
<script src="https://cdnjs.cloudflare./ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>
The previous answer doesn't work as of Chart.JS V3 because of these changes: https://github./chartjs/Chart.js/blob/master/docs/migration/v3-migration.md
The following code works in 4.1.1:
var chart = new Chart(ctx, {
type: 'bubble',
data: {
datasets: [{
label: 'Bubble',
data: [{
x: 5,
y: 55,
r: 27.5
}],
backgroundColor: 'rgba(0, 119, 290, 0.6)'
}]
},
options: {
plugins: {
tooltip: {
callbacks: {
label: function(item) {
return item.raw.r
}
}
}
}
}
});
<script src="https://npmcdn./chart.js@latest/dist/chart.umd.js"></script>
<canvas id="ctx"></canvas>
本文标签: javascriptChartjs bubble chart changing dataset labelsStack Overflow
版权声明:本文标题:javascript - Chart.js bubble chart changing dataset labels - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742325477a2453637.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论