Step 1 : From the Chart1 control on WebForm1.aspx, remove element and all the elements. The HTML of Chart1 control should now be as shown below.
Step 2 : Modify the code in the code-behind file as shown below
<table style="border: 1px solid black; font-family: Arial">
<tr>
<td>
<b>Select Chart Type:</b>
</td>
<td>
<asp:DropDownList ID="DropDownList1"
AutoPostBack="true"
runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Chart ID="Chart1" runat="server">
<Titles><asp:Title Text="Student Total
Marks" ></asp:Title></Titles>
<series>
<asp:Series Name="Series1"
ChartArea="ChartArea1"
ChartType="Pie">
</asp:Series>
</series>
<chartareas>
<asp:ChartArea Name="ChartArea1">
<AxisX Title="Student
Names"></AxisX>
<AxisY Title="Total
Marks"></AxisY>
</asp:ChartArea>
</chartareas>
</asp:Chart>
</td>
</tr>
</table>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;
namespace Settingcharttypepropertydynamically
{
public partial class WebForm1 :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetChartData();
GetChartTypes();
}
}
private void GetChartData()
{
Series xseries = Chart1.Series["Series1"];
xseries.Points.AddXY("Srinath",
200);
xseries.Points.AddXY("Naveen",
300);
xseries.Points.AddXY("Krishna",
400);
}
private void
GetChartTypes()
{
foreach (int
chartType in Enum.GetValues(typeof(SeriesChartType)))
{
ListItem li = new ListItem(Enum.GetName(typeof(SeriesChartType),
chartType), chartType.ToString());
DropDownList1.Items.Add(li);
}
}
protected void
DropDownList1_SelectedIndexChanged(object
sender, EventArgs e)
{
GetChartData();
this.Chart1.Series["Series1"].ChartType
= (SeriesChartType)Enum.Parse(
typeof(SeriesChartType),
DropDownList1.SelectedValue);
}
}
}
Run the Application
No comments:
Post a Comment