Chart Representation of Data in ASP.Net Web Pages 2

Introduction

Today, we'll learn about the data presentation in the graphical format. The graphical format representation of data is done by Chart Helper in Microsoft WebMatrix using ASP.NET Web Pages 2. If you are not familiar with WebMatrix then refer to Create Website using WebMatrix.

The Chart helper can render an image that displays the data in various chart types. There are 30 types of charts, like area chart, bar chart, column chart, stock chart and so on available to represent the data in graphical format. You probably are already familiar with them; they are also used in Microsoft Excel and other tools.

So, let's proceed with the following:

  • Create a Chart
  • Display Chart in a Webpage
  • Style the Chart

Creating Chart

The data to display in graphical form can be stored in the array, the database or the XML file.

Chart Representation Using Array

Now, here we use an array to represent the data using the following procedure.

Step 1: Create a new folder named Chart Representation in the website.

Step 2: Create a new CSHTML File in the folder.

Creating New File in Folder

Enter the name as ArrayChartPage.

Creating Cshtml File

Step 3: Now enter the following code in the code block:

  1. @{  
  2.     var ChartSample=new Chart(width: 500, height: 300)  
  3.         .AddTitle("Chart Sample")  
  4.         .AddSeries(  
  5.             name: "Cricketers",  
  6.             xValue: new[] {"Sachin""Ricky""Wasim","Brian""Saurav"},  
  7.             yValues: new[]{"10""8""9""6""7"}).Write();  
  8. } 

In the code above the chart is created with the mentioned width and height. The title is specified with AddTitle and we use AddSeries to add data. The xValue and yValue are the parameters. The Write method is used to render the chart with its default column chart because we didn't specify the chart type.

Step 4: Now launch the page in the browser and you'll see the Column chart representation of your data: 

Array Sample Chart

Chart Representation Using Database

Here we use a database to represent the data. We can run a query to fetch the data from the database and represent the results of data in the graphical format. Use the following procedure to do that.

Step 1: Add a folder named App_Data to work with the database, if the folder does not already exist. You can refer to Working with Database.

Step 2: Add a new file named DataChartPage in the Chart Representation folder and enter the following code in the code block:

  1. @{  
  2.    var CricDb= Database.Open("Cricketer Site");  
  3.    var DbQuery = CricDb.Query("Select Name, ODI from CricketerStatistics");  
  4.    var DataChart = new Chart(width: 500, height: 300)  
  5.                        .AddTitle("Cricketer Statistics")  
  6.                        .AddSeries("Cricketers",  
  7.                          xValue: DbQuery, xField: "Name",  
  8.                          yValues: DbQuery, yFields: "ODI")  
  9.                        .Write();  
  10. } 

In the code above the Cricketer Site is the database and the data is retrieved from the database to represent the CricketerStatistics table data. The code runs a query to fetch the Name and ODI details of each Cricketer. Next the chart is created with the AddSeries method. This is very flexible because we can specify the chart type and data more explicitly.

We can also use the DataBindTable method to bind the data as in the code shown below:

  1. @{  
  2.   var CricDb= Database.Open("Cricketer Site");  
  3.   var DbQuery = CricDb.Query("Select Name, ODI from CricketerStatistics");  
  4.   var DataChart = new Chart(width: 500, height: 300)  
  5.                        .AddTitle("Cricketer Statistics")  
  6.                        .DataBindTable(dataSource: DbQuery, xField: "Name")  
  7.                        .Write();  
  8. } 

But the same chart is rendered with the both code.

Step 3: Launch the page in the browser: 

Data Representation Chart

Chart Representation Using XML

In this section, we use XML to represent the data in the graphical format. This requires that the XML file also have a schema file (.xsd file) that describes the XML structure. Use the following procedure to do that.

Step 1: Create a new XML file named XmlData.xml in the App_Data folder.

Create XML file in WebMatrix

Step 2: Enter the following code to it:

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <NewDataSet xmlns="http://tempuri.org/XMLData.xsd">  
  3.    <Cricketer>  
  4.        <Name>Sachin</Name>  
  5.        <OdiRuns>18500</OdiRuns>  
  6.        <TestRuns>15550</TestRuns>  
  7.    </Cricketer>  
  8.    <Cricketer>  
  9.        <Name>Ricky</Name>  
  10.        <OdiRuns>13500</OdiRuns>  
  11.        <TestRuns>12550</TestRuns>  
  12.    </Cricketer>  
  13.    <Cricketer>  
  14.        <Name>Jacques</Name>  
  15.        <OdiRuns>12500</OdiRuns>  
  16.        <TestRuns>13550</TestRuns>  
  17.    </Cricketer>  
  18.    <Cricketer>  
  19.        <Name>Saurav</Name>  
  20.        <OdiRuns>10500</OdiRuns>  
  21.        <TestRuns>11550</TestRuns>  
  22.    </Cricketer>  
  23. </NewDataSet> 

Step 3: Now again create a new XML file named XmlData.xsd in the App_Data folder.

Step 4: Enter the following code into it:

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <xs:schema id="NewDataSet" targetNamespace="http://tempuri.org/XMLData.xsd"  
  3.           xmlns:mstns="http://tempuri.org/XMLData.xsd"  
  4.           xmlns="http://tempuri.org/XMLData.xsd"  
  5.           xmlns:xs="http://www.w3.org/2001/XMLSchema"  
  6.           xmlns:msdata="urn:schemas-micrososft-com:xml-msdata"  
  7.           attributeFormDefault="qualified"  
  8.           elementFormDefault="qualified">  
  9.    <xs:element name="NewDataSet" msdata:IsDataSet="true"  
  10.                msdata:EnforceConstraints="False">  
  11.        <xs:complexType>  
  12.            <xs:choice maxOccurs="unbounded">  
  13.                <xs:element name="Cricketer">  
  14.                    <xs:complexType>  
  15.                        <xs:sequence>  
  16.                            <xs:element name="Name"  
  17.                                        type="xs:string"  
  18.                                        minOccurs="0" />  
  19.                            <xs:element name="TestRuns"  
  20.                                        type="xs:int"  
  21.                                        minOccurs="0" />  
  22.                        </xs:sequence>  
  23.                    </xs:complexType>  
  24.                </xs:element>  
  25.            </xs:choice>  
  26.        </xs:complexType>  
  27.    </xs:element>  
  28. </xs:schema> 

Step 5: Add a new file named XmlChartPage in the Chart Representation folder and enter the following code in the code block:

  1. @using System.Data;  
  2. @{  
  3.    var DbSet= new DataSet();  
  4.    DbSet.ReadXmlSchema(Server.MapPath("~/App_Data/XMLData.xsd"));  
  5.    DbSet.ReadXml(Server.MapPath("~/App_Data/XMLData.xml"));  
  6.    var DbView = new DataView(DbSet.Tables[0]);  
  7.    var XmlChart= new Chart(width: 500, height: 300)  
  8.                        .AddTitle("Test Runs")  
  9.                        .AddSeries("Cricketer", chartType: "Column",  
  10.                        xValue: DbView, xField: "Name",  
  11.                        yValues: DbView, yFields: "TestRuns")  
  12.                        .Write();  
  13. } 

In the code above the DataSet object is created to fetch the data from the XML file and organize it depending on the data in the schema file. Next the code uses the DataView object to bind the data and display it in the Pie Chart.

Step 6: Launch the page in the browser.

Pie Representation Chart

Displaying Chart in Webpage

In this section we'll create a page to display the chart representation of data.

Step 1: Create a new CSHTML file named ChartPage in the folder and add the following code:

  1. <html lang="en">  
  2.    <head>  
  3.        <meta charset="utf-8" />  
  4.        <title></title>  
  5.    </head>  
  6.    <body>  
  7.        <h1>Chart Example</h1>  
  8.         <p>The following chart is generated by the   
  9.         <em>DataChartPage.cshtml</em> file:</p>  
  10.         <p><img src="DataChartPage.cshtml" alt="Cricketers"/> </p>  
  11.    </body>  
  12. </html> 

Step 2: Launch the page in the browser: 

Chart Page in WebMatrix

Styling Chart

Use the following procedure to represent the data by customizing it.

Step 1: Open the DataChartPage.cshtml page and modify the code with the highlighted code below: 

varDataChart=newChart(width:500,height:300,theme:ChartTheme.Blue)

Step 2: Now launch the page in the browser: 

Data Representation Chart with theme

Summary

This article will help you to learn how to represent the data in graphical format. You can also use a different data source to display it with the chart representation. Thanks for reading.


Similar Articles