|
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.
Before you go further with this page, we recommend you to please see the previous sections like "Plotting from Database Example" as we start off from concepts explained in those pages.
If you recall from previous example, we were showing the sum of factory output in a pie chart as under: |
|
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. |
|
To set up the pie chart to enable links for drill-down involves just minor tweaking of our previous BasicDBExample.aspx. We basically need to add the link attribute for each <set> element. We create a new page Default.aspx (in DB_DrillDown folder) from the previous page with the following code changes:
The code examples contained in this page are contained in Download Package > Code > CNET > DB_DrillDown folder. |
<%@ Page Language="C#" AutoEventWireup="false" CodeFile="Default.aspx.cs"
Inherits="DB_DrillDown_Default" %>
<HTML>
<HEAD>
<TITLE> FusionCharts Free - Database and Drill-Down Example </TITLE>
<%
%>
<SCRIPT LANGUAGE="Javascript" SRC="../FusionCharts/FusionCharts.js"></SCRIPT>
</HEAD>
<BODY>
<asp:Literal ID="FCLiteral" runat="server"></asp:Literal>
</BODY>
</HTML> |
Here, we have included FusionCharts.jsfile 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 CreateCharts() function does the generation, and is the code behind the file, Default.aspx.cs. Here is the content of the Default.aspx.cs: |
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataConnection;
using InfoSoftGlobal;
public partial class DB_DrillDown_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FCLiteral.Text = CreateChart();
}
public string CreateChart(){
DbConn oRs; string strQuery;
string strXML;
strXML = "<graph caption='Factory Output report' subCaption='By Quantity' decimalPrecision='0' showNames='1' numberSuffix=' Units' pieSliceDepth='30' formatNumberScale='0' >";
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";
oRs = new DbConn(strQuery);
while(oRs.ReadData.Read()){
&
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()) + "'/>";
}
strXML += "</graph>";
oRs.ReadData.Close();
return FusionCharts.RenderChart("../FusionCharts/FCF_Pie3D.swf", "", strXML, "FactorySum", "650", "450", false, false);
}
}
|
As you can see in the code above, we're doing the following:
- Include DataConnection and InfoSoftGlobal namespace to enable databse connection and easy embedding of FusionCharts.
(Know more about DataConnection Namespace)
- Then we declare variables strQuery to store SQL Query that fetches data from our MS Access database and strXML to store XML data.
- Thereafter, we generate the XML data document by iterating through the recordset. We store the XML data in strXML variable. To each <set> element, we add the link attribute, which points to Detailed.aspx - 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.
- Finally, we render the chart using renderChart() method and pass strXML as dataXML. RenderChart()is called form the Page_Load event lsitener.
Let's now shift our attention to Detailed.aspx page. |
|
The page Detailed.aspx contains the following code: |
<%@ Page Language="C#" AutoEventWireup="false" CodeFile="Detailed.aspx.cs"
Inherits="DB_DrillDown_Detailed" %>
<HTML>
<HEAD>
<TITLE>
FusionCharts Free - Database and Drill-Down Example
</TITLE>
<%
%>
<SCRIPT LANGUAGE="Javascript" SRC="../FusionCharts/FusionCharts.js"></SCRIPT>
</HEAD>
<BODY>
<asp:Literal ID="FCLiteral" runat="server"></asp:Literal>
</BODY>
</HTML> |
In the above code, we added the ASP literal control which acts as the container for the chart. The CreateCharts() function does the generation, and is the code behind the file Detailed.aspx.cs. Here is the code: |
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataConnection;
using Utilities;
using InfoSoftGlobal;
public partial class DB_DrillDown_Detailed : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FCLiteral.Text = CreateChart();
}
public string CreateChart(){
string FactoryId, FactoryName;
Util util = new Util();
FactoryId = Request["FactoryId"];
FactoryName = Request["FactoryName"];
DbConn oRs; string strQuery;
string strXML;
strXML = "<graph caption='" + FactoryName + " Output ' subcaption='(In Units)' xAxisName='Date' showValues='1' decimalPrecision='0' rotateNames='1' >";
strQuery = "select * from Factory_Output where FactoryId=" + FactoryId;
oRs = new DbConn(strQuery);
while(oRs.ReadData.Read()){
strXML += "<set name='" + Convert.ToDateTime(oRs.ReadData["DatePro"]).ToString("dd/MM/yyyy") + "' value='" + oRs.ReadData["Quantity"].ToString() + "' color='" + util.getFCColor() + "'/>";
}
strXML += "</graph>";
oRs.ReadData.Close();
return FusionCharts.RenderChart("../FusionCharts/FCF_Column2D.swf", "", strXML, "FactoryDetailed", "600", "300", false, false);
}
}
|
In this page, we're:
- Including DataConnection, Utility and InfoSoftGlobal namespaces from DbConn and Util class of App_Code folder and FusionCharts class of bin folder respectively.
(Know more about DataConnection Namespace)
- 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.
- We get the requisite data for this factory from database and then convert it into XML using string concatenation in the variable strXML.
- Finally, we render a Column 2D chart using RenderChart() method to show detailed data.RenderChart()is called form the Page_Load event lsitener.
When you now run the app, you'll see the detailed page as under: |
|