【问题标题】:Highcharts Update Grouped Series Data Point ColorsHighcharts 更新分组系列数据点颜色
【发布时间】:2018-02-10 18:16:01
【问题描述】:

我有一个类似于here 的烛台和成交量图表,但对成交量条和烛台有自定义颜色。这可以正常工作,直到由于添加了新数据或按下范围选择器按钮而重新绘制或分组图表。重新绘制或分组图表时,蜡烛颜色和音量条颜色似乎都没有正确转移。我正在使用以下重绘功能,但它似乎没有任何效果。奇怪的是,每次将数据分组到一个新的数据分组中时,当您将鼠标悬停在音量条上时,它们会显示正确的颜色,并且会保持正确的颜色,直到重新绘制图表。 Here 是显示这种情况的小提琴。我的重绘功能如下。

编辑:如果小提琴出现 500 错误,请尝试 here

有什么办法可以解决这个问题或得到它,以便我可以正确绘制颜色?

const redraw = (event) => {

 const chartTarget = event.target;

  if (chartTarget.series[0].hasGroupedData) {

    // Get all the candlesticks that are shown
    const candlesticks = chartTarget.series[0].points;

    // Get all the volume bards that are shown
    const volumeBars = chartTarget.series[1].points;

    // Go through the candle chart and volume points and update the colors
    for (let i = 0; i < candlesticks.length; i++) {
      const candle = candlesticks[i];
      const volumeBar = volumeBars[i];

      if (candle.close > candle.open) {
        const color = 'green';
        volumeBar.color = color;
        candle.color = color;
      } else if (candle.close < candle.open) {
        const color = 'red';
        candle.color = color;
        volumeBar.color = color;
      }
    }
  }
};

【问题讨论】:

  • 小提琴返回错误 500
  • 是的,我认为 jsfiddle 现在存在服务器问题。试试这个链接jsfiddle.net/x4gy675f

标签: highcharts highstock


【解决方案1】:

整个周末我都在为同一个问题头疼。我根本不是 highcharts 专家,但这就是我想出的。它的灵感来自 questionquestion

http://jsfiddle.net/b7cm8zum/

小提琴的相关部分是:

    Highcharts.stockChart('container', {
  chart: {
    events: {
      load: function(event) {
        // Get the volume series by id.
        var volSeries = this.series.find(function(s) {
          return s.userOptions.id === 'volume';
        });
        // Override the pointAttribs function on the volume series.
        volSeries.pointAttribs = (function(original) {
          return function(point, state) {
            // Call the original pointAttribs function.
            var attribs = original.apply(this, arguments);

            // Get the price series using the id.
            var priceSeries = point.series.chart.series.find(function(s) {
              return s.userOptions.id === 'price';
            });

            // Find the candle corresponding to the current volume point.
            var candle;
            if (point.series.hasGroupedData) {
              // With grouped data, we need to find the candle from the grouped data.
              var datagroup = point.dataGroup;
              var groupIdx = point.series.groupMap.indexOf(datagroup);

              candle = priceSeries.groupedData[groupIdx];
            } else {
              // Non-grouped data, we can just use the normal data.
              candle = priceSeries.data[point.index];
            }

            // Choose the color for the volume point based on the candle properties.
            var color = 'blue';
            if (candle.close > candle.open) {
              color = 'green';
            } else if (candle.close < candle.open) {
              color = 'red';
            }
            // Set the volume point's attribute(s) accordingly.
            attribs.fill = color;
            // Return the updated attributes.
            return attribs;
          };
        })(volSeries.pointAttribs);
        // Need to call update so the changes get taken into account on first draw.
        this.update({});
      }
    }
  },
...

基本上,我们会覆盖体积系列上的pointAttribs function,以便我们可以进行颜色计算。 pointAttribs 函数是一个内部函数,因此使用风险自负。

在性能方面,这不应该太昂贵 - 对于非分组数据,它使用预先存在的索引;对于分组数据,它必须在 series.groupMap 上使用 indexOf 来获取索引。

体积系列颜色设置为标准candlestick options - “color”和“upColor”,还有“lineColor”和“upLineColor”选项可用。

【讨论】:

  • 非常有趣。那行得通,但是如果我使用带有强制分组的自定义范围选择器按钮,那么它似乎会中断。在这里检查这个小提琴:jsfiddle.net/holtc/0v8f80dh
  • 在这种情况下,似乎自定义 pointAttribs 在初始绘制后没有被调用,我推测这可能是因为在加载函数完成并且第一次绘制发生后自定义点被替换为 highcharts。在我的应用程序中,我正在创建图表,然后添加不同的系列,然后添加自定义 pointAttribs 函数 - 这似乎有效,所以我怀疑这是 pointAttribs 被覆盖的时间。
【解决方案2】:

如果您使用这篇文章中的解决方案,一切似乎都可以正常工作:Highcharts update grouped data point color

    const color = 'green';
    volumeBar.graphic.css({
      color: color
    });

    candle.graphic.css({
      color: color
    });

现场演示: http://jsfiddle.net/BlackLabel/w7nv82r2/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多