简体   繁体   English

highcharts 中的条件标记颜色

[英]conditional marker colors in highcharts

I'm using Highcharts and I want to fill markers in line chart with different colors.我正在使用 Highcharts,我想在折线图中填充不同颜色的标记。 For example: when variable "a" is 1 then fill marker with red else fill with green.例如:当变量“a”为 1 时,用红色填充标记,否则用绿色填充。 Is it possible to do that?有可能这样做吗?

Here is the code: http://jsfiddle.net/EnyCJ/1/这是代码: http : //jsfiddle.net/EnyCJ/1/

I was trying to do that with formatter but it doesn't work.我试图用格式化程序来做到这一点,但它不起作用。 Any suggestions?有什么建议?

var a=1;

plotOptions: {
 series: {
  marker: {
   fillColor: {
    formatter: function () {
      if (a == 1) {
       return 'red'
      } else {
       return 'green'
      }
    }
   },
  lineWidth: 2,
  }
 }
},

Try:尝试:

fillColor: a==1 ? "#ff0000" : "#00ff00"

etc.等等。

You also use zones:您还可以使用区域:

$(function () {
    $('#container').highcharts({
        series: [{
            data: [-10, -5, 0, 5, 10, 15, 10, 10, 5, 0, -5],
            zones: [
              {
                value: 0,
                color: '#f7a35c'
              }, 
              {
                color: '#90ed7d'
              }
            ]
        }]
    });
});

Try it in jsfiddle.net 在 jsfiddle.net 中尝试

How about removing the logic from your chart and use it only to display data?如何从图表中删除逻辑并仅将其用于显示数据?

var a = 1,
    color = a ? 'red' : 'green';

plotOptions: {
     series: {
         marker: {
             fillColor: color,
             lineWidth: 2,
         }
     }
}

Expanding upon Asad Saeeduddin's answer :扩展Asad Saeeduddin 的回答

I am using dataLabels in a doughnut pie chart and the proposed solution was very specific to a singular situation.我在甜甜圈饼图中使用了 dataLabels,并且建议的解决方案非常针对单一情况。 I wanted to change the label text colors for individual pie slices, based on conditional logic, comparing values.我想根据条件逻辑更改单个饼图的标签文本颜色,比较值。

Just sharing because my search brought me here.只是分享,因为我的搜索把我带到了这里。

data: outerData,
dataLabels: {
    formatter:
        function () {
            if ((outerData[this.point.x].y > innerData[this.point.x].y) && (this.point.x != 0)) {
                return this.y > 0.1 ? '<span style="fill: red;">' + this.point.name + ': ' + this.y + '% ' + '</span>' : null;
            } else {
                return this.y > 0.1 ? '<span style="fill: #006400;">' + this.point.name + ': ' + this.y + '% ' + '</span>' : null;
           }
        }
    }

I was working on something like this and I thought I would update this post with a 2021 response.我正在做这样的事情,我想我会用 2021 年的回复更新这篇文章。 Turns out with Highcharts you have to feed in an array into data, but it can be an array of objects that have parameters associated with the point.事实证明,在 Highcharts 中,您必须将数组输入数据,但它可以是具有与该点相关联的参数的对象数组。 I was attempting to make the point fill to be on a different scale than the y axis see below:我试图使点填充与 y 轴的比例不同,如下所示:

tempScale(value: any) {
      if(value > 32) {
        return 'red'
      }
      else if(value > 31) {
        return 'orange'
      }
      else if(value > 30) {
        return 'yellow'
      }
      else if(value > 28) {
        return 'green'
      }
      else if(value > 27) {
        return 'blue'
      }
      else {
        return '#99ffff'
      }
  }

I ended up finding this codepen where is shows an example on how you can feed in an array of objects and use the keys y, color, marker, and radius to feed attributes to each item in the array.我最终找到了这个代码笔,其中显示了一个示例,说明如何输入对象数组并使用键 y、颜色、标记和半径为数组中的每个项目提供属性。

   {
      y: 9,
      color: 'green',
      marker: {
        radius: 10
      }
    }

I ended up combining the 2 and calculating out the color for each of them before they were pushed into the array and it worked great for my purposes.在将它们推入数组之前,我最终组合了 2 并计算了它们中的每一个的颜色,这对我的目的非常有用。

array.push({
          y: item.attributes.Sal, 
          color: this.tempScale(item.attributes.Temp), 
          marker: {
            lineWidth: 1,
            lineColor: 'black',
          }
        })

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

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