如何在Symfony 3中使用Charts.js根据我的数据创建图形

时间:2018-10-31 17:43:25

标签: javascript symfony charts twig

我想在我的太空医生中看到一张图表,让他可以查看每月的约会次数。因此,我制作了控制器和存储库,使我可以拥有每月的约会次数,并且在我的视图中有一个对Charts.js的调用,该调用显示了带有预填充数据的图形,但是我希望图形能够用查询中的结果数据完成了每月的约会次数。

控制器

 public function statistiqueAction()
{

    $em = $this->getDoctrine->getManager;
    $repos = $em->getRepository("DoctixFrontBundle:Booking");
    $nb = $repos->getNb();


     return $this->render('DoctixMedecinBundle:Medecin:index.html.twig', array(
        'nb' => $nb 
     ));
}

存储库         

    namespace Doctix\FrontBundle\Repository;

 /**
 * BookingRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class BookingRepository extends \Doctrine\ORM\EntityRepository
{

public function getNb(){

    $qb = $this->createQueryBuilder('b')
               ->select('COUNT(b.id) AS nbr_rdv, MONTH(b.date_rdv) AS mois')
               ->groupBy('mois');

        return $qb->getQuery()
                  ->getResult();

}
}

Charts.js和查看

// Chart.js scripts
// -- Set new default font family and font color to mimic Bootstrap's default styling
Chart.defaults.global.defaultFontFamily = '-apple-system,system-ui,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif';
Chart.defaults.global.defaultFontColor = '#292b2c';
// -- Area Chart Example
var ctx = document.getElementById("myAreaChart");
var myLineChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ["Mar 1", "Mar 2", "Mar 3", "Mar 4", "Mar 5", "Mar 6", "Mar 7", "Mar 8", "Mar 9", "Mar 10", "Mar 11", "Mar 12", "Mar 13"],
    datasets: [{
      label: "Sessions",
      lineTension: 0.3,
      backgroundColor: "rgba(2,117,216,0.2)",
      borderColor: "rgba(2,117,216,1)",
      pointRadius: 5,
      pointBackgroundColor: "rgba(2,117,216,1)",
      pointBorderColor: "rgba(255,255,255,0.8)",
      pointHoverRadius: 5,
      pointHoverBackgroundColor: "rgba(2,117,216,1)",
      pointHitRadius: 20,
      pointBorderWidth: 2,
      data: [10000, 30162, 26263, 18394, 18287, 28682, 31274, 33259, 25849, 24159, 32651, 31984, 38451],
    }],
  },
  options: {
    scales: {
      xAxes: [{
        time: {
          unit: 'date'
        },
        gridLines: {
          display: false
        },
        ticks: {
          maxTicksLimit: 7
        }
      }],
      yAxes: [{
        ticks: {
          min: 0,
          max: 40000,
          maxTicksLimit: 5
        },
        gridLines: {
          color: "rgba(0, 0, 0, .125)",
        }
      }],
    },
    legend: {
      display: false
    }
  }
});
<div class="box_general padding_bottom">
            <div class="header_box version_2">
                <h2><i class="fa fa-bar-chart"></i>Statistiques 222</h2>
            </div>
            <canvas id="myAreaChart" width="100%" height="30" style="margin:45px 0 15px 0;"></canvas>
        </div>

谢谢

0 个答案:

没有答案