Monday, 14 May 2012

ASP.NET 4.0 CHART CONTROL DYNAMIC

STEP1) Create ASP.NET WEB APPLICTAION USING .NET 4.0 FRAMERWORK

STEP 2)  DRAG & DROP CHART CONTROL

BEFORE THAT

create database table called "sales data" 

USE [test]
GO


/****** Object:  Table [dbo].[Sales Data]    Script Date: 05/15/2012 00:40:34 ******/
SET ANSI_NULLS ON
GO


SET QUOTED_IDENTIFIER ON
GO


CREATE TABLE [dbo].[Sales Data](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Sales Man] [nchar](20) NOT NULL,
[2005] [int] NOT NULL,
[2006] [int] NOT NULL,
[2007] [int] NOT NULL,
 CONSTRAINT [PK_Sales Data] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


GO

INSET DATA INTO TABLE


INSERT INTO [Sales Data]([Sales Man],[2005],[2006],[2007]) VALUES('CAROL',2000,3000,4000)
INSERT INTO [Sales Data]([Sales Man],[2005],[2006],[2007]) VALUES('JAMES',2445,435435,55656)
INSERT INTO [Sales Data]([Sales Man],[2005],[2006],[2007]) VALUES('PETER',25555,34444,46456)
INSERT INTO [Sales Data]([Sales Man],[2005],[2006],[2007]) VALUES('NANCY',20000,30000,40000) 
......

create stored procedure 

USE [test]
GO


/****** Object:  StoredProcedure [dbo].[getsalesdata]    Script Date: 05/15/2012 00:42:26 ******/
SET ANSI_NULLS ON
GO


SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[getsalesdata]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;


    -- Insert statements for procedure here
SELECT * from [sales data]
END


GO




test the stored procedure
exec getsalesdata


in ASPX  PAGE ADD FOLLOWING CODE 



    protected void Page_Load(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;Integrated Security=True;trusted_connection=yes"))
        {
            using (SqlDataAdapter ad = new SqlDataAdapter("exec getsalesdata", conn))
            {
                using (DataSet ds = new DataSet())
                {
                    ad.Fill(ds);


//ADD  3 CHART SERIES ,  BECAUSE WE HAVE 3 VALUES TO REPRESENT 1.2005,2.2006,3.2007 YR



                    System.Web.UI.DataVisualization.Charting.Series
series1 = new System.Web.UI.DataVisualization.Charting.Series();
series1.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
series1.XAxisType= System.Web.UI.DataVisualization.Charting.AxisType.Primary;
                    series1.XValueMember="Sales Man";
series1.YAxisType=System.Web.UI.DataVisualization.Charting.AxisType.Secondary;
                    series1.YValueMembers="2005";
                    series1.ChartArea = "ChartArea1";
                    Chart1.Series.Add(series1);
System.Web.UI.DataVisualization.Charting.Series
series2 = new System.Web.UI.DataVisualization.Charting.Series();
                    series2.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
                    series2.XAxisType = System.Web.UI.DataVisualization.Charting.AxisType.Primary;
                    series2.XValueMember = "Sales Man";
                    series2.YAxisType = System.Web.UI.DataVisualization.Charting.AxisType.Secondary;
                    series2.YValueMembers = "2006";
                    series1.ChartArea = "ChartArea1";
                    Chart1.Series.Add(series2);


                    System.Web.UI.DataVisualization.Charting.Series
series3 = new System.Web.UI.DataVisualization.Charting.Series();
                    series3.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
                    series3.XAxisType = System.Web.UI.DataVisualization.Charting.AxisType.Primary;
                    series3.XValueMember = "Sales Man";
                    series3.YAxisType = System.Web.UI.DataVisualization.Charting.AxisType.Secondary;
                    series3.YValueMembers = "2007";
                    series3.ChartArea = "ChartArea1";
                    Chart1.Series.Add(series3);


                    Chart1.DataSource = ds.Tables[0];
                    Chart1.DataBind();








                }
            }
        }
    }





OUTPUT