xAxis Labels.formatter does not work if changed to ES6 style #8580
Comments
Hi @Scarittagle, Internal note |
This is caused how fat arrow functions deal with context. It's not only syntactic sugar, "ES6 style", but also new Highcharts.chart('container', {
...
formatter: function () {
// this -> refers to the context passed on, in this case: "label"
}
...
}); new Highcharts.chart('container', {
...
formatter: () => {
// this -> refers to the context where chart was created, in this case "document"
}
...
}); Changed Edit: new Highcharts.chart('container', {
chart: {
events: {
load: function () {
setTimeout(() => {
console.log(this); // this refers to "chart" as it's a context of the higher scope
});
}
}
},
... new Highcharts.chart('container', {
chart: {
events: {
load: function () {
setTimeout(function () {
console.log(this); // this refers to "window" as it's a context of anonymous function
});
}
}
},
...
}); |
As @pawelfus mentioned this is the expected result when using arrow function, and we can not go around it either by using A possibility to consider though is providing the data on I'm not sure what would be the sensible action here. Do you have an opinion on this @TorsteinHonsi? |
The params are currently not used for anything, so that makes sense. But we should keep the scope as is, and just add a parameter. Demo with fix applied: http://jsfiddle.net/tDSrd/310/ |
Can we also apply this update to the tooltip formatter? |
We could, but then we risk breaking backwards compatibility because the tooltip formatter currently takes the |
Ah. Second parameter maybe? |
Expected behaviour
So In ES5 the format of writing the
labels.formatter
is like this:labels: { formatter: function() { //something..... } }
but if I changed to ES6 style:
labels: { formatter: () => { //something..... } }
It should render the label as usual.
Actual behaviour
Instead the chart stopped render all the xAxis labels.
Live demo with steps to reproduce
http://jsfiddle.net/tDSrd/291/
Product version
Highcharts 6.1.1
Affected browser(s)
Chrome Version 67.0.3396.87 (Official Build) (64-bit)
The text was updated successfully, but these errors were encountered: