Highcharts Stacked Bar Chart

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:

  • 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: '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 chart
  • title.text is set to 'Stacked Bar Chart' to set the main title
  • xAxis.categories is set to an array of category labels for the x-axis
  • yAxis.title is used to set the label for the y-axis
  • plotOptions.bar.stacking is set to 'normal' to enable stacking of bars
  • series 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.

  1. 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]
        }]
    });
    
  2. 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]
    }]
    
  3. 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]
    }]
    
  4. 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}'
    }
    
  5. 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]
    }]