Skip to main content
PHP Blog

Back to all posts

How to Sort Tooltip Value In Chart.js?

Published on
3 min read
How to Sort Tooltip Value In Chart.js? image

Best Chart.js Tooltip Sorting Tips to Buy in October 2025

1 Mastering D3.js - Data Visualization for JavaScript Developers

Mastering D3.js - Data Visualization for JavaScript Developers

BUY & SAVE
$36.99
Mastering D3.js - Data Visualization for JavaScript Developers
+
ONE MORE?

To sort tooltip values in Chart.js, you can use the tooltips.callbacks.label function to customize the tooltip label. Within this function, you can access the tooltip item array and sort the values as needed before displaying them in the tooltip. By sorting the tooltip values, you can present the data in a more organized and meaningful way for the users.

How to customize the sorting function for tooltip values in chart.js?

To customize the sorting function for tooltip values in Chart.js, you can use the tooltips.callbacks.label function. This function allows you to define custom sorting logic for the tooltip labels.

Here is an example of how you can customize the sorting function for tooltip values in Chart.js:

var myChart = new Chart(ctx, { type: 'bar', data: { labels: ["A", "B", "C", "D", "E"], datasets: [{ label: 'Data', data: [10, 20, 15, 25, 30], backgroundColor: 'rgba(255, 99, 132, 0.2)', borderColor: 'rgba(255, 99, 132, 1)', borderWidth: 1 }] }, options: { tooltips: { callbacks: { label: function(tooltipItem, data) { var dataset = data.datasets[tooltipItem.datasetIndex]; var value = dataset.data[tooltipItem.index]; var sortedData = dataset.data.slice().sort((a, b) => a - b); var label = data.labels[tooltipItem.index] + ': ' + value;

                // Custom sorting logic
                var sortedIndex = sortedData.indexOf(value);
                return label + ' (Sorted Index: ' + sortedIndex + ')';
            }
        }
    }
}

});

In this example, we have defined a custom sorting logic inside the tooltips.callbacks.label function. We first retrieve the value of the tooltip item and create a sorted copy of the dataset data array. We then find the index of the value in the sorted array and append it to the tooltip label. This way, we can customize the sorting function for the tooltip values in Chart.js.

What are the different options for sorting tooltip values in chart.js?

There are various options for sorting tooltip values in chart.js, including:

  1. Ascending: Display tooltip values in ascending order.
  2. Descending: Display tooltip values in descending order.
  3. Nearest: Display the tooltip value closest to the mouse cursor.
  4. Range: Display tooltip values within a specified range.
  5. Custom: Allows for custom sorting of tooltip values based on user-defined criteria.

These options can be set using the "mode" property in the tooltip configuration section of the chart.js settings.

What is the default sorting method for tooltip values in chart.js?

The default sorting method for tooltip values in chart.js is to sort them in ascending order.

How to reset the sorting of tooltip values in chart.js?

To reset the sorting of tooltip values in a chart created with Chart.js, you can use the following steps:

  1. Get the reference to the chart instance that you want to reset the tooltip sorting for.

var myChart = new Chart(ctx, { type: 'bar', data: data, options: options });

var chartInstance = myChart;

  1. Access the options object of the chart instance and set the tooltip mode to 'index'.

chartInstance.options.tooltips.mode = 'index';

  1. Update the chart instance to apply the changes.

chartInstance.update();

By setting the tooltip mode to 'index', the tooltip values will be displayed in the same order as the data points in the dataset, effectively resetting the sorting.