banner



How To Create Bar Chart In Asp Net C#

  • Updated date Apr 12, 2021
  • 51k
  • 6

In this article, we will learn about the Bar and StackedBar Charts of Asp.Net.

Background

ASP.Net Chart is a powerful control that creates interactive charts. Today we will discuss the Bar and StackedBar charts of ASP.Net. So let us start to learn about these chart type controls step-by-step.

Let us explain it with a practical example by creating a simple web application.

Step 1

Use the following script to create and insert data into the table for the chart:

  1. SET  ANSI_NULLS ON
  2. GO
  3. SET  QUOTED_IDENTIFIER ON
  4. GO
  5. SET  ANSI_PADDING ON
  6. GO
  7. CREATE TABLE  [dbo].[orderdet](
  8.     [id] [int ] IDENTITY(1,1) NOT NULL ,
  9.     [Month ] [ varchar ](50) NULL ,
  10.     [Orders] [int ] NULL ,
  11. CONSTRAINT  [PK_Order Table ] PRIMARY KEY  CLUSTERED
  12. (
  13.     [id]ASC
  14. )WITH  (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON  [ PRIMARY ]
  15. )ON  [ PRIMARY ]
  16. GO
  17. SET  ANSI_PADDING OFF
  18. GO
  19. SET  IDENTITY_INSERT [dbo].[orderdet] ON
  20. GO
  21. INSERT  [dbo].[orderdet] ([id], [ Month ], [Orders]) VALUES  (1, N 'Jan' , 0)
  22. GO
  23. INSERT  [dbo].[orderdet] ([id], [ Month ], [Orders]) VALUES  (2, N 'Feb' , 5)
  24. GO
  25. INSERT  [dbo].[orderdet] ([id], [ Month ], [Orders]) VALUES  (3, N 'March' , 20)
  26. GO
  27. INSERT  [dbo].[orderdet] ([id], [ Month ], [Orders]) VALUES  (4, N 'April' , 40)
  28. GO
  29. INSERT  [dbo].[orderdet] ([id], [ Month ], [Orders]) VALUES  (5, N 'May' , 15)
  30. GO
  31. INSERT  [dbo].[orderdet] ([id], [ Month ], [Orders]) VALUES  (6, N 'Jun' , 60)
  32. GO
  33. INSERT  [dbo].[orderdet] ([id], [ Month ], [Orders]) VALUES  (7, N 'July' , 75)
  34. GO
  35. INSERT  [dbo].[orderdet] ([id], [ Month ], [Orders]) VALUES  (8, N 'Aug' , 80)
  36. GO
  37. INSERT  [dbo].[orderdet] ([id], [ Month ], [Orders]) VALUES  (9, N 'Sep' , 85)
  38. GO
  39. INSERT  [dbo].[orderdet] ([id], [ Month ], [Orders]) VALUES  (10, N 'Oct' , 100)
  40. GO
  41. INSERT  [dbo].[orderdet] ([id], [ Month ], [Orders]) VALUES  (11, N 'Nov' , 2)
  42. GO
  43. INSERT  [dbo].[orderdet] ([id], [ Month ], [Orders]) VALUES  (12, N 'Dec' , 90)
  44. GO
  45. SET  IDENTITY_INSERT [dbo].[orderdet] OFF
  46. Go

After running the preceding script the records in the table will look as follows:

Step 2Create a Stored Procedure to fetch the records from the database.

  1. Create procedure  [dbo].[GetCharData]
  2. (
  3. @idint  = null
  4. )
  5. as
  6. begin
  7. Select Month ,Orders from  Orderdet
  8. End

Step 3

Create a Web Application. Create the website with:

  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New Project" - "C#" - "Empty Website" (to avoid adding a master page).
  3. Provide the project a name such as "UsingBarandSatckedbarChart" or another as you wish and specify the location.
  4. Then right-click on the Solution Explorer and select "Add New Item" then select Default.aspx page.
  5. Drag and Drop a Chart control from the ToolBox onto the Default.aspx page.

Now the Default.aspx source code will be as follows:

  1. < %@ Page Language = "C#" AutoEventWireup = "true" CodeFile = "Default.aspx.cs" Inherits = "_Default"  % >
  2. < %@ Register Assembly = "System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  3. Namespace = "System.Web.UI.DataVisualization.Charting" TagPrefix = "asp"  % >
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. < html xmlns = "http://www.w3.org/1999/xhtml" >
  6. < head runat = "server" >
  7. < title > Article by Vithal Wadje </ title >
  8. </ head >
  9. < body bgcolor = "blue" >
  10. < form id = "form1" runat = "server" >
  11. < h4 style = "color: White;" >
  12.         Article for C#Corner
  13. </ h4 >
  14. < asp:Chart ID = "Chart1" runat = "server" BackColor = "128, 64, 0" BackGradientStyle = "LeftRight"
  15. BorderlineWidth = "0" Height = "340px" Palette = "None" PaletteCustomColors = "64, 0, 0"
  16. Width = "360px" BorderlineColor = "192, 64, 0" >
  17. < Series >
  18. < asp:Series Name = "Series1" YValuesPerPoint = "6" >
  19. </ asp:Series >
  20. </ Series >
  21. < ChartAreas >
  22. < asp:ChartArea Name = "ChartArea1" >
  23. </ asp:ChartArea >
  24. </ ChartAreas >
  25. </ asp:Chart >
  26. </ form >
  27. </ body >
  28. </ html >

Step 4Create a method to bind the chart control.

Now open the default.aspx.cs page and create the following function named Bindchart to bind the Chart Control as in the following:

  1. private void  Bindchart()
  2. {
  3.     connection();
  4.     com =new  SqlCommand( "GetCharData" , con);
  5.     com.CommandType = CommandType.StoredProcedure;
  6.     SqlDataAdapter da =new  SqlDataAdapter(com);
  7.     DataSet ds =new  DataSet();
  8.     da.Fill(ds);
  9.     DataTable ChartData = ds.Tables[0];
  10. string [] XPointMember = new string [ChartData.Rows.Count];
  11. int [] YPointMember = new int [ChartData.Rows.Count];
  12. for  ( int  count = 0; count < ChartData.Rows.Count; count++)
  13.     {
  14.         XPointMember[count] = ChartData.Rows[count]["Month" ].ToString();
  15.         YPointMember[count] = Convert.ToInt32(ChartData.Rows[count]["Orders" ]);
  16.     }
  17.     Chart1.Series[0].Points.DataBindXY(XPointMember, YPointMember);
  18.     Chart1.Series[0].BorderWidth = 10;
  19.     Chart1.Series[0].ChartType = SeriesChartType.Bar;
  20.     Chart1.ChartAreas["ChartArea1" ].Area3DStyle.Enable3D = true ;
  21.     con.Close();
  22. }

Now, call the preceding function on page load as in the following:

  1. protected void  Page_Load( object  sender, EventArgs e)
  2. {
  3. if  (!IsPostBack)
  4.     {
  5.         Bindchart();
  6.     }
  7. }

The entire code of the default.aspx.cs page will look as follows:

  1. using  System;
  2. using  System.Collections.Generic;
  3. using  System.Linq;
  4. using  System.Web;
  5. using  System.Web.UI;
  6. using  System.Web.UI.WebControls;
  7. using  System.Data.SqlClient;
  8. using  System.Configuration;
  9. using  System.Data;
  10. using  System.Web.UI.DataVisualization.Charting;
  11. public  partial class  _Default : System.Web.UI.Page
  12. {
  13. private  SqlConnection con;
  14. private  SqlCommand com;
  15. private string  constr, query;
  16. private void  connection()
  17.     {
  18.         constr = ConfigurationManager.ConnectionStrings["getconn" ].ToString();
  19.         con =new  SqlConnection(constr);
  20.         con.Open();
  21.     }
  22. protected void  Page_Load( object  sender, EventArgs e)
  23.     {
  24. if  (!IsPostBack)
  25.         {
  26.             Bindchart();
  27.         }
  28.     }
  29. private void  Bindchart()
  30.     {
  31.         connection();
  32.         com =new  SqlCommand( "GetCharData" , con);
  33.         com.CommandType = CommandType.StoredProcedure;
  34.         SqlDataAdapter da =new  SqlDataAdapter(com);
  35.         DataSet ds =new  DataSet();
  36.         da.Fill(ds);
  37.         DataTable ChartData = ds.Tables[0];
  38. string [] XPointMember = new string [ChartData.Rows.Count];
  39. int [] YPointMember = new int [ChartData.Rows.Count];
  40. for  ( int  count = 0; count < ChartData.Rows.Count; count++)
  41.         {
  42.             XPointMember[count] = ChartData.Rows[count]["Month" ].ToString();
  43.             YPointMember[count] = Convert.ToInt32(ChartData.Rows[count]["Orders" ]);
  44.         }
  45.         Chart1.Series[0].Points.DataBindXY(XPointMember, YPointMember);
  46.         Chart1.Series[0].BorderWidth = 10;
  47.         Chart1.Series[0].ChartType = SeriesChartType.Bar;
  48.         Chart1.ChartAreas["ChartArea1" ].Area3DStyle.Enable3D = true ;
  49.         con.Close();
  50.     }
  51. }

Now, we have the entire logic to bind the chart from the database, let us run the application. The Bar Chart will look as follows:

Now hide the chart Grid Lines as:

  1. Chart1.ChartAreas["ChartArea1" ].AxisX.MajorGrid.Enabled = false ;
  2. Chart1.ChartAreas["ChartArea1" ].AxisY.MajorGrid.Enabled = false ;

Then the Bar chart will look as follows:

Now enable the 3D Style as in the following:

  1.  Chart1.ChartAreas["ChartArea1" ].Area3DStyle.Enable3D = true ;

Now 3D style Bar Chart will look as follows:

Now let us switch to a StackedBar chart as in the following:

  1.   Chart1.Series[0].ChartType = SeriesChartType.StackedBar;

Now run the application and the StackedBar type chart will look as follows:

Now hide the chart Grid Lines as:

  1.  Chart1.ChartAreas["ChartArea1" ].AxisX.MajorGrid.Enabled = false ;
  2.   Chart1.ChartAreas["ChartArea1" ].AxisY.MajorGrid.Enabled = false ;

Then the StackedBar chart will look as follows:

Now enable the 3D Style as in the following:

  1.  Chart1.ChartAreas["ChartArea1" ].Area3DStyle.Enable3D = true ;

Now the 3D style StackedBar Chart will look as follows:

Now from all the preceding explanations we saw how to create and use a Bar and StackedBar chart.

Notes

  • Download the Zip file from the attachment for the full source code of the application.
  • Change the connection string in the web.config file to specify your server location.

Summary

My next article explains another chart type of ASP.Net. I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.

How To Create Bar Chart In Asp Net C#

Source: https://www.c-sharpcorner.com/UploadFile/0c1bb2/bar-and-stackedbar-charts-in-Asp-Net/

Posted by: knighthattlem.blogspot.com

0 Response to "How To Create Bar Chart In Asp Net C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel