ArticleZip > How To Display The Value Instead Of Percentage In A Pie Chart Using Jquery Highcharts

How To Display The Value Instead Of Percentage In A Pie Chart Using Jquery Highcharts

Pie charts are a popular way to visualize data in a digestible format. They make it easy to see proportions and percentages at a glance. If you're working with a pie chart using jQuery Highcharts and you want to display the actual value instead of the percentage, you've come to the right place! Let's dive into how you can achieve this tweak.

First things first, you'll need to have jQuery Highcharts set up in your project. If you haven't done so already, make sure to include the necessary Highcharts libraries in your HTML file. Once that's all set, you can proceed with the following steps to display the actual value in your pie chart.

To display the value in your chart, you'll need to manipulate the data labels. Highcharts allows you to customize these labels to show the values you want. Here's a simple example of how you can achieve this using jQuery:

Javascript

Highcharts.chart('container', {
    chart: {
        type: 'pie'
    },
    title: {
        text: 'Fruit Consumption'
    },
    series: [{
        data: [
            {
                name: 'Apples',
                y: 5,
                displayValue: '5 apples'
            },
            {
                name: 'Oranges',
                y: 3,
                displayValue: '3 oranges'
            }
        ]
    },
    plotOptions: {
        pie: {
            dataLabels: {
                enabled: true,
                format: '{point.displayValue}'
            }
        }
    }
});

In this code snippet, we've added a `displayValue` property to each data point in the series. This property holds the custom text you want to display instead of the default percentage value. By setting the `format` property in the `dataLabels` section to `{point.displayValue}`, we tell Highcharts to use the `displayValue` property of each point for the data label.

This simple customization allows you to override the default behavior of displaying percentages and show specific values in your pie chart. You can tweak the formatting to suit your needs, such as including additional information or styling the text differently.

Remember to adjust the data and labels to match your dataset and desired display format. You can customize further by leveraging Highcharts' extensive options for styling and configuration, allowing you to create visually appealing and informative charts tailored to your requirements.

By following these steps and customizing the data labels in your pie chart using jQuery Highcharts, you can present your data more effectively and provide valuable insights at a glance. Experiment with different configurations and styles to find the presentation that best suits your project's needs. Happy charting!