KGRKJGETMRETU895U-589TY5MIGM5JGB5SDFESFREWTGR54TY
Server : Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8e-fips-rhel5 DAV/2 PHP/5.2.17
System : Linux localhost 2.6.18-419.el5 #1 SMP Fri Feb 24 22:47:42 UTC 2017 x86_64
User : nobody ( 99)
PHP Version : 5.2.17
Disable Function : NONE
Directory :  /home/queenjbs/www/FusionChart/Contents/Code/J2EE/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/queenjbs/www/FusionChart/Contents/Code/J2EE/JSP_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>Using FusionCharts XT with JSP - Charting Data from
        an Array    </title>
<link rel="stylesheet" href="../../assets/ui/css/style.css" type="text/css" />
<script type="text/javascript" src="../../assets/prettify/prettify.js"></script>
<link rel="stylesheet" type="text/css" href="../../assets/prettify/prettify.css" />
<script type="text/javascript" src="../../assets/ui/js/jquery.min.js" ></script>
<style type="text/css">
<!--
div.WebHelpPopupMenu { position:absolute;
left:0px;
top:0px;
z-index:4;
visibility:hidden; }

a.whtbtnhide, a.whtbtnshow, a.whtbtnhidenav , a.whtbtnshownav { border-bottom:none !important; }
-->
</style>
<script type="text/javascript" language="javascript1.2" src="../../assets/ui/js/whmsg.js"></script>
<script type="text/javascript" language="javascript" src="../../assets/ui/js/whver.js"></script>
<script type="text/javascript" language="javascript1.2" src="../../assets/ui/js/whproxy.js"></script>
<script type="text/javascript" language="javascript1.2" src="../../assets/ui/js/whutils.js"></script>
<script type="text/javascript" language="javascript1.2" src="../../assets/ui/js/whlang.js"></script>
<script type="text/javascript" language="javascript1.2" src="../../assets/ui/js/whtopic.js"></script>
<script type="text/javascript" src="../../assets/ui/js/lib.js"></script>
</head>

<body>
<!-- breadcrumb starts here -->
<div id="breadcrumb"></div>
<script type="text/javascript">
	document.write( addFCBreadcrumb( [ "Home|../../Introduction/Overview.html", "Guide for web developers", "Using with J2EE|JSP_GettingStarted.html", "Charting data from Array" ] ) );
</script>
<!-- breadcrumb ends here -->


<table width="98%" border="0" cellspacing="0" cellpadding="3" align="center">
  <tr>
    <td class="pageHeader">Charting Data from
        an Array</td>
  </tr>
  <tr>
    <td valign="top" class="text">
      <p>In this section, we will show you how to use FusionCharts XT and JSP to plot
        charts from data contained in JSP arrays - the same can then be extended to ArrayList or any other collection.</p>
      <p>We will cover the following examples
            here:</p>
      <ul>
        <li><a href="#single">Creating a single series chart from data contained in arrays</a></li>
        <li><a href="#multi">Creating a multi-series chart from data contained in arrays</a></li>
      </ul>
      <p><strong>Before you go further with this page, we recommend you to please see the previous section, <a href="JSP_BasicExample.html">Basic Examples</a>, 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 &gt; Code &gt; J2EE &gt; ArrayExample</span>
        folder. </p>    </td>
  </tr>
  
  <tr>
    <td valign="top" class="header"><a name="single" id="single"></a>Creating a single series chart from data contained in arrays</td>
  </tr>
  <tr>
    <td valign="top" class="text"><p>The code to create a single series chart is
      contained in <span class="codeInline">SingleSeries.jsp</span> and can be
      listed as under: </p></td>
  </tr>
  <tr>
    <td class="text">        <pre class="prettyprint php code_container">&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %&gt;
&lt;%@ taglib uri=&quot;http://www.fusioncharts.com/jsp/core&quot; prefix=&quot;fc&quot; %&gt;      
&lt;%@ taglib prefix=&quot;tags&quot; tagdir=&quot;/WEB-INF/tags&quot; %&gt;
<strong>&lt;jsp:useBean id=&quot;chartData&quot; 
  class=&quot;com.fusioncharts.sampledata.ArrayRenderData&quot;/&gt;</strong>
