Hi,I am trying to create a graph using chartjs,I am still new on this creating graph and i think i am in big trouble,what i want to achieve is that i am going to get all the number registered employee in each month from jan-dec. and to draw it to a graph with the use of jquery.Ajax,…my question on this do i need to create 12 php script for counting the registered employe in each month ?and how do i set them to the graph ?
This is my code please correct me if i am wrong.
$.ajax({
type: 'post',
url : 'countemployee.php',
success: function(data){
jan = data.january;
feb = data.february;
ect...
}
});
countemployee.php
$jan='';
try{
//query for the mont of january
$connectionString = $db->prepare("select (*) as count from employee
where date_hired > = '2013-01-01' AND date_hired <= '2013-01-31'");
$jan = $sql->fetchColumn();
}
catch(){
//log error message here.
}
try{
//another count query for month of february
}
catch(){
//log error message here.
}
try{
//another count query for month of march
}
catch(){
//log error message here.
}
etc...
encode to json
$month = array("$january"=>'$jan',
"$february"=>'$feb',
etc..);
json_encode($month);
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale = 1, user-scalable = no">
<script src="Chart.js-master/Chart.js"></script>
</head>
<body>
<canvas id="myChart" height="450" width="600"></canvas>
<script type="text/javascript">
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
datasets: [
{
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,1)",
data: jan,feb,etc..
}
]
}
var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx).Bar(data);
</script>
</body>
</html>