【问题标题】:Set Color for Pie Chart using HighChart + JSON data使用 HighChart + JSON 数据为饼图设置颜色
【发布时间】:2016-01-12 06:00:21
【问题描述】:

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

Ex : if value @ 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() 并定义颜色代码。

【问题讨论】:

    标签: javascript highcharts highstock


    【解决方案1】:

    更好的选择是根据您的 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>

    【讨论】:

      【解决方案2】:

      我们可以通过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,
      }
      

      【讨论】:

      • 感谢您的回复,但这不是我正在寻找的解决方案.. 我从 JSON 文件中获取值,需要根据这些值定义颜色。
      • 您也可以在 json 数据中设置颜色选项。
      猜你喜欢
      • 1970-01-01
      • 2014-02-19
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多