Why do my dates not display on this graph (drupal, php, mysql)?
My mission this week has been to create a sales graph for my Drupal 6,
Ubercart and Ubercart Marketplace installation.
I settled on the charts_graphs module and decided to use the Bluff library
to graph the data. Although the supported module views_charts allows you
to create a wide range of charts, it wasn't able to retrieve the dataset
that I needed to graph the sales data. If anyone wants the details of why
not, just ask and I'll do my best to elaborate.
So I found an example of the php needed to generate a bluff graph:
<?php
$canvas = charts_graphs_get_graph('bluff');
$canvas->title = "Bluff Chart";
$canvas->type = "line"; // a chart type supported by the charting
engine. See further in the doc for the list.
$canvas->y_legend = "Y Legend";
$canvas->colour = '#D54C78';
$canvas->theme = 'keynote';
$canvas->series = array(
'Some Value' => array(9,6,7,9,5,7,6,9,7),
'Page Views' => array(6,7,9,5,7,6,9,7,3),
);
$canvas->x_labels = array('one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine');
$out = $canvas->get_chart();
return $out;
}
?>
This code then generates this graph...
The next stage of course is to get the data from a MySQL query into the
graph. This is where I'm outside of my comfort zone because I haven't
really done anything previously with PHP.
So after some digging around I found this post with an example that gave
me something to work with. I then used views to help get me started with
the SQL. I'm so close I can taste it but this last issue has had me
snagged and I can't think of any more searches to do... I'm sure you can
relate with that feeling :)
So this is the code I've developed so far:
<?php
global $user;
$uname = $user->uid;
$sql = "SELECT DATE_FORMAT((FROM_UNIXTIME(uc_orders.created) + INTERVAL
3600 SECOND), '%d/%m/%Y') as OrderDate, round(SUM(uc_order_products.cost *
uc_order_products.qty),2) AS SellerCommission,
round(SUM(uc_order_products.price * uc_order_products.qty),2) AS
CustomerPrice
FROM uc_order_products
LEFT JOIN node node_uc_order_products ON uc_order_products.nid =
node_uc_order_products.nid
LEFT JOIN uc_orders uc_orders ON uc_order_products.order_id =
uc_orders.order_id
INNER JOIN users node_uc_order_products__users ON
node_uc_order_products.uid = node_uc_order_products__users.uid
WHERE (node_uc_order_products__users.uid = $uname )
AND (uc_orders.created >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL
DAYOFMONTH(CURDATE())-1 DAY)))
AND (uc_orders.order_status in ('payment_received', 'completed'))
GROUP BY DAY(FROM_UNIXTIME(uc_orders.created))
ORDER BY MONTH(FROM_UNIXTIME(uc_orders.created)) desc,
DAY(FROM_UNIXTIME(uc_orders.created)) desc
LIMIT 31";
$result = db_query($sql,$uname);
while($row = db_fetch_array($result))
{
$sellercommission[] = (int) $row[SellerCommission];
$customerprice[] = (int) $row[CustomerPrice];
$orderdate[] = (string) $row[OrderDate];
}
$canvas = charts_graphs_get_graph('bluff');
$canvas->title = "Volume of sales";
$canvas->type = "line"; // a chart type supported by the charting
engine. See further in the doc for the list.
$canvas->y_legend = "Y Legend";
$canvas->colour = '#FFFFFF';
$canvas->width = '450';
$canvas->height = '300';
$canvas->theme = '';
$canvas->series = array(
'Seller Commission' =>array_values($sellercommission),
'Customer price' =>array_values($customerprice),
);
$canvas->x_labels = array_values($orderdate);
$out = $canvas->get_chart();
return $out;
?>
This code generates a perfect graph, all but the x-axis only displays "//"
for the dates...
When I run the query on the database this is the result set that I get.
Obviously I replace the placeholder with a UID:
09/09/2013 1328.13 1897.32
07/09/2013 455.00 650.00
What I can't work out is the type for "$orderdate[] ...." that I should be
using. I've tried varchar, int (displays 0), date (breaks it and displays
nothing), datetime (breaks it again)... Or perhaps I am retrieving the
wrong data as OrderDate in the SQL. Any ideas would be greatly
appreciated...
No comments:
Post a Comment