score:124
In the V2.0 the tooltipTemplate option is deprecated. Instead you can use callbacks to modify the displayed tooltips. There is a sample for the usage of callbacks here and you can find the possible callbacks in the documentation under Chart.defaults.global.tooltips
In your case I would do the following:
window.myLine = new Chart(chart, {
type: 'line',
data: lineChartData,
options: {
tooltips: {
enabled: true,
mode: 'single',
callbacks: {
label: function(tooltipItems, data) {
return tooltipItems.yLabel + ' €';
}
}
},
}
});
Don't forget to set the HTML meta tag:
<meta charset="UTF-8">
score:0
To generalize code, let's use unitPrefix
and unitSuffix
for the datasets, for example:
datasets: [
{
label: 'Loss Rate',
data: [],
unitSuffix: "%",
},
{
label: 'Price',
data: [],
unitPrefix: "$",
},
Then we could have this code:
options: {
tooltips: {
callbacks: {
label: function (tooltipItem, data) {
let dataset = data.datasets[tooltipItem.datasetIndex];
let blocks = [];
if (dataset.label) {
blocks.push(${dataset.label} + ':');
}
if (dataset.unitPrefix) {
blocks.push(dataset.unitPrefix);
}
blocks.push(dataset.data[tooltipItem.index])
if (dataset.unitSuffix) {
blocks.push(dataset.unitSuffix);
}
return blocks.join(' ');
},
},
},
},
score:0
As we continue our way along the release chain, the answer once again changes in Chart.js v3.X with the updated options API.
An example of adding temperature units is as follows:
const options = {
plugins: {
tooltip: {
callbacks: {
label: (item) =>
`${item.dataset.label}: ${item.formattedValue} °C`,
},
},
},
}
score:7
Just updating @Luc Lérot's answer. I had the same problem and his code helped me out but not fixed it, I had to modify it to work for me. Using Chart.js version 2.6.0
var ctx = $(chartCanvas);
window.chartObject = new Chart(ctx, {
type: 'bar',
data: chartData,
options: {
tooltips: {
callbacks: {
label: function (tooltipItems, data) {
return data.datasets[tooltipItems.datasetIndex].label + ': ' + data.datasets[tooltipItems.datasetIndex].data[tooltipItems.index] + ' €';
}
}
}
}
});
score:8
If you have a stacked bar chart (and I assume a stacked line chart) and you want to format all the data points included in a single bar with a currency symbol, you can do something like this:
tooltips: {
mode: 'label',
callbacks: {
label: function (tooltipItems, data) {
return '$' + tooltipItems.yLabel;
}
}
},
Note the value of mode
.
If you want to have finer control of the tool tip, for example include the labels as they appear the chart's legend, you can do something like this:
tooltips: {
mode: 'single', // this is the Chart.js default, no need to set
callbacks: {
label: function (tooltipItems, data) {
var i, label = [], l = data.datasets.length;
for (i = 0; i < l; i += 1) {
label[i] = data.datasets[i].label + ' : ' + '$' + data.datasets[i].data[tooltipItems.index];
}
return label;
}
}
},
score:11
This set "label + value + €"
options: {
legend: {
display: false
},
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
return data.labels[tooltipItem.index] + ': ' + data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index] + '€';
}
}
}
}
score:20
See if it helps:
var config = {
options: {
tooltips: {
callbacks: {
title: function (tooltipItem, data) { return data.labels[tooltipItem[0].index]; },
label: function (tooltipItem, data) {
var amount = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
var total = eval(data.datasets[tooltipItem.datasetIndex].data.join("+"));
return amount + ' / ' + total + ' ( ' + parseFloat(amount * 100 / total).toFixed(2) + '% )';
},
//footer: function(tooltipItem, data) { return 'Total: 100 planos.'; }
}
},
}
};
score:58
In addition to iecs' answer, if you want to return the exact default text and add some more (like a € sign in your case), use following syntax :
var ctx = $(chartCanvas);
window.chartObject = new Chart(ctx, {
type: 'bar',
data: chartData,
options: {
tooltips: {
callbacks: {
label: function(tooltipItems, data) {
return data.datasets[tooltipItems.datasetIndex].label +': ' + tooltipItems.yLabel + ' €';
}
}
}
}
});
Source: stackoverflow.com
Related Query
- Chart.js V2: Add prefix or suffix to tooltip label
- chart.js scatter chart - displaying label specific to point in tooltip
- show label in tooltip but not in x axis for chartjs line chart
- ChartJS add tooltip to a grouped bar chart
- Add all data in the tooltip of Chart JS
- Chart.js line chart tooltip shows wrong label when line doesn't start at first label
- Add a custom label to the top or bottom of a stacked bar chart
- Add a text as tooltip for each point on a line chart
- How to add label in chart.js for polar chart area
- ChartJS version 3 how to add percentage to pie chart tooltip
- Add HTML to label of bar chart - chart js
- How to sort XY line chart tooltip items based on value and add thousands separators?
- ChartJS add custom tooltip to a stacked bar chart
- I want to add label on only specific vue chart
- Chartjs: get the label title on the radar's chart tooltip
- Chart JS tooltip label not showing correct value
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- Chart.js v3.60 - Add % symbol to the end of the tooltip label on a doughnut chart?
- Chart JS tooltip and label width is zeroed
- Add Extra label in a pie chart
- Chart.js add label inside bubble chart
- How to add label square to Bar Chart using Chart.js
- Chartjs pie chart tooltip mode label
- Chart.js - How to Add Text in the label of the Chart with JavaScript?
- How to add text inside the doughnut chart using Chart.js?
- Chart Js Change Label orientation on x-Axis for Line Charts
- How to remove square label from tooltip and make its information in one line?
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- How to modify chartjs tooltip so i can add customized strings in tooltips
- ChartJS - Draw chart with label by month, data by day
More Query from same tag
- UnitStepSize for regular time interval with Chartjs
- Dynamic dataset from MySQL query with Chart.js
- How to make bars different colours in Chart.js with csv data?
- How to rotate the Label text in doughnut chart slice vertically in chart.js canvas, Angular 12?
- Chart.js - Connect two datasets in a line chart at same y value
- How to display multiple y axis titles on seperate lines and rotated 90 degrees clockwise in chart.js
- Chart.js canvas and chart width gets overwritten when redrawn
- Multiple chart on one page using chart.js and Flask app
- How do I access chart.js options dynamically?
- Chart.js zoom start at zoomed in
- Chart.js (Radar Chart) different scaleLineColor for each scaleLine
- Chart js update multiple charts
- Chart.js How to make more space between doughnut inside doughnut?
- How do I implement Laravel Analytics with Chart.js
- Fill Chart.js bar chart with diagonal stripes or other patterns
- Dynamically created linechart holds previous data value
- Chart.js - Shift/Stagger labels horizontally (for x axis) instead of rotating
- Convert PHP array to chart.js compatible data
- ChartJS create chart Ajax
- How to display Labels on Doughnut chart in Chart.js
- Is there a way to draw floating rectangle using chart.js
- How can I set 3 color to the same chartjs bar?
- Chart.js - tooltips issue with multiple charts
- Draw vertical and horizonal lines on the radar chart (like x-y axes)
- Chart.js Box Annotations Fill Entire Chart
- How to hide the labels of graphs that are not toggled in ChartJS
- Connecting points between missing data in chart.js
- How to hide tooltips with their respective dataset
- Chart.Js how to hide datapoints with "0" value in Stacked Bar Chart
- ChartJS bar chart with legend which corresponds to each bar