Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart.js: Widen hover distance for points

Tags:

chart.js

Is there a Chart.js option for increasing the distance from a point at which its tooltip becomes active?

By default, a point is "active" and tooltips are displayed when the mouse is hovering directly over a point. I'd like to give the user a little more area around points to make them "active."

Thanks!

like image 618
Donald Taylor Avatar asked Jun 19 '17 18:06

Donald Taylor


1 Answers

You can achieve this, by setting pointHitRadius property to a value of hit distance, for your dataset, like so ...

...
datasets: [{
      pointHitRadius: 20,
      ...
}]
...

Working example

var ctx = document.getElementById("myChart").getContext('2d');

var myChart = new Chart(ctx, {
   type: 'line',
   data: {
      labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
      datasets: [{
         label: 'Rating',
         data: [1, 2, 3, 4, 5, 6],
         backgroundColor: 'rgba(209, 230, 245, 0.5)',
         borderColor: 'rgba(56, 163, 236, 1)',
         borderWidth: 1,
         pointHitRadius: 20 //set as you wish
      }]
   },
   options: {
      responsive: false,
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true
            }
         }]
      }
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<canvas id="myChart" height="200"></canvas>
like image 155
ɢʀᴜɴᴛ Avatar answered Sep 28 '22 10:09

ɢʀᴜɴᴛ