Thursday 16 February 2012

Chart type selection from Dropdown and Adding new Series from Code behind using dot net 4.0

Design Code:


<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Selected="True">Select</asp:ListItem>
<asp:ListItem Value="Line">Line Chart</asp:ListItem>
<asp:ListItem Value="Area">Area Chart</asp:ListItem>
</asp:DropDownList>

<asp:Chart ID="Chart1" runat="server">
<Series>
<asp:Series Name="Series1">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>

C# 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;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    private static int counter = 1;
    SqlConnection con;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["CONN"].ConnectionString.ToString());
        
        // Collection to hold X Axis and Y Axis Values
        Dictionary<string,Decimal> xyValue = new Dictionary<string, Decimal>();

        // Call to chart Data method which return dictionary collection of demo data
        xyValue = chartData();

        // Binding collection key value pair to chart's default series
        Chart1.Series[0].Points.DataBindXY(xyValue.Keys, xyValue.Values);
    }
    protected Dictionary<string, Decimal> chartData()
    {
        // Object to generate random numbers to populate demo data
        //Random rnd = new Random(1);

        DataTable dtsale = FetchSale();     

        Dictionary<string, Decimal> values = new Dictionary<string, Decimal>();

        //Populating collection with random values
        for (int i = 0; i < dtsale.Rows.Count; i++)
        {
            values.Add(dtsale.Rows[i][0].ToString(),Convert.ToDecimal(dtsale.Rows[i][1].ToString()));
        }
        return values;
    }
    public DataTable FetchSale()
    {
        SqlDataAdapter da = new SqlDataAdapter("Select * from SALE", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!DropDownList1.SelectedValue.Equals("Select"))
        {
            Dictionary<string, Decimal> xyValue = new Dictionary<string, Decimal>();
            xyValue = chartData();
            
            // Creating new series with counter
            // so name will be unique when we add this series to chart series collection
            Series newSeries = new Series("newSeries" + counter);
            counter++;
            
            // Here we are using Enum.Parse to parse string from dropdown to     
            // SeriesChartType enumeration type
            newSeries.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), DropDownList1.SelectedValue.ToString());

            newSeries.Points.DataBindXY(xyValue.Keys, xyValue.Values);

            // Assigning chart area of new series to same as chart area of default series
            newSeries.ChartArea = Chart1.Series[0].ChartArea;

            // Adding chart to chart1's series collection
            Chart1.Series.Add(newSeries);
        }
    }
}

Page View:




No comments:

Post a Comment