Google Chart API Module

I was recently asked to take some civicrm demographic data from various groups and graph the data so that a visual comparison could be made between groups. In the course of looking for some graphing/charting technique that would integrate well with drupal & civicrm I discovered the google chart api module. I decided to use it to display some nifty looking 3D pie charts with a breakdown of different group demographics where each demographic was appropriately labeled on the pie chart.

The API allows one to do this pretty easily:

    // Set up the chart:
$chart1

[$group_name] = array('#chart_id' => $group_name .'_chart1',
'#title' => t($group_name .' - chart1'),
'#type' => CHART_TYPE_PIE_3D,
'#size' => chart_size(750, 300),
);

// Now add data & labels to the chart

while (looping through group data pulled from civicrm db)

This worked great with one problem… Many times the labels were too long and would get cut off at the edge of the chart making them difficult or impossible to read. After a bit of research I found that one work around for this was to add a margin to the url used to generate the chart. The margin is sent in the query string like so:

&chma=50,50,50,50

This will increase the margin around the chart preventing the labels from being cut off.
After looking at the available options in the chart API module, I found that margin wasn’t one of them, so I created a quick patch file to add it.
Inside of the function chart_build($chart) I added one line:

_chart_append('chma', $chart['#margins'],                 $data);

Then I needed to alter the _chart_append() function to expect ‘#margins’, so I added the following case in the switch statement there:

switch($attr) {
// Margins
case 'chma':
// Remove any whitespace from this value
// Otherwise these spaces are encoded and the margins will not work
$data[$attr] = str_replace(" ", "", $value);
break;

Once I applied the patch I was able to create my charts like so:

    // Set up the chart:
$chart1[$group_name] = array('#chart_id' => $group_name .'_chart1',
'#title' => t($group_name .' - chart1'),
'#type' => CHART_TYPE_PIE_3D,
'#size' => chart_size(750, 300),
'#margin' => '50, 50, 50, 50',
);

Then add the data and labels as before and presto! You can see all the labels without them being cut off.
The patch for this can be found at http://drupal.org/node/691904.

2017-03-31T06:20:16+00:00 Categories: Development, Drupal|

About the Author:

AJ worked with CivicActions from 2006 to 2013.