[Chartjs]-Chart.js 2.0 add new property to dataset

3👍

While you could extend the chart to do this, if you don’t mind relying on internal properties (that could possibly change in later versions of chart.js), you can just use the _datasetIndex and _index properties to retrieve your extra properties from the chart configuration, like so

  options: {
    onClick: function(e, d) {
      if (d.length > 0) {
        console.log(this.config.data.datasets[d[0]._datasetIndex].code[d[0]._index]);
      }
    }
    ...

and

  ...
  datasets: [{
    code: ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
    ...

Fiddle – http://jsfiddle.net/55cdvb20/

Leave a comment