Search code examples
javascripttooltipchart.js

chart.js substitute tooltipItems


I'm trying to create a bar chart using chart.js (2.0) that return a different string for each tooltipItems instead of the tooltipItem itself: I've seen how to add a prefixed text after the tooltipItem but I haven't found any solution for my problem. I've tryied something like this:

tooltips: {

                    mode: 'single',
                     callbacks: {
                            label: function(tooltipItems, data) { 
                                    for (i=0; i<tipData.length; i++) {
                                          return tipData[i];
                                    }
// other possible version
//                                  for(var p in tipData){
//                                      return tipData[p];
//                                  }
                        }
                       }
                      },

but it's not working. Thank you in advance for any help and best regards.


Solution

  • Try to change your code the following way:

    tooltips: {
        mode: 'single',
        callbacks: {
             label: function(tooltipItems, data) { 
                    return tipData[tooltipItems.index]
             }  
        }
    }