Friday, August 31, 2012

How to use MsChart in Asp.Net Using c#

Here i am going to explain how to use MsCharts in Asp.Net using C#.Net  and DataSet's





<asp:Chart id="Chart1" runat="server" ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)" Width="500px" Height="400px" ImageType="Png" BackColor="#D3DFF0" Palette="BrightPastel" BorderDashStyle="Solid" BackSecondaryColor="White" BackGradientStyle="TopBottom" BorderWidth="2" BorderColor="26, 59, 105">
        <borderskin SkinStyle="Emboss"></borderskin>
            <Series>
                <asp:Series Name="Series1" ChartType="Column"          CustomProperties="DrawingStyle=Cylinder" BorderColor="180, 26, 59, 105"
                    Color="220, 224, 64, 10" YValueType="Double"
                ToolTip="#PERCENT{P}" MarkerStyle="Circle" Palette="Excel">              
                </asp:Series>
            </Series>          
            <ChartAreas>          
                <asp:ChartArea Name="ChartArea1">
                <Area3DStyle Enable3D="true" />              
                </asp:ChartArea>          
            </ChartAreas>
            <titles>
            <asp:Title ShadowColor="32, 0, 0, 0" Font="Trebuchet MS, 14.25pt, style=Bold" ShadowOffset="3" Text="Title Of the Chart" ForeColor="26, 59, 105"></asp:Title>
            </titles>
        </asp:Chart>


Code Behind :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace MSwebChart
{
    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ConnectionString);
        SqlDataAdapter da;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ChartBind();
            }
        }

        private void ChartBind()
        {
            try
            {
                DataSet ds = new DataSet();
                da = new SqlDataAdapter("select designation, count(designation) as Total from EMPTable group by designation", con);
                da.Fill(ds,"Chart");

                //Chart1.Legends["Default"].DockedToChartArea = "Default";

                Chart1.DataSource = ds;
                Chart1.Series["Series1"].XValueMember = "designation";
                Chart1.Series["Series1"].YValueMembers = "Total";
                Chart1.DataBind();
            }
            catch (Exception ex)
            {
                string ErrMsg = ex.Message;
            }
        }
    }
}
 

1 comment: