I swear I've seen this done before, but can't seem to find the answer.
When I spin up Highcharts, is there a way to have an inline statement determine the marker symbol.
For example:
Highcharts.stockChart(chartElementId, {
title: {
text: 'Foo chart'
},
series: [{
name: 'Foo',
data: ...,
marker: {
enabled: true,
symbol: this.x > 1 ? 'circle' : 'square'
}
}]
});
I've seen something similar done in post-processing (after the chart is rendered), but I was wondering if there's a way to do it in the middle of rendering.
You can hook to a function responsible for drawing points - and before it is called, update points according to a symbol callback defined in the options.
Wrapping drawPoints
method:
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 config:
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>
In that case series should have 'x'. The idea is to use self-reference in series object:
{ x:2,
name: 'Tokyo',
marker: {
enabled: true
},
init : function(){
this.marker.symbol = this.x > 1 ? 'circle' : 'square';
return this;
},
data: ....
}.init()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.