Highcharts display legend pie chart

To display a legend in a pie chart using Highcharts, you can follow these steps:

  • Include the Highcharts library in your HTML file. You can either download it from the Highcharts website or include it from a CDN like this:
<script src="https://code.highcharts.com/highcharts.js"></script>
  • Create a container element for the chart in your HTML file, like this:
<div id="chart-container"></div>
  • In your JavaScript code, create a Highcharts chart object and configure it with the data and chart options. For example:
Highcharts.chart('chart-container', {
  chart: {
    type: 'pie'
  },
  title: {
    text: 'Pie Chart Example'
  },
  series: [{
    name: 'Sales',
    data: [
      {name: 'Chrome', y: 61.41},
      {name: 'Firefox', y: 10.85},
      {name: 'Edge', y: 4.67},
      {name: 'Safari', y: 4.18},
      {name: 'Other', y: 18.89}
    ]
  }],
  plotOptions: {
    pie: {
      allowPointSelect: true,
      cursor: 'pointer',
      dataLabels: {
        enabled: true,
        format: '{point.name}: {point.y:.2f}%'
      },
      showInLegend: true
    }
  },
  legend: {
    align: 'right',
    layout: 'vertical',
    margin: 0,
    verticalAlign: 'middle',
    itemMarginTop: 5,
    itemMarginBottom: 5
  }
});

In this example, we're creating a pie chart with the Highcharts chart function and passing in a set of options:

  • chart.type is set to 'pie' to create a pie chart
  • title.text is set to 'Pie Chart Example' to set the main title
  • series is set to an array with one object containing name and data properties to set the chart data
  • plotOptions.pie is set to configure the pie chart options, including the ability to select points, data labels, and legend
  • plotOptions.pie.dataLabels.format is set to a string format to customize the data labels for each pie slice
  • plotOptions.pie.showInLegend is set to true to show the legend in the chart
  • legend is set to configure the legend options, including alignment, layout, and margin

Note that you can customize many other options to further customize your pie chart and legend, such as the colors and styles of the pie slices and legend items, the position and styling of the chart title and axis labels, and the behavior of the chart tooltip. You can find more details and examples in the Highcharts documentation.

  1. Customizing legend appearance in Highcharts Pie Chart:

    • Customize the appearance of the legend in the Highcharts Pie Chart.
    Highcharts.chart('container', {
        chart: {
            type: 'pie'
        },
        title: {
            text: 'Customizing Legend Appearance in Highcharts Pie Chart'
        },
        legend: {
            backgroundColor: '#f2f2f2',
            borderRadius: 5,
            borderWidth: 1,
            borderColor: '#999999'
        },
        series: [{
            data: [{
                name: 'Category 1',
                y: 30
            }, {
                name: 'Category 2',
                y: 70
            }],
            colorByPoint: true
        }],
        // Additional configuration options...
    });
    
  2. Adding labels to Highcharts Pie Chart with legend:

    • Add labels to each data point in the Pie Chart and display them in the legend.
    series: [{
        data: [{
            name: 'Category 1',
            y: 30
        }, {
            name: 'Category 2',
            y: 70
        }],
        showInLegend: true,
        dataLabels: {
            enabled: true,
            format: '<b>{point.name}</b>: {point.y:.2f}%'
        },
        colorByPoint: true
    }],
    
  3. Toggle visibility of legend in Highcharts Pie Chart:

    • Allow users to toggle the visibility of the legend in the Pie Chart.
    legend: {
        enabled: true // Set to false to hide the legend initially
    },
    
  4. Interactive features in Highcharts Pie Chart with legend:

    • Implement interactive features with the legend for a better user experience.
    series: [{
        data: [{
            name: 'Category 1',
            y: 30
        }, {
            name: 'Category 2',
            y: 70
        }],
        events: {
            legendItemClick: function () {
                // Handle legend item click event
                alert('Legend item clicked: ' + this.name);
            }
        },
        colorByPoint: true
    }],
    
  5. Configuring position and style of legend in Highcharts:

    • Adjust the position and style of the legend for better readability.
    legend: {
        layout: 'vertical',
        align: 'right',
        verticalAlign: 'middle',
        itemStyle: {
            color: '#333333',
            fontSize: '12px'
        }
    },
    
  6. Highcharts Pie Chart with dynamic legend:

    • Dynamically update the legend based on external factors or user interactions.
    series: [{
        data: [{
            name: 'Category 1',
            y: 30
        }, {
            name: 'Category 2',
            y: 70
        }],
        events: {
            legendItemClick: function () {
                // Handle dynamic changes to the legend
                alert('Legend item clicked: ' + this.name);
            }
        },
        colorByPoint: true
    }],
    
  7. Hiding specific series in legend for Highcharts Pie Chart:

    • Hide specific series from being displayed in the legend.
    series: [{
        data: [{
            name: 'Category 1',
            y: 30
        }, {
            name: 'Category 2',
            y: 70,
            showInLegend: false // Hide this series in the legend
        }],
        colorByPoint: true
    }],