For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > July 2007 > Image_Graph confusion









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Image_Graph confusion
brian

2007-07-19, 4:03 am

I've managed to cobble together a simple line graph showing the increase
in membership of a client's organisation (ie. a running total). I'm
feeding an assoc. array with dates (1st of each month as 'yyyy-mm-dd')
as the keys and the total number of members at the end said month. The
dates are placed along the X axis, each rotated 90 degrees.

What i'd like to add, though, is the number of new members for each
month. That is, the number of members who have joined during that month.
These could be arranged, not rotated, below each date or just above the
corresponding point along the line.

But i'm completely flummoxed as to how get this to work. Following is
what i have to work with.

-- snip --
/*
$apps_sum -> assoc. array, eg.
'2006-05-01' => 432
'2005-06-01' => 528

$apps_by_month -> assoc. array, eg.
'2006-05-01' => 89
'2005-06-01' => 96 <- new members in june, 528 - 432
*/

$Graph =& Image_Graph::factory('graph', array(700, 300));

$Graph->add(
Image_Graph::vertical(
$Title =& Image_Graph::factory('title', array('New Memberships', 11)),
$Plotarea = Image_Graph::factory('plotarea', array('axis_date')),
5
)
);

/* get start & end dates from the array keys
*/
$keys = array_keys($apps_sum);
$start_date = array_shift($keys);
$end_date = array_pop($keys);

$AxisX =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X);
$AxisX->setDateFormat('Y-m-d');
$AxisX->setStartDate($start_date);
$AxisX->setEndDate($end_date);

/* preprocessor; see below
*/
$AxisX-> setDataPreprocessor(Image_Graph::factory
('Image_Graph_DataPreprocessor_Function'
,
'first_only'));
$AxisX->setFontAngle('90');
$AxisX->setLabelOption('offset', 60);
$AxisX->setTickOptions(0, 0);

/* import dates and running totals
*/
$DataSet =& Image_Graph::factory('Image_Graph_Datase
t_Trivial',
array(&$apps_sum));

$Plot =& $Plotarea->addNew('line', array(&$DataSet));
$Plot->setLineColor('red');

$Font =& $Graph->addNew('font', 'Verdana');
$Font->setSize(8);
$Graph->setFont($Font);

$Graph->done();

/**
* Remove all label data excepting those that are the 1st of the month.
* This seems to be necessary to keep the X axis from having
* extraneous labels being written on top of each other. I think
* i might be able to do away with this by setting an interval
* but nothing i've tried has worked. There aren't any array keys that
* don't represent the 1st of a month, so i guess these extra points
* are the in-betweens. Or something.
*/
function first_only($date)
{
return (date('j', strtotime($date)) == '1') ? $date : '';
}

-- snip --

So, what i'd like to do is to overlay a plot containing the values in
$apps_by_month. As i said, they can be plotted along the line somehow,
or along the bottom, just below the labels on the X axis. But i'm
stumped on how to add these in. I've played around with
Image_Graph_Marker_Value, which seemed to be the thing i wanted, but no joy.

Any hints?

brian
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com