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.
 
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 (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 > VBNET > DB_DrillDown folder.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="DB_DrillDown_Default" %>

<HTML>
  <HEAD>
    <TITLE> FusionCharts Free - Database and Drill-Down Example </TITLE>
    <%
      'You need to include the following JS file, if you intend to embed the chart using JavaScript.
    %>
    <SCRIPT LANGUAGE="Javascript" SRC="../FusionCharts/FusionCharts.js"></SCRIPT>
  </HEAD>

  <BODY>
    <%=CreateChart() %>
  </BODY>

</HTML>

Here, we have included FusionCharts.js file and called CreateChart() function from the code behind file Default.aspx.vb. Here is the content of the Default.aspx.vb:

Imports DataConnection
Imports InfoSoftGlobal
Partial Class DB_DrillDown_Default
  Inherits System.Web.UI.Page

Public Function CreateChart() As String
  'In this example, we show how to connect FusionCharts to a database.
  'For the sake of ease, we've used an Access database which is present in
  '../App_Data/FactoryDB.mdb. It just contains two tables, which are linked to each
  'other.

  'Database Objects - Initialization
  Dim oRs As DbConn, strQuery As String
  'strXML will be used to store the entire XML document generated
  Dim strXML As String

  'Generate the graph element
  strXML = "<graph caption='Factory Output report' subCaption='By Quantity' decimalPrecision='0' showNames='1' numberSuffix=' Units' pieSliceDepth='30' formatNumberScale='0' >"

  'Iterate through each factory
  strQuery = "select * from Factory_Master"
  oRs = New DbConn(strQuery)

  While oRs.ReadData.Read()
    'Now create second recordset to get details for this factory
    strQuery = "select sum(Quantity) as TotOutput from Factory_Output where FactoryId=" & oRs.ReadData("FactoryId").ToString()
    Dim oRs2 As New DbConn(strQuery)
    oRs2.ReadData.Read()
    'Generate <set name='..' value='..' link='..' />
    'Note that we're setting link as Detailed.aspx?FactoryId=<<FactoryId>>

    strXML = strXML & "<set name='" & oRs.ReadData("FactoryName").ToString() & "' value='" & oRs2.ReadData("TotOutput").ToString() & "' link='" & Server.UrlEncode("Detailed.aspx?FactoryId=" & oRs.ReadData("FactoryId").ToString()) & "'/>"
    'Close recordset
    oRs2.ReadData.Close()

  End While
  'Finally, close <graph> element
  strXML = strXML & "</graph>"
  oRs.ReadData.Close()

  'Create the chart - Pie 3D Chart with data from strXML
  Return FusionCharts.RenderChart("../FusionCharts/FCF_Pie3D.swf", "", strXML, "FactorySum", "650", "450", False, False)
  End Function
End Class

As you can see in the code above, we're doing the following:

  1. Include DataConnection and InfoSoftGlobal namespace to enable databse connection and easy embedding of FusionCharts.
    (Know more about DataConnection namespace)
  2. Then we declare variables strQuery to store SQL Query that fetches data from our MS Access database and strXML to store XML data.
  3. 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.
  4. Finally, we render the chart using renderChart() method and pass strXML 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="VB" AutoEventWireup="false" CodeFile="Detailed.aspx.vb"
Inherits="DB_DrillDown_Detailed" %>

<HTML>
  <HEAD>
    <TITLE>
      FusionCharts Free - Database and Drill-Down Example
    </TITLE>
    <%
      'You need to include the following JS file, if you intend to embed the chart using JavaScript.
    %>
    <SCRIPT LANGUAGE="Javascript" SRC="../FusionCharts/FusionCharts.js"></SCRIPT>
  </HEAD>

  <BODY>
      <%=CreateChart() %>
  /BODY>

</HTML>

Here we have included FusionCharts.js file for easy chart rendering and then we have called CreateChart() function from the code behind file Detailed.aspx.vb. Here is the code:

Imports DataConnection
Imports Utilities
Imports InfoSoftGlobal
Partial Class DB_DrillDown_Detailed
  Inherits System.Web.UI.Page

  Public Function CreateChart() As String
    '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
    Dim FactoryId As Integer
    Dim util As New Util
    'Request the factory Id from Querystring
    FactoryId = Request.QueryString("FactoryId")

    Dim oRs As DbConn, strQuery As String
    'strXML will be used to store the entire XML document generated
    Dim strXML As String

    'Generate the graph element string
    strXML = "<graph caption='Factory " & FactoryId & " Output ' subcaption='(In Units)' xAxisName='Date' showValues='1' decimalPrecision='0' rotateNames='1' >"
    'Now, we get the data for that factory
    strQuery = "select * from Factory_Output where FactoryId=" & FactoryId
    oRs = New DbConn(strQuery)
    While oRs.ReadData.Read()
      'Here, we convert date into a more readable form for set name.

      strXML = strXML & "<set name='" & Convert.ToDateTime(oRs.ReadData("DatePro")).ToString("dd") & "/" & Convert.ToDateTime(oRs.ReadData("DatePro")).ToString("MM") & "' value='" & oRs.ReadData("Quantity").ToString() & "' color='" & util.getFCColor() & "'/>"

    End While
    'Close <graph> element
    strXML = strXML & "</graph>"
    oRs.ReadData.Close()

    'Create the chart - Column 2D Chart with data from strXML
    Return FusionCharts.RenderChart("../FusionCharts/FCF_Column2D.swf", "", strXML, "FactoryDetailed", "600", "300", False, False)
  End Function

End Class

In this page, we're:

  1. 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)
  2. 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.
  3. We get the requisite data for this factory from database and then convert it into XML using string concatenation in the variable strXML.
  4. 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: