Kvasimodo
Kvasimodo

Reputation: 151

How to make gradient in highcharts?

I need to make different gradient color for different lines

For example, now I have two lines, but both of them has the same gradient color (blue):

Highcharts.chart('container', {
    chart: {
        type: 'areaspline'
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },

    plotOptions: {
        series: {
            fillColor: {
                linearGradient: [0, 0, 0, 300],
                stops: [
                    [0, Highcharts.getOptions().colors[0]],
                    [1, Highcharts.color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
                ]
            }
        }
    },

    series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    },
    {
        data: [10, 20, 79.4, 50.2, 40.0, 50.0, 110.6, 90.5, 113.4, 144.1, 75.6, 24.4]
    }]
});

https://jsfiddle.net/5g3khvme/4/

I'd like to use default colors of highcharts for lines, but for blue it should be blue gradient, for black line it should be black gradient and etc.

Upvotes: 1

Views: 46

Answers (1)

Weedoze
Weedoze

Reputation: 13943

You can use the callback function of your chart to iterate the series and update the fillColor based on the serie.color

Highcharts.chart('container', {
  chart: {
    type: 'areaspline'
  },
  xAxis: {
    categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  },
  series: [{
      data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    },
    {
      data: [10, 20, 79.4, 50.2, 40.0, 50.0, 110.6, 90.5, 113.4, 144.1, 75.6, 24.4]
    }
  ]
}, (chart) => {
  chart.series.forEach((serie) => {
    serie.update({
      fillColor: {
        linearGradient: [0, 0, 0, 300],
        stops: [
          [0, serie.color],
          [1, Highcharts.color(serie.color).setOpacity(0).get('rgba')]
        ]
      }
    })
  })
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>

<div id="container"></div>

Upvotes: 1

Related Questions