|
FusionCharts can effectively be used with PHP to plot dynamic data-driven charts. With FusionCharts PHP Class chart rendering becomes even easier. Let's see how to make use of FusionCharts PHP Class functions and create charts in few easy steps.
We'll cover the following examples here:
- Creating a simple chart using FusionCharts PHP Class
- Creating a chart with external XML file created using FusionCharts PHP Class
Let's quickly see each of them. Before you proceed with the contents in this page, we strictly recommend you to please go through the section "How FusionCharts works?". |
|
All code discussed here is present in Download Package > Code > PHPClass > BasicExample folder. |
|
In our code, we've used the charts and FusionCharts.js contained in Download Package > Code > FusionCharts folder. We have kept FusionCharts_Gen.php and FusionCharts.php in Download Package > Code > PHPClass > Includes folder. When you run your samples, you need to make sure that all the files are in proper location. |
Let's now get to building our first chart. In this example, we'll create a "Monthly Unit Sales" chart. |
|
<?php
include("../Includes/FusionCharts_Gen.php");
?>
<HTML>
<HEAD>
<TITLE>
FusionCharts Free - Simple Column 3D Chart
</TITLE>
<?php
?>
<SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT>
</HEAD>
<BODY>
<?php
$FC = new FusionCharts("Column3D","600","300");
$FC->setSwfPath("../../FusionCharts/");
$strParam="caption=Monthly Unit Sales;xAxisName=Month;yAxisName=Units;decimalPrecision=0; formatNumberScale=0";
$FC->setChartParams($strParam);
$FC->addChartData("462","name=Jan");
$FC->addChartData("857","name=Feb");
$FC->addChartData("671","name=Mar");
$FC->addChartData("494","name=Apr");
$FC->addChartData("761","name=May");
$FC->addChartData("960","name=Jun");
$FC->addChartData("629","name=Jul");
$FC->addChartData("622","name=Aug");
$FC->addChartData("376","name=Sep");
$FC->addChartData("494","name=Oct");
$FC->addChartData("761","name=Nov");
$FC->addChartData("960","name=Dec");
$FC->renderChart();
?>
</BODY>
</HTML> |
What we did in this program? |
- Included FusionCharts_Gen.php class and FusionCharts.js class
- Created FusionCharts PHP class object $FC for a Column 3D chart with 600 pixels width and 300 pixels height
- Set relative path of chart SWF file using setSwfPath
- Stored delimiter separated chart attributes in a variable $strParam
- Set these chart attributes to our chart using setChartParams
- Added chart values and category names that are to be plotted on the Column 3D chart
- Finally rendered the chart using renderChart
|
Please go through FusionCharts PHP Class API Reference section to know more about the functions used in the above code. |
|
When you now run this page, you'll see a chart like the one below.
If you do not see a chart like the one below, please follow the steps listed in Debugging your Charts section of this documentation. |
|
|
So, you just saw how simple it is to create a chart using FusionCharts PHP Class functions. |
|
Now, we will create the same chart in a different way. Here, we will be using two different programs. One program creates the chart XML using FusionCharts PHP Class and the other uses this XML to render the chart using dataURL method. Code written inside Data.php file creates the XML and SimpleChart.php file uses the XML to render chart. |
|
All code discussed here is present in Download Package > Code > PHPClass > BasicExample folder and Download Package > Code > PHPClass > BasicExample > Data folder. |
|
Let's consider the code in Data.php file first. This code is similar like the code in the above example; the only difference is it does not render the chart but streams XML to SimpleChart.php file. |
<?php
include("../../Includes/FusionCharts_Gen.php");
?>
<?php
$FC = new FusionCharts("column3D","600","300");
$FC->setSwfPath("../../FusionCharts/");
$strParam="caption=Monthly Unit Sales;xAxisName=Month;yAxisName=Units;decimalPrecision=0; formatNumberScale=0;showNames=1";
$FC->setChartParams($strParam);
$FC->addChartData("462","name=Jan");
$FC->addChartData("857","name=Feb");
$FC->addChartData("671","name=Mar");
$FC->addChartData("494","name=Apr");
$FC->addChartData("761","name=May");
$FC->addChartData("960","name=Jun");
$FC->addChartData("629","name=Jul");
$FC->addChartData("622","name=Aug");
$FC->addChartData("376","name=Sep");
$FC->addChartData("494","name=Oct");
$FC->addChartData("761","name=Nov");
$FC->addChartData("960","name=Dec");
$strXML=$FC->getXML();
header('Content-type: text/xml');
print $strXML;
?>
|
|
What we did here: |
- Included FusionCharts_Gen.php class and FusionCharts.js class
- Created FusionCharts PHP class object $FC for a Column 3D chart with 600 pixels width and 300 pixels height
- Set relative path of chart SWF file
- Stored delimiter separated chart attributes in a variable $strParam
- Set these chart attributes to our chart
- Added chart data that are to be plotted on the Column 3D chart
- Finally returned the XML and wrote it to the response stream
|
|
Let's now look at SimpleChart.php function: |
<?php
include("../Includes/FusionCharts.php");
?>
<HTML>
<HEAD>
<TITLE>
FusionCharts Free - Simple Column 3D Chart using dataURL method
</TITLE>
<?php
?>
<SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT>
</HEAD>
<BODY>
<?php
echo renderChart("../../FusionCharts/FCF_Column3D.swf", "Data/Data.php", "", "myFirst", 600, 300, false, false);
?>
</BODY>
</HTML>
|
|
Steps involved in this function: |
- Include FusionCharts.php and FusionCharts.js class
- Call renderChart() function with data from Data.php function.
|
|
Note: The renderChart() function used in this code is not the same with the one we used in the previous example, though they bear same name. This is FusionCharts PHP chart embedding function; please go through Using with PHP > Basic Examples to know more about it. |
|
Here is the output. As you can see, the same chart has been created but in a different way. |
|
|
|