Current File : /home/caballoscriollos/www/espanol/encuesta/graphs/contents/phpclass_array.html
<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>FusionCharts Free Documentation</title>
<link rel="stylesheet" href="Style.css" type="text/css" />
</head>
<body>
<table width="98%" border="0" cellspacing="0" cellpadding="3" align="center">
<tr>
<td><h2 class="pageHeader">Using FusionCharts PHP Class > Charting Data from Array </h2></td>
</tr>
<tr>
<td valign="top" class="text"><p>In this section, we'll show you how to use FusionCharts PHP class functions to plot charts from data contained in PHP arrays. We'll cover the following examples here:</p>
<ul>
<li>Creating a single series chart from data contained in an array</li>
<li>Creating a multi-series chart from data contained in arrays </li>
</ul>
<p><strong>Before you go further with this page, we recommend you to please see the previous section "Basic Examples" as we start off from concepts explained in that page. </strong></p>
<p class="highlightBlock">The code examples contained in this page are present in <span class="codeInline">Download Package > Code > PHPClass > ArrayExample</span> folder. </p></td>
</tr>
<tr>
<td valign="top" class="text"> </td>
</tr>
<tr>
<td valign="top" class="header">Creating a single series chart from data contained in arrays</td>
</tr>
<tr>
<td valign="top" class="text">The code to create a single series chart is contained in <span class="codeInline">SingleSeries.php</span> and can be listed as under: </td>
</tr>
<tr>
<td valign="top" class="codeBlock">
<p><?php<br />
<span class="codeComment"> //We've included ../Includes/FusionCharts_Gen.php, which contains FusionCharts PHP Class<br />
//to help us easily embed the charts.</span><br />
<span class="codeComment"> </span>include("../Includes/FusionCharts_Gen.php");<br />
?><br />
<HTML><br />
<HEAD><br />
<br />
<span class="codeComment"> </span><TITLE><br />
<span class="codeComment"> </span><span class="codeComment"> </span>FusionCharts Free - Array Example using Single Series Column 3D Chart<br />
<span class="codeComment"> </span></TITLE><br />
<br />
<?php<br />
<span class="codeComment"> //You need to include the following JS file, if you intend to embed the chart using JavaScript.<br />
//Embedding using JavaScripts avoids the "Click to Activate..." issue in Internet Explorer<br />
//When you make your own charts, make sure that the path to this JS file is correct. <br />
//Else, you would get JavaScript errors.</span><br />
?> <br />
<span class="codeComment"> </span><SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT><br />
<br />
</HEAD><br />
<br />
<BODY><br />
<CENTER><br />
<br />
<?php<br />
<span class="codeComment"> //In this example, using FusionCharts PHP Class we plot a single series chart <br />
//from data contained in an array. <br />
<br />
//The array needs to have two columns - first one for data labels/names<br />
//and the next one for data values.<br />
<br />
//Let's store the sales data for 6 products in our array. We also store<br />
//the name of products.<br />
<br />
<strong>//Store Name of Products in the first column of the array </strong></span><br />
<span class="codeComment"> </span>$arrData[0][0] = "Product A";<br />
<span class="codeComment"> </span>$arrData[1][0] = "Product B";<br />
<span class="codeComment"> </span>$arrData[2][0] = "Product C";<br />
<span class="codeComment"> </span>$arrData[3][0] = "Product D";<br />
<span class="codeComment"> </span>$arrData[4][0] = "Product E";<br />
<span class="codeComment"> </span>$arrData[5][0] = "Product F";<br />
<span class="codeComment"> <br />
<strong>//Store sales data in the second column of the array </strong></span><br />
<span class="codeComment"> </span>$arrData[0][1] = 567500;<br />
<span class="codeComment"> </span>$arrData[1][1] = 815300;<br />
<span class="codeComment"> </span>$arrData[2][1] = 556800;<br />
<span class="codeComment"> </span>$arrData[3][1] = 734500;<br />
<span class="codeComment"> </span>$arrData[4][1] = 676800;<br />
<span class="codeComment"> </span>$arrData[5][1] = 648500;</p>
<p> <span class="codeComment"> # Create FusionCharts PHP Class object for single series column3d chart</span><br />
<span class="codeComment"> </span>$FC = new FusionCharts("Column3D","600","300"); </p>
<p> <span class="codeComment"> # Set Relative Path of chart swf file.</span><br />
<span class="codeComment"> </span>$FC->setSwfPath("../../FusionCharts/");<br />
<br />
<span class="codeComment"> # Store chart attributes in a variable</span><br />
<span class="codeComment"> </span>$strParam="caption=Sales by Product;numberPrefix=$;formatNumberScale=0;decimalPrecision=0";</p>
<p> <span class="codeComment"> # Set chart attributes</span><br />
<span class="codeComment"> </span>$FC->setChartParams($strParam);<br />
<br />
<br />
<span class="codeComment"><strong> # Call FusionCharts PHP Class Function to add data from the array </strong></span><strong><br />
<span class="codeComment"> </span>$FC->addChartDataFromArray($arrData);</strong><br />
<br />
<span class="codeComment"> # Render the chart</span><br />
<span class="codeComment"> </span>$FC->renderChart();<br />
?><br />
<br />
</CENTER><br />
</BODY><br />
</HTML></p> </td>
</tr>
<tr>
<td valign="top" class="text" style="line-height:20px;">In the above example:
<ul>
<li>We have included <span class="codeInline">FusionCharts_Gen.php</span> and <span class="codeInline">FusionCharts.js</span>.</li>
<li>Stored the category names/labels in the first column of the 2-dimensional array <span class="codeInline">$arrData[]</span> </li>
<li>Then we stored respective data values in the second column of the 2-dimensional array <span class="codeInline">$arrData[]</span> </li>
<li>Created an instance of the FusionCharts PHP class for Column 3D chart with 600 pixels width, 300 pixels height.</li>
<li>Set relative path of chart SWF file</li>
<li>Rendered chart using <span class="codeInline">renderChart()</span> function </li></ul> </td>
</tr>
<tr>
<td valign="top" class="highlightBlock">Please go through <a href="PHPClassAPI/Functions.html">FusionCharts PHP Class API Reference</a> section to know more about the functions used in the above code. </td>
</tr>
<tr>
<td valign="top" class="text"> </td>
</tr>
<tr>
<td valign="top" class="text">When you view the chart, you'll see a chart as under: </td>
</tr>
<tr>
<td valign="top" class="text"><img src="Images/Code_ArraySS.jpg" /></td>
</tr>
<tr>
<td valign="top" class="text"> </td>
</tr>
<tr>
<td valign="top" class="header">Creating a multi-series chart from data contained in arrays </td>
</tr>
<tr>
<td valign="top" class="text">Let us now create a multi-series chart from data contained in arrays. We create a file <span class="codeInline">MultiSeries.php</span> with the following code: </td>
</tr>
<tr>
<td valign="top" class="codeBlock">
<p><?php<br />
<span class="codeComment"> //We've included ../Includes/FusionCharts_Gen.php, which contains FusionCharts PHP Class<br />
//to help us easily embed the charts.</span><br />
<span class="codeComment"> </span>include("../Includes/FusionCharts_Gen.php");<br />
?><br />
<br />
<HTML><br />
<HEAD><br />
<span class="codeComment"> </span><TITLE><br />
<span class="codeComment"> </span><span class="codeComment"> </span>FusionCharts Free - Array Example using Multi Series Column 3D Chart<br />
<span class="codeComment"> </span></TITLE><br />
<br />
<?php<br />
<span class="codeComment"> //You need to include the following JS file, if you intend to embed the chart using JavaScript.<br />
//Embedding using JavaScripts avoids the "Click to Activate..." issue in Internet Explorer<br />
//When you make your own charts, make sure that the path to this JS file is correct.<br />
//Else, you would get JavaScript errors.</span><br />
?> <br />
<br />
<span class="codeComment"> </span><SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT><br />
</HEAD><br />
<br />
<BODY><br />
<CENTER><br />
<br />
<?php</p>
<p> <br />
<span class="codeComment"> //In this example, using FusionCharts PHP Class <br />
//we plot a mulitseries chart from data contained in arrays<br />
<br />
/* The arrays need to be of the following structure :<br />
<br />
1. Array to store Category Names :<br />
<br />
A single dimensional array storing the category names<br />
<br />
2. A 2 Dimensional Array to store data values </span></p>
<p><span class="codeComment"> ** Each row will store data for 1 dataset<br />
<br />
Column 1 will store : Dataset Series Name.<br />
Column 2 will store : Dataset attributes <br />
(as list separated by delimiter.)<br />
Column 3 and rest will store : values of the dataset<br />
<br />
*/<br />
//Let's store the sales data for 6 products in our array. We also store the name of products. <br />
<br />
<strong>//Store Name of Products</strong></span><br />
<span class="codeComment"> </span>$arrCatNames[0] = "Product A";<br />
<span class="codeComment"> </span>$arrCatNames[1] = "Product B";<br />
<span class="codeComment"> </span>$arrCatNames[2] = "Product C";<br />
<span class="codeComment"> </span>$arrCatNames[3] = "Product D";<br />
<span class="codeComment"> </span>$arrCatNames[4] = "Product E";<br />
<span class="codeComment"> </span>$arrCatNames[5] = "Product F";<br />
<span class="codeComment"> <strong>//Store sales data for current year</strong></span><br />
<span class="codeComment"> </span>$arrData[0][0] = "Current Year";<br />
<span class="codeComment"> </span>$arrData[0][1] = "";<span class="codeComment"> // Dataset Parameters</span><br />
<span class="codeComment"> </span>$arrData[0][2] = 567500;<br />
<span class="codeComment"> </span>$arrData[0][3] = 815300;<br />
<span class="codeComment"> </span>$arrData[0][4] = 556800;<br />
<span class="codeComment"> </span>$arrData[0][5] = 734500;<br />
<span class="codeComment"> </span>$arrData[0][6] = 676800;<br />
<span class="codeComment"> </span>$arrData[0][7] = 648500;<br />
<span class="codeComment"> <strong>//Store sales data for previous year</strong></span><br />
<span class="codeComment"> </span>$arrData[1][0] = "Previous Year";<br />
<span class="codeComment"> </span>$arrData[1][1] = ""; <span class="codeComment">// Dataset Parameter</span><br />
<span class="codeComment"> </span>$arrData[1][2] = 547300;<br />
<span class="codeComment"> </span>$arrData[1][3] = 584500;<br />
<span class="codeComment"> </span>$arrData[1][4] = 754000;<br />
<span class="codeComment"> </span>$arrData[1][5] = 456300;<br />
<span class="codeComment"> </span>$arrData[1][6] = 754500;<br />
<span class="codeComment"> </span>$arrData[1][7] = 437600;</p>
<p> <span class="codeComment"> # Create FusionCharts PHP Class object for multiseies column3d chart</span><br />
<span class="codeComment"> </span>$FC = new FusionCharts("MSColumn3D","600","300"); </p>
<p> <span class="codeComment"> # Set Relative Path of chart swf file.</span><br />
<span class="codeComment"> </span>$FC->setSwfPath("../../FusionCharts/");<br />
<br />
<span class="codeComment"> # Store chart attributes in a variable</span><br />
<span class="codeComment"> </span>$strParam="caption=Sales by Product;numberPrefix=$;formatNumberScale=1;rotateValues=1;decimalPrecision=0";</p>
<p> <span class="codeComment"> # Set chart attributes</span><br />
<span class="codeComment"> </span>$FC->setChartParams($strParam);<br />
<br />
<span class="codeComment"> <strong># Pass the 2 arrays storing data and category names to <br />
# FusionCharts PHP Class function addChartDataFromArray</strong></span><strong><br />
$FC->addChartDataFromArray($arrData, $arrCatNames);</strong><br />
<br />
<span class="codeComment"> # Render the Chart </span><br />
<span class="codeComment"> </span>$FC->renderChart();</p>
<p>?><br />
<br />
</CENTER><br />
</BODY><br />
</HTML></p> </td>
</tr>
<tr>
<td valign="top" class="text" style="line-height:20px;">Let's analyze what we have done in the code above:
<ul>
<li>We included <span class="codeInline">FusionCharts_Gen.php</span> class and <span class="codeInline">FusionCharts.js</span> classes</li>
<li>We cteated 2 arrays - one single dimensional aray and one 2-dimensional array. The single dimensional array <span class="codeInline">$arrCatNames</span> stores category names. The 2-dimensional array <span class="codeInline">$arrData</span> stores dataset name, dataset parameters and values in its each row.<br />
<strong>Note:</strong> dataset parameters separated by delimiters should be stored as string. It is optional for multi series charts, but compulsory for combination charts. </li>
<li>We created FusionCharts PHP class object for Multi-series Column 3D chart with 600 pixels width, 300 pixels height </li>
<li>We set relative path of chart SWF file using <span class="codeInline">setSwfPath()</span> function</li>
<li>We stored delimiter separated chart attributes in <span class="codeInline">$strParam</span> variable and paseed this variable through <span class="codeInline"><br />
setChartParams()</span> function to set chart attributes. </li>
<li>Next, we called <span class="codeInline">addChartDataFromArray()</span> function and passed <span class="codeInline">$arrData</span> and <span class="codeInline">$arrCatNames</span> arrays through it. The arrays should be passed in this order only, i.e., <span class="codeInline">$arrData</span> is to be the first parameter and <span class="codeInline">$arrCatNames</span> the second.</li>
<li>Finally we rendered the chart using <span class="codeInline">renderChart()</span> function</li>
</ul>
</td>
</tr>
<tr>
<td valign="top" class="highlightBlock">Please go through <a href="PHPClassAPI/Functions.html">FusionCharts PHP Class API Reference</a> section to know more about the functions used in the above code. </td>
</tr>
<tr>
<td valign="top" class="text"> </td>
</tr>
<tr>
<td valign="top" class="text">When you view the chart, you'll see a chart as under: </td>
</tr>
<tr>
<td valign="top" class="text"><img src="Images/Code_ArrayMS.jpg" alt="" /></td>
</tr>
<tr>
<td valign="top" class="highlightBlock">In <span class="codeInline">Download Package > Code > PHPClass > ArrayExample</span>, we've more example codes to create Stacked and Combination Charts too, which have not been explained here, as they're similar in concept. You can directly see the code if you want to. </td>
</tr>
</table>
</body>
</html>
Mr. DellatioNx196 GaLers xh3LL Backd00r 1.0, Coded By Mr. DellatioNx196 - Bogor BlackHat