Mahmood Kohansal
Mahmood Kohansal

Reputation: 1041

Gradient background bars in Highchart bar chart

My problem is changing background bars in a highchart bar chart. I can change bars to a specific color like below but I want to make it gradient.

In this code I used red color (FF0000) in chart options. How to change it for gradient backgroud?

HTML :

<div id="container" style="min-width: 310px; max-width: 800px; height: 400px; margin: 0 auto"></div>

JS :

$(function () {
    $('#container').highcharts({
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Historic World Population by Region'
        },
        colors: [
            '#ff0000'
            ],
        xAxis: {
            categories: ['Africa', 'America', 'Asia', 'Europe', 'Oceania'],
            title: {
                text: null
            }
        },
        yAxis: {
            min: 0,
            max: 100,
            title: {
                text: 'Percentage',
                align: 'high'
            },
            labels: {
                overflow: 'justify'
            }
        },
        tooltip: {
            valueSuffix: ' millions'
        },
        plotOptions: {
            bar: {
                dataLabels: {
                    enabled: true
                }
            }
        },
        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: [{
            name: 'Year 1800',
            data: [10, 31, 63, 3, 28]
        }]
    });
});

JSFiddle

Upvotes: 0

Views: 988

Answers (1)

Neville Dabreo
Neville Dabreo

Reputation: 329

JsFiddle

Please refer above fiddle to check the results

First, declare a gradient as per Highcharts

const color_grad1 = {
                    linearGradient: { x1: 0, x2: 0, y1: 0, y2: 1 },
                    stops: [
                    [0, '#fdfc47'],
                    [1, '#24fe41']
                                    ]
                        };

Then use gradient as follows:

data: [{y: 10, color: color_grad1}, 31, 63, 3, 28]

Upvotes: 1

Related Questions