Aniket Tiwari
Aniket Tiwari

Reputation: 3998

stacked bar chart with separate label

How to have separate labels for each bar chart in the below-stacked bar chart. Currently, if you notice for 2 bars I have one category. For every bar, I need a separate category. I tried to play with the name options in the series but it didn't work for me. Also, I tried to give multiple categories that didn't work either.

    $(function() {
  $('#container').highcharts({
    chart: {
      type: 'bar'
    },
    title: {
      text: 'Historic World Population by Region'
    },
    subtitle: {
      text: 'Source: <a href="https://en.wikipedia.org/wiki/World_population">Wikipedia.org</a>'
    },
    xAxis: {
      categories: ['Africa', 'America', 'Asia', 'Europe', 'Oceania'],
      title: {
        text: null
      }
    },
    yAxis: {
      min: 0,
      title: {
        text: 'Population (millions)',
        align: 'high'
      },
      labels: {
        overflow: 'justify'
      }
    },
    tooltip: {
      valueSuffix: ' millions'
    },
    plotOptions: {
      bar: {
        dataLabels: {
          enabled: true
        },
        stacking: 'normal'
      }
    },
    legend: {
      layout: 'vertical',
      align: 'right',
      verticalAlign: 'top',
      x: -40,
      y: 80,
      floating: true,
      borderWidth: 1,
      backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
      shadow: true
    },
    credits: {
      enabled: false
    },
    series: [
    // first stack
        {
            data: [29.9, 71.5, 106.4, 129.2, 144.0],
            stack: 0
        }, {
            data: [30, 176.0, 135.6, 148.5, 216.4],
            stack: 0
        // second stack
        }, {
            data: [106.4, 129.2, 144.0, 29.9, 71.5],
            stack: 1
        }, {
            data: [148.5, 216.4, 30, 176.0, 135.6],
            stack: 1
        }
    ]
  });
});

If you notice in the below image rather than having 1 label for 2 bars. I need a separate label for each bar

jsfiddleimage

Upvotes: 0

Views: 107

Answers (1)

Aniket Tiwari
Aniket Tiwari

Reputation: 3998

I have found the solution by using the x-axis formatter instead of categories

var labels = ['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth']
var labels1 = ['Overall Rate', 'Overall Rate', 'Overall Rate', 'Overall Rate', 'Overall Rate', 'Overall Rate']
$(function() {
  $('#container').highcharts({
    chart: {
      type: 'bar'
    },
    title: {
      text: 'Historic World Population by Region'
    },
    subtitle: {
      text: 'Source: <a href="https://en.wikipedia.org/wiki/World_population">Wikipedia.org</a>'
    },
    xAxis: {
     // categories: ['Africa', 'America', 'Asia', 'Europe', 'Oceania'],
     labels: {
      formatter: function() {
       
        return labels[this.pos] + "<br/>" + labels1[this.pos];
      }
    },
      title: {
        text: null
      }
    },
    yAxis: {
      min: 0,
      title: {
        text: 'Population (millions)',
        align: 'high'
      },
      labels: {
        overflow: 'justify'
      }
    },
    tooltip: {
      valueSuffix: ' millions'
    },
    plotOptions: {
      bar: {
        dataLabels: {
          enabled: true
        },
        stacking: 'normal'
      }
    },
    legend: {
      layout: 'vertical',
      align: 'right',
      verticalAlign: 'top',
      x: -40,
      y: 80,
      floating: true,
      borderWidth: 1,
      backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
      shadow: true
    },
    credits: {
      enabled: false
    },
    series: [
    // first stack
        {
            data: [29.9, 71.5, 106.4, 129.2, 144.0],
            stack: 0
        }, {
            data: [30, 176.0, 135.6, 148.5, 216.4],
            stack: 0
        // second stack
        }, {
            data: [106.4, 129.2, 144.0, 29.9, 71.5],
            stack: 1
        }, {
            data: [148.5, 216.4, 30, 176.0, 135.6],
            stack: 1
        }
    ]
  });
});

image

Upvotes: 1

Related Questions