In this section, we'll show you how to use FusionCharts with ASP.NET to plot data collected in forms.

We'll build a simple restaurant sales example, where the user will enter the items sold by a restaurant in a given week. This data will be submitted in a form to the server. We'll acquire this data and plot in on a chart. For the sake of simplicity, we wouldn't do any processing on this data. However, your real life applications might process data before presenting it on the chart.

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 > FormBased folder.
 
Building the Form
The form is contained in Default.aspx and looks as under:
It's a very simple form which submits to Chart.aspx. As such, we wouldn't go into the code of this form. You can directly open the source from download and see it.
 
Requesting the data and Creating the Chart
The work of requesting the data from submitted form and creating the chart is done in Chart.aspx, present in the same folder. It contains the following code:

<%@ Page Language="C#" AutoEventWireup="false"
CodeFile="Chart.aspx.cs" Inherits="FormBased_Chart" %>
<HTML>
  <HEAD>
    <TITLE> FusionCharts Free - Form Based Data Charting 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>
     <asp:Literal ID="FCLiteral" runat="server"></asp:Literal>
  </BODY>
</HTML>

In the above code 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 to generate code for chart.

Let's take a look at the code behind file, Chart.aspx.cs now:

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 InfoSoftGlobal;
public partial class FormBased_Chart : System.Web.UI.Page
{

   protected void Page_Load(object sender, EventArgs e)
   {
      // Generate chart in Literal Control
      FCLiteral.Text = CreateChart();
   }

 

  public string CreateChart()
  {
    //We first request the data from the form (Default.aspx)
    string intSoups, intSalads, intSandwiches, intBeverages, intDesserts;

    intSoups = Context.Items["Soups"].ToString();
    intSalads = Context.Items["Salads"].ToString();
    intSandwiches = Context.Items["Sandwiches"].ToString();
    intBeverages = Context.Items["Beverages"].ToString();
    intDesserts = Context.Items["Desserts"].ToString();

    //In this example, we're directly showing this data back on chart.
    //In your apps, you can do the required processing and then show the
    //relevant data only.

    //Now that we've the data in variables, we need to convert this into XML.
    //The simplest method to convert data into XML is using string concatenation.

    string strXML;
    //Initialize <graph> element
    strXML = "<graph caption='Sales by Product Category' subCaption='For this week'     showPercentageInLabel='1' pieSliceDepth='25' decimalPrecision='0' showNames='1'>";
    //Add all data
    strXML += "<set name='Soups' value='" + intSoups + "' />";
    strXML += "<set name='Salads' value='" + intSalads + "' />";
    strXML += "<set name='Sandwiches' value='" + intSandwiches + "' />";
    strXML += "<set name='Beverages' value='" + intBeverages + "' />";
    strXML += "<set name='Desserts' value='" + intDesserts + "' />";
    //Close <graph> element
    strXML += "</graph>";

    //Create the chart - Pie 3D Chart with data from strXML
    return FusionCharts.RenderChart("../FusionCharts/FCF_Pie3D.swf", "", strXML, "Sales", "600", "350", false, false);
  }
}

 

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

  • Include InfoSoftGlobal namespace.
  • Requesting data from the submitted form and storing it in local variables.
  • Creating an XML data document using string concatenation and storing it in strXML variable.
  • Creating a Pie 3D chart using RenderChart() function and passing strXML as dataXML for the chart. RenderChart()is called form the Page_Load event lsitener.

When you finally run the code, you'll see a chart as under: