Current File : /home/caballoscriollos/public_html/espanol/encuesta/graphs/contents/vbnet_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 VB.NET (ASP.NET) > 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 with ASP.NET to plot charts from data contained in ASP.NET 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 "<a href="VBNET_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 > Code > VBNET > 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.aspx</span> and can be listed as under: <br /></td>
</tr>
<tr>
<td valign="top" class="codeBlock"><p><%@ Page Language="VB" AutoEventWireup="false" CodeFile="SingleSeries.aspx.vb" Inherits="ArrayExample_SingleSeries" %></p>
<p><HTML><br />
<HEAD><br />
<TITLE> <br />
FusionCharts Free - Array Example using Single Series Column 3D Chart <br />
</TITLE><br />
<%<br />
<span class="codeComment">'You need to include the following JS file, if you intend to embed the chart using JavaScript.</span><br />
%> <br />
<strong><SCRIPT LANGUAGE="Javascript" SRC="../FusionCharts/FusionCharts.js"></SCRIPT></strong><br />
</HEAD><br />
<BODY><br />
<strong><asp:Literal ID="FCLiteral" runat="server"></asp:Literal></strong><br />
</BODY><br />
</HTML></p></td>
</tr>
<tr>
<td valign="top" class="text"><p>In the above code, we first include <span class="codeInline">FusionCharts.js</span> file to enable us embed the chart using JavaScript. We have also added an ASP literal server control which acts as a fusion charts container.</p>
<p>Let's see the code of the code behind file <span class="codeInline">SingleSeries.aspx.vb</span>: </p></td>
</tr>
<tr>
<td valign="top" class="text codeBlock"><p>Imports Utilities<br />
Imports InfoSoftGlobal<br />
Partial Class ArrayExample_SingleSeries<br />
Inherits System.Web.UI.Page</p>
<p> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load<br />
<span class="codeComment">' Generate chart in Literal Control</span><br />
<strong>FCLiteral.Text = CreateCharts()</strong><br />
End Sub</p>
<p><br />
Public Function <strong>CreateChart()</strong> As String</p>
<p> <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.</span></p>
<p> <span class="codeComment"> 'Let's store the sales data for 6 products in our array). We also store<br />
'the name of products. </span><br />
Dim arrData(6, 2) As String<br />
<span class="codeComment">'Creating util Object</span><br />
Dim util As New Util()<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 />
<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 />
Dim strXML As String, i As Integer<br />
<span class="codeComment">'Initialize <graph> element</span><br />
strXML = "<graph caption='Sales by Product' numberPrefix='$' formatNumberScale='0' decimalPrecision='0'>"</p>
<p> <span class="codeComment">'Convert data to XML and append</span><br />
For i = 0 To UBound(arrData) - 1<br />
<span class="codeComment">'add values using <set name='...' value='...' color='...'/></span><br />
strXML = strXML & "<set name='" & arrData(i, 1) & "' value='" & arrData(i, 2) & "' color='" & util.getFCColor() & "' />"<br />
Next<br />
<span class="codeComment">'Close <graph> element</span><br />
strXML = strXML & "</graph>"</p>
<p> <span class="codeComment">'Create the chart - Column 3D Chart with data contained in strXML</span><br />
<strong>Return FusionCharts.RenderChart("../FusionCharts/FCF_Column3D.swf", "", strXML, "productSales", "600", "300", False, False)</strong><br />
<br />
End Function<br />
End Class<br />
</p> </td>
</tr>
<tr>
<td valign="top" class="text"><ul>
<li>In the above example, we first include <span class="codeInline">Utilites</span> and <span class="codeInline">InfoSoftGlobal</span> namespace.</li>
<li>Then we define the <span class="codeInline">CreateChart()</span> function. </li>
<li>Inside the function, we declare an 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.</li>
<li>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. </li>
<li>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>. </li>
</ul>
<p class="highlightBlock">We included <span class="codeInline">Utilities</span> namespace, 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 <span class="codeInline">getFCColor()</span> function in cyclic iteration returns these color values.</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_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.aspx</span> with the following code: </td>
</tr>
<tr>
<td valign="top" class="codeBlock"><p><%@ Page Language="VB" AutoEventWireup="false" CodeFile="MultiSeries.aspx.vb" <br />
Inherits="ArrayExample_MultiSeries" %></p>
<p><HTML><br />
<HEAD><br />
<TITLE><br />
FusionCharts Free - Array Example using Multi Series Column 3D Chart<br />
</TITLE><br />
<%<br />
<span class="codeComment">'You need to include the following JS file, if you intend to embed the chart using JavaScript.</span><br />
%> <br />
<strong> <SCRIPT LANGUAGE="Javascript" SRC="../FusionCharts/FusionCharts.js"></SCRIPT></strong><br />
</HEAD><br />
<br />
<BODY><br />
<strong><asp:Literal ID="FCLiteral" runat="server"></asp:Literal></strong><br />
</BODY><br />
</HTML></p></td>
</tr>
<tr>
<td valign="top" class="text"><p>In the above code, we first include <span class="codeInline">FusionCharts.js</span> file to enable us embed the chart using JavaScript. We then call method <span class="codeInline"><strong>CreateChart()</strong></span> from the code behind to generate code for chart.</p>
<p>Take a look at the code behind file <span class="codeInline">MultiSeries.aspx.vb</span>: </p></td>
</tr>
<tr>
<td valign="top" class="text codeBlock"><p>Imports InfoSoftGlobal<br />
Partial Class ArrayExample_MultiSeries<br />
Inherits System.Web.UI.Page</p>
<p> Protected Sub <b>Page_Load</b>(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load<br />
<span class="codeComment"> ' Generate chart in Literal Control</span><br />
<b> FCLiteral.Text = CreateChart()</b><br />
End Sub</p>
<p> </p>
<p> Public Function <strong>CreateChart()</strong> As String</p>
<p> <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.</span></p>
<p> <span class="codeComment"> 'Let's store the sales data for 6 products in our array. We also store<br />
'the name of products. </span><br />
Dim arrData(6, 3) As String<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 />
<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 />
<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 />
Dim strXML As String, strCategories As String, strDataCurr As String, strDataPrev As String, i As Integer</p>
<p> <span class="codeComment"> 'Initialize <graph> element</span><br />
strXML = "<graph caption='Sales by Product' numberPrefix='$' decimalPrecision='0' >"</p>
<p> <span class="codeComment"> 'Initialize <categories> element - necessary to generate a multi-series chart</span><br />
strCategories = "<categories>"</p>
<p> <span class="codeComment"> 'Initiate <dataset> elements</span><br />
strDataCurr = "<dataset seriesName='Current Year' color='AFD8F8'>"<br />
strDataPrev = "<dataset seriesName='Previous Year' color='F6BD0F'>"</p>
<p> <span class="codeComment">'Iterate through the data </span><br />
For i = 0 To UBound(arrData) - 1<br />
<span class="codeComment"> 'Append <category name='...' /> to strCategories</span><br />
strCategories = strCategories & "<category name='" & arrData(i, 1) & "' />"<br />
<span class="codeComment">'Add <set value='...' /> to both the datasets</span><br />
strDataCurr = strDataCurr & "<set value='" & arrData(i, 2) & "' />"<br />
strDataPrev = strDataPrev & "<set value='" & arrData(i, 3) & "' />"<br />
Next</p>
<p> <span class="codeComment">'Close <categories> element</span><br />
strCategories = strCategories & "</categories>"</p>
<p> <span class="codeComment"> 'Close <dataset> elements</span><br />
strDataCurr = strDataCurr & "</dataset>"<br />
strDataPrev = strDataPrev & "</dataset>"</p>
<p> <span class="codeComment">'Assemble the entire XML now</span><br />
strXML = strXML & strCategories & strDataCurr & strDataPrev & "</graph>"</p>
<p> <span class="codeComment">'Create the chart - MS Column 3D Chart with data contained in strXML</span><br />
<strong> Return FusionCharts.RenderChart("../FusionCharts/FCF_MSColumn3D.swf", "", strXML, "productSales", "600", "300", False, False)</strong></p>
<p> End Function<br />
End Class<br />
</p></td>
</tr>
<tr>
<td valign="top" class="text"><ul>
<li>In the above code we import InfoSoftGlobal namespace first.</li>
<li>Then we define the <span class="codeInline">CreateChart ()</span> function. </li>
<li>We declare an 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 for current year and the second one for previous year.</li>
<li>We define a variable <span class="codeInline">strXML</span> to store the entire XML data.</li>
<li>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.</li>
<li>We concatenate the entire XML in <span class="codeInline">strXML</span>. </li>
<li>Finally, we return the chart HTML using RenderChartHTML() method present in InfoSoftGlobal.FusionCharts class. <span class="codeInline">RenderChart()is called form the Page_Load</span><span class="codeInline"> event listener</span>. </li>
</ul></td>
</tr>
<tr>
<td valign="top" class="text"><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 > VBNET > ArrayExample</span>, we've more example codes to create Stacked and Combination Charts too, which we have not 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