score:0
Accepted answer
You get access to the chart object and have the data index so you can just get the correct label from the labels array like so:
title: (items) => {
const item = items[0];
const {
chart
} = item;
const nextLabel = chart.data.labels[item.dataIndex + 1] || '';
return `${item.label}, next label: ${nextLabel}`;
}
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'orange'
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderColor: 'pink'
}
]
},
options: {
plugins: {
tooltip: {
callbacks: {
title: (items) => {
const item = items[0];
const {
chart
} = item;
const nextLabel = chart.data.labels[item.dataIndex + 1] || '';
return `${item.label}, next label: ${nextLabel}`;
}
}
}
}
}
}
const 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.7.1/chart.js"></script>
</body>
Source: stackoverflow.com
Related Query
- ChartJS 3.7.1 tooltip callback, get label value for the next index
- ChartsJS get value of Data/Dataset Label for Tooltip
- how I can get label name when user click on particular area in doughnut chart.js? I am not able to find the index of selected area for chart.js
- Truncating canvas labels in ChartJS while keeping the full label value in the tooltips
- show label in tooltip but not in x axis for chartjs line chart
- Chartjs change the specific label color in x axis in callback function
- How to get onClick Event for a Label in ChartJS and React?
- How to get rid of the white square outline or border in the tooltip for chartjs?
- Charts.js - Display data label only for the last value
- Chart.js: Can't get a coordinate for value for x axis unless x axis has the exact same value
- problem hidding the label in chartjs (ng2chart for angular)
- How to add the value for each label to pie legend
- Why this Tooltip callback for ChartJS works and this doesn't
- Chartjs: get the label title on the radar's chart tooltip
- ChartJS tooltip label for pie cart being cut
- How to get the index of the barchart label that has been clicked with react-chart js-2?
- How do I prevent Chartjs tooltip callback returning multiple instances of the same value?
- ChartJS fails to render one of the lines in cartesian chart following update after change to max Y axis label value
- Can't get if else statement in tooltip label to work in chartjs
- How can i get the Chart JS Bar Graph Bar Label and Value on click?
- How to disable a tooltip for a specific dataset in ChartJS
- Chartjs v2 - format tooltip for multiple data sets (bar and line)
- ChartJS Line Graph - Multiple Lines, Show one Value on Tooltip
- How to add label for ChartJs Legend
- Border radius for the bar chart in ChartJS
- Chart JS show multiple data points for the same label
- 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
- how to change Y axis value dynamically based on user input in Chartjs for Line chart?
- React ChartJS 2 : Get data on clicking the chart
More Query from same tag
- ChartJS react-chart-js2 Options not working
- Chart.js: scrollable/swipable time-based x-axis
- code works fine on jsfiddle but one function is not working on website
- chart.JS time axis labels should be just in hours format
- Drill down charts dynamic functionality
- Chart.js 3.5.1 Attribute question about zeroLineColor
- Can the colors of bars in a bar chart be varied based on their value?
- Is it possible to draw min / max lines on Chart JS
- Add all data in the tooltip of Chart JS
- On click event to show name of pie chart slice in chartsJS
- chartJS line chart not plotting values that are less than minY
- Canvas doens't display in SAPUI5
- vb.net string to chartjs data field?
- Angular 4: Different color for bar in bar chart dynamically using ChartJS
- Sql Server query that returns prices from each shop on each date and adds a 0 if no data is present for the shopId on a particular date
- ChartJS distinguish clicks between chart area, dataset and legend
- ChartJs Scatter with Bars
- Chart.js time scale graph - xAxis labelling
- chart.js wide bar with time scale
- Pie Donut chart with different sector sizes
- Uncaught ReferenceError: require is not defined using react-chartjs.min.js
- ChartJS - Bottom labels displayed vertically
- Angular: How to change color of chartjs?
- Force chart to redraw or update Chart.js
- Angular-chartjs Data labels over bar chart
- Chartjs axis description is not selectable and therefor not copyable
- ChartJS adding 3yAxes
- Chart.js bumpy line
- Chart.js zoom start at zoomed in
- Line graph with linear timescale in Chart.js