setting ChartType property of asp.net chart control dynamically

There are 2 ways to set the ChartType property

1. Declaratively in the HTML using ChartType attribute.


<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">
            <Points>
            <asp:DataPoint AxisLabel="Srinath" YValues="200" />
             <asp:DataPoint AxisLabel="Naveen" YValues="300" />
              <asp:DataPoint AxisLabel="Krishna" YValues="400" />
           
           
           
            </Points>
            </asp:Series>
        </series>
        <chartareas>
            <asp:ChartArea Name="ChartArea1">
            <AxisX Title="Student Names"></AxisX>
            <AxisY Title="Total Marks"></AxisY>
            </asp:ChartArea>
        </chartareas>
    </asp:Chart>


2. Programatically in code. We want to display all the different ChartTypes available in a DropDownList. When the user selects an option from the DropDownList we want to set the selected chart type as the value for the ChartType property dynamically in code





Modify the HTML on WebForm1.aspx as shown below. Notice that we have included a DropDownList to display the different ChartTypes that are available. We have also set the AutoPostBack property of the DropDownList to True.


<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">
            <Points>
            <asp:DataPoint AxisLabel="Srinath" YValues="200" />
             <asp:DataPoint AxisLabel="Naveen" YValues="300" />
              <asp:DataPoint AxisLabel="Krishna" YValues="400" />
           
           
           
            </Points>
            </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>

WebForm1.aspx.cs code


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)
            {
                GetChartTypes();
            }

        }
        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)
        {
            this.Chart1.Series["Series1"].ChartType = (SeriesChartType)Enum.Parse(
             typeof(SeriesChartType), DropDownList1.SelectedValue);
        }
    }
}

No comments:

Post a Comment