Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change point size and color on hover in chartjs

I have made a line chart using chartjs. Now I want is whenever someone hover on the points the points size and color will change. I tried with some options but didn’t manage to get it working. Can someone help me on this please?

javascript:

var ctx = $('#chart');
var myLineChart = new Chart(ctx, {
  type: 'line',
  data: {
   labels: [1, 2, 3, 4, 5],
   datasets: [{
    label: '# of votes',
     data: [1, 2, 3, 4, 5],
     fill: false
   }]
 }
})
like image 820
Lars Micheal Avatar asked May 24 '17 17:05

Lars Micheal


2 Answers

To change the data point­'s color and size on hover, you'll need to set pointHoverBackgroundColor and pointHoverRadius property (as needed) respectively for the dataset, like so ...

datasets: [{
   ...
   pointHoverRadius: 5,
   pointHoverBackgroundColor: 'red'
}]

ᴅᴇᴍᴏ

var ctx = $('#chart');
var myLineChart = new Chart(ctx, {
   type: 'line',
   data: {
      labels: [1, 2, 3, 4, 5],
      datasets: [{
         label: '# of votes',
         data: [1, 2, 3, 4, 5],
         fill: false,
         pointHoverRadius: 5,
         pointHoverBackgroundColor: 'red'
      }]
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="chart"></canvas>
like image 161
ɢʀᴜɴᴛ Avatar answered Sep 30 '22 03:09

ɢʀᴜɴᴛ


Answering an old thread as accepted answer didn't work for me for bar-chart using ChartsJS. May be that was older version or the question was not for bar-chart, not sure. The following works on v2.8 for bar-chart:

hoverBackgroundColor: 'red',
hoverBorderColor: 'blue',
hoverBorderWidth : '3'

Ref1 : https://www.chartjs.org/docs/latest/charts/bar.html#interactions

Ref2 : https://www.chartjs.org/docs/latest/configuration/elements.html#point-configuration

Hoping it may help someone like me.

like image 28
Ajay Kumar Avatar answered Sep 30 '22 03:09

Ajay Kumar