[Chartjs]-How to Modify ToolTips in Yii2 using dosamigos\chartjs\ChartJs?

2πŸ‘

βœ…

To modify the tooltip, you can use a callback function for tooltips label, as such :

...
'clientOptions' => [
    'tooltips'=> [
         'callbacks'=> [
             'label'=> new JsExpression("function(t, d) {
                     var label = d.labels[t.index];
                     var data = d.datasets[t.datasetIndex].data[t.index];
                     if (t.datasetIndex === 0)
                     return label + ': ' + data + ' Products';
                     else if (t.datasetIndex === 1)
                     return label + ': $' + data.toLocaleString();
              }")
          ]
     ],
     ...
],
...

note :

  • correct property name for chart options is clientOptions
  • JsExpression class should be used to compile the JS callback function

Leave a comment