Chartjs-PHP string data passed to Chart.js not rendering well

1πŸ‘

βœ…

In php you can do this

//send the string values to javascript
$grade_range_string = mb_substr($grade_range_string, 0, -2);
$percentage_count_string = implode(", ", $percentage_count_array);
//chart colors
$grade_chart_colors_string = "";
$grade_chart_colors_array = grade_chart_colors();

// New Code
$grade_chart_colors_string = join(",", $grade_chard_colors_array); // will join all the colors with ','
//for eg. $grade_chart_colors_string = "red,blue,yellow" 


$grade_chart_colors_string = mb_substr($grade_chart_colors_string, 0, -2);

In javascript make the string back to an array

var grade_chart_colors_string = $('#grade_chart_colors_string').attr('data-value');
var grade_chart_colors_array = grade_chart_colors_string.split(',');

That should pass it as an array in data. You can do the rest like this

0πŸ‘

You should change this:

<?php echo $percentage_count_string; ?>

to:

<?php echo json_encode($percentage_count_string); ?>

It may solve your problem

Cheers

Leave a comment