Current File : /home/caballoscriollos/www/espanol/encuesta/graphs/contents/vbnet_drill.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) > Creating Drill-down charts </h2></td>
</tr>
<tr>
<td valign="top" class="text"><p>In our previous example, we had used FusionCharts to plot a chart using data stored in database. We'll now extend that example itself to create a drill-down chart which can show more information.</p>
<p><strong>Before you go further with this page, we recommend you to please see the previous sections like "<a href="VBNET_DB.html">Plotting from Database Example</a>", as we start off from concepts explained in those pages. </strong></p>
<p>If you recall from <a href="VBNET_DB.html">previous example</a>, we were showing the sum of factory output in a pie chart as under: </p></td>
</tr>
<tr>
<td valign="top" class="text"><img src="Images/Code_DBOut.jpg" class="imageBorder" /></td>
</tr>
<tr>
<td valign="top" class="text">In this example, we'll extend this example, so that when a user clicks on a pie slice for a factory, he can drill down to see date wise production for that factory. </td>
</tr>
<tr>
<td valign="top" class="text"> </td>
</tr>
<tr>
<td valign="top" class="header">Setting up the pie chart for Link </td>
</tr>
<tr>
<td valign="top" class="text">To set up the pie chart to enable links for drill-down involves just minor tweaking of our previous <span class="codeInline">BasicDBExample.aspx</span>. We basically need to add the <span class="codeInline">link</span> attribute for each<span class="codeInline"> <set></span> element. We create a new page <span class="codeInline">Default.aspx</span> (in<span class="codeInline"> DB_DrillDown</span> folder) from the previous page with the following code changes:
<p class="highlightBlock">The code examples contained in this page are contained in<span class="codeInline"> Download Package > Code > VBNET > DB_DrillDown</span> folder. </p></td>
</tr>
<tr>
<td valign="top" class="codeBlock"><p><%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" <br />
Inherits="DB_DrillDown_Default" %></p>
<p><HTML><br />
<HEAD><br />
<TITLE> FusionCharts Free - Database and Drill-Down Example </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 />
<br />
</HTML></p> </td>
</tr>
<tr>
<td valign="top" class="text">Here, we have included <span class="codeInline">FusionCharts.js</span> file to enable us embed the chart using JavaScript. We are also adding an ASP control literal which acts as the container for the charts. The <span class="codeInline"><strong>CreateCharts()</strong></span> function does the generation, and is the code behind the file <span class="codeInline">Default.aspx.vb</span>. Here is the content of the <span class="codeInline">Default.aspx.vb</span>: </td>
</tr>
<tr>
<td valign="top" class="codeBlock"><p>Imports DataConnection<br />
Imports InfoSoftGlobal<br />
</p>
<p>Partial Class DB_DrillDown_Default<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 = CreateCharts()</b><br />
End Sub<br />
<br />
Public Function CreateChart() As String<br />
<span class="codeComment"> 'In this example, we show how to connect FusionCharts to a database.<br />
'For the sake of ease, we've used an Access database which is present in<br />
'../App_Data/FactoryDB.mdb. It just contains two tables, which are linked to each<br />
'other. </span></p>
<p> <span class="codeComment"> 'Database Objects - Initialization</span><br />
Dim oRs As DbConn, strQuery As String<br />
<span class="codeComment"> 'strXML will be used to store the entire XML document generated</span><br />
Dim strXML As String</p>
<p> <span class="codeComment">'Generate the graph element</span><br />
strXML = "<graph caption='Factory Output report' subCaption='By Quantity' decimalPrecision='0' showNames='1' numberSuffix=' Units' pieSliceDepth='30' formatNumberScale='0' >"</p>
<p> <span class="codeComment"> ' SQL Query</span><br />
strQuery = "select a.FactoryId,a.FactoryName, sum(b.Quantity) as TotOutput from factory_master a,factory_output b where a.FactoryId=b.FactoryId group by a.FactoryId,a.FactoryName"</p>
<p> <span class="codeComment"> ' Open data reader</span><br />
oRs = New DbConn(strQuery)</p>
<p> <span class="codeComment"> 'Iterate through each factory</span><br />
While oRs.ReadData.Read()<br />
<br />
<span class="codeComment"> 'Generate <set name='..' value='..' link='..' /><br />
'Note that we're setting link as Detailed.asp?FactoryId=<<FactoryId>><br />
'&FactoryName=<<FactoryName>></span><br />
strXML = strXML & "<set name='" & oRs.ReadData("FactoryName").ToString() & "' value='" & oRs.ReadData("TotOutput").ToString() & "' link='" & Server.UrlEncode("Detailed.aspx?FactoryId=" & oRs.ReadData("FactoryId").ToString()) & "&FactoryName=" & oRs.ReadData("FactoryName").ToString() & "'/>"<br />
<br />
End While<br />
<span class="codeComment"> 'Finally, close <graph> element</span><br />
strXML = strXML & "</graph>"<br />
<span class="codeComment">' Close Data Reader</span><br />
oRs.ReadData.Close()</p>
<p> <span class="codeComment"> 'Create the chart - Pie 3D Chart with data from strXML</span><br />
<b> Return FusionCharts.RenderChart("../FusionCharts/FCF_Pie3D.swf", "", strXML, "FactorySum", "650", "450", False, False)</b><br />
End Function</p>
<p><br />
End Class<br />
</p></td>
</tr>
<tr>
<td valign="top" class="text"><p>As you can see in the code above, we're doing the following:</p>
<ol>
<li>Include <span class="codeInline">DataConnection</span> and <span class="codeInline">InfoSoftGlobal</span> namespace to enable databse connection and easy embedding of FusionCharts.<br />
(Know more about <a href="VBNET_DB.html#DataConn">DataConnection namespace</a>) </li>
<li>Then we declare variables <span class="codeInline">strQuery</span> to store SQL Query that fetches data from our MS Access database and <span class="codeInline">strXML</span> to store XML data. </li>
<li>Thereafter, we generate the XML data document by iterating through the recordset. We store the XML data in <span class="codeInline">strXML</span> variable. To each <span class="codeInline"><set></span> element, we add the <span class="codeInline">link</span> attribute, which points to <span class="codeInline">Detailed.aspx</span> - the page that contains the chart to show details. We pass the factory id of the respective factory by appending it to the link. We finally URL Encode the link, which is a very important step. </li>
<li>Finally, we render the chart using <span class="codeInline">renderChart()</span> method and pass <span class="codeInline">strXML</span> as <span class="codeInline">dataXML</span>. <span class="codeInline">RenderChart()is called form the Page_Load</span><span class="codeInline"> event listener</span>. </li>
</ol>
<p>Let's now shift our attention to <span class="codeInline">Detailed.aspx</span> page. </p></td>
</tr>
<tr>
<td valign="top" class="text"> </td>
</tr>
<tr>
<td valign="top" class="header">Creating the detailed data chart page </td>
</tr>
<tr>
<td valign="top" class="text">The page <span class="codeInline">Detailed.aspx</span> contains the following code: </td>
</tr>
<tr>
<td valign="top" class="codeBlock"><p><%@ Page Language="VB" AutoEventWireup="false" CodeFile="Detailed.aspx.vb" <br />
Inherits="DB_DrillDown_Detailed" %></p>
<p><HTML><br />
<HEAD><br />
<TITLE><br />
FusionCharts Free - Database and Drill-Down Example<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></p>
<p></HTML></p></td>
</tr>
<tr>
<td valign="top" class="text">Here we have included <span class="codeInline">FusionCharts.js </span>file to enable us embed the chart using JavaScript. We are also adding an ASP control literal which acts as the container for the charts. The <span class="codeInline"><strong>CreateCharts()</strong></span> function does the generation, and is the code behind the file <span class="codeInline">Detailed.aspx.vb</span>. Here is the code: </td>
</tr>
<tr>
<td valign="top" class="codeBlock"><p>Imports DataConnection<br />
Imports Utilities<br />
Imports InfoSoftGlobal<br />
</p>
<p>Partial Class DB_DrillDown_Detailed<br />
Inherits System.Web.UI.Page<br />
<br />
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> Public Function <b>CreateChart</b>() As String<br />
<span class="codeComment">'This page is invoked from Default.asp. When the user clicks on a pie</span><br />
<span class="codeComment"> 'slice in Default.asp, the factory Id is passed to this page. We need<br />
'to get that factory id, get information from database and then show<br />
'a detailed chart.</span></p>
<p> <span class="codeComment"> 'First, get the factory Id</span><br />
Dim FactoryId As String, FactoryName As String<br />
Dim util As New Util<br />
<span class="codeComment">'Request the factory Id from Querystring</span><br />
FactoryId = Request.QueryString("FactoryId")<br />
FactoryName = Request.QueryString("FactoryName")</p>
<p> Dim oRs As DbConn, strQuery As String<br />
<span class="codeComment"> 'strXML will be used to store the entire XML document generated</span><br />
Dim strXML As String</p>
<p> <span class="codeComment"> 'Generate the graph element string</span><br />
strXML = "<graph caption='Factory " & FactoryName & " Output ' subcaption='(In Units)' xAxisName='Date' showValues='1' decimalPrecision='0' rotateNames='1' >"</p>
<p> <span class="codeComment">' SQL Query</span><br />
strQuery = "select * from Factory_Output where FactoryId=" & FactoryId</p>
<p> <span class="codeComment"> ' Open Data Reader</span><br />
oRs = New DbConn(strQuery)<br />
<span class="codeComment">'Now, we get the data for that factory</span><br />
While oRs.ReadData.Read()<br />
<span class="codeComment">'Here, we convert date into a more readable form for set name.</span><br />
strXML = strXML & "<set name='" & Convert.ToDateTime(oRs.ReadData("DatePro")).ToString("dd/MM/yyyy") & "' value='" & oRs.ReadData("Quantity").ToString() & "' color='" & util.getFCColor() & "'/>"</p>
<p> End While<br />
<span class="codeComment"> 'Close <graph> element</span><br />
strXML = strXML & "</graph>"<br />
<span class="codeComment"> ' Close Data Reader</span><br />
oRs.ReadData.Close()</p>
<p> <span class="codeComment"> 'Create the chart - Column 2D Chart with data from strXML</span><br />
<b> Return FusionCharts.RenderChart("../FusionCharts/FCF_Column2D.swf", "", strXML, "FactoryDetailed", "600", "300", False, False)</b><br />
End Function</p>
<p><br />
End Class<br />
</p></td>
</tr>
<tr>
<td valign="top" class="text"><p>In this page, we're:</p>
<ol>
<li>Including <span class="codeInline">DataConnection, Utility </span><span class="text">and</span><span class="codeInline"> InfoSoftGlobal </span><span class="text">namespaces from </span><span class="codeInline">DbConn</span><span class="text"> and </span><span class="codeInline">Util</span><span class="text"> class of </span><span class="codeInline">App_Code</span><span class="text"> folder and </span><span class="codeInline">FusionCharts</span><span class="text"> class of bin folder respectively. </span><br />
(Know more about <a href="VBNET_DB.html#DataConn">DataConnection namespace</a>) </li>
<li>Requesting the factory id for which we've to show detailed data. This data was sent to us as query string, as a part of pie chart link.</li>
<li>We get the requisite data for this factory from database and then convert it into XML using string concatenation in the variable <span class="codeInline">strXML</span>.</li>
<li>Finally, we render a Column 2D chart using <span class="codeInline">RenderChart()</span> method to show detailed data. <span class="codeInline">RenderChart()is called form the Page_Load</span><span class="codeInline"> event listener</span>. </li>
</ol>
<p>When you now run the app, you'll see the detailed page as under: </p></td>
</tr>
<tr>
<td valign="top" class="text"><img src="Images/Code_Drill.jpg" class="imageBorder" /> </td>
</tr>
</table>
</body>
</html>
Mr. DellatioNx196 GaLers xh3LL Backd00r 1.0, Coded By Mr. DellatioNx196 - Bogor BlackHat