Using FusionCharts with C# (ASP.NET) > Plotting data from a database |
In this section, we'll show you how to use FusionCharts and ASP.NET to plot charts from data contained in a database. We'll create a pie chart to show "Production by Factory" using:
For the sake of ease, we'll use an Access Database. The database is present in Download Package > Code > VBNET > DB folder. You can, however, use any database with FusionCharts including MS SQL, Oracle, MySQL etc. 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. The code examples contained in this page are present in Download Package > Code > CNET > DBExample folder. The Access database is present in Download Package > Code > CNET > DB. |
Database Structure |
Before we code the ASP.NET pages to retrieve data, let's quickly have a look at the database structure. |
The database contains just 2 tables:
For demonstration, we've fed some dummy data in the database. Let's now shift our attention to the ASP.NET page that will interact with the database, fetch data and then render a chart. |
Building the ASP.NET Page for dataXML Method |
The ASP.NET page for dataXML method example is named as BasicDBExample.aspx (in DBExample folder). It contains the following code: |
<%@ Page Language="C#" AutoEventWireup="false" CodeFile="BasicDBExample.aspx.cs" Inherits="DBExample_BasicDBExample" %> <HTML> <BODY> |
In the above code, we have included FusionCharts.js file to render chart through 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, BasicDBExample.aspx.cs. Let's take a look at the code behind file: |
using System; public partial class DBExample_BasicDBExample : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e)
public string CreateChart(){ //Database Objects - Initialization //strXML will be used to store the entire XML document generated //Generate the graph element //SQL Query // Open Data Reader //Iterate through each factory //Create the chart - Pie 3D Chart with data from strXML } |
The following actions are taking place in this code:
When you now run the code, you'll get an output as under: |
Converting the example to use dataURL method |
Let's now convert this example to use dataURL method. As previously explained, in dataURL mode, you need two pages:
The pages in this example are contained in Download Package > Code > CNET > DB_dataURL folder. |
Chart Container Page - Default.aspx |
Default.aspx contains the following code to render the chart: |
<%@ Page Language="C#" %> protected void Page_Load(object sender, EventArgs e) //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 //Variable to contain dataURL //Create the chart - Pie 3D Chart with dataURL as strDataURL <HTML> <asp:Literal ID="FCLiteral" runat="server"></asp:Literal> |
In the above code, we:
|
Creating the data provider page PieData.aspx |
PieData.aspx contains the following code to output XML Data: |
<%@ Page Language="C#" Debug="true" %> <script runat="server"> //For the sake of ease, we've used an Access database which is present in //Database Objects - Initialization //strXML will be used to store the entire XML document generated //Generate the graph element //Iterate through each factory while (oRs.ReadData.Read()) DbConn oRs2 = new DbConn(strQuery); } //Set Proper output content-type //Just write out the XML data }
|
In the above page:
When you view this page, you'll get the same output as before. |
Inside DataConnection Namespace |
We have used DataConnection Namespace in the above code and in all subsequent Database examples. Using this class we establish connection to the MS Access database with ADO.NET component. Let's go through the lines of code inside this class: |
using System; namespace DataConnection public OdbcConnection connection; // SQL Server DataBase Connection - Defined in Web.Config // Creating Connection string using web.config connection string // Create an instance dataReader |
What it does:
|