繁体   English   中英

使用HighChart + JSON数据为饼图设置颜色

[英]Set Color for Pie Chart using HighChart + JSON data

我正在从以JSON格式存储的数据生成饼图。 我正在尝试根据JSON值更改颜色。

例如:如果值@ json [0] ['data'] [0] [0] =“ FAILED” // setColor(RED)。

我可以使用options.series.color为列堆栈图设置颜色,但是当我尝试将此选项与饼图一起使用时,它会将数据转换为系列,并且无法在容器上呈现该图。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
  function getData(id) {
    $.getJSON("pie.php", {
      id: id
    }, function(json) {
      option.series = json;
      chart = new Highcharts.chart(options);
    });
  }
</script>

我们可以仅在调用“图表”之前在getData函数中设置颜色吗?还是我需要使用Highcharts.setOptions()并定义颜色代码。

更好的选择是根据您的json数据创建系列。 这是您可以根据数据指定颜色的方法。

var serie = {
    data: []
};
var series = [serie];

jQuery.each(jsonData, function(index, pointData) {

    var point = {
        name: pointName,
        y: pointData.Value,
        color: pointData.Value == 'FAILED' ? 'ff0000' : '00ff00',    
        serverData: pointData
    };

    serie.data.push(point);
});
chart.series = series;

要么

看看这个更简单的版本

的jsfiddle

 $( document ).ready(function() { var data = [{ "name": "Tokyo", "data": 3.0 }, { "name": "NewYork", "data": 2.0 }, { "name": "Berlin", "data": 3.5 }, { "name": "London", "data": 1.5 }]; // Highcharts requires the y option to be set $.each(data, function (i, point) { point.y = point.data; point.color = parseFloat(point.data) > 3 ? '#ff0000' : '#00ff00'; }); var chart = new Highcharts.Chart({ chart: { renderTo: 'container', type: 'pie' }, series: [{ data: data }] }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://code.highcharts.com/highcharts.js"></script> <div id="container" style="height: 300px"></div> 

我们可以通过setOption函数设置highchart自定义颜色

Highcharts.setOptions({
  colors: ['#F64A16', '#0ECDFD',]
});

它为我的饼图设置了颜色。

动态3D色彩的另一种解决方案

实际上是针对主题选择的自定义功能

3种颜色设置为颜色可变

var colors = Highcharts.getOptions().colors;
        $.each(colors, function(i, color) {
            colors[i] = {
                linearGradient: { x1: 0, y1: 0, x2: 1, y2: 0 },
                stops: [
                    [0, '#0ECDFD'],
                    [0.3, '#F64A16'],
                    [1, color]
                ]
            };

        });

并直接按顺序分配

 {
  type : 'column',
    name : 'bug',
    data : [],
    color : colors,                 
    pointWidth : 28,
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM