[Chartjs]-Chartjs โ€“ Insert additional data into chart tooltip

3๐Ÿ‘

โœ…

You can use closure in createDonatsChart functions. Set const as const originalData = [...data] and then you can access to data in afterLabel callback (as example):

tooltips: {
    callbacks: {
      afterLabel: function(tooltipItem, data) {
        const sexArray = originalData[tooltipItem['index']].sex
        const precent = sexArray.reduce((a, b) => a + b, 0) // your calculation here          
        return '(' + precent + '%)';
    }
  }
}

See example in playground: https://jsfiddle.net/denisstukalov/upw6asjm/63/#&togetherjs=3CN0LJDjbl

Leave a comment