如何更改特定范围的 highcharts 中的不透明度

yzckvree  于 2022-11-10  发布在  Highcharts
关注(0)|答案(1)|浏览(252)

我有一个系列,我想在Highcharts中将以下数据的x轴上的不透明度从23更改为0.2,更改为52
在x轴上,从23到52个数据点的不透明度应为0.2,其余数据点将保持不变。

series: [{
    data: [23, 42, 52, 47, 64],
    zoneAxis: 'x',
    zones: [{
        value: 1.00001,
    }, {
        value: 2.00001,
    }, {
        value: 3.00001,
    }]
}]

我如何才能做到这一点?

vc9ivgsu

vc9ivgsu1#

使用半透明颜色来实现这一点- RGBA或HEX。

series: [{
    data: [23, 42, 52, 47, 64],
    zoneAxis: 'x',
    zones: [{
        value: 1.00001,
        color:  'rgba(0, 255, 0, 0.2)'
    }, {
        value: 2.00001,
        color:  'rgba(255, 0, 0, 0.2)'
    }, {
        value: 3.00001,
        color: 'green'
    }, {
        color: 'red'
    }]
}]

演示:https://jsfiddle.net/BlackLabel/1b0zsw5r/
**EDIT:**区域不透明度选项在API中不可用,但您可以扩展Higcharts来实现。下面是一个示例插件:

(function(H) {
  H.Point.prototype.getZone = function() {

    function addAlpha(color, opacity) {
      const _opacity = Math.round(Math.min(Math.max(opacity || 1, 0), 1) * 255);
      return color + _opacity.toString(16).toUpperCase();
    }

    var series = this.series,
      zones = series.zones,
      zoneAxis = series.zoneAxis || 'y',
      zone,
      i = 0;

    zone = zones[i];
    while (this[zoneAxis] >= zone.value) {
      zone = zones[++i];
    }

    function customZone(noOpacityColor) {
      let isOpacityColor = addAlpha(noOpacityColor, zone.opacity).slice(0, 9);
      if (zone.opacity) {
        zone.color = isOpacityColor
      } else {
        this.color = noOpacityColor
      }
    }

    if (!this.nonZonedColor) {
      this.nonZonedColor = this.color;
    }

    if (zone && zone.color && !this.options.color) {
      customZone(zone.color)
      this.color = zone.color;
    } else {
      customZone(this.nonZonedColor)
    }

    return zone;
  };
}(Highcharts));

https://www.highcharts.com/docs/extending-highcharts/extending-highcharts

相关问题