使用Javascript的条件Highcharts标记符号

时间:2017-02-15 20:38:41

标签: javascript highcharts

我发誓我之前已经看过这件事,但似乎无法找到答案。

当我旋转Highcharts时,有没有办法让内联语句确定标记符号。

例如:

Highcharts.stockChart(chartElementId, {
    title: {
        text: 'Foo chart'
    },
    series: [{
        name: 'Foo',
        data: ...,
        marker: { 
            enabled: true,
            symbol: this.x > 1 ? 'circle' : 'square'
        }
    }]
});

我在post-processing中看到了类似的内容(在呈现图表之后),但我想知道是否有一种方法可以在渲染过程中完成。

2 个答案:

答案 0 :(得分:2)

您可以挂钩到负责绘制点的函数 - 在调用它之前,根据选项中定义的符号回调更新点。

包装var result = dbContext.Items.GroupBy(x => x.MobileNo) .Select(x => x.OrderByDescending(y => y.CreatedDate).First()); 方法:

drawPoints

系列配置:

const H = Highcharts;

H.wrap(H.Series.prototype, 'drawPoints', function(p) {
  const options = this.options;
  const symbolCallback = options.marker && options.marker.symbolCallback;
  const points = this.points;

  if (symbolCallback && points.length) {
    points.forEach(point => {
      const symbol = symbolCallback.call(point);
      if (symbol) {
        point.update({
          marker: {
            symbol: symbol
          }
        }, false)
      }
    })
  }

  p.call(this);
})

实例

http://jsfiddle.net/09mqttxn/

series: [{
  marker: {
    symbolCallback: function() {
      return this.x > 2 ? this.y > 150 ? 'circle' : 'triangle' : 'square';
    }
  },
var H = Highcharts;

H.wrap(H.Series.prototype, 'drawPoints', function(p) {
  const options = this.options;
  const symbolCallback = options.marker && options.marker.symbolCallback;
  const points = this.points;

  if (symbolCallback && points.length) {
    points.forEach(point => {
      const symbol = symbolCallback.call(point);
      if (symbol) {
        point.update({
          marker: {
            symbol: symbol
          }
        }, false)
      }
    })
  }

  p.call(this);
})

H.chart('container', {
  series: [{
    marker: {
      symbolCallback: function() {
        return this.x > 2 ? this.y > 150 ? 'circle' : 'triangle' : 'square';
      }
    },
    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]
  }]
});

答案 1 :(得分:0)

在这种情况下,系列应该有' x'。我们的想法是在系列对象中使用自引用:

{   x:2,
    name: 'Tokyo',
    marker: {
        enabled: true
    },
    init : function(){
        this.marker.symbol = this.x > 1 ? 'circle' : 'square';
        return this;
    },
    data: ....            
}.init()

jsfiddle