dotn30471 2017-02-23 05:46
浏览 43
已采纳

将json编码的php推入chart.js

I am trying to push json encoded form my database select into chart.js data set but im not sure how to go about it without over engineering it.

Here is a simple select of scores from its retrospective table:

if($teamData == 0){
  $allTeams = 'All';
} else{
$sql = "SELECT * FROM compScore WHERE memberId = 1";
$result = $conn->query($sql);


  if ($result->num_rows > 0) {
      while($row = $result->fetch_assoc()) {
          $jsonScores = json_encode($row, JSON_PRETTY_PRINT);
    }
  }
}
header('Content-type: application/json');
echo $jsonScores;

the output of this is:

{ "id": "1", "score1": "2", "score2": "3", "score3": "5", "score4": "4", "score5": "3", "score6": "2", "score7": "1", "score8": "3", "memberId": "1" }

I have one issue where it only fetches 1 record and I also want to exclude the field memberId

Even more so how do I push that result into:

  datasets : [
    {
      fillColor: "rgba(102,45,145,.1)",
      strokeColor: "rgba(102,45,145,1)",
      pointColor : "rgba(220,220,220,1)",
      pointStrokeColor : "#fff",

      data : [] // HERE IS WHERE THE DATA NEEDS TO GO
    }

展开全部

  • 写回答

1条回答 默认 最新

  • dongnuo4594 2017-02-23 05:58
    关注

    I think you should take a look into the json layout expected by charts.js. https://developers.google.com/chart/interactive/docs/reference#dataparam It requires a first level of cols and rows, where every column is described by an id, a label and a type. The google documentation is pretty good, despite the format expected is kind of tedius.

    And here a PHP example for populating the chart.

    https://developers.google.com/chart/interactive/docs/php_example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部