我正在使用带有Angular 分量的HighChart,同时我正在使用以下数据制作柱状图.

  accessGroupUsageData: AnalyticsStatus[] = [
    { y: 430, name: 'Public', percent: Math.round((430 / 4185) * 100) }, 
    { y: 430, name: 'Executivers', percent: Math.round((430 / 4185) * 100) },
    { y: 400, name: 'Convention Grounds', percent: Math.round((400 / 4185) * 100) },
    { y: 400, name: 'Group 4', percent: Math.round((400 / 4185) * 100) },
    { y: 400, name: 'Group 5', percent: Math.round((400 / 4185) * 100) },
    { y: 350, name: 'Group 6', percent: Math.round((350 / 4185) * 100) },
    { y: 350, name: 'Group 7', percent: Math.round((350 / 4185) * 100) },
    { y: 300, name: 'Group 8', percent: Math.round((300 / 4185) * 100) },
    { y: 250, name: 'Group 9', percent: Math.round((250 / 4185) * 100) },
    { y: 250, name: 'Group 10', percent: Math.round((250 / 4185) * 100) },
    { y: 225, name: 'Group 11', percent: Math.round((225 / 4185) * 100) },
    { y: 200, name: 'Group 12', percent: Math.round((200 / 4185) * 100) },
    { y: 200, name: 'Group 13', percent: Math.round((200 / 4185) * 100) }
  ]
// here are the colors
  gradientColors = {
    purple: 'var(--analytics-gradient-purple, #ffffff)', //#9379F9
    blue: 'var(--analytics-gradient-blue, #ffffff)', // #56A5FB
    lightblue: 'var(--analytics-gradient-lightblue, #ffffff)' // #35BDDC
  }

我想要实现的是 AccessGroupUsageData[0]为紫色到浅蓝色, 对每一列重复向下,以50%以上的紫色减少.50%以下的高度和任何50%以下的高度都可以从蓝色到浅蓝色.

第一次try 时,所有的柱子都从紫色变成了浅蓝色.

series: [
        {
          name: undefined,
          legendSymbol: undefined,
          color: {
            linearGradient: { x1: 0, x2: 0, y1: 0, y2: 1 },
            stops: [
              [0, this.gradientColors.purple], // start 
              [0.5, this.gradientColors.blue], // middle
              [1, this.gradientColors.lightblue] // end
            ],

          },
          type: 'column',
          borderRadius: 0,
          borderWidth: 0,
          zIndex: 3,
          stickyTracking: false,
          tooltip: {
            followPointer: false,
          },
          data: this.chartDataSource
        }
      ]

第二次try ,结果是所有列都是淡蓝色(并且手动更改为Percent Calculate*100仍然将 colored颜色 保留为浅蓝色),使用以下函数:

calculateGradientStops(data: number[]): Highcharts.GradientColorObject {
    const max = Math.max(...data);
    const stops: Highcharts.GradientColorStopObject[] = [];

    data.forEach(value => {
      const percent = value / max;
      if (percent <= .5) {
        stops.push([percent, this.gradientColors.purple]);
      } else {
        stops.push([percent, this.gradientColors.blue]);
      }
    });

    console.log(stops)

    return {
      linearGradient: { x1: 0, x2: 0, y1: 0, y2: 1 },
      stops: stops
    }
  }

除了上面的内容,我还在考虑使用style模式=true,但这会导致比目前所能做的更多的重构.

链接: 这就是它目前所做的-https://imgur.com/a/GOtFOKC 这就是我想要实现的--https://imgur.com/a/CkDIdte

推荐答案

所以基本上你想要实现这样的事情?

Highcharts.chart('container', {
  series: [{
    type: 'column',
    color: {
      linearGradient: [0, 0, 0, 300],
      stops: [
        [0, 'red'],
        [0.5, 'orange'],
        [1, 'green']
      ]
    },
    data: [2, 5, 9, 3, 6, 5]
  }]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>

<div id="container"></div>

Demo: https://jsfiddle.net/BlackLabel/4hbxn38v/
API: https://api.highcharts.com/class-reference/Highcharts.GradientColorObject

Css相关问答推荐

与Chrome和Edge相比,背景位置动画在Firefox中运行并不流畅

Tailwind仅在启用时才在按钮上设置group-hover

Angular Material:如何同时使用2个徽章

我找不出Firefox上奇怪的css行为的原因

条件宽度和省略号不适用于样式化组件 - React.js

包含tailwind 中固定纵横比框的内容

循环一个 sass/scss 变量以生成 css 变量

如何在特定元素之前 Select 每个元素

zoom 时闪烁的背景滤镜模糊

预期失败时使用toHaveStyle进行测试

如何使单选按钮看起来像切换按钮

选中时从 HTML 文本输入中移除蓝色光晕

固定元素在 Chrome 中消失

我应该在 HTML 中为我的 IMG 指定高度和宽度属性吗?

如何通过 JavaScript 重新触发 WebKit CSS 动画?

如何在 html/css 中的图像旁边垂直居中文本?

如何隐藏 Chrome 中 HTML5
元素上默认显示的箭头?

如何对齐标签和文本区域?

CSS3可以转换字体大小吗?

HTML5 和边框