I have a PHP While loop:
$workgroups = mysqli_query($con,"SELECT a.TASK_ID, a.DELETE_WEEK, SUM(a.MON_BILL+a.TUE_BILL+a.WED_BILL+a.THU_BILL+a.FRI_BILL) AS hours, b.ID, b.TITLE, b.GROUP_ID, c.ID, c.NAME, d.VALUE_ID, reverse(reverse(d.VALUE) + 0) AS companyid, e.VALUE_ID, e.UF_CRM_1384938829 AS hourrate, f.ID, f.TITLE as companyname FROM b_report_week a INNER JOIN b_tasks b ON a.TASK_ID = b.ID INNER JOIN b_sonet_group c ON b.GROUP_ID = c.ID INNER JOIN b_utm_tasks_task d ON a.TASK_ID = d.VALUE_ID INNER JOIN b_uts_crm_company e ON reverse(reverse(d.VALUE) + 0) = e.VALUE_ID INNER JOIN b_crm_company f ON reverse(reverse(d.VALUE) + 0) = f.ID WHERE a.DELETE_WEEK = 0 GROUP BY a.TASK_ID");
while($row = mysqli_fetch_array($workgroups))
{
$taskTitle = $row['TITLE'];
$taskTotalHours = $row['hours'];
$companyName = $row['companyname'];
$companyRate = number_format($row['hourrate'] / 7.50,2);
$valueTotal = number_format($row['hours'] * number_format($row['hourrate'] / 7.50,2),2);
}
This produces the correct results in a table if I were to draw a table inside the loop (Displays about 8 rows with the 5 columns identified above). What I am wanting is to display the $companyname down the Y axis and the $valueTotal across the x axis. I rather new to Google graphs but I've tried the following (to no joy):
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Company', 'Sales'],
<I INSERTED THE ABOVE PHP CODE WITH CONNECTION DETAILS HERE WITH A CLOSING ?>>
['<?php echo $companyName; ?>', <?php echo $valueTotal; ?>],
<?php } ?> <!-- Ends the While Loop -->
['Last Row', 0]
]);
var options = {
title: 'Company Performance',
vAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<div id="chart_div" style="width: 900px; height: 800px;"></div>
use orientation : 'vertical', parameter in options array
try this
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Company', 'Sales'],
<?php while ($row = mysql_fetch_array($res)) {?>
['<?php echo $row['company']?>', <?php echo $row['valueTotal']?>],
<?php } ?>
]);
var options = {
title: 'Company Performance',
orientation : 'vertical',
vAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
Data should be numeric, number_format
returns string
. Use round
as suggested above or cast to appropriate type with (float)
or (int)
Try this instead:
<?php
$workgroups = mysqli_query($con,"SELECT a.TASK_ID, a.DELETE_WEEK, SUM(a.MON_BILL+a.TUE_BILL+a.WED_BILL+a.THU_BILL+a.FRI_BILL) AS hours, b.ID, b.TITLE, b.GROUP_ID, c.ID, c.NAME, d.VALUE_ID, reverse(reverse(d.VALUE) + 0) AS companyid, e.VALUE_ID, e.UF_CRM_1384938829 AS hourrate, f.ID, f.TITLE as companyname FROM b_report_week a INNER JOIN b_tasks b ON a.TASK_ID = b.ID INNER JOIN b_sonet_group c ON b.GROUP_ID = c.ID INNER JOIN b_utm_tasks_task d ON a.TASK_ID = d.VALUE_ID INNER JOIN b_uts_crm_company e ON reverse(reverse(d.VALUE) + 0) = e.VALUE_ID INNER JOIN b_crm_company f ON reverse(reverse(d.VALUE) + 0) = f.ID WHERE a.DELETE_WEEK = 0 GROUP BY a.TASK_ID");
$data = array(array('Company', 'Sales'));
while($row = mysqli_fetch_array($workgroups)) {
$taskTitle = $row['TITLE'];
$taskTotalHours = $row['hours'];
$companyName = $row['companyname'];
$companyRate = number_format($row['hourrate'] / 7.50,2);
$valueTotal = number_format($row['hours'] * number_format($row['hourrate'] / 7.50,2),2);
$data[] = array($companyName, $valueTotal);
}
$data[] = array('Last Row', 0);
?>
In javascript:
var data = google.visualization.arrayToDataTable(<?php echo json_encode($data, JSON_NUMERIC_CHECK); ?>);