Chartjs-Adding additional properties to a Chart JS dataset

1👍

getSegmentsAtEvent (and its equivalents for other chart types) return chart elements (sectors, points, bars, etc.). You have to use any of the chart element properties and the data object to figure out the index and then use that to get the additional property that you want.

For instance, if pieData is your data object

$("#review-type").click(
    function (evt) {
        var activePoints = reviewChart.getSegmentsAtEvent(evt);

        if (activePoints.length)
        alert(pieData.filter(function (e) { return e.label === activePoints[0].label; })[0].segment);
            ...

You could also set additional properties on the properties that get transferred from the object to the element (e.g. label), but that’s a bit hacky.


Fiddle – http://jsfiddle.net/nm9ay728/

Leave a comment