Current File : /home/caballoscriollos/www/espanol/encuesta/graphs/contents/asp_db.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 ASP > Plotting data from a database </h2></td>
</tr>
<tr>
<td valign="top" class="text"><p>In this section, we'll show you how to use FusionCharts and ASP to plot charts from data contained in a database. We'll create a pie chart to show "Production by Factory" using: </p>
<ul>
<li><span class="codeInline">dataXML</span> method first.</li>
<li>Thereafter, we'll convert this chart to use <span class="codeInline">dataURL</span> method. </li>
</ul>
<p>For the sake of ease, we'll use an Access Database. The database is present in <span class="codeInline">Download Package > Code > ASP > DB </span>folder. You can, however, use any database with FusionCharts including MS SQL, Oracle, MySQL etc. </p>
<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 > ASP > DBExample </span> folder. The Access database is present in <span class="codeInline">Download Package > Code > ASP ></span> <span class="codeInline">DB</span>. </p></td>
</tr>
<tr>
<td valign="top" class="text"> </td>
</tr>
<tr>
<td valign="top" class="header">Database Structure </td>
</tr>
<tr>
<td valign="top" class="text">Before we code the ASP pages to retrieve data, let's quickly have a look at the database structure. </td>
</tr>
<tr>
<td valign="top" class="text"><img src="Images/Code_DB.gif" /></td>
</tr>
<tr>
<td valign="top" class="text"><p>The database contains just 2 tables:</p>
<ol>
<li><span class="codeInline">Factory_Master</span>: To store the name and id of each factory</li>
<li><span class="codeInline">Factory_Output</span>: To store the number of units produced by each factory for a given date.</li>
</ol>
<p>For demonstration, we've fed some dummy data in the database. Let's now shift our attention to the ASP page that will interact with the database, fetch data and then render a chart. </p></td>
</tr>
<tr>
<td valign="top" class="text"> </td>
</tr>
<tr>
<td valign="top" class="header">Building the ASP Page for dataXML Method </td>
</tr>
<tr>
<td valign="top" class="text">The ASP page for <span class="codeInline">dataXML</span> method example is named as <span class="codeInline">BasicDBExample.asp</span> (in <span class="codeInline">DBExample</span> folder). It contains the following code: </td>
</tr>
<tr>
<td valign="top" class="codeBlock"><p><%@ Language=VBScript %><br />
<HTML><br />
<HEAD><br />
<TITLE>FusionCharts Free - Database Example</TITLE><br />
<SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT><br />
</HEAD><br />
<!-- #INCLUDE FILE="../Includes/FusionCharts.asp" --><br />
<span class="codeComment"> 'Adding library script that will connect to the Access Databse file</span> automatically. <br />
<!-- #INCLUDE FILE="../Includes/DBConn.asp" --><br />
<BODY><br />
<CENTER><br />
<% <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 />
'../DB/FactoryDB.mdb. It just contains two tables, which are linked to each<br />
'other. <br />
<br />
'Database Objects - Initialization</span><br />
Dim oRs, oRs2, strQuery<br />
<span class="codeComment"> 'strXML will be used to store the entire XML document generated</span><br />
Dim strXML<br />
<br />
<span class="codeComment"> 'Create the recordset to retrieve data</span><br />
Set oRs = Server.CreateObject("ADODB.Recordset")</p>
<p> <span class="codeComment"> 'Generate the chart element</span><br />
strXML = "<graph caption='Factory Output report' subCaption='By Quantity' decimalPrecision='0' showNames='1' numberSuffix=' Units' pieSliceDepth='30' formatNumberScale='0'>"<br />
<br />
<span class="codeComment"> 'Iterate through each factory</span><br />
strQuery = "select * from Factory_Master"<br />
Set oRs = oConn.Execute(strQuery)<br />
<br />
While Not oRs.Eof<br />
<span class="codeComment"> 'Now create second recordset to get details for this factory</span><br />
Set oRs2 = Server.CreateObject("ADODB.Recordset")<br />
strQuery = "select sum(Quantity) as TotOutput from Factory_Output where FactoryId=" & ors("FactoryId")<br />
Set oRs2 = oConn.Execute(strQuery) <br />
<span class="codeComment"> 'Generate <set name='..' value='..'/>
</span><br />
strXML = strXML & "<set name='" & ors("FactoryName") & "' value='" & ors2("TotOutput") & "' />"<br />
<span class="codeComment"> 'Close recordset</span><br />
Set oRs2 = Nothing<br />
oRs.MoveNext<br />
Wend<br />
<span class="codeComment"> 'Finally, close <chart> element</span><br />
strXML = strXML & "</chart>"<br />
Set oRs = nothing<br />
<br />
<span class="codeComment"> 'Create the chart - Pie 3D Chart with data from strXML</span><br />
Call renderChart("../../FusionCharts/FCF_Pie3D.swf", "", strXML, "FactorySum", 650, 450)<br />
%><br />
</BODY><br />
</HTML></p></td>
</tr>
<tr>
<td valign="top" class="text"><p>The following actions are taking place in this code:</p>
<ol>
<li>We first include <span class="codeInline">FusionCharts.js</span> JavaScript class and <span class="codeInline">FusionCharts.asp</span> , to enable easy embedding of FusionCharts.</li>
<li>We then include <span class="codeInline"> DBConn.asp</span>, which contains connection parameters to connect to Access database. </li>
<li>Thereafter, we generate the XML data document by iterating through the recordset and store it in <span class="codeInline">strXML</span> variable. </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>. </li>
</ol>
<p>When you now run the code, you'll get an output 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"> </td>
</tr>
<tr>
<td valign="top" class="header">Converting the example to use dataURL method </td>
</tr>
<tr>
<td valign="top" class="text"><p>Let's now convert this example to use dataURL method. As previously explained, in dataURL mode, you need two pages:</p>
<ol>
<li><strong>Chart Container Page</strong> - The page which embeds the HTML code to render the chart. This page also tells the chart where to load the data from. We'll name this page as <span class="codeInline">Default.asp</span>. </li>
<li><strong>Data Provider Page</strong> - This page provides the XML data to the chart. We'll name this page as <span class="codeInline">PieData.asp</span></li>
</ol>
<p class="highlightBlock">The pages in this example are contained in<span class="codeInline"> Download Package > Code > ASP > DB_dataURL</span> folder. </p> </td>
</tr>
<tr>
<td valign="top" class="text"> </td>
</tr>
<tr>
<td valign="top" class="header">Chart Container Page - <span class="codeInline">Default.asp </span></td>
</tr>
<tr>
<td valign="top" class="text"><span class="codeInline">Default.asp</span> contains the following code to render the chart: </td>
</tr>
<tr>
<td valign="top" class="codeBlock"><%@ Language=VBScript %><br />
<HTML><br />
<HEAD><br />
<TITLE> FusionCharts Free - dataURL and Database Example</TITLE><br />
<SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT><br />
</HEAD><br />
<!-- #INCLUDE FILE="../Includes/FusionCharts.asp" --> <br />
<BODY><br />
<%<br />
<span class="codeComment"> 'In this example, we show how to connect FusionCharts to a database <br />
'using dataURL method. In our previous example, we've used dataXML method<br />
'where the XML is generated in the same page as chart. Here, the XML data<br />
'for the chart would be generated in PieData.asp.<br />
<br />
'For the sake of ease, we've used an Access database which is present in<br />
'../DB/FactoryDB.mdb. It just contains two tables, which are linked to each<br />
'other.<br />
<br />
'Variable to contain dataURL</span><br />
Dim strDataURL <br />
<span class="codeComment">'the asp file piedata.asp interacts with the database,</span> <br />
<span class="codeComment">'converts the data into proper XML form and finally</span> <br />
<span class="codeComment">'relays XML data document to the chart</span><br />
strDataURL = "PieData.asp"<br />
<br />
<span class="codeComment">'Create the chart - Pie 3D Chart with dataURL as strDataURL</span><br />
Call renderChart("../../FusionCharts/FCF_Pie3D.swf", strDataURL, "", "FactorySum", 650, 450)<br />
%><br />
</BODY><br />
</HTML></td>
</tr>
<tr>
<td valign="top" class="text"><p>In the above code, we:</p>
<ol>
<li>Include <span class="codeInline">FusionCharts.js</span> JavaScript class and <span class="codeInline">FusionCharts.asp,</span></li>
<li>Create the <span class="codeInline">dataURL</span> string and store it in <span class="codeInline">strDataURL</span> variable. </li>
<li>Finally, we render the chart using <span class="codeInline">renderChart()</span> method and set <span class="codeInline">dataURL</span> as <span class="codeInline">strDataURL</span>. </li>
</ol> </td>
</tr>
<tr>
<td valign="top" class="header">Creating the data provider page <span class="codeInline">PieData.asp </span></td>
</tr>
<tr>
<td valign="top" class="text">PieData.asp contains the following code to output XML Data: </td>
</tr>
<tr>
<td valign="top" class="codeBlock"><p><%@ Language=VBScript %></p>
<p><!-- #INCLUDE FILE="../Includes/DBConn.asp" --><br />
<%<br />
<span class="codeComment">'This page generates the XML data for the Pie Chart contained in<br />
'Default.asp. <br />
<br />
'For the sake of ease, we've used an Access database which is present in<br />
'../DB/FactoryDB.mdb. It just contains two tables, which are linked to each<br />
'other. <br />
<br />
'Database Objects - Initialization</span><br />
Dim oRs, oRs2, strQuery<br />
<span class="codeComment"> 'strXML will be used to store the entire XML document generated</span><br />
Dim strXML<br />
<br />
<span class="codeComment"> </span><span class="codeComment"> 'Create the recordset to retrieve data</span><br />
Set oRs = Server.CreateObject("ADODB.Recordset")</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'>"<br />
<br />
<span class="codeComment"> 'Iterate through each factory</span><br />
strQuery = "select * from Factory_Master"<br />
Set oRs = oConn.Execute(strQuery)<br />
<br />
While Not oRs.Eof<br />
<span class="codeComment"> 'Now create second recordset to get details for this factory</span><br />
Set oRs2 = Server.CreateObject("ADODB.Recordset")<br />
strQuery = "select sum(Quantity) as TotOutput from Factory_Output where FactoryId=" & ors("FactoryId")<br />
Set oRs2 = oConn.Execute(strQuery) <br />
<span class="codeComment"> 'Generate <set name='..' value='..' /></span><br />
strXML = strXML & "<set name='" & ors("FactoryName") & "' value='" & ors2("TotOutput") & "' />"<br />
<span class="codeComment"> 'Close recordset</span><br />
Set oRs2 = Nothing<br />
oRs.MoveNext<br />
Wend<br />
<span class="codeComment"> 'Finally, close <graph> element</span><br />
strXML = strXML & "</graph>"<br />
Set oRs = nothing<br />
<br />
<span class="codeComment"> 'Set Proper output content-type</span><br />
Response.ContentType = "text/xml"<br />
<br />
<span class="codeComment">'Just write out the XML data<br />
<strong> 'NOTE THAT THIS PAGE DOESN'T CONTAIN ANY HTML TAG, WHATSOEVER</strong></span><br />
Response.Write(strXML)<br />
%><br />
</p></td>
</tr>
<tr>
<td valign="top" class="text"><p>In the above page:</p>
<ol><li>We generate the data and store it in <span class="codeInline">strXML</span> variable</li>
<li>Finally, we write this data to output stream without any HTML tags. </li>
</ol>
<p>When you view this page, you'll get the same output as before. </p></td>
</tr>
</table>
</body>
</html>
Mr. DellatioNx196 GaLers xh3LL Backd00r 1.0, Coded By Mr. DellatioNx196 - Bogor BlackHat