简体   繁体   中英

Add a custom text tooltip that differs from point to point in Highcharts

I am trying to develop a chart that displays a custom 'text' tooltip that I can set to change from point to point. Here is my code as of now:

            tooltip: {
                headerFormat: '<b>{series.name}</b><br>',
                pointFormat: '{point.x:%e. %b}: {point.y:.2f} m'
            },

            series: [{
                name: 'Winter 2007-2008',
                data: [
                    [Date.UTC(1970,  9, 27), 0   ],
                    [Date.UTC(1970, 10, 10), 0.6 ],
                    [Date.UTC(1970, 10, 18), 0.7 ],
                    [Date.UTC(1970, 11,  2), 0.8 ],
                    [Date.UTC(1970, 11,  9), 0.6 ],
                    [Date.UTC(1970, 11, 16), 0.6 ],
                    [Date.UTC(1970, 11, 28), 0.67],
                    [Date.UTC(1971,  0,  1), 0.81]
                ]
            }

Now this works as intended, but I am trying to add an extra bit of text that will appear in the tooltip when hovering over a point. This is my desired input for a data point (although I dont know if I will have to change it for my final product).

 [Date.UTC(1970, 10, 18), 0.7 , 'custom tooltip1'],
 [Date.UTC(1970, 10, 18), 0.7 , 'custom tooltip2'],

I would want the text displayed in a newline below the current tooltip:

当前工具提示

Please let me know if you have any suggestions or can point me somewhere I can find some answers.

Thanks!

Change your data to be something like this:

data: [
   {x: Date.UTC(1970, 10, 18), 
    y: 0.7 ,
    customtooltip: 'custom tooltip1'},
   {x: Date.UTC(1970, 10, 18), 
    y: 0.7 ,
    customtooltip: 'custom tooltip2'}
]

Then in do a tooltip formatter function like this:

tooltip: {
     formatter: function() {
          var dt = new Date(this.x);
          var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
          var dtstr = dt.getDate()+". "+months[dt.getMonth()];
          var tooltip = '<b>'+this.series.name+'</b><br>'+dtstr+': '+this.y+' m<br>'+this.point.options.customtooltip;
          return tooltip;
     }
}

See how that works for you.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM