[英]Conditional Highcharts Marker Symbol Using Javascript
我發誓我以前看過這件事,但似乎找不到答案。
當我旋轉Highcharts時,有沒有一種方法可以讓內聯語句確定標記符號。
例如:
Highcharts.stockChart(chartElementId, {
title: {
text: 'Foo chart'
},
series: [{
name: 'Foo',
data: ...,
marker: {
enabled: true,
symbol: this.x > 1 ? 'circle' : 'square'
}
}]
});
我已經看到了在后期處理中完成的類似操作(在繪制圖表之后),但是我想知道在渲染過程中是否有辦法做到這一點。
您可以連接到負責繪制點的函數-在調用該函數之前,根據選項中定義的符號回調更新點。
包裝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';
}
},
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>
在這種情況下,系列應具有“ x”。 這個想法是在系列對象中使用自引用:
{ x:2,
name: 'Tokyo',
marker: {
enabled: true
},
init : function(){
this.marker.symbol = this.x > 1 ? 'circle' : 'square';
return this;
},
data: ....
}.init()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.