Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highchart/stock tooltip formatter for both series and flags

In the following example how can i format the tooltip for both series AND flags?

http://jsfiddle.net/gh/get/jquery/1.7.1/highslide-software/highcharts.com/tree/master/samples/stock/plotoptions/flags/

I want to show extra information in series tooltip and other data in flag tooltip.

like image 619
nelsonvarela Avatar asked May 10 '12 13:05

nelsonvarela


3 Answers

Use following function as tooltip formatter -

tooltip: {
  shared:true,
formatter: function(){
        var p = '';
        console.log(this);

        if(this.point) {
          p += '<b>'+ Highcharts.dateFormat('%A, %b %e, %Y', this.point.x) +'</b><br/>';
          p += this.point.config.text // This will add the text on the flags
        }
        else {              
          p += '<b>'+ Highcharts.dateFormat('%A, %b %e, %Y', this.x) +'</b><br/>';
          $.each(this.points, function(i, series){
            p += '<span style="color:' + this.series.color + '">' + this.series.name + '</span>: <b>'+ this.y + ' kWh ($' + Highcharts.numberFormat(this.y * rate, 0) + ')</b><br/>';
          });

        }
        return p;
      }}

Also refer to this JSFiddle : http://jsfiddle.net/aTcFe/

like image 172
crazycrv Avatar answered Nov 13 '22 22:11

crazycrv


One way is to create a tooltip formatter that checks if the current object is a point.

tooltip: {
            formatter: function(){
                if(this.point) {
                    return "this is a flag"
                }
                else {
                    return "this is a line"
                }
            }                
        }

You could go one step further and give your flags names and then check if the point has a name (instead of if it just exists) to prevent non-flag points from getting the same format.

Here is your example modified to reflect the former http://jsfiddle.net/aTcFe/

like image 45
Yanofsky Avatar answered Nov 13 '22 20:11

Yanofsky


With Highstock 2.1.7 you always get a this.point object, so you should use this.series.type === 'flags' to detect whether flags are present or not.

An example would be:

formatter: function () {
     if (this.series.type === 'flags') {
          // Flags formatting
     }
     else {
          // Default formatting
     }
}
like image 45
meh-uk Avatar answered Nov 13 '22 22:11

meh-uk