Highcharts Tutorial
Chart Types
A stacked bar chart is a chart type that displays data as a series of bars stacked on top of each other, with each bar representing a category or data series and the height of the bar representing the value of that category or series. Highcharts supports stacked bar charts with the chart.type
option set to 'bar'
and the plotOptions.bar.stacking
option enabled.
To create a stacked bar 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: 'bar' }, title: { text: 'Stacked Bar Chart' }, xAxis: { categories: ['Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5'] }, yAxis: { title: { text: 'Y Axis Label' } }, plotOptions: { bar: { stacking: 'normal' } }, series: [{ name: 'Series 1', data: [10, 20, 30, 40, 50] }, { name: 'Series 2', data: [20, 30, 40, 50, 60] }, { name: 'Series 3', data: [30, 40, 50, 60, 70] }] });
In this example, we're creating a stacked bar chart with the Highcharts chart
function and passing in a set of options:
chart.type
is set to 'bar'
to create a bar charttitle.text
is set to 'Stacked Bar Chart'
to set the main titlexAxis.categories
is set to an array of category labels for the x-axisyAxis.title
is used to set the label for the y-axisplotOptions.bar.stacking
is set to 'normal'
to enable stacking of barsseries
is set to an array of objects with a name
and data
property to define the series data. The data
property contains an array of values for each category, which are displayed as stacked bars. The bars are stacked on top of each other to show the contribution of each data series to the overall value.With these options, you can create a stacked bar chart with customizable data and appearance. Note that Highcharts supports other options to customize the appearance of bar charts, including the color
, marker
, and tooltip
options.
Customizing appearance in Highcharts Stacked Bar Chart: To customize the appearance of a Stacked Bar Chart in Highcharts, you can use various options like colors, border styles, and more. Here's an example code snippet:
Highcharts.chart('container', { chart: { type: 'bar' }, plotOptions: { series: { stacking: 'normal', borderColor: 'white', borderWidth: 2 } }, series: [{ name: 'Stack 1', data: [10, 20, 30, 40, 50] }, { name: 'Stack 2', data: [5, 10, 15, 20, 25] }] });
Adding data to Highcharts Stacked Bar Chart:
Adding data to a Stacked Bar Chart involves specifying the stacking
property. Use the data
property within the series
object. Here's an example:
series: [{ name: 'Stack 1', data: [10, 20, 30, 40, 50] }, { name: 'Stack 2', data: [5, 10, 15, 20, 25] }]
Grouped Stacked Bar Chart in Highcharts: To create a grouped Stacked Bar Chart, use multiple series within the same chart. Here's an example:
series: [{ name: 'Group 1', data: [10, 20, 30, 40, 50] }, { name: 'Group 2', data: [5, 10, 15, 20, 25] }]
Interactive features in Highcharts Stacked Bar Chart: Highcharts supports interactive features like tooltips, click events, and legends in Stacked Bar Charts. You can enable these features to enhance user interaction. Here's a snippet for tooltip configuration:
tooltip: { pointFormat: 'Value: {point.y}' }
Handling multiple series in Highcharts Stacked Bar Chart: Handling multiple series in a Stacked Bar Chart involves defining each series with its own set of data. Here's an example:
series: [{ name: 'Series 1', data: [10, 20, 30, 40, 50] }, { name: 'Series 2', data: [5, 10, 15, 20, 25] }]