Current File : /home/caballoscriollos/www/espanol/encuesta/graphs/contents/jsp_array.html
<script language="JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
// -->
</script>
<?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 JSP > 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 JSP to plot
charts from data contained in JSP 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 > JSP > ArrayExample</span>
folder. </p>
<p class="header">Creating a single series chart from data contained in arrays</p>
<p class="text">The code to create a single series chart is contained in <span class="codeInline">SingleSeries.jsp</span> and can be listed as under: </p>
<p class="codeBlock"><%@ page import="com.infosoftglobal.fusioncharts.FusionChartsHelper"%><br />
<HTML><br />
<HEAD><br />
<TITLE>FusionCharts Free - Array Example using Single Series Column3D Chart</TITLE><br />
<%<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. Else, you would get JavaScript errors.<br />
*/</span><br />
%><br />
<SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT><br />
<style type="text/css"><br />
<!--<br />
body {<br />
font-family: Arial, Helvetica, sans-serif;<br />
font-size: 12px;<br />
}<br />
--><br />
</style><br />
</HEAD><br />
<BODY><br />
<CENTER><br />
<h2><a href="http://www.fusioncharts.com" target="_blank">FusionCharts Free</a> Examples</h2><br />
<h4>Plotting single series chart from data contained in Array.</h4><br />
<%<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 />
String[][] arrData = new String[6][2];<br />
<span class="codeComment">//Store Name of Products</span><br />
arrData[0][0] = "Product A";<br />
arrData[1][0] = "Product B";<br />
arrData[2][0] = "Product C";<br />
arrData[3][0] = "Product D";<br />
arrData[4][0] = "Product E";<br />
arrData[5][0] = "Product F";<br />
<br />
<span class="codeComment">//Store sales data</span><br />
arrData[0][1] = "567500";<br />
arrData[1][1] = "815300";<br />
arrData[2][1] = "556800";<br />
arrData[3][1]= "734500";<br />
arrData[4][1] = "676800";<br />
arrData[5][1] = "648500";<br />
<br />
<span class="codeComment">// we will use the FusionChartsHelper class for colors</span><br />
FusionChartsHelper colorHelper= new FusionChartsHelper();<br />
<span class="codeComment">//Now, we need to convert this data into XML. We convert using string concatenation.</span><br />
String strXML;<br />
int i=0;<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 />
for(i=0;i<arrData.length;i++){<br />
strXML = strXML + "<set name='" +arrData[i][0] + "' value='" + arrData[i][1] + "' color='" + colorHelper.getFCColor() + "'/>";<br />
}<br />
<span class="codeComment">//Close <graph> element</span><br />
strXML = strXML + "</graph>";<br />
<br />
<span class="codeComment">//Create the chart - Column 3D Chart with data contained in strXML</span><br />
%> <br />
<jsp:include page="../Includes/FusionChartsRenderer.jsp" flush="true"> <br />
<jsp:param name="chartSWF" value="../../FusionCharts/FCF_Column3D.swf" /> <br />
<jsp:param name="strURL" value="" /> <br />
<jsp:param name="strXML" value="<%=strXML %>" /> <br />
<jsp:param name="chartId" value="productSales" /> <br />
<jsp:param name="chartWidth" value="600" /> <br />
<jsp:param name="chartHeight" value="300" /> <br />
<jsp:param name="debugMode" value="false" /> <br />
<jsp:param name="registerWithJS" value="false" /><br />
</jsp:include><br />
<BR><br />
<BR><br />
<a href='../NoChart.html' target="_blank">Unable to see the chart<br />
above?</a><BR><H5 ><a href='../default.htm'>&laquo; Back to list of examples</a></h5><br />
</CENTER><br />
</BODY><br />
</HTML><br />
</p>
<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">FusionChartsRenderer.jsp</span> wherever we want to render the chart. We import FusionChartsHelper class to obtain colors for the columns.</p>
<p>Thereafter, we define an JSP array <span class="codeInline">arrData</span> to store sales data for 6 different products. The array has two dimensions - 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 use string concatenation. Finally, we render the chart by including FusionChartsRenderer.jsp and passing strXML and other parameters.</p>
<p>When you view the chart, you'll see a chart as under: </p> <p class="text"><img src="Images/Code_ArraySS.jpg" width="584" height="287" class="imageBorder" /></p>
<p class="text"><span class="header">Creating a multi-series chart from data contained in arrays </span></p>
<p class="text">Let us now create a multi-series chart from data contained in arrays. We create a file <span class="codeInline">MultiSeries.jsp</span> with the following code: </p>
<p class="codeBlock"><HTML><br />
<HEAD><br />
<TITLE>FusionCharts Free - Array Example using Multi Series Column 3D<br />
Chart</TITLE><br />
<%<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. Else, you would get JavaScript errors.<br />
*/</span><br />
%><br />
<SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT><br />
<style type="text/css"><br />
<!--<br />
body {<br />
font-family: Arial, Helvetica, sans-serif;<br />
font-size: 12px;<br />
}<br />
--><br />
</style><br />
</HEAD><br />
<BODY><br />
<CENTER><br />
<h2><a href="http://www.fusioncharts.com" target="_blank">FusionCharts Free</a> Examples</h2><br />
<h4>Plotting multi-series chart from data contained in Array.</h4><br />
<%<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. <br />
*/</span><br />
String[][] arrData = new String[6][3];<br />
<span class="codeComment">//Store Name of Products</span><br />
arrData[0][0] = "Product A";<br />
arrData[1][0] = "Product B";<br />
arrData[2][0] = "Product C";<br />
arrData[3][0] = "Product D";<br />
arrData[4][0] = "Product E";<br />
arrData[5][0] = "Product F";<br />
<br />
<span class="codeComment">//Store sales data</span><br />
arrData[0][1] = "567500";<br />
arrData[1][1] = "815300";<br />
arrData[2][1] = "556800";<br />
arrData[3][1]= "734500";<br />
arrData[4][1] = "676800";<br />
arrData[5][1] = "648500";<br />
<br />
<span class="codeComment"> //Store sales data for previous year</span><br />
arrData[0][2]= "547300";<br />
arrData[1][2] = "584500";<br />
arrData[2][2]= "754000";<br />
arrData[3][2]= "456300";<br />
arrData[4][2]= "754500";<br />
arrData[5][2]= "437600";<br />
<br />
String strXML = "<graph caption='Sales by Product' numberPrefix='$' formatNumberScale='1' decimalPrecision='0' >";<br />
<br />
<span class="codeComment">//Initialize <categories> element - necessary to generate a multi-series chart</span><br />
String strCategories = "<categories>";<br />
<br />
<span class="codeComment">//Initiate <dataset> elements</span><br />
String strDataCurr = "<dataset seriesName='Current Year' color='AFD8F8'>";<br />
String strDataPrev = "<dataset seriesName='Previous Year' color='F6BD0F'>";<br />
<br />
<span class="codeComment">//Iterate through the data </span><br />
for(int i=0;i<arrData.length;i++){<br />
<span class="codeComment">//Append <category name='...' /> to strCategories</span><br />
strCategories = strCategories + "<category name='" + arrData[i][0] + "' />";<br />
<span class="codeComment">//Add <set value='...' /> to both the datasets</span><br />
strDataCurr = strDataCurr + "<set value='" + arrData[i][1] + "' />";<br />
strDataPrev = strDataPrev + "<set value='" + arrData[i][2] + "' />"; <br />
}<br />
<br />
<span class="codeComment">//Close <categories> element</span><br />
strCategories = strCategories + "</categories>";<br />
<br />
<span class="codeComment">//Close <dataset> elements</span><br />
strDataCurr = strDataCurr + "</dataset>";<br />
strDataPrev = strDataPrev + "</dataset>";<br />
<br />
<span class="codeComment">//Assemble the entire XML now</span><br />
strXML = strXML + strCategories + strDataCurr + strDataPrev + "</graph>";<br />
<br />
<span class="codeComment">//Create the chart - MS Column 3D Chart with data contained in strXML</span><br />
%><br />
<jsp:include page="../Includes/FusionChartsRenderer.jsp" flush="true"> <br />
<jsp:param name="chartSWF" value="../../FusionCharts/FCF_MSColumn3D.swf" /> <br />
<jsp:param name="strURL" value="" /> <br />
<jsp:param name="strXML" value="<%=strXML %>" /> <br />
<jsp:param name="chartId" value="productSales" /> <br />
<jsp:param name="chartWidth" value="600" /> <br />
<jsp:param name="chartHeight" value="300" /><br />
<jsp:param name="debugMode" value="false" /> <br />
<jsp:param name="registerWithJS" value="false" /><br />
</jsp:include><br />
<BR><br />
<BR><br />
<a href='../NoChart.html' target="_blank">Unable to see the chart<br />
above?</a><BR><H5 ><a href='../default.htm'>&laquo; Back to list of examples</a></h5><br />
</CENTER><br />
</BODY><br />
</HTML></p>
<p>In the above code, we first include <span class="codeInline">FusionCharts.js</span> file to enable us embed the chart using JavaScript. We also include <span class="codeInline">FusionChartsRenderer.jsp</span> wherever we want to render the chart. </p>
<p>Thereafter, we define a JSP array <span class="codeInline">arrData</span> to store sales data for 6 different products. The array has three dimensions - first one for data label (product) and the next two for data values. The first data value column would store sales information 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 use 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">FusionChartsRenderer.jsp </span>and passing <span class="codeInline">strXML</span> as <span class="text">parameter</span>. </p>
<p>When you view the chart, you'll see a chart as under: </p> <p class="text"><img src="Images/Code_ArrayMS.jpg" alt="" width="584" height="287" class="imageBorder" /></p>
<p class="highlightBlock">In <span class="codeInline">Download
Package > Code > JSP > 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 and understand them easily.</p>
</td>
</tr>
</table>
</body>
</html>
Mr. DellatioNx196 GaLers xh3LL Backd00r 1.0, Coded By Mr. DellatioNx196 - Bogor BlackHat