Symfony Ux using chartJs : How can i feed the x's axes data?

Viewed 41

I want to make a graph with specific y and x. I discovered that to make this kind of graph, the right type is "SCATTER" so here is my code in my service :

    $chart = $this->chartBuilder->createChart(Chart::TYPE_SCATTER);

    $chart->setData([
             'datasets' => [
                [
                  'backgroundColor' => 'rgb(255, 99, 132)',
                  'data' => [20.10],[30.20]
                ],

        ]]);

        $chart->setOptions([
            'scales' => [
                'y' => [
                    'suggestedMin' => 0,
                    'suggestedMax' => 100,
                ],
                'x' => [
                  'type' => 'linear',
                  'position' => 'bottom',
                  'suggestedMin' => 0,
                  'suggestedMax' => 100,
                ],
            ],
        ]);

        return $chart;

It doesnt trigger error but dont display the curve. What is the correct syntaxe for this ? Futhermore, i would like to make like 100 curve on this graph. What should be the best way to achieve this ? thank for helping !

1 Answers

I managed to make it, here what the syntaxe look like:

  while($i <= $N)
      {
        $data =
           [
            'label'=> 'Scatter Dataset',
            'data' => 
               [['x'=>$point1[$i],'y'=>$point1[$i]],  
               ['x'=>$point2[$i],'y'=>$point2[$i]]],
               'backgroundColor' => 'rgb(255, 99, 132)',
               'showLine'=> true,
             ];
          $datas[$i] = $data;
        $i++;
    }
    $chart->setData([
      'datasets' => $datas
     ]);
Related