AmCharts-PHP provides a new more convenient way to configure and integrate AmCharts in your website.


Sample Chart with AmChartsA few weeks ago I had the pleasure to integrate the very nice AmCharts into one of our products. After a few tests in general we decided to build a PHP5 library around the AmCharts Flash objects to create and configure charts in a more convenient way.

Usual integration

You usually configure AmCharts using one or more XML files (one for the settings and one for the data in most cases). This is great if you have static data and constant settings. We use AmCharts to create charts with data retrieved from a database. We could now use a PHP script to create the XML file AmCharts use. Our settings also vary from chart to chart. So we also need a settings file for each different chart. IMHO that's too much code for a few charts, so we had to find another way to create them.

The solution

AmCharts offers the possibility to pass all settings and data via flash variables instead of external XML files. And that's what we use in our Library. All parameters (chart type, settings, data) are passed via flash variables. That generates a lot of inline HTML code but eliminates a lot of files and headache when managing the source.

Because writing the XML strings for settings and data is not as easy as it should be we had to create a few classes to generate the ready-to-use HTML code directly in PHP.

An example

This example shows how to create a simple pie chart with two slices once in the plain old style and once using AmCharts-PHP.

Using XML files

settings.xml

<settings>   
  <pie>     
    <angle>45</angle>     
    <height>10</height>   
  </pie> 
</settings>

data.xml

<pie>
  <slice title="Democrats (Obama)" color="#0000FF" pull_out="true">365</slice>
  <slice title="Republicans (McCain)" color="#FF0000">173</slice>
</pie>

Website integration

<script type="text/javascript" src="amline/swfobject.js"></script>
<div id="flashcontent">
  <strong>You need to upgrade your Flash Player
</div>
<script type="text/javascript">
var so = new SWFObject("amline/ampie.swf", "ampie", "520", "400", "8", "#FFFFFF");
so.addVariable("path", "ampie/");
so.addVariable("settings_file", escape("settings.xml"));
so.addVariable("data_file", escape("data.xml"));
so.write("flashcontent");
</script>

Using AmCharts-PHP

$chart = new AmPieChart("election2008");
$chart->setConfig("pie.angle", 45);
$chart->setConfig("pie.height", 10);
$chart->addSlice("democrats", "Democrats (Barack Obama)", 365,
  array("color" => "#0000FF", "pull_out" => "true"));
$chart->addSlice("republicans", "Republicans (John McCain)", 173, 
  array("color" => "#FF0000"));
echo $chart->getCode();

As we see, it's much more comfortable to use and much more flexible.

Licencing

AmCharts-PHP is published under the GNU Lesser General Public Licence.

Download

You can download AmCharts-PHP from Sourceforge.


comments powered by Disqus