0

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
    }
4
  • first remove unnecessary elements from array. refer this link : stackoverflow.com/questions/4466159/… Commented Feb 23, 2017 at 13:52
  • after that encode it in to json format and pass in to the js library portion. Commented Feb 23, 2017 at 13:53
  • @prakashtank im not sure how to pass it into the js lib, thats where I get a little stuck Commented Feb 23, 2017 at 13:53
  • @Dude : stackoverflow.com/questions/30928840/… Commented Feb 23, 2017 at 13:55

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Obviously this documentation is reference to Google charts, but I can easily translate this into chart.js? The example looks very similar.
Chart.js expects a very similar structure and is the only way it can fail so, attention to the json format and you should be fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.