gpt4 book ai didi

javascript - 使用 Javascript 的条件 Highcharts 标记符号

转载 作者:行者123 更新时间:2023-12-01 04:00:04 24 4
gpt4 key购买 nike

我发誓我以前见过这样做,但似乎找不到答案。

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

例如:

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

我在 post-processing 中看到过类似的事情(在图表渲染之后),但我想知道是否有办法在渲染过程中做到这一点。

最佳答案

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

包装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);
})

系列配置:

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

实例

http://jsfiddle.net/09mqttxn/

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]
}]
});
<script src="https://code.highcharts.com/highcharts.src.js"></script>

<div id="container" style="height: 400px"></div>

关于javascript - 使用 Javascript 的条件 Highcharts 标记符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42259571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com