Highcharts gradient fill how to?

时间:2018-01-23 19:13:04

标签: php json highcharts raspberry-pi

I read a Json file to compile my highchart graph, but I want to have a nice gradient under the lines, Something like we see here. Highcharts Area chart gradient fill

I've tried already some options and had already a search in the highcharts, but I haven't find anything useable.

Any ideas? Thanks!

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Highcharts Example</title>
    <script type="text/javascript" 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script>
    <script type="text/javascript">
    $(document).ready(function() {
var options = {   
chart: {
    renderTo: 'container',
    type: 'spline',
    marginRight: 100,
    marginBottom: 60
},
title: {
    text: 'Temperature / Humidity / CPU',
    x: -20 //center
},
subtitle: {
    text: 'Overview 24u',
    x: -20
},
xAxis: {
    categories: []
},
yAxis: {
    title: {
        text: ''
    },
},
tooltip: {
    formatter: function() {
            return '<b>'+ this.series.name +'</b><br/>'+
            this.x +': <b>'+ this.y +'</b>';
    }
},

legend: {
    layout: 'vertical',
    align: 'right',
    verticalAlign: 'top',
    x: 0,
    y: 50,
    borderWidth: 0
},

series: []
}

        $.getJSON("data.php", function(json) {
            options.xAxis.categories = json[0]['data'];
            options.series[0] = json[1];
    options.series[0].color = '#e2432b';
    options.series[0].lineWidth = 4;

            options.series[1] = json[2];
    options.series[1].color = '#c4faff';

    options.series[2] = json[3];
    options.series[2].color = '#b0ffaa';
    options.series[2].linearGradient = 'x1:0,x2:0,y1:0,y2:1';
    options.series[2].stops = '0, #b0ffaa', '1, #554488';
    options.series[2].lineWidth = 2;
            chart = new Highcharts.Chart(options);
        });
    });
    </script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script src="http://code.highcharts.com/modules/exporting.js"></script>
</head>
<body>
    <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</body>
</html>

Changed code, see comments below. But no gradient :(

...
            options.series[1] = json[2];

    options.series[2] = json[3];
    options.series[2].fillColor = {
      linearGradient: {
                    x1: 0,
                    y1: 0,
                    x2: 0,
                    y2: 1
                },
       stops: [
         [0, '#000000'],
         [1, '#333333']
       ]
     };
    options.series[2].lineWidth = 2;
            chart = new Highcharts.Chart(options);
...

1 个答案:

答案 0 :(得分:0)

衬里梯度定义存在问题,应该是:

  series[2].fillColor = {
    linearGradient: {
      x1: 0,
      x2: 0,
      y1: 0,
      y2: 1
    },
    stops: [
      [0, '#b0ffaa'],
      [1, '#554488']
    ]
  };