-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart.php
More file actions
71 lines (62 loc) · 2.51 KB
/
chart.php
File metadata and controls
71 lines (62 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<?php
require("station.php");
$con = mysqli_connect("localhost","root","temppassword123!","maindb");
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT `station`, COUNT(*) as freq FROM `responses` GROUP BY `station`";
$result = mysqli_query($con, $sql);
$rows = mysqli_num_rows($result);
$stations = array();
$count = 0;
$stations = array();
while ($array = mysqli_fetch_array($result)) {
$station = new Station($array[0]);
array_push($stations, $station);
}
foreach ($stations as &$station) {
$sql = "SELECT COUNT(*) as freq FROM `responses` WHERE feedback = 1 AND station = '".$station->name."'";
$result = mysqli_query($con, $sql);
$value = mysqli_fetch_object($result);
$station->positive = $value->freq;;
}
foreach ($stations as &$station) {
$sql = "SELECT COUNT(*) as freq FROM `responses` WHERE feedback = 0 AND station = '".$station->name."'";
$result = mysqli_query($con, $sql);
$value = mysqli_fetch_object($result);
$station->negative = $value->freq;;
}
$rowsString = '[';
for ($i = 0; $i < count($stations) - 1; $i++)
{
$rowsString = $rowsString."['".$stations[$i]->name."',".round($stations[$i]->getRating()*100,0)."],";
}
$noOfStations = count($stations) - 1;
$rowsString = $rowsString."['".$stations[$noOfStations]->name."',".round($stations[$noOfStations]->getRating()*100,0)."]]";
echo $rowsString;
?>
<script type="text/javascript">
google.load('visualization', '1.0', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Stations');
data.addColumn('number', 'Feedback');
data.addRows(<?php echo $rowsString; ?>);
// Set chart options
var options = {'title':'Test',
'width':500,
'height':500};
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>