Question

The classical example from Highcharts tutorials is:

$(function () { 
 $('#container').highcharts({
    chart: {
        type: 'bar'
    },
    title: {
        text: 'Fruit Consumption'
    },
    xAxis: {
        categories: ['Apples', 'Bananas', 'Oranges']
    },
    yAxis: {
        title: {
            text: 'Fruit eaten'
        }
    },
    series: [{
        name: 'Jane',
        data: [1, 1, 4]
    }, {
        name: 'John',
        data: [5, 7, 3]
    }]
});
});

This creates 2 sets of 3 bars. How to add another bar which would be independent of the categories (i.e. one bar for the entire graph instead of adding it to every series)? E.g.: "Recommended servings of fruit per day"

Apples                                -
Bananas                               -
Oranges                               ----

Apples                                -----
Bananas                               -------
Oranges                               ---

Recommended servings of fruit per day ----
Was it helpful?

Solution

You can use different series type, (columnrange), to get this done, for example: http://jsfiddle.net/3bQne/1163/

$('#container').highcharts({
    chart: {
        type: 'bar'
    },
    title: {
        text: 'Fruit Consumption'
    },
    xAxis: {
        minRange: 0.5,
        categories: ['Apples', 'Bananas', 'Oranges', 'Recommended']
    },
    yAxis: {
        title: {
            text: 'Fruit eaten'
        }
    },
    series: [{
        name: 'Jane',
        data: [1, 1, 4]
    }, {
        name: 'John',
        data: [5, 7, 3]
    }, {
        type: 'columnrange',
        name: 'Recommended',
        data: [ [3, 0, 7] ],
        tooltip: {
            pointFormat: '<span style="color:{series.color}">\u25CF</span> {series.name}: <b>{point.high}</b><br/>'
        }
    }]
});

Note: To use columnrange, add highcharts-more.js file.

Note2: Added minRange for xAxis, to make at least one column visible

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top