简体   繁体   English

更改同一系列Highcharts中不同点的颜色

[英]Change color of different points in same series Highcharts

I have recently been working with highcharts alot, and originally I got a finished product using a bar chart, but all of the different variables were in different series so it was kinda crammed. 我最近一直在与highcharts一起工作,最初我使用条形图获得了成品,但是所有不同的变量都在不同的系列中,所以有点塞满了。 I am now trying to put stuff all in the same series (just trying different things) but am having some issues. 我现在尝试将所有内容放到同一系列中(只是尝试不同的内容),但是遇到了一些问题。

Depending on the y-value I want to be able to change the color of the bar for that point. 我希望能够根据y值更改该点的条形颜色。 I have done it in the past with something like this: 我过去是这样的:

  for (x = 0; x < 5; x++) {
    var color = '';
    if ($scope.jsondata[x].data[0] > 75) {
      $scope.jsondata[x].color = '#d9534f';
    } else if ($scope.jsondata[x].data[0] > 50) {
      $scope.jsondata[x].color = '#f0ad4e';
    } else {
      $scope.jsondata[x].color = '#5cb85c';
    }

    $scope.jsondata[x].name = setName($scope.jsondata[x].name);

  }

The data was in a different format in that case since the values were in different series. 在这种情况下,数据的格式不同,因为值的序列不同。 So I figured a few tweaks to where the data is coming from would be good, but no. 因此,我想了一下对数据来自何处进行一些调整的方法,但是没有。

I have this function where I am initially filling up the charts with data from a webservice and if you note the 2 console.debug() s where I first print out the series (which displays the entire series with all of its extra data including the data[] array that has the 5 values I want to access). 我具有此功能,最初我会使用来自Web服务的数据填充图表,如果您注意到2 console.debug() ,我将在其中首先打印出该系列(显示整个系列及其所有额外数据,包括data[]数组,该数组具有我要访问的5个值)。 But on the next console.debug() where I print the data just to see if I can access it it says its null? 但是在下一个console.debug() ,我在其中打印数据以查看是否可以访问它时说它为null? Makes no sense. 没有意义。

function initializeData() {
  $http.get(url).success(function(ret) {
    $scope.jsondata = ret;
    console.debug("here");
    var newdata = [];
    console.debug($scope.chart.series[0]);
    console.debug($scope.chart.series[0].data[0]);
    for (x = 0; x < 5; x++) {
      //$scope.chart.series[0].data[x].color = '#5cb85c';
      newdata.push([$scope.jsondata[x].name, $scope.jsondata[x].data]);
    }

    $scope.chart.series[0].setData(newdata);
  });
}

I want to set the colors of the bars in accordance to that first chunk of code, but even just trying to set them all like I did with //$scope.chart.series[0].data[x].color = '#5cb85c'; 我想根据第一段代码设置条形的颜色,但即使只是尝试像//$scope.chart.series[0].data[x].color = '#5cb85c'; it won't work, because it says the data array is empty. 它不起作用,因为它说data数组为空。 I have a plunker here with what I am doing. 我有一个在这里plunker与我在做什么。 Just doesnt make much sense. 只是没有多大意义。 You can view what is going on in the javascript console. 您可以在javascript控制台中查看发生的情况。 Like check out how the chart is full of its values, and you can scroll down to the data portion and it says data: Array[5] and you can go in there and see the values I am feeding it. 就像检查图表中的值如何充满一样,您可以向下滚动到data部分并显示data: Array[5]然后可以进入其中并查看我输入的值。 But the next line you can't access that same array. 但是下一行您将无法访问相同的数组。

All you need to do is to replace array form of your data to object. 您需要做的就是将数据的数组形式替换为对象。 This case you can set your color explicitly 这种情况下,您可以显式设置颜色

for (x = 0; x < 5; x++) {
  var item = $scope.jsondata[x];
  newdata.push({name:setName(item.name), y:item.data, color: getColor(item.data)});
}

Here's my update to your plunker http://plnkr.co/edit/UaW6RqM4PsGoy5IdpXDR 这是我对您的矮人的更新http://plnkr.co/edit/UaW6RqM4PsGoy5IdpXDR

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

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