FusionCharts and ASP.NET > Using dataURL method |
Here, we'll be using ASP.NET as our choice of scripting language and create the dynamic "Top 5 Countries" 3D column chart, with MS SQL Server as our database, using the dataURL method. We'll be using VB.NET as our choice of .NET language. If you want to use C# as your choice of language, you can download our FusionCharts Blueprint application in C# from our website (http://www.InfoSoftGlobal.com/FusionCharts) to learn more. Please do read the section "Using with scripts and database", if you've not already read, before reading this section. As we had earlier seen, the dataURL method requires two pages:
The above two are present in the downloads under the folder FusionCharts2_3 > Sample Code > DBExamples > ASP_NET > dataURL. The page Data.aspx is responsible for relaying virtual XML documents to the FusionCharts embedded in Chart.aspx. You should note that Data.aspxdoes NOT write XML data documents to the server disk physically. It relays the XML data document virtually (i.e., no XML files are created and saved on the server). The code for this page can be reproduced as under: |
<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SQLClient" %> <script runat="server"> Sub Page_Load(obj as Object, e as EventArgs) 'Container for the XML Data Dim strFCXMLData as String 'Request the year passed to this page Dim strYear as String strYear = Request.QueryString("Year") 'Establish a connection Dim DS As DataSet Dim MyConnection As SqlConnection Dim MyCommand As SqlDataAdapter 'Get Top 5 Countries the database. MyConnection = New SqlConnection(ConfigurationSettings.AppSettings("appDSN")) MyCommand = New SqlDataAdapter("SELECT TOP 5 Country, SUM(ExtendedPrice) As Total, COUNT(DISTINCT OrderID) As orderNumber FROM Invoices WHERE YEAR(OrderDate)=" & strYear & " GROUP BY Country ORDER BY SUM(ExtendedPrice) DESC", MyConnection) 'Fill
the dataset |
In the above code, we're doing the following:
When you try viewing this page individually in the browser (in the format http://localhost/...Path.../DBExamples/ASP/dataURL/Data.aspx?year=1996), you'll get the following XML output. You should always make sure, that while using dataURL method, your dataURL script should output just XML data and no HTML tags. |
<graph caption='Top 5 Countries for the Year 1996' shownames='1'
showvalues='0' decimalPrecision='0' numberPrefix='$'> <set name='USA' value='38105.68' color='AFD8F8'/> <set name='Germany' value='35407.15' color='F6BD0F'/> <set name='Austria' value='25601.34' color='8BBA00'/> <set name='Brazil' value='20148.82' color='A66EDD'/> <set name='France' value='17372.76' color='F984A1'/> </graph> |
This is exactly the XML that we need for our 3D Column Chart. So, let's now shift our attention to Chart.aspx and see the code it contains to attain the following tasks:
Chart.aspx contains the following code: |
<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SQLClient" %> <script runat="server"> 'Initialize a variable that will store FusionCharts dataURL string Dim strFCdataURL as String Sub Page_Load(obj as Object, e as EventArgs) 'Set data page as Data.aspx - we'll add the year to it later strFCdataURL = "Data.aspx?Year=" 'If coming for the first time, fill the select (drop down list) If Not IsPostBack Then 'Establish a connection Dim DS As DataSet Dim MyConnection As SqlConnection Dim MyCommand As SqlDataAdapter 'Get the list of years from the database. MyConnection = New SqlConnection(ConfigurationSettings.AppSettings("appDSN")) MyCommand = New SqlDataAdapter("SELECT DISTINCT YEAR(OrderDate) As Year FROM Orders ORDER BY 1", MyConnection) 'Fill
the dataset |
Here, in the above code, we're doing the following:
When you now see Chart.aspx in your browser, you'll get the following output: |