【问题标题】:How to fetch data in GoogleCharts [Symfony4]如何在 Google Charts [Symfony 4] 中获取数据
【发布时间】:2019-03-29 10:12:50
【问题描述】:

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

$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;
}

//... getter 和 setter }

实体候选

班级候选人 {

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

}

【问题讨论】:

  • 你试过你的数据库里有没有数据吗? $categories$categories->getCandidats() 究竟包含什么?
  • categories 返回所有类别和categories->getCandidats() 返回所有candidats 类别,我添加了count(categories->getCandidats())
  • 究竟包含什么?数据格式是否与以前相同?
  • 我已经更新了我的问题查看部分控制器

标签: php mysql symfony google-visualization symfony4


【解决方案1】:

我已经解决了我的问题 我已经在控制器中编辑了这个

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

【讨论】:

    猜你喜欢
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2016-07-30
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多