Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ChartJS: Show default tooltip onclick

Tags:

chart.js

I have a chart.js line chart displaying properly. When you hover over the y-axis gridlines, the tooltips display as they should.

I'm trying to convert this to function on a touchscreen, so there is no hover. Is there a way to add a simple parameter to make the tooltip show on both hover and onclick?

Note, I know I could add a custom tooltip and add all of that functionality - I'm trying to see if there's just a parameter I can add as I don't need a custom tooltip.

like image 513
Lauren Avatar asked Apr 04 '16 17:04

Lauren


2 Answers

For Chart.js v2, you can specify those events at the root level of chart options.

options: {
    events: ['click']
}
like image 159
Lee Han Kyeol Avatar answered Jan 04 '23 17:01

Lee Han Kyeol


Just add "click" to your tooltipEvents list when specifying the options for the chart

    ...
    tooltipEvents: ["mousemove", "touchstart", "touchmove", "click"],
});

In the fiddle below, I've removed all other events from the list except for click to give you an idea of how it will be like on a mobile

Fiddle - http://jsfiddle.net/8uobybv3/

like image 21
potatopeelings Avatar answered Jan 04 '23 15:01

potatopeelings