&lt;c:set var=&quot;folderPath&quot; value=&quot;../../FusionCharts/&quot;/&gt;
&lt;c:set var=&quot;title&quot; value=&quot;FusionCharts - Array Example 
  using Single Series Column 3D Chart&quot; scope=&quot;request&quot;/&gt;
&lt;c:set var=&quot;header1&quot; value=&quot;FusionCharts - Examples&quot; scope=&quot;request&quot;/&gt;
&lt;c:set var=&quot;header2&quot; value=&quot;Plotting single series 
  chart from data contained in Array.&quot; scope=&quot;request&quot;/&gt;
&lt;c:set var=&quot;jsPath&quot; value=&quot;${folderPath}&quot; scope=&quot;request&quot;/&gt;
&lt;tags:template2&gt; 
	
<span class="codeComment">&lt;!-- In this example, we plot a single series chart from data contained in an array. 
The array will have two columns - first one for data label and the next one for data values.--&gt;
&lt;!-- Create the chart - Column 3D Chart with data contained in bean --&gt;</span>
<strong>&lt;fc:render chartId=&quot;${chartData.chartId}&quot;
  swfFilename=&quot;${folderPath}${chartData.filename}&quot;
  width=&quot;${chartData.width}&quot; height=&quot;${chartData.height}&quot; debugMode=&quot;false&quot;
  registerWithJS=&quot;false&quot; xmlData=&quot;${chartData.xml}&quot; /&gt;</strong>
&lt;/tags:template2&gt;</pre></td>
  </tr>
  <tr>
    <td valign="top" class="text">
      <p>As is the best practice, there is hardly any code in the above jsp. The tag used to render the chart  is <span class="codeInline">fc:render</span>. The data for the chart is obtained from the Java Bean class <span class="codeInline">com.fusioncharts.sampledata.ArrayRenderData</span>.</p>
      <p>As mentioned in the earlier examples, please do not forget to include the path to the folder containing the javascript files. This is achieved through the code:</p>
      <pre class="code_container prettyprint">&lt;c:set var=&quot;folderPath&quot; value=&quot;../../FusionCharts/&quot;/&gt; </pre>
      <p>Here, <span class="codeInline">FusionCharts</span> is the name of the folder containing <span class="codeInline">FusionCharts.js</span>, <span class="codeInline">.swf</span> and other JavaScript files.</p>
      <p>The bean containing the xml data is <span class="codeInline">ArrayRenderData</span>. In this bean, we define a 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">xml</span> to store the entire XML data. To build the XML, we iterate through the array and use string concatenation. </p>
      <p>Here is the code from <span class="codeInline">ArrayRenderData.java</span> relevant to constructing the Single series data XML:</p>
      <pre class="prettyprint php code_container"><span class="codeComment">/* Let's store the sales data for six products in our array. We also store
the name of products.*/</span>

String[][] arrData = new String[6][2];
<span class="codeComment">//Store Name of Products</span>
arrData[0][0] = &quot;Product A&quot;;
arrData[1][0] = &quot;Product B&quot;;
arrData[2][0] = &quot;Product C&quot;;
arrData[3][0] = &quot;Product D&quot;;
arrData[4][0] = &quot;Product E&quot;;
arrData[5][0] = &quot;Product F&quot;;

<span class="codeComment">//Store sales data</span>
arrData[0][1] = &quot;567500&quot;;
arrData[1][1] = &quot;815300&quot;;
arrData[2][1] = &quot;556800&quot;;
arrData[3][1]= &quot;734500&quot;;
arrData[4][1] = &quot;676800&quot;;
arrData[5][1] = &quot;648500&quot;;
int i=0;
<span class="codeComment">//Initialize &lt;chart&gt; element</span>
xml = &quot;&lt;chart caption='Sales by Product' numberPrefix='$' formatNumberScale='0'&gt;&quot;;

