【问题标题】:Highcharts Columnrange data labels high and low different colorsHighcharts Columnrange 数据标签高低不同颜色
【发布时间】:2016-10-31 04:37:26
【问题描述】:

在基本的 Highcharts 柱形图中,如何为低值分配一种颜色(蓝色)并为高值分配不同的颜色(红色)?

我指的图表是这个:http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/columnrange/

所以对于 1 月,“-9.7”应该是蓝色的,“9.4”应该是红色的。

在另一个例子中,我试过这个:

    series: [{
        name: 'Temperatures',
        data: [
           {low: 2, high: 6, color: 'green'},
           {low: 1, high: 9, color: 'yellow'},
           {low: -3, dataLabels: {color: 'red'}, high: 5, color: 'blue'},
           {low: 0, high: 7, color: 'orange'}
        ],
    color: '#b9deea',
    borderColor: '#92cbde',
    borderRadius: 4
    }]

但这会将蓝色列的低值和高值的数据标签颜色更改为红色。

提前致谢。

猴面包树

【问题讨论】:

    标签: javascript jquery highcharts label


    【解决方案1】:

    更新答案

    在格式化程序回调中,您可以在 span 内包装文本并正确设置样式。

    formatter: function () {
                        var color = this.y === this.point.high ? 'red' : 'blue';
    
                        return '<span style="color:' + color + '">' + this.y + '°C</span>';
                    }
    

    示例:http://jsfiddle.net/6ofbr32b/1/

    为点片段着色

    您必须将一个点分成两点 - 这将代表其负值和正值,将阈值设置为 0 和negativeColor。然后调整工具提示和数据标签。

    这样就可以实现分割;

    //plotOptions.columnRange
    negativeColor: 'red',
    threshold: 0,
    borderWidth: 0,
    
    //series
    keys: ['x', 'low', 'high', 'part'],
      data: [
        [0,-9.7, 0, 'neg'],
        [0,0,9.4, 'pos'],
        [1,-8.7, 0, 'neg'],
        [1, 0, 6.5, 'pos'],
        [2,-3.5, 0, 'neg'],
        [2, 0, 9.4, 'pos'],
        [3,0.0, 22.6],
        [4,2.9, 29.5],
      ]
    

    'part' 是一个帮助器,可用于调整工具提示和数据标签。

    数据标签,因此如果点被分割,将只显示一条边

    dataLabels: {
          enabled: true,
          formatter: function() {
            if (this.point.part === 'neg' && this.y === this.point.low) {
                return this.y + '°C';
            } else if (this.point.part === 'pos' && this.point.high === this.y) {
                return this.y + '°C';
            } else if (!this.point.part) {
                return this.y + '°C';
            }
            return '';
          }
        }
    

    和工具提示

    tooltip: {
      pointFormatter: function () {
        var points = this.series.points,
                low = this.low,
            high = this.high;
        if (this.part === 'neg') {
            low = this.low;
          high = points[this.index + 1].high;
        } else if (this.part === 'pos') {
            low = points[this.index - 1].low;
          high = this.high;
        }
        return '<span style="color:' + this.series.color + '">\u25CF</span> ' + this.series.name + ': <b>' + low + '°C</b> - <b>' + high + '°C</b><br/>';
      }
    },
    

    示例:http://jsfiddle.net/xdg67kuo/3/

    用堆积柱形图也可以达到想要的效果: example

    【讨论】:

    • 我不认为这回答了这个问题,这是关于为数据标签着色,而不是实际数据点,也不一定是关于正/负,而是关于数据点。您列出的相同策略也可以回答这个问题,但是,如果适用于专门解决数据标签的颜色,而不是点
    • 不错的答案,比我想象的要简单。
    • 谢谢。这根据需要起作用,并且比我预期的要简单得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多