score:20
Accepted answer
Chart.js listens for mousemove
events and checks if a datapoint is at the x/y coordinates. If so, it triggers the 'hover' behaviour for that point.
Borrowing from the Chart.js tooltips test code, I wrote the snippet below to demonstrate accessing the correct properties and triggering an event.
let c = new Chart($('#chart'), {
type: 'doughnut',
data: {
labels: ['a', 'b', 'c', 'd'],
datasets: [{
data: [1, 2, 4, 8],
backgroundColor: ['red', 'blue', 'green', 'orange']
}]
},
options: {
maintainAspectRatio: false
}
});
$('#a').on('click', function() { t(0); });
$('#b').on('click', function() { t(1); });
$('#c').on('click', function() { t(2); });
$('#d').on('click', function() { t(3); });
function t(idx) {
var meta = c.getDatasetMeta(0),
rect = c.canvas.getBoundingClientRect(),
point = meta.data[idx].getCenterPoint(),
evt = new MouseEvent('mousemove', {
clientX: rect.left + point.x,
clientY: rect.top + point.y
}),
node = c.canvas;
node.dispatchEvent(evt);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<button id="a">Hover a</button>
<button id="b">Hover b</button>
<button id="c">Hover c</button>
<button id="d">Hover d</button>
<canvas id="chart"></canvas>
Source: stackoverflow.com
Related Query
- How to trigger hover programmatically in chartjs
- How to render a vertical line on hover in chartjs
- How to change the cursor to a pointer when I hover over a bar in a ChartJS bar chart?
- How can I trigger the hover mode from outside the chart with charts.js 2?
- How to add ChartJS code in Html2Pdf to view image
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- ChartJS bar chart - trigger hover even when the cursor is outside of the bar
- How can I change the cursor on pie chart segment hover in ChartJS 3?
- How to externally trigger datalabels-plugin (active) - ChartJS
- Stacked horizontal chart. How to trigger individual hover in the stacked element?
- How to trigger ChartJS legend onClick with out interrupting its normal working
- How to change legend icon on hover Chartjs
- In ChartJS how do you format the hover over label in a time series?
- How to run Chart.js samples using source code
- How do i programmatically set bar fillColor in chartJs + angular
- How to clear a chart from a canvas so that hover events cannot be triggered?
- How to set ChartJS Y axis title?
- Chart.js - how to disable everything on hover
- How to disable chartjs legendclick
- how to change size of point in ChartJS
- How to disable a tooltip for a specific dataset in ChartJS
- How to modify chartjs tooltip so i can add customized strings in tooltips
- How to modify bar width in Chartjs 2 bar charts
- ChartJs 2.0 How do I obtain point information being clicked upon?
- how to plot multiple time series in chartjs where each time series has different times
- How to make integer scale in Chartjs
- How get sum of total values in stackedBar ChartJs
- chartjs : how to set custom scale in bar chart
- Chartjs linechart with only one point - how to center
- How to fix chart Legends width-height with overflow scroll in ChartJS
More Query from same tag
- how does this javascript recolor the doghnut?
- How call a function when my array is filled
- Chart js not working properly in Safari
- How to create datasets for all labels for stacked bar in chart.js?
- How to access global variable from outside of canvas.onmousemove function?
- React JS Chart JS 2 is not hiding the grid lines in the background
- ChartJS Annotations display custom label
- Chart.js bar thickness
- Chart.js - Connect two datasets in a line chart at same y value
- How can I display `Null` value data on Y Axis using the Primitive dataset format in Chart.js V3.7.0?
- charts.js chart showing old data on hover
- Remove/Hide (display:none) the graphical chart in small-mobile view - JS chart
- Chartjs (Non Vue) Not Rendering Graph Chart inside V-if/V-show
- Unable to change default values in chart.js
- Add space Between Columns in Bar chart. chartjs
- React-chartjs-2 gradient fill error on canvas context
- Chart.js line graph doesn't show up most of the time
- On hover bar is overlapped by label chartjs
- Passing Global Options to line Chart in 2.1.6/Chart.js
- How can I remove the white border from Chart.js V2.6.0 pie chart?
- Remove Decimal like " [Decimal('1220'), Decimal('160')] "from Django Queryset results
- Chart.js making this chart more readable / scrollable
- angular-chart chart-line not working
- How to use chart.js in Angular2?
- Graph is not displayed
- Chart.js Subtitle won't display
- ChartJs: How to pan programmatically by time values (instead of pixels)
- Rails chart.js data coffee script
- Chart.js - Line chart does not render all points when using Point[] format
- Adding chartjs-plugin-datalabels to Chart.js in Aurelia