score:19
You should be able to use the getElementsAtEvent
method, like so
document.getElementById("myChart").onclick = function(evt){
var activePoints = myRadar.getElementsAtEvent(evt);
// use _datasetIndex and _index from each element of the activePoints array
};
Fiddle - http://jsfiddle.net/uwaszvk7/
score:4
Calling
getElementsAtEvent(event)
on your Chart instance passing an argument of an event, or jQuery event, will return the point elements that are at that the same position of that event.
...
var canvas = document.getElementById("myChart");
var myRadar = new Chart(canvas, config);
canvas.onclick = function(evt) {
// => activePoints is an array of points on the canvas that are at the same position as the click event.
var activePoint = myRadar.getElementsAtEvent(evt)[0];
if (activePoint !== undefined) {
var dataset = myRadar.data.datasets[activePoint._datasetIndex];
var title = myRadar.data.labels[activePoint._index];
var oldValue = dataset.data[activePoint._index];
var newValue = oldValue + 10;
dataset.data[activePoint._index] = newValue;
}
// Calling update now animates element from oldValue to newValue.
myRadar.update();
console.log("Dataset: '"+ dataset.label + "' Element '" + title + "' Value updated from: " + oldValue + " to: " + newValue);
};
score:6
Another way to achieve this in ChartJS 2.0 is to use the lastActive property of the radar chart.
var myRadar = new Chart(document.getElementById("myChart"), config);
Chart.defaults.global.responsive = true;
Chart.defaults.global.hover.mode = 'single';
document.getElementById("myChart").onclick = function (evt) {
var activePoint = myRadar.lastActive[0];
if (activePoint !== undefined) {
var datasetIndex = activePoint._datasetIndex;
var index = activePoint._index;
var datasetName = config.data.datasets[datasetIndex].label;
var title = config.data.labels[index];
var dataValue = config.data.datasets[datasetIndex].data[index];
alert("Dataset Name: [" + datasetName + "] title: [" + title + "] value: [" + dataValue + "]");
}
};
score:6
In addition you can use var activePoints = chartObj.getElementsAtXAxis(evt);
to get the nearest active element based just on the x-axis. (Super useful for when you have some really small values)
score:7
I have a graph with multiple data sets. Using the proposed getElementsAtEvent(evt)
function, returns all data sets at a given x position but not only the single dataset index that I clicked on. So, there is no possibility to identify the individual data set.
To get only the single point clicked on:
const activePoint = chartObj.getElementAtEvent(event);
const datasetIndex = activePoint[0]._datasetIndex;
const itemIndex = activePoint[0]._index;
Notice the missing "s" in getElementAtEvent()
compared to getElementsAtEvent()
.
Source: stackoverflow.com
Related Query
- ChartJs 2.0 How do I obtain point information being clicked upon?
- how to change size of point in ChartJS
- Chartjs linechart with only one point - how to center
- ChartJs how to get from mulitiple dateset which dataset bar is clicked
- 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 add new data point and remove leftmost data point dynamically in Chartjs
- Chartjs Radar chart - How to dynamically highlight one of the gridlines at a specific point
- Moving point to clicked point in ChartJS (radarchart)
- How to run Chart.js samples using source code
- How to set ChartJS Y axis title?
- How to remove square label from tooltip and make its information in one line?
- ChartJS - Different color per data point
- How to disable chartjs legendclick
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- How do I prevent the scale labels from being cut off in chartjs?
- How to disable a tooltip for a specific dataset in ChartJS
- How to prevent first/last bars from being cut off in a chart with time scale
- How to modify chartjs tooltip so i can add customized strings in tooltips
- How to modify bar width in Chartjs 2 bar charts
- 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 hide data point labels
- 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
- How to display Line Chart dataset point labels with Chart.js?
- chartjs show dot point on hover over line chart
More Query from same tag
- Canvas Emptied but Reappears
- How do I keep chart.js charts in one JS file, and not get errors when the ID from the JS file don't exist on one specific html page?
- Google Pie Chart php mysql
- Am I using chart.update() correctly when trying to update the chart?
- getting additional value fields from data source for dx.chartjs doughnut chart
- How to fix bars in Chart.js with long labels
- how to only show zero grid axes at center and hide all other gridlines in chart js
- Angular canvas is undefined
- How to change color by clicking on the chart bar?
- Why my ChartJs only Coloring one section?
- Remove gap when updating ChartJS
- Set Scope in Angular-Charts.js after rendering
- Why is this bar chart using chart.js not rendering in react.js?
- destroy multiple charts on same page
- Vue chartjs is not rendering from API
- Display point style on legend in chart.js
- ChartJs Not displaying data
- Data will not draw onto my chart
- problem with chart js pie chart dataset data
- Chart.js data goes backwards when retroactively pushing datapoint from HTTP request using Angular
- Chart.js How to make a sample math axis?
- chart.js different x axis and tooltip format
- Using Chart.js with Pyramid Web Framework
- How to use computed property with data from JSON in data object for time axis in Vue using Chart.js
- how to modify horizontal bar chart using chart js
- How to change grid line width for one specific line
- Canvas static height Chartjs
- Chart.js horizontal bar grid line colors
- Reanimate Data Values in CharJS
- Angular-chart copied from example not showing up