0๐
โ
-
You need the
options.plugins.legend.onHover
callback function to capture the event when the legend has hovered. -
To highlight the bar elements, you need to call the
setActiveElements
method and provide items ofActivePoints[]
. The key point is theActivePoints
items must with thedatasetIndex
which is the currently selected legend item index (legendItem.datasetIndex
) andindex
for each item index in the dataset. Reference: Programmatic Event Triggers
import { Chart, ChartConfiguration, LegendItem } from 'chart.js';
import { BaseChartDirective } from 'ng2-charts';
@ViewChild(BaseChartDirective) baseChart!: BaseChartDirective;
ngAfterViewInit() {
this.barChartOptions = {
...this.barChartOptions,
plugins: {
legend: {
onHover: (evt, legendItem: LegendItem) => {
this.baseChart?.chart?.setActiveElements(
this.baseChart!.chart!.getDatasetMeta(
legendItem.datasetIndex!
).data!.map((x, i) => ({
datasetIndex: legendItem.datasetIndex!,
index: i,
}))
);
this.baseChart.update();
},
},
},
};
}
Source:stackexchange.com