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

The data file method requires the user to create a "data file" (which, in fact, is a function that returns the chart data), and Open Flash Chart 2 API will use the Drupal menu system to generate a URL that points to the function and pass it to open-flash-chart.swf. This may seems complicated at first, but once you get the concept, it's pretty simple.

The first thing is to create a new module. Let's assume you want to create a module named "My chart". Here's what you have to do:

  1. Create a directory named my_chart under sites/all/modules/custom or wherever you usually install your modules.
  2. You will need two files for the module to work in Drupal 6 -- my_chart.info and my_chart.module

NOTE: I assume that you have some experience with creating / modifyng modules, if not, please consider reading the Module developer's guide to get the idea of how Drupal's module system works.

Here's a sample content of my_chart.info:

; $Id$
name = My chart
description = A test module to create some charts using Open Flash Chart 2 API
core = 6.x
dependencies[] = ofc_api

Now we will create a function that returns chart data in my_chart.module. Suppose we want to create a line chart just like this example:

<?php
function my_chart_line() {
 
$data = array();
  for(
$i = 0; $i < 6.2; $i += 0.2) {
   
$data[] = (sin($i) * 1.9) + 4;
  }
 
$line = ofc_api_element('line');
 
$line->set('values', $data);
 
$line->set('width', 2);
 
$line->set('colour', '#3d5c56');
 
$dot = ofc_api_element('solid-dot');
 
$dot->set('dot-size', 3);
 
$dot->set('halo-size', 1);
 
$dot->set('colour', '#3d5c56');
 
$line->set('dot-style', $dot);
 
$y_axis = ofc_api_element();
 
$y_axis->set('min', 0);
 
$y_axis->set('max', 8);
 
$y_axis->set('steps', 4);
 
$title = array(
   
'text' => date("D M d Y"),
  );
 
$chart = ofc_api_chart();
 
$chart->add('element', $line);
 
$chart->set('title', $title);
 
$chart->set('y_axis' , $y_axis);
  return
$chart;
}
?>

OK, now it comes to the important part. Open Flash Chart 2 API will not accept the data returned from my_chart_line() unless you declared that the function is used to provide the chart data. To do so, we have to implement hook_ofc_api_data(). Let's add this piece of code to my_chart.module:

<?php
function my_chart_ofc_api_data() {
  return array(
'my_chart_line');
}
?>

As you can guess, the hook simply returns an array containing the name of the functions that will return chart data.

So it's time to see the result of what we have done. Go to /admin/build/modules and enable "My chart" module. Once the module is enabled, your chart data will be available at /ofc_api/data/my_chart_line. You can type in the URL to see if the data is really there.

A simple way to display the chart is to create a Page, Story, or Block, then change input format to PHP code (you will have to enable the PHP filter module) and use this simple code to render the chart:

<?php
 
print ofc_api_render('my_chart_line');
?>

You can create as many charts as you want in my_chart.module. Just do not forget to add the function name in my_chart_ofc_api_data().

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.