Digital Base – Blog » dbFusionChart

dbFusionChartPlugin : Simple Example

Posted by Gijs Nelissen in Programming

This first example will show you howto integrate a FusionChart (free / non-free ) graph in your symfony application, using the dbFusionChartPlugin . I will explain 'example1' included in version 0.0.2 of our plugin.

The Result

Before getting to the code, here is what the final graph/chart will look like

[partial]dbFusionChartExample/example1[/partial]

 

The Partial

The example1 is a partial that looks like this

[code]dbfusionchart-example1-partial[/code]

As you can see the code in this partial is pretty easy. First you tell the symfony application to load the FusionChart helper (using : use_helper). After that you load the graph/chart itself. dbfusionchart_js takes multiple arguments, the most important are the first 2.

  • the first argument is the type of chart (using class constants)
  • second argument is the url to the xml
  • extra arguments tell the dbFusionChart how big the chart should be (width & size).

Very important in the url to the xml is that symfony knows it needs to return valid xml (an no html).

The Action

In the action.class of the dbFusionChartExample module we have the following action :

[code]dbfusionchart-example1-action[/code]


Line 5 – 6
tell symfony to not include the debug bar and send the response as XML (and not html)
On line 8 you will see the declaration of the chart. The classes are pretty smart, but the above example is a very basic example (copy pasted from FusionChart documentation).

When you are ready setting the properties of your chart (we will explain that in further posts), you tell symfony to return the xml (see line : 40).

 

FusionCharts Symfony Plugin: dbFusionChartsPlugin

We're
currently writing an OO class to create FusionCharts
quickly, easily & intuitive, because the existing FusionCharts.php class is not OOP and, honestly, we thought we could do better…
Our package is based on and tested against the FusionCharts Free v2 package. It's written
symfony independent, but we provide a helper, for easy use in symfony.

The symfony plugin, dbFusionChartPlugin, is
in its alpha state now, because it only includes basic single & multi-series chart support. Although the basic code is ready, it needs to be extended, to work with all chart types, but a mediocre programmer should be able to understand its structure and adapt/extend it to his needs. We deeply appreciate all help and suggestions!

Symfony
helper

[code]FC helper ex[/code]

This helper creates the necessary JavaScript.

[code]FC javaScript[/code]

Documentation

You can find the dbFusionChart phpDoc at digitalbase.eu/dbFusionChart/doc/index.html

Logic

As you can see in the FusionCharts Documentation, you need to define your chart's data in an XML file. Our dbFusionChartXML class uses the php DOM extension to encapsulate that XML logic by providing methods to add categories and (datasets with) sets. It has (for now) the descendants dbFC_Single & dbFC_Multi. The latter will make sure there are always as many sets in each dataset as there are categories, when adding a dataset or category.

Stay tuned

We have recently added a check in the addCategory function so it
wouldn't add the same category twice, no matter how many times it's
being called, e.g. when looping Propel objects to init a dbFusionChartXML. It's checks like these that need to be added, among much much more… So stay tuned!