Hi I'm new to web designing and some of my jargon may be wrong. I'm trying to create a graph for lectures that shows the average rating each lecture gets. I have already calculated the average for each of the 18 lectures. Now I want to take the value for each one and insert it as the value in the graph.
Here is how I query and output my lectures:
<?php
ini_set('display_errors', 1);
include ('connection.php');
$queryLecture = "SELECT DISTINCT lectureID FROM LectureReview ORDER BY lectureID";
$resultLecture = $mysqli->query($queryLecture);
while ($rowLecture = $resultLecture->fetch_assoc()) {
echo "<div class=\"row\">";
echo "<div class=\"box\">Lecture{$rowLecture['lectureID']}:</div>";
$lectureID = $rowLecture['lectureID'];
$mysqli2 = new mysqli($hostname, $username, $password, $database);
$stmt = $mysqli2->prepare("SELECT AVG( lectureReview ) AS lectureAvg FROM LectureReview WHERE lectureID = ?");
$stmt->bind_param('i', $lectureID);
$stmt->execute();
$stmt->bind_result($lectureAvg);
$stmt->fetch();
echo "<div class=\"box\">Average Lecture Rating: {$lectureAvg}</div>";
echo "</div>";
}
?>
Then, OnLoad
I do this:
var chart = new CanvasJS.Chart("chartContainer", {
theme: "theme1",
title:{
text: "Average Lecture Score"
},
animationEnabled: true,
data: [
{
// Change type to "bar", "area", "spline", "pie",etc.
type: "column",
dataPoints: [
{ label: "Lecture 1", y: 4.5 },
{ label: "Lecture 2", y: 4.5 },
{ label: "Lecture 3", y: 5.0 },
{ label: "Lecture 4", y: 5.0 },
{ label: "Lecture 5", y: 5.0 },
{ label: "Lecture 6", y: 5.0 },
{ label: "Lecture 7", y: 5.0 },
{ label: "Lecture 8", y: 5.0 },
{ label: "Lecture 9", y: 5.0 },
{ label: "Lecture 10", y: 5.0 },
{ label: "Lecture 11", y: 5.0 },
{ label: "Lecture 12", y: 5.0 },
{ label: "Lecture 13", y: 5.0 },
{ label: "Lecture 14", y: 5.0 },
{ label: "Lecture 15", y: 5.0 },
{ label: "Lecture 16", y: 5.0 },
{ label: "Lecture 17", y: 5.0 },
{ label: "Lecture 18", y: 5.0 },
]
}
]
});
chart.render();
And my target container:
<div id="chartContainer"
style="height: 300px; width: 90%; position: absolute; padding-top:50px;">
</div>
At the moment it looks like this
How can I use the values from the database for the CanvasJS graph?
This is how I would do it:
Firstly, create an array outside your while
loop. This is going to hold the total dataset for your graph. So:
$dataset = array();
Then, inside your while
loop, create another array. This array will store the lecture name and its average rating. This array will have the associative names label
and y
(used in your chart dataset).
$lectureRating = array();
At the end of the loop, you then add the $lectureID
and the $lectureAvg
to that array. And then add it to the $dataset
array.
$lectureRating['label'] = $lectureID;
$lectureRating['y'] = $lectureAvg;
array_push($dataset, $lectureRating);
After the while loop is finished the $dataset
array should have a similar structure to:
Array[n] (
[0] Array[2](
['label'] => "Lecture ID 1"
['y'] => 4.3
)
[1] Array[2](
['label'] => "Lecture ID 2"
['y'] => 3.7
)
// And so on
)
We now need to transform this into a Javascript Object (JSON) so that the js can interpret it. This is done by encoding it.
$graphData = json_encode($dataset);
This turns the above array into something like this (look familiar?):
[{"label": "Lecture ID 1", "y": 4.3}, {"label": "Lecture ID 2", "y": 3.7}]
This can then be output into the dataPoints
object of your chart.
data: [
{
type: "column",
dataPoints: <?php echo $graphData; ?>
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.