score:0
Jumping off what you've already created, add a click event around each HTML generated legend item, and use that click event to hide indiv. datasets, based on matching conditions.
I configured this in Angular, but the logic is similar.
With the below HTML, I am generating a custom legend which displays each of my labels. When a label is clicked, the hideOneDataset fn is called, passing the value of the legendItem clicked.
<div *ngIf="legendData">
<ul class="my-legend">
<li class="legend-item" *ngFor="let legendItem of legendData" (click)="hideOneDataset($event.target.innerText)">
{{ legendItem.text }}
</li>
</ul>
</div>
Then, I configured the hideOneDataset function to loop through each dataset item and determine if the legendItem from my custom HTML legend === that of the current element's label. If yes, hide it.
hideOneDataset(legendItem) {
// GET each dataset in the chart
this.chartComponent.chart.data.datasets.forEach(ele => {
// IF the custom legend item clicked on equals the label of one of our datapoints, hide that data
if (ele.label === legendItem) {
ele.hidden = !ele.hidden;
}
});
this.chartComponent.update();
}
Hope this helps a bit for anyone who stumbles upon this.
Source: stackoverflow.com
Related Query
- chart.js custom legend - make legend items clickable to select/deselect data
- Chart.js HTML custom legend issues with doughnut chart
- Make months on x-axis clickable on chart.js line chart
- Custom data in label on ChartJS pie chart
- Custom data position on the doughnut chart in chart.js
- Angular chart how to show the legend data value by default along with legend name
- Using chart js version 3, how to add custom data to external tooltip?
- Chart JS pass in custom data for points
- Chart js custom separate legend returns error when clicking legend
- Make chartjs pie chart wiyh dynamic data
- How to set the gap between data items in a chartjs chart
- ChartJS 2 - Adding data to chart breaks after X number of items added
- Display legend of grouped data (different colors) in chart
- Chart js show data on hovering legend
- Doughnut chart from Chart.js, custom legend and call of original handler not works as expected
- How do I trigger a clickable event on my chart data
- ChartJS custom legend doughnut separate legend from chart area
- How do I destroy/update Chart Data in this chart.js code example?
- ChartJS: Custom legend not showing Labels for Multi-Pie chart
- How to assign specific colors to data items in a doughnut chart in Angular Chart?
- getting additional value fields from data source for dx.chartjs doughnut chart
- Chart.js how to make chart only show the most recent data
- Chartjs Custom Legend for Doughnut Chart Having Labelled Lines Sticking Out of Each Section
- Chart JS - Make chart scrollable if too much data
- How can I add functionality to Chartjs Doughnut chart custom legend
- how can I make selected data is highlighted on chart js?
- Make a chart clickable when placed under another SVG <View>
- Cannot make my chart to work with data from mongoDB
- JavaScript Chart.js - Custom data formatting to display on tooltip
- Chartjs Bar Chart showing old data when hovering
More Query from same tag
- Chartjs Stacked bar chart not displaying correctly
- Chart.js is not displayed when page loads first time
- Outputting multiple SQL queries in chart.js
- how to highlight a specific area on chartjs line chart
- Updating chart.js bar graph in real time
- Updating Chart.js Datasets - Array not re-initializing (Jquery / PHP)
- Invalid module name in augmentation. Module 'chart.js' resolves to an untyped module at '/src/chart.js', which cannot be augmented
- Align Title Left React Chart.js V2
- How add the sizes of the slices in the pie chart (at the top) in Chart.js?
- Chart js - set width to a specific bar
- Chartjs stacked bar separate tooltip for all stacked
- opening json file to create chart in chart.js
- Chart.js not initiating
- Chart.js HTML custom legend issues with doughnut chart
- Adding hours and minutes to a date object for time axis
- show xAxes on click
- Remove only y-axis line from chat drawn using chart.js 1.0.2
- is there a way to call a Component's function from click function of Chartjs
- Unable to see the lines in chart.js
- Chartjs break line for axes tick labels text
- Wrong chart.js points get highlighted on hover
- Plot time on Y axis
- Why using logarithmic gives 8e+2 value?
- Increase Chart.js Scale Padding/Margin
- Why must I create my VueJS application before using ChartJS?
- How to plot dates in Chart.js
- Chartjs for Line chart, values on y-axis
- react-chart2: Cannot read properties of undefined (reading 'map')
- chart.js custom legend colour based
- How to remove quotations from a dictionary in python