In this article we will learn how to create a chart for our ASP.NET application.
Here we will see how to draw a chart for our data in ASP.Net. We are using a third party control to draw our chart. This is a .dll file I found somewhere using Google search. It's not my .dll. This .dll file provides various types of charts such as LineChart, PieChart, CircleChart etc...
To prepare for a chart using this .dll file follow the steps given below.
Start a new web-site; give the name as ChartInAsp.Net. It will give you one Default.aspx and .cs file.
Download source code and copy the Webchart.dll file from the bin directory to some location. Now add a reference to this WebChart.dll file.
Now Register this dll in your Page directory such as shown bellow. And get the Chart control from this dll.
<%@ Register TagPrefix="Web" Namespace="WebChart" Assembly="WebChart" %>
<Web:ChartControl ID="ChartControl1" runat="Server" Height="366px"
Now move to Design view and see that the Chart control is created as shown below. Whenever we are creating this control the default chart is LineChart; you can change it to another type by setting it's type property in property window or in the .cs file. We will see how to change it again in the .cs file.
Now move to .cs file and Import the WebChart as shown below.
Now get your Data from database or prepare only Dataset or DataTable.
// Preparing Data Source For Chart Control
DataTable dt = new DataTable("Chart");
DataColumn dc = new DataColumn("Year", typeof(int));
DataColumn dc1 = new DataColumn("Aveg", typeof(double));
DataRow dr = dt.NewRow();
dr = 1995;
dr = 10.5;
DataRow dr1 = dt.NewRow();
dr1 = 2000;
dr1 = 11.5;
Now bind this data to the chart control. Here you can choose either LineChart, CircleChart or PieChart etcâ€¦ Here we will bind the data to our LineChart and color this chart as follows.
//Chart type you can change chart type here ex. pie chart,circle chart
LineChart chart = new LineChart();//Class instance for LineChart
chart.Fill.Color = Color.FromArgb(50, Color.SteelBlue);
chart.Line.Color = Color.SteelBlue;
chart.Line.Width = 2;
chart.Legend = "X Axis: Year.\nY Axis: Average";
//looping through datatable and adding to chart control
foreach (DataRow dr2 in dt.Rows)
chart.Data.Add(new ChartPoint(dr2["Year"].ToString(), (float)System.Convert.ToSingle(dr2["Aveg"])));
The ConfigureColors Method to color our chart.
private void ConfigureColors()
ChartControl1.Background.Color = Color.FromArgb(75, Color.SteelBlue);
ChartControl1.Background.Type = InteriorType.LinearGradient;
ChartControl1.Background.ForeColor = Color.SteelBlue;
ChartControl1.Background.EndPoint = new Point(500, 350);
ChartControl1.Legend.Position = LegendPosition.Bottom;
ChartControl1.Legend.Width = 40;
ChartControl1.YAxisFont.ForeColor = Color.SteelBlue;
ChartControl1.XAxisFont.ForeColor = Color.SteelBlue;
ChartControl1.ChartTitle.Text = "GRAPH EXAMPLE IN ASP.NET";
ChartControl1.ChartTitle.ForeColor = Color.White;
ChartControl1.Border.Color = Color.SteelBlue;
ChartControl1.BorderStyle = BorderStyle.Ridge;
Now run your application; it will show a nice chart Control to you. This dll will create a WebCharts folder in your application Directory for storing chart Images.
In this way you can prepare various types of charts for you ASP.Net application.