<span class="codeComment">//Convert data to XML and append</span>
for(i=0;i&lt;arrData.length;i++){
  xml+= &quot;&lt;set label='&quot; +arrData[i][0] + &quot;' value='&quot; + arrData[i][1] + &quot;' /&gt;&quot;;
}
<span class="codeComment">//Close &lt;chart&gt; element</span>
xml+= &quot;&lt;/chart&gt;&quot;;    </pre> 
<p>Finally, we render the chart using the <span class="codeInline">fc:render</span> tag and passing XML and other parameters.</p>
      <p>When you view the chart, you will see a chart as under: </p>
      <p><img src="../../guide-for-web-developers/Images/Code_ArraySS.jpg" width="584" height="287" class="imageBorder"/></p>      </td>
  </tr>
  <tr>
    <td valign="top" class="header"><a name="multi" id="multi"></a>Creating a multi-series chart from data contained in arrays </td>
  </tr>
  <tr>
    <td valign="top" class="text"><p>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></td>
  </tr>
  <tr>
    <td valign="top" class="text">
    <pre class="prettyprint xml code_container">&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %&gt;
&lt;%@ taglib uri=&quot;http://www.fusioncharts.com/jsp/core&quot; prefix=&quot;fc&quot; %&gt; 
&lt;%@ taglib prefix=&quot;tags&quot; tagdir=&quot;/WEB-INF/tags&quot; %&gt;
<strong>&lt;jsp:useBean id=&quot;chartData&quot; class=&quot;com.fusioncharts.sampledata.ArrayRenderData&quot;/&gt;</strong>
<strong>&lt;c:set var=&quot;folderPath&quot; value=&quot;../../FusionCharts/&quot;/&gt;</strong>
&lt;c:set var=&quot;title&quot; value=&quot;FusionCharts - Array Example using Multi Series Column 3D
Chart&quot; scope=&quot;request&quot;/&gt;
&lt;c:set var=&quot;header1&quot; value=&quot;FusionCharts - Examples&quot; scope=&quot;request&quot;/&gt;
&lt;c:set var=&quot;header2&quot; value=&quot;Plotting multi-series chart 
from data contained in Array.&quot; scope=&quot;request&quot;/&gt;
<strong>&lt;c:set var=&quot;jsPath&quot; value=&quot;${folderPath}&quot; scope=&quot;request&quot;/&gt;</strong>      
&lt;tags:template2&gt;
<span class="codeComment">&lt;!-- In this example, we plot a multi series chart from data contained in an array.
The array will have three columns - first one for data label (product) and the next two for data values.
 The first data value column will store sales information for current year and the second one for previous year.--&gt;

&lt;!-- Create the chart - MSColumn3D Chart with data contained in bean --&gt;</span>

<strong>&lt;fc:render chartId=&quot;${chartData.chartId}&quot; swfFilename=&quot;${folderPath}${chartData.multiSeriesFilename}&quot; 
  width=&quot;${chartData.width}&quot; height=&quot;${chartData.height}&quot; debugMode=&quot;false&quot; 
  registerWithJS=&quot;false&quot; xmlData=&quot;${chartData.multiSeriesXml}&quot; /&gt;</strong>

&lt;/tags:template2&gt;</pre>
<p>In the above code, we first set the value for the path to the JavaScript files<span class="codeInline"> (jsPath)</span> to enable us to embed the chart using JavaScript. We use the  ArrayRenderData bean for providing the data to the chart. We use the fc:render tag wherever we want to render the chart.</p> 
      <p>Let's quickly take a look at the part of the code (<span class="codeInline"> constructMultiSeriesXML()</span> function) from the bean which constructs the XML for the multi-series chart from the array.</p>
      <pre class="prettyprint xml code_container"> 
<span class="codeComment">/* Let's store the sales data for six products in our array. We also store

the name of products.*/</span>

String[][] arrData = new String[6][3];

<span class="codeComment">//Store Name of Products</span>
arrData[0][0] = &quot;Product A&quot;;
arrData[1][0] = &quot;Product B&quot;;
arrData[2][0] = &quot;Product C&quot;;
arrData[3][0] = &quot;Product D&quot;;
arrData[4][0] = &quot;Product E&quot;;
arrData[5][0] = &quot;Product F&quot;;

 <span class="codeComment">//Store sales data</span>
