Highcharts Tutorial
Chart Types
To display a legend in a pie chart using Highcharts, you can follow these steps:
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="chart-container"></div>
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 charttitle.text
is set to 'Pie Chart Example'
to set the main titleseries
is set to an array with one object containing name
and data
properties to set the chart dataplotOptions.pie
is set to configure the pie chart options, including the ability to select points, data labels, and legendplotOptions.pie.dataLabels.format
is set to a string format to customize the data labels for each pie sliceplotOptions.pie.showInLegend
is set to true
to show the legend in the chartlegend
is set to configure the legend options, including alignment, layout, and marginNote 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.
Customizing legend appearance in 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... });
Adding labels to Highcharts Pie Chart with 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 }],
Toggle visibility of legend in Highcharts Pie Chart:
legend: { enabled: true // Set to false to hide the legend initially },
Interactive features in Highcharts Pie Chart with legend:
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 }],
Configuring position and style of legend in Highcharts:
legend: { layout: 'vertical', align: 'right', verticalAlign: 'middle', itemStyle: { color: '#333333', fontSize: '12px' } },
Highcharts Pie Chart with dynamic legend:
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 }],
Hiding specific series in legend for Highcharts Pie Chart:
series: [{ data: [{ name: 'Category 1', y: 30 }, { name: 'Category 2', y: 70, showInLegend: false // Hide this series in the legend }], colorByPoint: true }],