I want to display sales and purchase graph in my webpage. User can select category Purchase, Sales, Production. I have separate table for both Purchase and Sales(AccPurchase and AccSales). And production is a combination of both. After that user can select view, it will be weekly where graph will display week wise of selected month. Monthly means graph of all the months of year selected. And Yearly means it will display year wise. In all the graphs i want to display only sum of amount. After that there is 2 more dropdown to select year and month.
Link of page where i want to display graph
past.html
<head>
<title>Past Performance Graph</title>
<link rel="stylesheet" type="text/css" href="css/pastperfomance.css">
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.charts.load('current', {'packages':['bar']});
function drawChart() {
var category = document.getElementById('category');
var categorySelected = category.options[category.selectedIndex].value;
var view = document.getElementById('view');
var viewSelected = view.options[view.selectedIndex].value;
var month = document.getElementById('month');
var monthSelected = month.options[month.selectedIndex].value;
var year = document.getElementById('year');
var yearSelected = year.options[year.selectedIndex].value;
var settings = {
"url": "php/past.php",
"method": "post",
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
},
"data": {
"category": categorySelected,
"view": viewSelected,
"month": monthSelected,
"year": yearSelected
}
}
$.ajax(settings).done(function(response) {
var data = google.visualization.arrayToDataTable([
['Month', 'Amount'], response
]);
var options = {
chart: {
title: 'Past Performance Graph',
subtitle: 'Duration : Jan 2018- Jun 2018',
}
};
var chart = new google.charts.Bar(document.getElementById('graph'));
chart.draw(data, google.charts.Bar.convertOptions(options));
});
}
</script>
</head>
past.php
<?php
$category = $_POST["category"];
$view = $_POST["view"];
$month = $_POST["month"];
$year = $_POST["year"];
$con = mysqli_connect("localhost","username","pw","db");
if($category == "Purchase"){
if($view == "Weekly"){
$sql = "SELECT Date,SUM(Amount) from AccPurchase WHERE Date LIKE '$year-$month%' GROUP BY WEEK(Date)";
}else if($view == "Monthly"){
$sql = "SELECT Date,SUM(Amount) from AccPurchase WHERE Date LIKE '$year%' GROUP BY MONTH(Date)";
}else if($view == "Yearly"){
$sql = "SELECT Date,SUM(Amount) from AccPurchase GROUP BY YEAR(Date)";
}
}else if($category == "Sales"){
if($view == "Weekly"){
$sql = "SELECT Date,SUM(Amount) from AccSales WHERE Date LIKE '$year-$month%' GROUP BY WEEK(Date)";
}else if($view == "Monthly"){
$sql = "SELECT Date,SUM(Amount) from AccSales WHERE Date LIKE '$year%' GROUP BY MONTH(Date)";
}else if($view == "Yearly"){
$sql = "SELECT Date,SUM(Amount) from AccSales GROUP BY YEAR(Date)";
}
}
$exc = mysqli_query($con, $sql);
$rows = array();
while ($row = mysqli_fetch_assoc($exc)) {
$rows[] = array("v"=>$row["Date"], "f"=>$row["SUM(Amount)"]);
}
header('Content-Type: application/json');
echo json_encode($rows);
mysqli_close($con);
?>
Something is wrong in my echoing array from php script.
Data may be needed in this format- https://stackoverflow.com/a/15381171/9786296