How To Use And Bind Chart Control In ASP.NET With XML Data

We will use XML to provide the data for binding in Chart control and the approach is simple. We will take the XML file, where we store some specific student course resulta in a cumulative manner with Pie chart and then on the button click, we will display each student's individual result from another XML data source. For simplicity, I had normalized the whole data in an XML, so that it would be a simple tutorial for me as well as for others, who are still in the learning process.

INITIAL CHAMBER

Step 1

Open your Visual Studio 2010 and create an empty Website. Give a suitable name [data_demo].

Step 2

In Solution Explorer, you get your empty Website, Add a Web form and XML file by going, as shown below.

For Web Form

data_demo (Your Empty Website) -> Right click Add New Item -> Web Form. Name it as data_demo.aspx.

For XML

data_demo (Your Empty Website) -> Right click Add New Item -> XML. Name it as studentdetails.xml

Studentdetails.xml 

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2.     <persondetails>    
  3.         <person>    
  4.             <id>01</id>    
  5.             <name>Nilesh</name>    
  6.             <per>85</per>    
  7.         </person>    
  8.         <person>    
  9.             <id>02</id>    
  10.             <name>Purnima</name>    
  11.             <per>90</per>    
  12.         </person>    
  13.         <person>    
  14.             <id>03</id>    
  15.             <name>Chandni</name>    
  16.             <per>89</per>    
  17.         </person>    
  18.         <person>    
  19.             <id>04</id>    
  20.             <name>Rinku</name>    
  21.             <per>78</per>    
  22.         </person>    
  23.         <person>    
  24.             <id>05</id>    
  25.             <name>Nilu</name>    
  26.             <per>60</per>    
  27.         </person>      
  28.     </persondetails>     

IndividualStudentdetails.xml 

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2.     <StudentDetailsMarks>    
  3.         <subjects>  
  4.   
  5.           <subject>JAVA</subject>  
  6.           <marks>82</marks>  
  7.   
  8.         </subjects>  
  9.   
  10.       <subjects>  
  11.         <subject>Asp.Net</subject>  
  12.         <marks>85</marks>  
  13.       </subjects>  
  14.   
  15.   
  16.       <subjects>  
  17.         <subject>Php</subject>  
  18.         <marks>78</marks>  
  19.       </subjects>  
  20.           
  21.         
  22.       <subjects>  
  23.           <subject>Android</subject>  
  24.             <marks>70</marks>    
  25.         </subjects>  
  26.           
  27.         
  28.     </StudentDetailsMarks>    

DESIGN CHAMBER

Step 4

Open your data_demo.aspx file to create the design. We will create an HTML table and place our chartcontrol there. Afterwards, we will drag the button and 5 other chartcontrols. Your design will look, as shown below.

asp.net

CODE CHAMBER

Step 5

Open data_demo.aspx.cs file to write the code for binding our chart control with XML data source. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Web.UI.DataVisualization.Charting;  
  9.   
  10. public partial class chart : System.Web.UI.Page  
  11. {  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.   
  15.         DataSet ds = new DataSet();  
  16.   
  17.         ds.ReadXml(Server.MapPath("~/XMLFile.xml"));  
  18.         Chart1.Titles.Add("Computer Result Analysis");  
  19.   
  20.          
  21.         Chart1.Series["Series1"].XValueMember = "name";  
  22.         Chart1.Series["Series1"].YValueMembers = "per";  
  23.           
  24.         Chart1.DataSource = ds;  
  25.         Chart1.DataBind();  
  26.          
  27.         Chart1.Series[0].IsValueShownAsLabel = true;  
  28.         Chart1.Series[0].Label = "#PERCENT";  
  29.         Chart1.Series[0].LegendText = "#AXISLABEL";  
  30.   
  31.         this.Chart1.Legends.Add("Legend1");  
  32.         this.Chart1.Legends[0].Enabled = true;  
  33.         this.Chart1.Legends[0].Docking = Docking.Bottom;  
  34.         this.Chart1.Legends[0].Alignment = System.Drawing.StringAlignment.Center;  
  35.         this.Chart1.Series[0].LegendText = "#VALX (#PERCENT)";  
  36.   
  37.   
  38.   
  39.     }  
  40.   
  41.     protected void Button1_Click(object sender, EventArgs e)  
  42.     {  
  43.         DataSet ds1 = new DataSet();  
  44.         ds1.ReadXml(Server.MapPath("~/Nilesh.xml"));  
  45.         Chart2.Titles.Add("Nilesh Result");  
  46.         Chart2.Series["Series1"].XValueMember = "subject";  
  47.         Chart2.Series["Series1"].YValueMembers = "marks";  
  48.         Chart2.DataSource = ds1;  
  49.         Chart2.DataBind();  
  50.   
  51.   
  52.         DataSet ds2 = new DataSet();  
  53.         ds2.ReadXml(Server.MapPath("~/Purnima.xml"));  
  54.         Chart3.Titles.Add("Purnima Result");  
  55.         Chart3.Series["Series2"].XValueMember = "subject";  
  56.         Chart3.Series["Series2"].YValueMembers = "marks";  
  57.         Chart3.DataSource = ds2;  
  58.         Chart3.DataBind();  
  59.   
  60.   
  61.         DataSet ds3 = new DataSet();  
  62.         ds3.ReadXml(Server.MapPath("~/Rinku.xml"));  
  63.         Chart4.Titles.Add("Rinku Result");  
  64.         Chart4.Series["Series1"].XValueMember = "subject";  
  65.         Chart4.Series["Series1"].YValueMembers = "marks";  
  66.         Chart4.DataSource = ds3;  
  67.         Chart4.DataBind();  
  68.   
  69.   
  70.         DataSet ds4 = new DataSet();  
  71.         ds4.ReadXml(Server.MapPath("~/Nilu.xml"));  
  72.         Chart5.Titles.Add("Nilu Result");  
  73.         Chart5.Series["Series1"].XValueMember = "subject";  
  74.         Chart5.Series["Series1"].YValueMembers = "marks";  
  75.         Chart5.DataSource = ds4;  
  76.         Chart5.DataBind();  
  77.   
  78.   
  79.         DataSet ds5 = new DataSet();  
  80.         ds5.ReadXml(Server.MapPath("~/Chandni.xml"));  
  81.         Chart6.Titles.Add("Chandni Result");  
  82.         Chart6.Series["Series1"].XValueMember = "subject";  
  83.         Chart6.Series["Series1"].YValueMembers = "marks";  
  84.         Chart6.DataSource = ds5;  
  85.         Chart6.DataBind();  
  86.   
  87.     }  
  88. }   

We are not going for hardcore coding in an XML, as the code for xmldata source will be messy for the new comers. We will see that part too in our next tutorials, but for the beginners, this is a way. Here, we had taken too much of XML due to which I had said that I had taken a bit normalized data.

There might be a problem with Pie chart to show the percentage and the label. For this, you can apply the code given below.

  1. this.Chart1.Legends.Add("Legend1");  
  2. this.Chart1.Legends[0].Enabled = true;  
  3. this.Chart1.Legends[0].Docking = Docking.Bottom;  
  4. this.Chart1.Legends[0].Alignment = System.Drawing.StringAlignment.Center;  
  5. this.Chart1.Series[0].LegendText = "#VALX (#PERCENT)";  

OUTPUT CHAMBER
asp.net

This is the overall result for the branch, where you can see the percentage of each student. Now, we will see those student results individually by pressing the button.

asp.net

Hope, you like it. Have a good Day. Thank you for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all