Chartjs-Set labels from array

1👍

Regarding your own answer – you could change this part:

$arrChart = array();
foreach($chart as $c){
    $name = $c['name'];
    array_push($arrChart, $name);
}

into this:

$arrChart = array_column($chart, 'name');

You can read more about array_column here

0👍

Ok it worked for me this way :

public function show(Joueur $joueur): Response
{
    $em = $this->getDoctrine()->getManager();        
    $chart = $em->getRepository(Played::class)->find4Chart($joueur);
    $arrChart = array();
    foreach($chart as $c){
        $name = $c['name'];
        array_push($arrChart, $name);
    }

    return $this->render('joueur/show.html.twig', [
        'joueur' => $joueur,
        'chart' => $arrChart,
    ]);
}

Chart :

var ctx = document.getElementById('myChart');
var myChart = new Chart(ctx, {
    type: 'doughnut',
    data: {
        labels: {{chart | json_encode | raw}},

I don’t know if it’s the right way to do it but it works, if any of you have a better idea, I’ll take it 😉

Leave a comment