I would like to reproduce the hover behavior on multiple charts at the same time by clicking a button. I was thinking of putting the equivalent button id to the graphic labels.
For example, by clicking on the orange button (Cf. attached image), I get his ID and I would like to trigger the event hover in each orange item of each chart (which having a label identical to button's id).
I can detect which item has a label equivalent to my id, but I do not know how to replicate the hover's behavior on it.
Is there a way to force to set the hover mode active on an items in charts?
MY HTML:
<div class="graph">
<canvas id="ctx"></canvas>
<button type="button" class="btn btn-primary" id="OCG">Test</button>
</div>
MY JS:
var ctx = document.getElementById('ctx').getContext('2d');
var ocacolor = '#EE8B54',
ocbcolor = '#C2B49B',
ocbacolor = '#AECF86',
ocgcolor = '#E97676',
ocpcolor = '#088DA5';
var backgroundcolor = [ocacolor, ocbcolor, ocbacolor, ocgcolor, ocpcolor]
data = {
datasets: [{
data: [1, 1, 1, 4, 1],
backgroundColor: backgroundcolor
}],
labels: [
'OCA',
'OCB',
'OCBA',
'OCG',
'OCP'
]
};
//CREATING CHART
var myDoughnutChart = new Chart(ctx, {
type: 'doughnut',
data: data,
options: {
legend: {
display: false
},
onClick : function(evt,dataset){
//I GET THE LABEL TEXT OF THE CLICKED ITEM
console.log(dataset[0]._model.label);
}
}
});
$("#OCG").click(function(){
var plop = this.id
myDoughnutChart.data.labels.forEach(function(element) {
if (element == plop) {
//GET THIS ITEM ID AND PUT IT ON HOVER MODE
}
});
})
Here's a fiddle with the code: https://jsfiddle.net/8wye9vL4/28/