How to display pie chart data values of each slice in chart.js

How to display pie chart data values of each slice in chart.js

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Displaying Pie Chart Data Values in Chart.js

Achieving a specific visual style for chart data often requires stepping beyond default configurations. In Chart.js, the standard tooltip that appears on hover is the default behavior, but displaying exact values directly on the slices of a pie chart offers immediate data context without requiring user interaction. This approach transforms the chart from a qualitative overview into a quantitative display immediately visible to the viewer.

To implement this in Chart.js (specifically versions 2.x, which aligns with your provided code structure), you must configure the dataset options within the data array rather than relying solely on global chart options. The key lies in defining a label for each data point and setting the value to be displayed explicitly.

When constructing your dataset, ensure that every object includes both a numeric value and a corresponding label string. For example, if you are visualizing status counts, your data structure should look like this:

var piedata = [
    {
        value: 10,
        color: 'blue',
        label: 'Status A (10)'
    },
    {
        value: 5,
        color: 'red',
        label: 'Status B (5)'
    }
];

The critical detail here is the format of the label. Chart.js defaults to showing just the label text (e.g., "Status A"). To force the display of the value alongside the text, you must append it using a template string within the label property itself. This bypasses the need for complex tooltip logic and renders the text directly on the canvas during the initial draw cycle.

Your existing JavaScript function can be refactored to incorporate this change seamlessly. You don't need to generate a new legend or manipulate the canvas context manually; Chart.js handles the rendering of these labels automatically once they are defined in the data array. Here is how your drawPie function should look:

function drawPie(canvasId, data, legend) {
    var ctx = $("#pie-canvas-" + canvasId).get(0).getContext("2d");

    var piedata = [];
    $.each(data, function(i, val) {
        // Append the value to the label string to display both
        piedata.push({
            value: val.count,
            color: val.color,
            label: val.status + ' (' + val.count + ')' 
        });
    });

    var options = {
        // You can still customize tooltips if needed, but they are optional for on-slice labels
        tooltipTemplate: "<%=Math.round(circumference / 6.283 * 100)%>", 
        responsive: true
    };

    var pie = new Chart(ctx).Pie(piedata, options);
    
    if (legend) {
        document.getElementById("legend").innerHTML = pie.generateLegend();
    }
}

One significant consideration when adding labels directly to slices is readability. If your dataset contains many items or the values are large, the text can overlap with adjacent slices or obscure parts of the chart itself. This is a common trade-off between information density and visual clarity. In such cases, you might consider reducing the font size or using a smaller canvas width, though this risks making the numbers harder to read.

Alternatively, if you find that the on-slice labels become too cluttered for your specific data volume, Chart.js provides a robust alternative: custom tooltips. By removing the label property from the dataset objects and configuring a custom tooltip plugin or using the built-in tooltip with a specific template, you can keep the chart clean while providing detailed data upon hover. This approach is often preferred in modern dashboards where screen real estate is limited.

If you are building this within a Laravel application, ensuring that your PHP script correctly escapes quotes and handles JSON serialization for the data variable passed to the JavaScript function is vital. The PHP code snippet you provided uses string concatenation which works well for simple arrays but can break if the data contains special characters like quotes or newlines. Consider encoding the $data3 variable as a JSON string in PHP before injecting it into the script tag to maintain structural integrity.

For more complex interactivity, such as updating the chart dynamically without reloading the page, you would need to destroy the existing chart instance before re-initializing it with new data. However, for static reports rendered via PHP, the method described above provides a clean and efficient solution.

Our network