## Using FusionCharts with C# (ASP.NET)> Creating Drill-down charts
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. 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.
Setting up the pie chart for Link
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 from the previous page in DBExample folder with the following code changes:
<%@ Page language="c#" Codebehind="BasicDBExample.aspx.cs" AutoEventWireup="false" Inherits="InfoSoftGlobal.GeneralPages.ASP.NET.DBExample.BasicDBExample" %> <HTML> <HEAD> <TITLE>FusionCharts - Database Example </TITLE> <SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT> </HEAD> <body> <form id='form1' name='form1' method='post' runat="server"> <%=GetFactorySummaryChartHtml()%> </form> </body> </HTML> public string GetFactorySummaryChartHtml() { //xmlData will be used to store the entire XML document generated string xmlData; //Generate the chart element xmlData = "<chart caption='Factory Output report' subCaption='By Quantity' pieSliceDepth='30' showBorder='1' formatNumberScale='0' numberSuffix=' Units' animation=' " + animateChart + "'>"; //Iterate through each factory string factoryQuery = "select * from Factory_Master"; using (OdbcConnection connectin = FusionCharts.Connection(FusionCharts.ConnectionStringFactory)) { using (OdbcCommand factoryCommand = new OdbcCommand(factoryQuery, connectin)) { using (OdbcDataAdapter adapter = new OdbcDataAdapter(factoryCommand)) { DataTable table = new DataTable(); adapter.Fill ( table ); foreach ( DataRow row in table.Rows) { string quantityQuery = "select sum(Quantity) as TotOutput from Factory_Output where FactoryId=" + row["FactoryId"].ToString(); using (OdbcCommand quantityCommand = new OdbcCommand(quantityQuery,connectin)) { xmlData += "<set label='" + row["FactoryName"].ToString() + "' value='" + quantityCommand.ExecuteScalar().ToString() + "' link='" + Server.UrlEncode("Detailed.aspx?FactoryId=" + row["FactoryId"].ToString()) + "'/>"; } } } } connectin.Close(); xmlData += "</chart>"; } //Create the chart - Pie 3D Chart with data from xmlData return FusionCharts.RenderChart("../../FusionCharts/Pie3D.swf", "", xmlData, "FactorySum", "600", "300", false, false); }
As you can see in the code above, we're doing the following: Include FusionCharts.js JavaScript class to enable easy embedding of FusionCharts. We then open a connection using to Access database. Thereafter, we generate the XML data in the GetFactorySummaryChartHtml() method document by iterating through the recordset. We store the XML data in xmlData 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 xmlData as dataXML. Let's now shift our attention to Detailed.aspx page.
Creating the detailed data chart page
The page Detailed.aspx contains the following code:
%@ Page language="c#" Codebehind="Detailed.aspx.cs" AutoEventWireup="false" Inherits="InfoSoftGlobal.GeneralPages.ASP.NET.DBExample.Detailed" %> <HTML> <HEAD> <TITLE>FusionCharts - Database and Drill-Down Example </TITLE> <SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT> </HEAD> <body> <form id='form1' name='form1' method='post' runat="server"> <%=GetFactoryDetailedChartHtml()%> </form> </body> </HTML> public string GetFactoryDetailedChartHtml() { //This page is invoked from Default.aspx. When the user clicks on a pie //slice in Default.aspx, the factory Id is passed to this page. We need //to get that factory id, get information from database and then show //a detailed chart. //First, get the factory Id string factoryId; //Request the factory Id from Querystring factoryId = Request.QueryString["FactoryId"]; //xmlData will be used to store the entire XML document generated string xmlData; //Generate the chart element string xmlData = "<chart palette='2' caption='Factory " + factoryId +" Output ' subcaption='(In Units)' xAxisName='Date' showValues='1' labelStep='2' >"; //Now, we get the data for that factory string query = "select * from Factory_Output where FactoryId=" + factoryId; using (OdbcConnection connection = FusionCharts.Connection(FusionCharts.ConnectionStringFactory)) { using (OdbcCommand command = new OdbcCommand(query, connection)) { using (OdbcDataReader reader = command.ExecuteReader()) { while (reader.Read()) { xmlData +="<set label='" + ((DateTime)reader["DatePro"]).Day.ToString() + "/" + ((DateTime)reader["DatePro"]).Month.ToString() + "' value='" + reader["Quantity"].ToString() + "'/>"; } reader.Close(); } } } //Close element xmlData += " </chart>"; //Create the chart - Column 2D Chart with data from xmlData return FusionCharts.RenderChart("../../FusionCharts/Column2D.swf", "", xmlData, "FactoryDetailed", "600", "300", false, false); }
In this page, we're: Including FusionCharts.js JavaScript class to enable easy embedding of FusionCharts. Requesting the factory id for which we've to show detailed data. This data was sent to us as querystring, 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. Finally, we render a Column 2D chart using RenderChart() method to show detailed data. When you now run the app, you'll see the detailed page as under: