[Chartjs]-Is it possible to define data attributes for each dataset value in a Chart.js chart?

1πŸ‘

βœ…

It depends how you structure your data. In my example I made an object that contains all the data.

let dataObject = {
  Gabriel: {
    diapers: 40,
    age: 4
  }, 
  // Same structure here...
  Zelie: {...}, 
  Santiago: {...}, 
  Serafina: {...}, 
  Berenice: {...}, 
  Pia: {...}
}

This way you can easily access your data in your tooltip with

tooltips: {
  callbacks: {
    label: function(tooltipItem, data){
      return ['Name: '+tooltipItem.label, 'Diapers: '+tooltipItem.value, 'Age: '+dataObject[tooltipItem.label].age]
    }
  }
}

You should get all the information you need in the jsbin.

https://jsbin.com/qiyeyohavo/1/edit?html,js,output

Leave a comment