score:19

Accepted answer

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().


Related Query

More Query from same tag