0

I have a PHP-array for the Javascript Library chart.js and it´s like this:

$array = [ 'options' => [ 'plugins' => [ 'tooltip' => [ 'callbacks' => [ 'label' => "function(context) { let label = context.dataset.label || ''; if (label) { label += ': '; } if (context.parsed.y !== null) { label += context.parsed.y + ' ' + '€'; } return label; }" ] ] ] ] ];

This PHP-array is requested by AJAX and in JavaScript I have:

new Chart(ctx, array);

array contains the data from the PHP-array.

The problem is the 'label'-element that contains a callback-function for JavaScript. The function does not work in this way. Is it possible to get this to work or do I have to bring the callback-function back to JavaScript and only request the relevant data ('€')? The callback-function isn´t static, so I thought it´s better to have it in PHP.

Kalle
  • 1
  • 1
    What about the function isn't static? There probably aren't that many variations of it. You should have the function in client-side Javascript code, and only pass the changing values via JSON and pass them to the function as arguments. – deceze Jan 04 '23 at 11:14
  • finally I'll have to do it this way. I thought there would be a different notation that the function could be passed with php. – Kalle Jan 04 '23 at 16:44

0 Answers0