score:8
Accepted answer
If you log the context you could see its an array containing objects, with the default interaction mode you are using it only contains a single item so you can select that one and then access the label
attribute on it like so:
var options = {
type: 'doughnut',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
}]
},
options: {
plugins: {
tooltip: {
callbacks: {
label: function(context) {
let label = new Intl.NumberFormat('en-US', {
style: 'percent',
minimumFractionDigits: 0,
maximumFractionDigits: 0
}).format(context.formattedValue);
return label;
},
title: function(context) {
let title = context[0].label;
return title;
}
},
}
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.js"></script>
</body>
Source: stackoverflow.com
Related Query
- Chart.JS tooltip callbacks label and title (v3.5)
- Chartjs: get the label title on the radar's chart tooltip
- 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 tooltip and label width is zeroed
- How to remove square label from tooltip and make its information in one line?
- chart.js scatter chart - displaying label specific to point in tooltip
- show label in tooltip but not in x axis for chartjs line chart
- Chart.js bar chart : Grid color and hide label
- ng2-charts customize data and whole html content of tooltip displayed when hovering on bar chart
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- Remove the label and show only value in tooltips of a bar chart
- Chart js: how can I align the legend and the title
- Chart.js line chart tooltip shows wrong label when line doesn't start at first label
- Chartjs adding icon to tooltip and label
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How to change the label and grid line position on a timeseries chart in Chart.js 3?
- Chart.js: adding a custom label to each item, and displaying it with a custom tooltip
- How to underline and bold tooltip chart js
- How to sort XY line chart tooltip items based on value and add thousands separators?
- How can I have different values for the chart and the tooltip in chart.js?
- How to increase space between label and chart area in chart.js
- Map with pie chart and label
- show label inside the chart - angular.js and chart.js
- Chart with Time axis only displaying first grid line and tick label (unitStepSize)
- How do you set x and y axis and Title for a line chart using charts.js?
- How to customize the title and tooltip in ux-chartjs?
- Chart.js - x-axis and tooltip label shows "undefined" in Chrome when browser language is NOT English
- Charts JS: Doughnut chart and hiding tooltip for specific data index within each dataset
- Chart Piechart in only show percentage on hover but it shows data and label with percentage both i just want to show percentage only percentage
- Move chart x axis label and borders
More Query from same tag
- Chart.js - Uncaught ReferenceError: chart is not defined
- Flickering of charts and getcontext error with chartjs in the context of Vuejs
- Why do literal arrays work, but not coded arrays?
- Remove data text from Chart.js on hover
- remove undefined label in the upper part of a chart
- First point on scatter plot on JavaScript chart.js not showing
- Chart.js chart flickering when re-opening page
- Charts.js How to add a link when you click a barchart?
- Trying to have Doughnut chart with dql result chartjs
- Full size download Chartjs canvas on mobile device
- chart.js data wont update
- passing json data to chartjs
- How to get certain text from json object value
- Trying to create something from chart.js
- How to avoid detached HTMLCanvasElements when updating canvas with JavaScript?
- Adding space to begin of xAxis ticks in chartJS
- ChartJs bar chart - keep bars left instead of equally spread across the width
- How to use images as key for chart.js legend
- How to know which stack is clicked, in a grouped stacked bar (chartjs)?
- how change color of labels of legends of chart.js
- js error: while trying to display graph using chart.js: Uncaught TypeError: Cannot read property 'call' of undefined
- Bootstrap > Extend ChartJS canvas horizontally to match overflowing columns
- How to get clicked bar chart tooltip data?
- Chart.js unable to display data
- Chart.js v3.x time series on x axis
- Updating a chart from chart.js in vue
- Error: Cannot read property 'props' of undefined, Calling parent Method in Child
- How to get hundreds of JS object properties into arrays to use as chart.js axes/data
- Chart.js bar chart mouse hovering highlights all the datasets instead of just the selected one
- Is it possible to make 3D pie chart using chart.js?