score:56
Better Approach
no need of jQuery to select canvas element (line-chart
).
1 ▸ Solution :
add the following in your chart options :
legend: {
onHover: function(e) {
e.target.style.cursor = 'pointer';
}
},
hover: {
onHover: function(e) {
var point = this.getElementAtEvent(e);
if (point.length) e.target.style.cursor = 'pointer';
else e.target.style.cursor = 'default';
}
}
2 ▸ Solution :
set tooltip's mode
to dataset
:
tooltips: {
mode: 'dataset'
}
ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩
var line_chart = new Chart(document.getElementById("line-chart"), {
type: 'line',
data: {
labels: ['May', 'June', 'July'],
datasets: [{
data: [15, 25, 15],
label: "My Dataset1",
borderColor: "#00F",
fill: false
}, {
data: [35, 15, 25],
label: "My Dataset2",
borderColor: "#F00",
fill: false
}]
},
options: {
tooltips: {
mode: 'dataset',
},
legend: {
onHover: function(e) {
e.target.style.cursor = 'pointer';
}
},
hover: {
onHover: function(e) {
var point = this.getElementAtEvent(e);
if (point.length) e.target.style.cursor = 'pointer';
else e.target.style.cursor = 'default';
}
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<div style='width:80%'>
<canvas id="line-chart" width="800" height="450"></canvas>
</div>
score:0
Took me quite some time, but almost none of the other answers work with the latest version of ChartJS (3.7.0 as of now). In the current version, there is a simple way to configure this behavior for all charts:
Chart.defaults.plugins.legend.onHover = function() {
document.body.style.cursor = 'pointer';
};
Chart.defaults.plugins.legend.onLeave = function() {
document.body.style.cursor = 'unset';
};
More details can be found in the official documentation: https://www.chartjs.org/docs/latest/configuration/legend.html
score:0
As other folks pointed out for others frameworks, in Vue is needed to aim native
property. In addition, the target
would be the canvas
so you need to:
onHover: function (e) {
e.native.target.style.cursor = "pointer";
},
onLeave: function (e) {
e.native.target.style.cursor = "default";
},
score:1
You can do it with jquery grabbing chart selector which is line-chart
.
hover: {
onHover: function(e, el) {
$("#line-chart").css("cursor", el[0] ? "pointer" : "default");
}
}
score:2
If you are using react-chartjs-2 wrapper for chartjs. This might help you🤓.
In react-chartjs-2 we can make the legends have cursor as 👆pointer using the following configuration for the options prop.
const options = {
plugins: {
legends: {
onHover: (event) => {
event.native.target.style.cursor = 'pointer'
}
}
}
}
score:2
Thanks to @ɢʀᴜɴᴛ and @Fred Lintz, I finally arrived at this on Chart js version 3.5.1:
onHover: (event, activeElements) => {
if (activeElements?.length > 0) {
event.native.target.style.cursor = 'pointer';
} else {
event.native.target.style.cursor = 'auto';
}
},
Note: There is another option of using document.getElementById('myChart').style.cursor = 'pointer';
which works well, it's just not as clean as I would like it since it sets the cursor for the whole chart, but it will survive if Chart.js changes the API again.
score:4
ChartJs 2 provides onHover
and onLeave
handlers. We can use them to change the cursor:
legend: {
display: true,
onHover: function (e) {
e.target.style.cursor = 'pointer'
},
onLeave: function (e) {
e.target.style.cursor = 'default'
}
}
score:4
Expanding on @joshfindit's answer here, for anyone that's looking for a Typescript solution for chart.js v3.5.1:
onHover: (event, activeEvents) => {
(event?.native?.target as HTMLElement).style.cursor =
activeEvents?.length > 0 ? 'pointer' : 'auto';
}
score:8
As of Chart js version 3.5.1 the onHover() option looks slightly different and the method to find the points has changed as well.
'target' is nested one more level under 'e.native'
By setting interaction mode to 'index' I'm able to see my combined chart values in the one tooltip on hover.
options: { interaction: { mode: 'index' }, onHover: function (e) { const points = this.getElementsAtEventForMode( e, 'index', { axis: 'x', intersect: true }, false ); if (points.length) e.native.target.style.cursor = 'pointer'; else e.native.target.style.cursor = 'default'; } }
score:21
Just add this to the options:
onHover: (event, chartElement) => {
const target = event.native ? event.native.target : event.target;
target.style.cursor = chartElement[0] ? 'pointer' : 'default';
}
Source: stackoverflow.com
Related Query
- Chart.js how to show cursor pointer for labels & legends in line chart
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Chart.js how to show line chart without displaying the labels on xaxis and yaxis
- How to show tick marks and labels with grid line for the specific one on the X axis with Chart.js?
- how to show X and Y labels on points in Chart js 2 Line Chart
- How to display Line Chart dataset point labels with Chart.js?
- show label in tooltip but not in x axis for chartjs line chart
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to align Chart.JS line chart labels to the center
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- how to set chart.js grid color for line chart
- PrimeNg bar chart how to show a label for the y-axis
- Multiple line labels for chart js
- How to show data values in top of bar chart and line chart in chart.js 3
- How to draw outer labels for polar chart using ng2-charts and chart.js at fixed positions outside the rings?
- In Chart.js >3.0, on axis of type time, how to show labels and ticks only for existing data points (make labels reflect data)?
- How to show different product name for every bar in chart js
- How to show scale labels and set min/max for yAxes?
- Chart.js: How to get x-axis labels to show on top of bars in bar chart
- How do I customize y-axis labels and randomly pick the value from the data range for x-axis in Chart js
- How to change background color of labels in line chart from chart.js?
- How to show multiple datasets in line chart js
- How to show the chartjs bar chart data values labels as text?
- How do you set x and y axis and Title for a line chart using charts.js?
- how can i show labels and value in both on bar chart
- Char.js - How to show labels by default in pie chart
- How remove duplicates xAxis labels and show all values on chart
- How to show bar chart labels clearly using ChartJS?
- how to show only each 15th x-labels in line chart chart.js in angular 6?
- How to start Y Axis Line at 0 from right for negative values in chart js 2?
More Query from same tag
- how to disable last/max value shown on x axis in chart js?
- chartjs undefined length when using 2 datasets
- How to pass model object to javascript function in asp.net Core
- Using Chart.js with Dates as x-Axis labels
- Converting JSON data into Chart.js array from strings
- How to set default callback for tooltip title with chart.js
- Chart.update is not a function typeError
- Chartjs extended doughnut with text tooltip issue
- How to display the more then one value inside tooltip in bar chart.js?
- How to change the Chart.js legend rectangles to squares
- How can I change the starting point of horizontal bars in Charts.js
- I'm trying to use chart.js in angular to create a simple bar chart but I'm getting an error as 'legend' type doesn't exist for options
- Text in center of chartJS lead to infiniteloop
- Custom tooltip styling in Chart.js
- web page doesn't display chart or any error
- How do you set pie chart colors in angular-chart.js
- ChartJS v2 custom tooltip for rLabel
- Angular charts not working with Angular 5, Showing below error attached screen shot
- Chart.js line split color
- Chartjs v2 on retina display has label size issue (text is too large)
- ERROR TypeError: Cannot read property 'nativeElement' of undefined in Ionic 5 and chart.js
- Deleting and recreating an element with React
- Detecting hover events over parts of a chart using Chart.js
- With Chart js, I am trying to color every 7th vertical (x axis) grid line (representing each week beginning visually)
- Unable to hide X-Axis in horizontal stacked chart.js
- React render function preventing creating new canvas element on each time invoked - Charts.js
- Sending data to ChartJsLineChart in ChartJs.Blazor
- How to update data Chart in async way in Angular Chart.js?
- Bar Chart of ChartJS does not Render
- How to apply each color to each dataset