question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tooltip Formatter Callback Context

See original GitHub issue

Hi, i am trying to write a custom tooltip formatter. But when i use the options.tooltip.formatter callback. The this context inside the formatter callback gets bound to the tooltip and so i cant access my component data.

You can see this issue here: http://plnkr.co/edit/0ZJ29O8YqxvNW5486CDI?p=preview

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
Nelladelcommented, Feb 5, 2018

This has to do with lexical scopes. There is a way how you can solve the issue, and it involves including in the Highchart’s closure a reference to the component’s this. Anywhere you like (for instance in the ngOninit lifecycle hook -once the chart object has been initialised-) do something like this:

ngOnInit() {
    var comp = this;
    this.chart.options.tooltip.formatter = function() {
        console.log(this); // points to the Highchart's formatter scope
        console.log(comp); // points to the Component's scope    
    }
}

This way you can reference methods, properties, or injected dependencies in the component, as well as the series, point, and any other information from the formatter’s scope.

5reactions
matthiaskomarekcommented, Jun 24, 2017

Bind or Arrow Functions dont solve the problem, as you can see on my provided plunker: the actual value of the hovered datapoint is the this context inside the tooltip formatter function. If i use bind or an arrow function, i will set the context to my component and i will lose the datapoint value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

tooltip.formatter | highcharts API Reference
Callback function to format the text of the tooltip from scratch. In case of single or shared tooltips, a string should be returned....
Read more >
Highcharts tooltip formatter - Stack Overflow
The tooltip formatter function has access to a number of different parameters. Instead of this.x , you could use this.point.name . For example:...
Read more >
Tooltip - Chart.js
A tooltip item context is generated for each item that appears in the tooltip. This is the primary model that the callback methods...
Read more >
Format tooltip with custom callback function formatter
Description. The following code shows how to format tooltip with custom callback function formatter.
Read more >
Timeline | Tooltip Callback | JET Developer Cookbook - Oracle
Tooltip Callback Overview Animations Selection Context Menu Date and Time Formats Discrete Navigation Timeline Custom Time Axis Formatting Custom Timescale ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found