Current File : /home/caballoscriollos/www/espanol/encuesta/graphs/contents/php_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 with PHP > Charting Data from an Array </h2></td>
</tr>
<tr>
<td valign="top" class="text"><p>In this section, we'll show you how to use FusionCharts and PHP 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 arrays</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 > PHP > 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.php, which contains functions<br />
//to help us easily embed the charts.</span><br />
include("../Includes/FusionCharts.php");<br />
<span class="codeComment">//a file having a list of colors to be applied to each column (using getFCColor() function</span>)<br />
include("../Includes/FC_Colors.php");<br /> ?><br /><BR />
<HTML><br />
<HEAD><br />
<TITLE> FusionCharts Free - Array Example using Single Series Column 3D Chart</TITLE><br />
<SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT><br />
</HEAD><br />
<BODY><br />
<?php<br />
<span class="codeComment"> //In this example, we plot a single series chart from data contained<br />
//in an array. The array will have two columns - first one for data label<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. </span><br />
<span class="codeComment"> //Store Name of Products</span><br />
$arrData[0][1] = "Product A";<br />
$arrData[1][1] = "Product B";<br />
$arrData[2][1] = "Product C";<br />
$arrData[3][1] = "Product D";<br />
$arrData[4][1] = "Product E";<br />
$arrData[5][1] = "Product F";<br />
<br />
<span class="codeComment"> //Store sales data</span><br />
$arrData[0][2] = 567500;<br />
$arrData[1][2] = 815300;<br />
$arrData[2][2] = 556800;<br />
$arrData[3][2] = 734500;<br />
$arrData[4][2] = 676800;<br />
$arrData[5][2] = 648500;</p>
<p> <span class="codeComment"> //Now, we need to convert this data into XML. We convert using string concatenation.</span><br />
<span class="codeComment"> //Initialize <graph> element</span><br />
$strXML = "<graph caption='Sales by Product' numberPrefix='$' formatNumberScale='0' decimalPrecision='0' >";<br />
<span class="codeComment"> //Convert data to XML and append</span><br />
foreach ($arrData as $arSubData)<br />
$strXML .= "<set name='" . $arSubData[1] . "' value='" . $arSubData[2] . "' colo='" .getFCColor() . "/>";<br />
<span class="codeComment"> 'Close <graph> element</span><br />
$strXML .= "</graph>";<br />
<br />
<span class="codeComment"> //Create the chart - Column 3D Chart with data contained in strXML</span><br />
echo renderChart("../../FusionCharts/FCF_Column3D.swf", "", $strXML, "productSales", 600, 300);<br />
?><br />
</BODY><br />
</HTML></p></td>
</tr>
<tr>
<td valign="top" class="text"><p>In the above example, we first include <span class="codeInline">FusionCharts.js</span> file to enable us embed the chart using JavaScript. We also include <span class="codeInline">FusionCharts.php</span> to help us easily embed the charts.</p>
<p>Thereafter, we define a PHP array <span class="codeInline">arrData</span> to store sales data for 6 different products. The array has two columns - first one for data label and the next one for data values. </p>
<p>We define a variable <span class="codeInline">strXML</span> to store the entire XML data. To build the XML, we iterate through the array and using string concatenation. Finally, we render the chart using <span class="codeInline">renderChart()</span> function and pass <span class="codeInline">strXML</span> as <span class="codeInline">dataXML</span>. </p>
We include FC_Colors.php, which contains the default color set for the FusionCharts. These colors in Hex code are optimized to give a snazzy look to the charts. The getFCColor() function in cyclic iteration returns these color values.
<p>When you view the chart, you'll see a chart as under: </p></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.php, which contains functions<br />
//to help us easily embed the charts.</span><br />
include("../Includes/FusionCharts.php");<br />
?><br />
<HTML><br />
<HEAD><br />
<TITLE> FusionCharts Free - Array Example using Multi Series Column 3D Chart </TITLE><br />
<SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT><br />
</HEAD><br />
<BODY></p>
<p> <?php<br />
<span class="codeComment"> //In this example, we plot a multi series chart from data contained<br />
//in an array. The array will have three columns - first one for data label (product)<br />
//and the next two for data values. The first data value column would store sales information<br />
//for current year and the second one for previous year.<br />
<br />
//Let's store the sales data for 6 products in our array. We also store<br />
//the name of products. </span><br />
<span class="codeComment"> //Store Name of Products</span><br />
$arrData[0][1] = "Product A";<br />
$arrData[1][1] = "Product B";<br />
$arrData[2][1] = "Product C";<br />
$arrData[3][1] = "Product D";<br />
$arrData[4][1] = "Product E";<br />
$arrData[5][1] = "Product F";<br />
<br />
<span class="codeComment"> //Store sales data for current year</span><br />
$arrData[0][2] = 567500;<br />
$arrData[1][2] = 815300;<br />
$arrData[2][2] = 556800;<br />
$arrData[3][2] = 734500;<br />
$arrData[4][2] = 676800;<br />
$arrData[5][2] = 648500;<br />
<br />
<span class="codeComment"> 'Store sales data for previous year</span><br />
$arrData[0][3] = 547300;<br />
$arrData[1][3] = 584500;<br />
$arrData[2][3] = 754000;<br />
$arrData[3][3] = 456300;<br />
$arrData[4][3] = 754500;<br />
$arrData[5][3] = 437600;</p>
<p> <span class="codeComment"> //Now, we need to convert this data into multi-series XML. <br />
//We convert using string concatenation.<br />
// $strXML - Stores the entire XML<br />
// $strCategories - Stores XML for the <categories> and child <category> elements<br />
// $strDataCurr - Stores XML for current year's sales<br />
// $strDataPrev - Stores XML for previous year's sales</span><br />
<br />
<span class="codeComment"> //Initialize <graph> element</span><br />
$strXML = "<graph caption='Sales by Product' numberPrefix='$' decimalPrecision='0' >";<br />
<br />
<span class="codeComment"> //Initialize <categories> element - necessary to generate a multi-series chart</span><br />
$strCategories = "<categories>";<br />
<br />
<span class="codeComment"> 'Initiate <dataset> elements</span><br />
$strDataCurr = "<dataset seriesName='Current Year' color='AFD8F8' >";<br />
$strDataPrev = "<dataset seriesName='Previous Year' color='F6BD0F' >";<br />
<br />
<span class="codeComment"> //Iterate through the data </span><br />
foreach ($arrData as $arSubData) {<br />
<span class="codeComment"> //Append <category name='...' /> to strCategories</span><br />
$strCategories .= "<category name='" . $arSubData[1] . "' />";<br />
<span class="codeComment"> //Add <set value='...' /> to both the datasets</span><br />
$strDataCurr .= "<set value='" . $arSubData[2] . "' />";<br />
$strDataPrev .= "<set value='" . $arSubData[3] . "' />";<br />
}<br />
<br />
<span class="codeComment"> //Close <categories> element</span><br />
$strCategories .= "</categories>";<br />
<br />
<span class="codeComment"> //Close <dataset> elements</span><br />
$strDataCurr .= "</dataset>";<br />
$strDataPrev .= "</dataset>";<br />
<br />
<span class="codeComment"> //Assemble the entire XML now</span><br />
$strXML .= $strCategories . $strDataCurr . $strDataPrev . "</graph>";<br />
<br />
<span class="codeComment"> //Create the chart - MS Column 3D Chart with data contained in strXML</span><br />
echo renderChart("../../FusionCharts/MSColumn3D.swf", "", $strXML, "productSales", 600, 300);<br />
?><br />
</HTML></p></td>
</tr>
<tr>
<td valign="top" class="text"><p>In the above example, we first include <span class="codeInline">FusionCharts.js</span> file to enable us embed the chart using JavaScript. We also include <span class="codeInline">FusionCharts.php</span> to help us easily embed the charts.</p>
<p>Thereafter, we define a PHP array <span class="codeInline">arrData</span> to store sales data for 6 different products. The array has three columns - first one for data label (product) and the next two for data values. The first data value column would store sales information<br />
for current year and the second one for previous year.</p>
<p>We define a variable <span class="codeInline">strXML</span> to store the entire XML data. We also define <span class="codeInline">strCategories</span>, <span class="codeInline">strDataCurr</span> and <span class="codeInline">strDataPrev</span> variables to store XML data for categories elements, current year's dataset and previous year's dataset respectively. To build the XML, we iterate through the array and using string concatenation. We concatenate the entire XML finally in <span class="codeInline">strXML</span>. </p>
<p>Finally, we render the chart using <span class="codeInline">renderChart()</span> function and pass <span class="codeInline">strXML</span> as <span class="codeInline">dataXML</span>. </p>
<p>When you view the chart, you'll see a chart as under: </p></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 > PHP > 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