score:70
According to the docs there is a onClick
handler for the legend exposing the event object. If you stopPropagation
it stops the data series hiding:
let chart = new Chart(elem.find('canvas')[0], {
type: 'line',
data: {
labels: [],
datasets: []
},
options: {
responsive: true,
maintainAspectRatio: false,
legend: {
onClick: (e) => e.stopPropagation()
}
}
});
The above is ES6, if your not using a supported browser below is the older ES5 equivilant.
legend: {
onClick: function (e) {
e.stopPropagation();
}
}
Chartjs must register its own click event after the legend.onClick
which is why this stop it executing.
score:-1
You need to add properties legendItemClick
to override default action. It's work good for pie chart
legendItemClick: function(e){
console.log("Clicked an item with text: " + e.text);
e.preventDefault();
}
https://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/chart/events/legenditemclick
score:7
To override the default behavior of clicking on a legend item, that is showing/hiding the associated dataset in the chart, you may use the following option (shown inside options
for clarity):
options: {
legend: {
onClick: function(event, legendItem) {}
}
}
This is the way to override the default behavior, that is by supplying a function with the same arguments. According to your requirements, this function should have an empty body (and, thus, return immediately), since absolutely nothing should happen when clicking on a legend item. Look for legend.onClick
in the docs. While it currently appears under only two chart types, this option should work for all chart types.
score:7
At the time of writing (Chart.js v3.5.1), the correct answer is
options: {
plugins: {
legend: {
onClick: null
}
}
}
As per Natan Almeida de Lima's comment above. Adding it as an answer since I didn't see it as a one-line comment which I only found after figuring it out for myself.
score:16
Also, you can use null
or any value which is evaluated false
to disable click event on all legend.
options: {
legend: {
onClick: null
}
}
Note: Ignore click event by onClick
on following code in Chart.js ( https://github.com/chartjs/Chart.js/blob/6bea15e7cf89003e3a5945a20cf1d2cc5096728e/src/plugins/plugin.legend.js#L481 )
Source: stackoverflow.com
Related Query
- How to disable chartjs legendclick
- How to disable a tooltip for a specific dataset in ChartJS
- 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?
- How to disable click in graph ChartJS
- How to disable chartjs tooltip on mibile devices and small screens?
- How to run Chart.js samples using source code
- How to set ChartJS Y axis title?
- Chart.js - how to disable everything on hover
- how to change size of point 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
- How to retrieve ChartJS instance after initialization
- ChartJS : How to leave just points without lines
- ChartJS – How to show border on empty pie chart?
- How to add label for ChartJs Legend
- How to trigger hover programmatically in chartjs
- how to change background in chartjs and remove background lines?
- How to properly use the chartjs datalabels plugin
- ChartJs how to get from mulitiple dateset which dataset bar is clicked
- How to change the color of legend in chartjs and be able to add one more legend?
- How to render a vertical line on hover in chartjs
- How to disable Chart JS tooltip when there is no value?
More Query from same tag
- Chart.js passing value data dynamic
- Laravel - Bootstrap Javascript's conflict with Chart.js
- Custom script tag in index.html is removed on each build in Ionic 2 RC.0
- Cannot call a function in Javascript - Chart.js
- Display values outside of pie chart in chartjs
- Chart.js using the value of certain data in external json file
- How can I use an array of {x,y} objects to make a chart?
- ReactJS Components Not loading unless you reload the site for the 2nd time
- Canvas size not changing
- Emberjs binding properties within Ember object
- Floating clone does not show in chrome if dragged element contains a chartjs component
- chart.js and asynchronous loading
- Chartjs bar order adjustment after bar chart is drawn
- Setting ChartJS plugin on vue-chartjs not working
- Is it possible to display values where the display ends instead of on top of the points with chartjs?
- Chart.js - each dataset value = different axis
- chart.js? Specifically Radar Charta and Negative Numbers
- Chart.js How to align x-ticks?
- chartjs - canvas size error
- ChartJS, merge legends for multiple charts
- How to add the value for each label to pie legend
- Display Charts based on year select in django
- chart.js filltext object moved when tooltip is generated because of legend not displaying
- Can I remove the Y-axis counter on chart.js?
- ChartJS charts not generating within tabs
- How i can refresh chart without refresh page?
- Chart.js (line chart) tooltip duration/delay
- Animate Chartist on show
- Print pie chart in chartjs
- Converting Chart.js canvas chart to image using .toDataUrl() results in blank image