Community Documentation

Practical examples

Last updated April 24, 2009. Created by kong on April 24, 2009.
Log in to edit this page.

Here is an example of how to use Open Flash Chart 2 API according to ionmedia's support request #435426: how to boild charts from cck fields ?.

I was a little bit confused with his explanation but here's what I guess he wants to do:

He created a new content type called "material" and in this content type there is a field named "importance", which its value is numeric data between 1-5. He created 10 materials and assigned them their importance values. And now he wants to build a pie chart to show the summary of all materials, grouped by their importance value e.g. How many materials has importance level 1, level 2, and so on.

So I created a test site with the following setup:

  1. First, I created a new content type and set its internal name to material
  2. Then I created an Integer field named importance, set form element type to Select list, and set its allowed value list to be 1, 2, 3, 4, and 5
  3. Next, I created 10 nodes of type material and assigned their importance to 1, 2, 1, 1, 1, 3, 4, 2, 5, 1 respectively

Now it's time to create the pie chart. I created a new Page and set input format to PHP code, then type in the following code:

<?php

// count all importance value from published (status = 1) node of type "material"
$sql = "SELECT field_importance_value AS importance, COUNT(m.nid) AS total
  FROM {content_type_material} m
  LEFT JOIN {node} n
  ON n.nid = m.nid
  WHERE n.type = 'material' AND n.status = 1
  GROUP BY field_importance_value"
;

$result = db_query(db_rewrite_sql($sql));

// create new chart
$chart = ofc_api_chart();

// set its title
$chart->set('title', array(
 
'text' => 'Material importance',
 
'style' => 'font-family: Verdana, Helvetica, Arial, serif; font-size: 20px; color: #808080;',
));

// create pie chart element
$pie = ofc_api_element('pie');

// set some effects and colors
$pie->set('alpha', 0.8);

$fade = ofc_api_element('fade');

$bounce = ofc_api_element('bounce');
$bounce->set('distance', 5);

$pie->set('animate', array(
 
$fade,
 
$bounce,
));

$pie->set('colours', ofc_api_color_theme('retro-spanky'));

// set label and value for each pie slice
while ($data = db_fetch_object($result)) {
 
$slice = ofc_api_element();
 
$slice->set('label', 'Level ' . $data->importance);
 
$slice->set('value', $data->total);

 
$pie->add('value', $slice);
}

// add pie element to the chart
$chart->add('element', $pie);

// render the chart
print ofc_api_render($chart);

?>

And here's the result on Flickr (Sigh, I cannot attach the image to documentation page.)

As you may already realized, the important issue here is "how do we extract the data from the database?" In this case I just use plain SQL to select the data. However, there should be a better way to do it such as using Views to build the query, etc. So please feel free to share you ideas and suggestions.

NOTE: If you want to use the data file method, just create a new module, then wrap this code snippet in a function and change the last line from print ofc_api_render($chart) to return $chart. Don't forget to add the function name in hook_ofc_api_data().

About this page

Drupal version
Drupal 6.x

Develop for Drupal

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.