Step 1: Add Employee.xml to project
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
<Employee>
<ID>1</ID>
<EmployeeName>Srinath</EmployeeName>
<Salary>50000</Salary>
</Employee>
<Employee>
<ID>2</ID>
<EmployeeName>Naveen</EmployeeName>
<Salary>60000</Salary>
</Employee>
<Employee>
<ID>3</ID>
<EmployeeName>Krishna</EmployeeName>
<Salary>70000</Salary>
</Employee>
</Employees>
Step 2:Here is the HTML code in Webform1.aspx
<table style="font-family: Arial; border: 1px solid black">
<tr>
<td>
Sort By :
<asp:DropDownList ID="ddlSortBy"
AutoPostBack="true"
runat="server"
onselectedindexchanged="ddlSortBy_SelectedIndexChanged">
<asp:ListItem Text="Employee Name" Value="AxisLabel"></asp:ListItem>
<asp:ListItem Text="Salary" Value="Y"></asp:ListItem>
</asp:DropDownList>
</td>
<td>
Sort Direction :
<asp:DropDownList ID="ddlSortDirection"
AutoPostBack="true"
runat="server"
onselectedindexchanged="ddlSortDirection_SelectedIndexChanged">
<asp:ListItem Text="Ascending" Value="ASC"></asp:ListItem>
<asp:ListItem Text="Descending" Value="DESC"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<asp:Chart ID="Chart1" runat="server">
<Series>
<asp:Series Name="Series1">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
</td>
</tr>
</table>
Step 3: Write the code in Webform1.aspx.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
namespace Binding_XML_file_to_ASP.NET_Chart_Control
{
public partial class WebForm1 :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Use the default sort - Sort by TotalMarks
in Ascending order
GetChartData();
}
}
private void
GetChartData()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Employee.xml"));
Chart1.Series["Series1"].XValueMember
= "EmployeeName";
Chart1.Series["Series1"].YValueMembers
= "Salary";
Chart1.DataSource =ds;
Chart1.DataBind();
// Sorting
if (ddlSortDirection.SelectedValue == "ASC")
{
Chart1.Series["Series1"].Sort(PointSortOrder.Ascending,
ddlSortBy.SelectedValue);
// OR
//
Chart1.DataManipulator.Sort(PointSortOrder.Ascending,
//
ddlSortBy.SelectedValue, "Series1");
}
else
{
Chart1.Series["Series1"].Sort(PointSortOrder.Descending,
ddlSortBy.SelectedValue);
// OR
// Chart1.DataManipulator.Sort(PointSortOrder.Descending,
// ddlSortBy.SelectedValue,
"Series1");
}
}
protected void
ddlSortBy_SelectedIndexChanged(object sender, EventArgs e)
{
GetChartData();
}
protected void
ddlSortDirection_SelectedIndexChanged(object
sender, EventArgs e)
{
GetChartData();
}
}
}
Run the Application
No comments:
Post a Comment