score:-3
onLegendClick: function (e) {
var series = e.target;
series.isVisible() ? series.hide() : series.show();
},
dxcharts have a function called onLegendClick, a few others are onSeriesClick and onPointClick. I hope this helps.
score:0
I've found no way to do this. It's easy to put some onclick behaviour on the legend template, and you can easily change the series stroke and colors alpha channel to 0 so that the area and stroke disappear but I've found no way to do this on points.
I've decided to use google charts for this particular chart, and chart.js whenever I do not need that behavior, hoping that the good creators of chart.js will add it in the future.
score:1
An update to @benallansmith answer, the link to the documentation is now :
https://www.chartjs.org/docs/latest/configuration/legend.html
My version of the example code, to have datasets 2 and 3 hidden while clicking on legend for dataset 1 (datasets 2 and 3 have their legends hidden with the filter function):
public static readonly myChart: Chart.ChartConfiguration = {
type: 'line',
data: {
datasets: [
{
label: 'A',
borderColor: 'red',
fill: false,
lineTension: 0
},
{
label: 'B',
borderColor: 'blue',
fill: false,
lineTension: 0,
pointRadius: 0
},
{
borderColor: 'blue',
borderDash: [5, 10],
borderWidth: 1,
fill: false,
lineTension: 0,
pointRadius: 0
},
{
borderColor: 'blue',
borderDash: [5, 10],
borderWidth: 1,
fill: false,
lineTension: 0,
pointRadius: 0
}
]
},
options: {
legend: {
labels: {
filter: function(legendItem, chartData) {
if (legendItem.datasetIndex > 1) {
return false;
}
return true;
}
},
onClick: function(e, legendItem) {
const index = legendItem.datasetIndex;
if (index === 1) {
const ci = this.chart;
[
ci.getDatasetMeta(1),
ci.getDatasetMeta(2),
ci.getDatasetMeta(3)
].forEach(function(meta) {
meta.hidden = meta.hidden === null ? !ci.data.datasets[1].hidden : null;
});
ci.update();
} else {
// Do the original logic
Chart.defaults.global.legend.onClick.call(this, e, legendItem);
}
}
},
responsive: false,
animation: {
duration: 0
},
}
...
};
score:1
my version is v2.8.0 I find a work way, try it.
add
legend : {
}
into option
var donutOptions = {
maintainAspectRatio : false,
responsive : true,
animation: {
animateScale: true,
animateRotate: true
},
tooltips: {
intersect: false,
callbacks: {
label: function (tooltipItem, data) {
...
}
},
},
plugins: {
datalabels: {
...
},
},
},
legend : {
}
};
and use this
donutChart.chart.getDatasetMeta(0).data[index].hidden = true;
donutChart.update();
score:4
This is now available in Charts.js
From the documentation http://www.chartjs.org/docs/#chart-configuration-legend-configuration
Legend Configuration
The legend configuration is passed into the options.legend namespace. The global options for the chart legend is defined in Chart.defaults.global.legend.
onClick function(event, legendItem) {} A callback that is called when a 'click' event is registered on top of a label item
onHover function(event, legendItem) {} A callback that is called when a 'mousemove' event is registered on top of a label item
score:14
You can try this
Create store for hidden datasets
window.chartName = new Chart(...
window.chartName.store = new Array();
Then use this function to update chart, must be handled by click on legend item
function updateDataset(legendLi, chart, label) {
var store = chart.store;
var exists = false;
for (var i = 0; i < store.length; i++) {
if (store[i][0] === label) {
exists = true;
chart.datasets.push(store.splice(i, 1)[0][1]);
legendLi.fadeTo("slow", 1);
}
}
if (!exists) {
for (var i = 0; i < chart.datasets.length; i++) {
if (chart.datasets[i].label === label) {
chart.store.push([label, chart.datasets.splice(i, 1)[0]]);
legendLi.fadeTo("slow", 0.33);
}
}
}
chart.update();
}
Don't forget updated legend template in chart options
legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li class=\"legend-item\" onclick=\"updateDataset($(this), window.chartName, '<%=datasets[i].label%>')\"><span style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
Shortly, i added this onclick handler for li component
<li class=\"legend-item\" onclick=\"updateDataset($(this), window.chartName , '<%=datasets[i].label%>')\"><
For example fiddle
Source: stackoverflow.com
Related Query
- Chart.js: hiding series by clicking on legend
- Chart js custom separate legend returns error when clicking legend
- how to highlight the bars in stacked bar chart of chart.js on clicking a legend
- Chart.js - Increase spacing between legend and chart
- Pie Chart Legend - Chart.js
- Chartjs Bar Chart Legend
- chart js how to fill legend box with colour
- Chart JS - set start of week for x axis time series
- ChartJS bar chart with legend which corresponds to each bar
- Chart.js HTML custom legend issues with doughnut chart
- Highlight Line Series on legend hover
- Series Details Not Showing in Angular Chart with Charts.js
- React ChartJS 2 : Get data on clicking the chart
- Chart js: how can I align the legend and the title
- Increase padding between legend and chart with react-chartjs-2
- Chart,js Pie Chart can the gap between a pie chart and the legend be adjusted
- Hiding legend in PrimeNG
- Chart.js Radar chart legend label font size doesn't work
- Using Chart.js - Creating Legend for Doughnut Chart
- Time series line chart is not displayed
- Time Series Line chart js in react not working
- Hiding the ticks on the x-axes clips the chart (Chart.JS)
- Angular chart how to show the legend data value by default along with legend name
- Chart.js: Pie chart legend "onclick" gets overwritten by "options.onclick" if both are present
- Chart JS Legend Styling
- Chart Click Event - Clicking label of doughnut chart, doesn't return label ng2-charts
- Chart.js chart onclick disables legend onclick
- chartjs Adding percentages to Pie Chart Legend
- Chart Js update legend boxes of graph with graph line style
- ChartJS horizontal chart display legend on each bar
More Query from same tag
- Show/Hide data from Barchart with Chart.js
- How to add a vertical line on the end of the chart.js scatter graph
- How to change the color of legend in chartjs and be able to add one more legend?
- Cant assign data to ng2 piechart after receiving it in HTTP response
- how can i use chartjs-plugin-annotation?
- mysql sum query for chartjs
- How to save in chart original ID of element in array that is used to build chart data labels?
- AngularJs ChartJs tooltip z-index issue
- Drawing line chart in chart.js with json response
- ChartJS Line chart cut off at the top and bottom
- How to show Nested charts Radar chart within Doughnut Chart?
- How to label x-Axis in Chart.js by days?
- Why is the child component updating but not re-render Reactjs
- Display line chart with connected dots using chartJS
- Chartjs chart not rendering with pug template
- Chart JS: All Values Displayed on x-axis After Callback Conversion
- Chartjs Radar chart - How to dynamically highlight one of the gridlines at a specific point
- How to add vertical line in bar chart using chartJs and ng2-charts?
- In chart.js, Is it possible to hide x-axis label/text of bar chart if accessing from mobile?
- Chart JS tick options not working for y axis
- ReactJS - Loading data using Axios + chartjs
- ChartJS Separate Labels for each dataset/independent datasets?
- chart.js cuts data points to half
- Chart.options returns "Property 'options' does not exist on type 'chart'"
- Chart js shows old chart data when hover
- Chart Js V2 draw Horizontal Bar(Average) Over Vertical Bar
- Javascript Chart.js scale fix
- Chart.js only displays on refresh in Rails app
- ChartJS with Symfony 4.4 (UX component)
- ERROR TypeError: "this.canvas is undefined" | Problem with creating a chart with angular and chart.js