001.
<?php
002.
$servername
=
"localhost"
;
003.
$username
=
"root"
;
004.
$password
=
"44@44"
;
005.
$dbName
=
"hrdata"
;
006.
$conn
=
new
mysqli(
$servername
,
$username
,
$password
,
$dbName
);
007.
mysqli_query(
$conn
,
"SET NAMES 'utf8' "
);
008.
if
(
$conn
->connect_error) {
009.
die
(
"Connection failed: "
.
$conn
->connect_error);
010.
}
011.
012.
$query
=
" SELECT * FROM data_chart_count "
;
013.
014.
$result
=
$conn
->query(
$query
);
015.
016.
017.
$jsonArray
=
array
();
018.
$jsonArrayMale
=
array
();
019.
$jsonArrayFeMale
=
array
();
020.
021.
if
(
$result
->num_rows > 0) {
022.
while
(
$row
=
$result
->fetch_assoc()) {
023.
024.
$jsonArrayItemSum
=
array
();
025.
026.
$jsonArrayItemSum
[
'label'
] =
$row
[
'New'
];
027.
028.
$jsonArrayItemSum
[
'value'
] =
$row
[
'Allsum'
];
029.
array_push
(
$jsonArray
,
$jsonArrayItemSum
);
030.
031.
032.
033.
$jsonArrayItemMale
=
array
();
034.
$jsonArrayItemMale
[
'label'
] =
$row
[
'New'
];
035.
036.
$jsonArrayItemMale
[
'value'
] =
$row
[
'MMale'
];
037.
array_push
(
$jsonArrayMale
,
$jsonArrayItemMale
);
038.
039.
040.
$jsonArrayItemFeMale
=
array
();
041.
$jsonArrayItemFeMale
[
'label'
] =
$row
[
'New'
];
042.
043.
$jsonArrayItemFeMale
[
'value'
] =
$row
[
'FFemale'
];
044.
array_push
(
$jsonArrayFeMale
,
$jsonArrayItemFeMale
);
045.
046.
047.
$dataPoints1
=
array
(
048.
array
(
"label"
=>
"2010"
,
"y"
=> 36.12),
049.
array
(
"label"
=>
"2011"
,
"y"
=> 34.87),
050.
array
(
"label"
=>
"2012"
,
"y"
=> 40.30),
051.
array
(
"label"
=>
"2013"
,
"y"
=> 35.30),
052.
array
(
"label"
=>
"2014"
,
"y"
=> 39.50),
053.
array
(
"label"
=>
"2015"
,
"y"
=> 50.82),
054.
array
(
"label"
=>
"2016"
,
"y"
=> 74.70)
055.
);
056.
$dataPoints2
=
array
(
057.
array
(
"label"
=>
"2010"
,
"y"
=> 64.61),
058.
array
(
"label"
=>
"2011"
,
"y"
=> 70.55),
059.
array
(
"label"
=>
"2012"
,
"y"
=> 72.50),
060.
array
(
"label"
=>
"2013"
,
"y"
=> 81.30),
061.
array
(
"label"
=>
"2014"
,
"y"
=> 63.60),
062.
array
(
"label"
=>
"2015"
,
"y"
=> 69.38),
063.
array
(
"label"
=>
"2016"
,
"y"
=> 98.70)
064.
);
065.
$dataPoints3
=
array
(
066.
array
(
"label"
=>
"2010"
,
"y"
=> 63.61),
067.
array
(
"label"
=>
"2011"
,
"y"
=> 73.55),
068.
array
(
"label"
=>
"2012"
,
"y"
=> 73.50),
069.
array
(
"label"
=>
"2013"
,
"y"
=> 83.30),
070.
array
(
"label"
=>
"2014"
,
"y"
=> 65.60),
071.
array
(
"label"
=>
"2015"
,
"y"
=> 69.38),
072.
array
(
"label"
=>
"2016"
,
"y"
=> 92.70)
073.
);
074.
}
075.
}
076.
077.
$conn
->close();
078.
079.
080.
echo
json_encode(
$jsonArray
).
'<p> = sum </p> <br> '
;
081.
echo
json_encode(
$jsonArrayMale
).
'<p> = Male </p> <br> '
;
082.
echo
json_encode(
$jsonArrayFeMale
).
'<p> = FeMale </p><br> '
;
083.
084.
085.
?>
086.
<!DOCTYPE HTML>
087.
<html>
088.
<head>
089.
<script>
090.
window.onload =
function
() {
091.
092.
var
chart =
new
CanvasJS.Chart(
"chartContainer"
, {
093.
animationEnabled: true,
094.
theme:
"light2"
,
095.
title:{
096.
text:
"Average Amount Spent on Real and Artificial X-Mas Trees in U.S."
097.
},
098.
legend:{
099.
cursor:
"pointer"
,
100.
verticalAlign:
"center"
,
101.
horizontalAlign:
"right"
,
102.
itemclick: toggleDataSeries
103.
},
104.
data:[{
105.
type:
"column"
,
106.
name:
"Sum"
,
107.
indexLabel:
"{y}"
,
108.
yValueFormatString:
"0.##"
,
109.
showInLegend:true,
110.
dataPoints:<?php
echo
json_encode(
$dataPoints1
); ?>
111.
},
112.
{
113.
type:
"column"
,
114.
name:
"Male"
,
115.
indexLabel:
"{y}"
,
116.
yValueFormatString:
"0.##"
,
117.
showInLegend:true,
118.
dataPoints:<?php
echo
json_encode(
$dataPoints2
); ?>
119.
},
120.
{
121.
type:
"column"
,
122.
name:
"Female"
,
123.
indexLabel:
"{y}"
,
124.
yValueFormatString:
"0.##"
,
125.
showInLegend:true,
126.
dataPoints:<?php
echo
json_encode(
$dataPoints3
); ?>
127.
}
128.
]
129.
});
130.
chart.render();
131.
132.
function
toggleDataSeries(e){
133.
if
(typeof(e.dataSeries.visible) ===
"undefined"
|| e.dataSeries.visible) {
134.
e.dataSeries.visible = false;
135.
}
136.
else
{
137.
e.dataSeries.visible = true;
138.
}
139.
chart.render();
140.
}
141.
142.
}
143.
</script>
144.
</head>
145.
<body>
146.
<div id=
"chartContainer"
style=
"height: 370px; width: 100%;"
></div>
148.
</body>
149.
</html>