列高图中的渐变

voi*_*oid 3 javascript css highcharts

我正在使用highcharts绘制柱形图,但我想要的是所有列的渐变颜色,即每列应具有不同的颜色.

我可以在整个图形中将n梯度放在n列中,但不能跨越梯度.

附上简短的想法: 在此输入图像描述

wer*_*eld 6

这是一个有趣的想法,所以我对它进行了一次尝试.使用Highcharts单色填充PIE 演示的变体,我能够获得接近您想要的东西.棘手的部分是知道你有多少数据,以便在系列结束前亮度计算不会被最大化.如果你知道你将拥有多少数据点,你可以预先填充原型,或者你可以使它成为一个以点数作为参数的函数.无论如何,这是我的尝试:

$(function() {
  // Make monochrome colors and set them as default for all pies
  Highcharts.getOptions().plotOptions.column.colors = (function() {
    var colors = [],
      base = '#e2535c', //Set to the starting color you want.
      i;
    // This is the part you need to setup. 
    //The "50" is just some arbitrarily large value for this demo.
    for (i = 0; i < 50; i += 1) {
      // Start out with a darkened base color (negative brighten), and end
      // up with a much brighter color
      // I modified the math here to use 25 as the increment.
      // Your needs may vary.
      colors.push(Highcharts.Color(base).brighten((i - 3) / 25).get());
    }
    return colors;
  }());

  var chart = new Highcharts.Chart({
    chart: {
      renderTo: 'container',
      type: 'column'
    },
    xAxis: {
      categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    plotOptions: {
      column: {
        colorByPoint: true // This needs to be true.
      }
    },
    series: [{
      data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    }]
  });
});
Run Code Online (Sandbox Code Playgroud)

现场演示.我很确定这可以用另一种方式完成,但这让我有90%的方式.

经过一些搜索可以在javascript中生成颜色渐变的方法后,我遇到了RainbowVis-JS.这使您可以设置开始和结束颜色(以及任何中间颜色)以及您需要多少停止.我用这种chart.events.load方法设置了一种方法.我正在做的是获取我的多少点series.data并将其设置为范围.然后我循环遍历数据中的数据点并使用索引值返回颜色并使用以下内容更新点Point.update:

chart: {
  renderTo: 'container',
  type: 'column',
  events: {
    load: function() {
      var rainbow = new Rainbow();
      // Set start and end colors
      rainbow.setSpectrum('#e2535c', '#ea9056');

      // Set the min/max range
      var theData = this.series[0].data;
      var rangeLow = 0;
      var rangeHigh = theData.length
      rainbow.setNumberRange(rangeLow, rangeHigh);

      // Loop over data points and update the point.color value
      for (index = 0; index < theData.length; ++index) {

        this.series[0].data[index].update({
          color: '#' + rainbow.colourAt(index)
        });
      }
    }
  }
},
Run Code Online (Sandbox Code Playgroud)

演示.