如何在GoogleCharts中获取数据[Symfony4]

时间:2019-03-29 10:12:51

标签: php mysql symfony google-visualization symfony4

我使用GoogleChartsBundle,并且想要恢复数据库中的数据,例如,我想查看每个类别的候选候选人数,例如:

$pieChart = new PieChart();
$pieChart->getData()->setArrayToDataTable(
    [['Categorie', 'Number'],
     ['Categorie Web',     11],
     ['Categorie Mobile',      2]
    ]
);

这是工作 但我想将其添加到动态中(更多类别),我已经在 controller 中尝试了此操作,但不起作用: 更新

 $data = null;
        foreach($categories as $categorie)
        {
            $data[] = array(
                $categorie->getTitre(), count($categorie->getCandidats()),
            );
        }
return $this->json($data);
        $pieChart = new PieChart();
        $pieChart->getData()->setArrayToDataTable(
            [$data]
        );
  

PS:$ data包含[[“ Web”,1],[“ Mobile”,1]],但是在图表中我看不到数据

这是实体类别和实体候选人的示例

Entity Categorie
class Categorie
{ 
//...
    /**
     * @ORM\Column(type="string", length=255)
     */
    private $titre;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Candidat", mappedBy="categorie")
     */
    private $candidats;

public function __toString()
{
    return $this->getTitre();
}

/**
 * @return Collection|Candidat[]
 */
public function getCandidats(): Collection
{
    return $this->candidats;
}

// ...获取器和设置器 }

实体候选人

级候选人 {

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Categorie", inversedBy="candidats")
 */
private $categorie;

}

1 个答案:

答案 0 :(得分:0)

我解决了我的问题 我已经在控制器中对此进行了编辑

$data = [['Categorie','Nombre de candidats']];
    foreach($categories as $categorie)
    {
        $data[] = array(
            $categorie->getTitre(), count($categorie->getCandidats()),
        );
    }
    $pieChart = new PieChart();
    $pieChart->getData()->setArrayToDataTable(
        $data
    );
相关问题