Can I use two different formatters for highchart tooltips?
Asked Answered
C

3

16

I have a highcharts table with two data series that use named values. In my tooltips for one series, I want to reference a data point from the series. So the solution in this answer: How to use a different formatter on Highcharts in each curve of the same graphic? doesn't help me. I need more than just tooltipText, I need a formatter:

For one:

formatter: function() {
    return this.x + ': ' + this.series.name +
    '<br> $' + Highcharts.numberFormat(this.y, 0);
     }

And for the other:

formatter: function() {
    return 'In ' + this.x + ' the median value was' + this.median + 
    'and the total $' + Highcharts.numberFormat(this.y, 0);                        
 }
Caitlyncaitrin answered 19/12, 2012 at 2:29 Comment(0)
P
31

Inside formatter this reffers to the focused serie, you can add an if/else inside the formatter and return a string for each one, like the following.

tooltip: {
    shared: false,
    formatter: function() {
        var text = '';
        if(this.series.name == 'MSFT') {
            text = this.x + ': ' + this.series.name +
                   '<br> $' + Highcharts.numberFormat(this.y, 0);
        } else {
            text = 'In ' + this.x + ' the median value was' + this.median +
                   'and the total $' + Highcharts.numberFormat(this.y, 0);
        }
        return text;
    }
}

demo

Prank answered 19/12, 2012 at 3:19 Comment(1)
+1, thanks - though in my opinion, it's better to use this.series.options.id instead of this.series.name so it does not rely on something that might easily change. The id can be set when initializing the series.Internationalize
C
3

You can easily define a toolip formatter for each series - see here: http://api.highcharts.com/highcharts/plotOptions.series.tooltip

{
  tooltip: {
          shared: true
  },
  series: {
     name: 'some series name',
     data: mydata,
     tooltip: {
        valueSuffix: ' bar'
     }
  }
}

Example: http://jsfiddle.net/28qzg5gq/

Creaky answered 25/11, 2016 at 14:25 Comment(1)
That is not a formatter, it's just suffixes. Adding a formatter in the config object of only one series must be done via the pointFormatter function - example.Loreeloreen
Y
-1

Here's an example. The fiddle example is here:

tooltip: {
            formatter: function () {
                var s = '<b>' + Highcharts.dateFormat('%A, %b %e, %Y', this.x) + '</b>';

                $.each(this.points, function () {
                    s += '<br/>1 USD = ' + this.y + ' EUR';
                });

                return s;
            }
        },
Yoakum answered 9/11, 2014 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.