简体   繁体   中英

Highcharts - Show tooltip onClick instead of hover

I have tried the solution at https://stackoverflow.com/a/24206104/5653279 but to no avail as my Highcharts consist of 2 series of data.

Following the above solution returns me the error

Uncaught TypeError: Cannot read property 'category' of undefined

$(function () {
$('#container').highcharts({
    chart: {
        type: 'spline',
        zoomType: 'xy',
        events: {
                load: function(){
                    this.myTooltip = new Highcharts.Tooltip(this, this.options.tooltip);                    
                }
            }
    },
    tooltip: {
            enabled: false,
        headerFormat: '<b>{point.x: %A, %b %e at %I:%M %p}</b><br>',
            shared: true,
        borderRadius: 10,
        crosshairs: [true, false] //x,y
    },
    plotOptions: {
            series: {
                stickyTracking: false,
                events: {
                    click: function(evt) {
                        this.chart.myTooltip.refresh(evt.point, evt);
                    },
                    mouseOut: function() {
                        this.chart.myTooltip.hide();
                    }                       
                }
             }
        },
    title: {
        text: 'Glucose/Carbohydrate'
    },
    subtitle: {
        text: 'Ratio between Glucose and Glucose'
    },
    xAxis: {
        type: 'datetime',
        dateTimeLabelFormats: {
            month: '%e/%b',
        },
        title: {
            text: 'Date'
        }
    },
     yAxis: [{ // Glucose yAxis
        labels: {
            format: '{value}mmol/L',
            style: {
                color: Highcharts.getOptions().colors[0]
            }
        },
        title: {
            text: 'Glucose',
            style: {
                color: Highcharts.getOptions().colors[0]
            }
        }
    }, { // Carbohydrate yAxis
        title: {
            text: 'Carbohydrate',
            style: {
                color: Highcharts.getOptions().colors[1]
            }
        },
        labels: {
            style: {
                color: Highcharts.getOptions().colors[1]
            }
        },
        opposite: true
    }],
    series: [{
        name: 'Glucose',
        data: glucose,
        marker: {
            enabled: true
        },
        tooltip: {
            valueSuffix: ' mmol/L'
        }
    },{
        name: 'Carbohydrate',
        data: carbohydrate,
        dashStyle: 'shortdot',
        yAxis: 1,
        marker: {
            enabled: true
        }
    }
    ]}
    ); 
});

However, the error would be "solved" if I edit plotOptions as seen.

plotOptions: {
            series: [{
                stickyTracking: false,
                events: {
                    click: function(evt) {
                        this.chart.myTooltip.refresh(evt.point, evt);
                    },
                    mouseOut: function() {
                        this.chart.myTooltip.hide();
                    }                       
                }
             },
             {
                stickyTracking: false,
                events: {
                    click: function(evt) {
                        this.chart.myTooltip.refresh(evt.point, evt);
                    },
                    mouseOut: function() {
                        this.chart.myTooltip.hide();
                    }                       
                }
             }]
        },

But still, the tooltip does not show when I clicked on any point.

Solved the issue!

Just changed

this.chart.myTooltip.refresh(evt.point, evt);

to

this.chart.myTooltip.refresh([evt.point], evt);

But the only limitation to this fix is if there's multiple series (eg Line A and B) and the data of both series falls on the same X-Axis (eg on the same date), clicking on it will only show the tooltip of that particular series's data (if I click on series A's data, the tooltip only shows A's data without B, even though they are sharing the same X-Axis).

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