arrData[0][1] = &quot;567500&quot;;
arrData[1][1] = &quot;815300&quot;;
arrData[2][1] = &quot;556800&quot;;
arrData[3][1]= &quot;734500&quot;;
arrData[4][1] = &quot;676800&quot;;
arrData[5][1] = &quot;648500&quot;;

<span class="codeComment">//Store sales data for previous year</span>
arrData[0][2]= &quot;547300&quot;;
arrData[1][2] = &quot;584500&quot;;
arrData[2][2]= &quot;754000&quot;;
arrData[3][2]= &quot;456300&quot;;
arrData[4][2]= &quot;754500&quot;;
arrData[5][2]= &quot;437600&quot;;
multiSeriesXml = &quot;&lt;chart caption='Sales by Product' numberPrefix='$' 
formatNumberScale='1' rotateValues='1' placeValuesInside='1' decimals='0' &gt;&quot;;

<span class="codeComment">//Initialize &lt;categories&gt; element - necessary to generate a multi-series chart</span>
String strCategories = &quot;&lt;categories&gt;&quot;;

<span class="codeComment">//Initiate &lt;dataset&gt; elements</span>
String strDataCurr = &quot;&lt;dataset seriesName='Current Year'&gt;&quot;;
String strDataPrev = &quot;&lt;dataset seriesName='Previous Year'&gt;&quot;;

<span class="codeComment">//Iterate through the data </span>
for(int i=0;i&lt;arrData.length;i++){

  <span class="codeComment">//Append &lt;category label='...' /&gt; to strCategories</span>
  strCategories = strCategories + &quot;&lt;category label='&quot; + arrData[i][0] + &quot;' /&gt;&quot;;

 <span class="codeComment"> //Add &lt;set value='...' /&gt; to both the datasets</span>
  strDataCurr = strDataCurr + &quot;&lt;set value='&quot; + arrData[i][1] + &quot;' /&gt;&quot;;
  strDataPrev = strDataPrev + &quot;&lt;set value='&quot; + arrData[i][2] + &quot;' /&gt;&quot;; 
}

<span class="codeComment">//Close &lt;categories&gt; element</span>
strCategories = strCategories + &quot;&lt;/categories&gt;&quot;;

<span class="codeComment">//Close &lt;dataset&gt; elements</span>
strDataCurr = strDataCurr + &quot;&lt;/dataset&gt;&quot;;
strDataPrev = strDataPrev + &quot;&lt;/dataset&gt;&quot;;

<span class="codeComment">//Assemble the entire XML now</span>
multiSeriesXml+= strCategories + strDataCurr + strDataPrev + &quot;&lt;/chart&gt;&quot;;
	</pre>
      <p>Here, we define an  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 will store sales information for current year and the second one for previous year.</p>
      <p>We define a variable <span class="codeInline">multiSeriesXml</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">multiSeriesXml</span>. </p>
      <p>Finally, we render the chart using the <span class="codeInline">fc:render</span> tag and passing <span class="codeInline">multiSeriesXml </span>from the bean, as an attribute. </p>
      <p>When you view the chart, you will see a chart as under: </p>
	  <p><img src="../../guide-for-web-developers/Images/Code_ArrayMS.jpg"  class="imageBorder"/></p>
	  <p class="highlightBlock">In <span class="codeInline">Download
      Package &gt; Code &gt; J2EE &gt; ArrayExample</span>, we have more example
      codes to create Stacked and Combination Charts too, which have not been
      explained here, 
      as they are similar in concept. You may directly see the
      code and understand them easily.</p> </td>
  </tr>
</table>
<!-- footer links starts-->
<div id="fcfooter"></div>
<script type="text/javascript">
	document.getElementById("fcfooter").innerHTML =  addFCFooter("Basic Examples|JSP_BasicExample.html","Using with data in Forms|JSP_Form.html");
</script>
<!-- footer links ends -->
<script type="text/javascript" language="javascript1.2">//<![CDATA[
<!--

	highlightSearch();

//-->
//]]></script>
</body>
</html>

Anon7 - 2021