如何实现Highcharts面积图fillColor随zone分区颜色动态变化
实现方案
你只需要将fillColor配置从全局plotOptions.area中移除,改为在每个zone配置项中单独定义对应颜色的渐变即可,Highcharts的zones本身支持为每个分区独立配置填充色属性。
修改后的代码示例如下:
this.chartOptions = { ... plotOptions: { area: { pointStart: chartStart, // 移除全局统一的fillColor配置 } }, series: [ { type: 'area', data: this.avgVoltageSeries, pointInterval: ticksTime, pointStart: chartStart, zones: [{ value: this.fenceNodeConfiguration.criticalVoltage / 1000, color: 'red', // 单独配置当前分区的红色渐变填充 fillColor: { linearGradient: {x1: 0, x2: 0, y1: 0, y2: 1}, stops: [ [0, 'red'], [1, 'white'] ] } }, { value: this.fenceNodeConfiguration.warningVoltage / 1000, color: 'orange', // 单独配置当前分区的橙色渐变填充 fillColor: { linearGradient: {x1: 0, x2: 0, y1: 0, y2: 1}, stops: [ [0, 'orange'], [1, 'white'] ] } }, { color: '#009900', // 单独配置当前分区的绿色渐变填充 fillColor: { linearGradient: {x1: 0, x2: 0, y1: 0, y2: 1}, stops: [ [0, '#009900'], [1, 'white'] ] } }], }] };
改动说明
- 删除了全局统一的填充色配置,避免所有分区共用同一种渐变
- 为每个zone新增了独立的fillColor属性,渐变规则保持你原有的从上到下从对应刻度色过渡到白色的逻辑,不同分区的填充渐变会自动随所属区域的刻度色切换
内容的提问来源于stack exchange,提问作者Riccardo Fachin
相关产品推荐
相关产品推荐

