admin管理员组文章数量:1023758
Using charts.js, how do I add custom tooltip text for each dataset?
For dataset 1, I want to add "some text 1" to the tooltip.
For dataset 2, I want to add "some text 2" to the tooltip.
Using tooltip callbacks I can add extra text to the tooltip, but it applies the same text to the tooltips of both datasets. My code below is using a callback to do that. But how can I change the tooltip text for each dataset?
var chart = new Chart(ctx, {
// The type of chart we want to create
type: "line",
// The data for our dataset
data: {
labels: monthLabels,
datasets: [{
label: "Dataset 1",
data: [12, 123, 234, 32, 23],
}, {
label: "Dataset 2",
data: [4, 54, 765, 45, 5],
}]
},
// Configuration options go here
options: {
tooltips: {
enabled: true,
mode: 'single',
callbacks: {
label: function(tooltipItems, data) {
return tooltipItems.yLabel + 'some text here';
}
}
}
}
});
Using charts.js, how do I add custom tooltip text for each dataset?
For dataset 1, I want to add "some text 1" to the tooltip.
For dataset 2, I want to add "some text 2" to the tooltip.
Using tooltip callbacks I can add extra text to the tooltip, but it applies the same text to the tooltips of both datasets. My code below is using a callback to do that. But how can I change the tooltip text for each dataset?
var chart = new Chart(ctx, {
// The type of chart we want to create
type: "line",
// The data for our dataset
data: {
labels: monthLabels,
datasets: [{
label: "Dataset 1",
data: [12, 123, 234, 32, 23],
}, {
label: "Dataset 2",
data: [4, 54, 765, 45, 5],
}]
},
// Configuration options go here
options: {
tooltips: {
enabled: true,
mode: 'single',
callbacks: {
label: function(tooltipItems, data) {
return tooltipItems.yLabel + 'some text here';
}
}
}
}
});
Share
Improve this question
asked May 1, 2020 at 7:39
TinyTigerTinyTiger
2,19112 gold badges66 silver badges131 bronze badges
1
- Have you tried reading this? chartjs/docs/latest/configuration/… – Federico Provenziani Commented May 1, 2020 at 7:52
1 Answer
Reset to default 4You can do this easily by referring to the current dataset index tooltipItems.datasetIndex
and then based on that index set the tooltip text like:
label: function(tooltipItems, data) {
var text = tooltipItems.datasetIndex === 0 ? 'some text 1' : 'some text 2'
return tooltipItems.yLabel + ' ' + text;
}
Working Demo:
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: "line",
// The data for our dataset
data: {
labels: Array.from({length: 5}, (x,i)=> `Label ${i+1}`),
datasets: [{
label: "Dataset 1",
data: [12, 123, 234, 32, 23],
}, {
label: "Dataset 2",
data: [4, 54, 765, 45, 5],
}]
},
// Configuration options go here
options: {
tooltips: {
enabled: true,
mode: 'single',
callbacks: {
label: function(tooltipItems, data) {
var text = tooltipItems.datasetIndex === 0 ? 'some text 1' : 'some text 2'
return tooltipItems.yLabel + ' ' + text;
}
}
}
}
});
.chart-container {
width: 500px;
}
#myChart {
display: block;
width: 500px;
}
<script src="https://cdn.jsdelivr/npm/[email protected]"></script>
<div class="chart-container">
<canvas id="myChart"></canvas>
</div>
Using charts.js, how do I add custom tooltip text for each dataset?
For dataset 1, I want to add "some text 1" to the tooltip.
For dataset 2, I want to add "some text 2" to the tooltip.
Using tooltip callbacks I can add extra text to the tooltip, but it applies the same text to the tooltips of both datasets. My code below is using a callback to do that. But how can I change the tooltip text for each dataset?
var chart = new Chart(ctx, {
// The type of chart we want to create
type: "line",
// The data for our dataset
data: {
labels: monthLabels,
datasets: [{
label: "Dataset 1",
data: [12, 123, 234, 32, 23],
}, {
label: "Dataset 2",
data: [4, 54, 765, 45, 5],
}]
},
// Configuration options go here
options: {
tooltips: {
enabled: true,
mode: 'single',
callbacks: {
label: function(tooltipItems, data) {
return tooltipItems.yLabel + 'some text here';
}
}
}
}
});
Using charts.js, how do I add custom tooltip text for each dataset?
For dataset 1, I want to add "some text 1" to the tooltip.
For dataset 2, I want to add "some text 2" to the tooltip.
Using tooltip callbacks I can add extra text to the tooltip, but it applies the same text to the tooltips of both datasets. My code below is using a callback to do that. But how can I change the tooltip text for each dataset?
var chart = new Chart(ctx, {
// The type of chart we want to create
type: "line",
// The data for our dataset
data: {
labels: monthLabels,
datasets: [{
label: "Dataset 1",
data: [12, 123, 234, 32, 23],
}, {
label: "Dataset 2",
data: [4, 54, 765, 45, 5],
}]
},
// Configuration options go here
options: {
tooltips: {
enabled: true,
mode: 'single',
callbacks: {
label: function(tooltipItems, data) {
return tooltipItems.yLabel + 'some text here';
}
}
}
}
});
Share
Improve this question
asked May 1, 2020 at 7:39
TinyTigerTinyTiger
2,19112 gold badges66 silver badges131 bronze badges
1
- Have you tried reading this? chartjs/docs/latest/configuration/… – Federico Provenziani Commented May 1, 2020 at 7:52
1 Answer
Reset to default 4You can do this easily by referring to the current dataset index tooltipItems.datasetIndex
and then based on that index set the tooltip text like:
label: function(tooltipItems, data) {
var text = tooltipItems.datasetIndex === 0 ? 'some text 1' : 'some text 2'
return tooltipItems.yLabel + ' ' + text;
}
Working Demo:
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: "line",
// The data for our dataset
data: {
labels: Array.from({length: 5}, (x,i)=> `Label ${i+1}`),
datasets: [{
label: "Dataset 1",
data: [12, 123, 234, 32, 23],
}, {
label: "Dataset 2",
data: [4, 54, 765, 45, 5],
}]
},
// Configuration options go here
options: {
tooltips: {
enabled: true,
mode: 'single',
callbacks: {
label: function(tooltipItems, data) {
var text = tooltipItems.datasetIndex === 0 ? 'some text 1' : 'some text 2'
return tooltipItems.yLabel + ' ' + text;
}
}
}
}
});
.chart-container {
width: 500px;
}
#myChart {
display: block;
width: 500px;
}
<script src="https://cdn.jsdelivr/npm/[email protected]"></script>
<div class="chart-container">
<canvas id="myChart"></canvas>
</div>
本文标签: javascriptChartsjsHow to set custom tooltip text for each datasetStack Overflow
版权声明:本文标题:javascript - Charts.js - How to set custom tooltip text for each dataset - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745522811a2154392.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论