-
Notifications
You must be signed in to change notification settings - Fork 0
/
pie_chart_php
122 lines (92 loc) · 2.33 KB
/
pie_chart_php
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
index.php
**********************
<!DOCTYPE html>
<html>
<head>
<title>Creating Dynamic Data Graph using PHP and Chart.js</title>
<style type="text/css">
BODY {
width: 550PX;
}
#chart-container {
width: 100%;
height: auto;
}
</style>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/Chart.min.js"></script>
</head>
<body>
<canvas id="myChart" width="400" height="400"></canvas>
<script>
$(document).ready(function () {
showGraph();
});
function showGraph()
{
{
$.post("piechart_data.php", function(data){
console.log(data);
var name = [];
var marks = [];
for (var i in data) {
name.push(data[i].region);
marks.push(data[i].total);
}
var ctx = document.getElementById('myChart').getContext('2d');
//start
// For a pie chart
var myPieChart = new Chart(ctx, {
type: 'pie',
data: {
labels: name/*['Red', 'Yellow', 'Green', 'Purple', 'Orange']*/,
datasets: [{
label: '# of Votes',
data: marks/*[12, 19, 3, 5, 2]*/,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
//end
});
}
}
</script>
</body>
</html>
piechart_data.php
<?php
header('Content-Type: application/json');
$conn = mysqli_connect("localhost","root","","coronadb");
$sqlQuery ='SELECT region, count(region) as total FROM tbl_victim GROUP BY region';
$result = mysqli_query($conn,$sqlQuery);
$data = array();
foreach ($result as $row) {
$data[] = $row;
}
mysqli_close($conn);
echo json_encode($data);
?>