必承其重 | 欲带皇冠 2017-06-30 15:21 采纳率: 0%
浏览 388

ChartJS和Ajax调用

I would like to make charts using ChartJS and PHP ( Silex Framework )

This is my ajax call

$.ajax({ url: 'stats',
   data: {method: 'dossierRepartitionType'},
   type: 'post',
   datatype: 'json',
   success: function(output) {
               dataDossierRepartitionType=output;
             },
   error: function () {
                 alert("Oops there is an error.");
  }});

This is my PHP function which i managed to call

public function dossier(){
$stmt = "SELECT count(*) FROM dossier GROUP BY typedossier";
$stmt = $this->db->prepare($stmt);
$rows=$stmt->execute();
$rows = $stmt->fetch(PDO::FETCH_NUM);
return ?????

}

And here is my chart :

var ctx = document.getElementById("myChart");
ctx.width = 400;
ctx.height = 400;
data = {
  datasets: [{
  data: [dataDossierRepartitionType, 20],
  backgroundColor: [
  'rgb(255, 99, 132)',
  'rgb(54, 162, 235)',
  ],
  borderColor: [
  'white',
  'white',
  ],
  borderWidth: 1
  }],

  // These labels appear in the legend and in the tooltips when hovering        different arcs
  labels: [
    'Red',
    'Blue',
  ]
  };
var myDoughnutChart = new Chart(ctx, {
type: 'doughnut',
data: data,
options: {
legend: {
labels: {
    fontColor: "white",
    fontSize: 18
}
},
maintainAspectRatio: false,
responsive: false
}
});

Route.php

$app->post('/stats', function () use ($app) {
session_start();
if(isset($_POST['method']) && !empty($_POST['method'])) {
  $method = $_POST['method'];
  switch($method) {
    case 'dossierRepartitionType' :
     $dossiers=$app['dao.dossier']->dossierRepartitionType();
     break;
    }
  }
  return new ResponseSilex("$dossiers");
 });

So my AJAX call the route and then get the result of the function into $dossiers which is ouput in the Reponse, am i doing it right ?

How can i return an array with all the datas value for each count ? I struggle to catch error and to find a proper way to bind MYSQL count value to my chart

Thank you

  • 写回答

3条回答 默认 最新

  • 关注

    The main idea is that you should format your data in your model, then return JSON to the front end via json_encode. After that, you would parse the json in your ajax returns and then pass the appropriate data to the chart.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题