Spline and Line Chart in ASP.Net

Background

Sometimes we need to show data in a Line Chart, so by considering that requirement and to introduce the ASP.Net Line and Spline Chart controls I have decided to write this article.

Let us learn about the ASP.Net chart types Line and Spline that provide a powerful UI and design quality. So let us start to learn about these chart type controls step-by-step. All the charts are in the System.Web.UI.DataVisualization.Charting namespace.
 
Chart data is represented using the following points:
  1. X Axis: the horizontal line of the chart termed the X axis
  2. Y Axis: the vertical line of the chart termed the Y axis
Step 1: Create a table for the chart data
 
Now before creating the application, let us create a table named orderdet in a database from where we show the records in a chart, the table has the following script (shown in the following image):
  1. USE [CBS]    
  2. GO    
  3.     
  4. /****** Object:  Table [dbo].[QuarterwiseSale]    Script Date: 19-12-2014 00:49:50 ******/    
  5. SET ANSI_NULLS ON    
  6. GO    
  7.     
  8. SET QUOTED_IDENTIFIER ON    
  9. GO    
  10.     
  11. SET ANSI_PADDING ON    
  12. GO    
  13.     
  14. CREATE TABLE [dbo].[QuarterwiseSale](    
  15.     [id] [int] IDENTITY(1,1) NOT NULL,    
  16.     [Quarter] [varchar](50) NULL,    
  17.     [SalesValue] [money] NULL,    
  18.  CONSTRAINT [PK_QuarterwiseSale] PRIMARY KEY CLUSTERED     
  19. (    
  20.     [id] ASC    
  21. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]    
  22. ON [PRIMARY]    
  23.     
  24. GO    
  25.     
  26. SET ANSI_PADDING OFF    
  27. GO 
Now the design will look such as:
 
 
Now insert some records as listed in the following image into the table:
 
 
I hope you have the same type of table and records as above.
 
Step 2: Create Web Application

Now create the project with:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".

  2. "File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).

  3. Provide the project a name such as LineAndSpline or another as you wish and specify the location.

  4. Then right-click on Solution Explorer and select "Add New Item" then select Default.aspx page.

  5. Drag and Drop Chart control from 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.   
  3. <%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"  
  4.     Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  6. <html xmlns="http://www.w3.org/1999/xhtml">  
  7. <head runat="server">  
  8.     <title>Article by Vithal Wadje</title>  
  9. </head>  
  10. <body bgcolor="blue">  
  11.     <form id="form1" runat="server">  
  12.     <h4 style="color: White;">  
  13.         Article for C#Corner  
  14.     </h4>  
  15.     <asp:Chart ID="Chart1" runat="server" BackColor="64, 0, 0"   
  16.         BackGradientStyle="LeftRight" Height="350px" Palette="None"   
  17.         PaletteCustomColors="192, 0, 0" Width="350px">  
  18.         <Series>  
  19.             <asp:Series Name="Series1">  
  20.             </asp:Series>  
  21.         </Series>  
  22.         <ChartAreas>  
  23.             <asp:ChartArea Name="ChartArea1" >  
  24.             </asp:ChartArea>  
  25.         </ChartAreas>  
  26.         <BorderSkin BackColor="" PageColor="192, 64, 0" />  
  27.     </asp:Chart>  
  28.   
  29.   
  30.     </form>  
  31. </body>  
  32. </html> 
Step 3: Create method to bind the chart control
 
The Important and complex thing is to bind the chart control properly, because charts exist in a nested series so it's a problem to identify how to set the values.
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.     query = "select *from Orderdet";//not recommended this i have written just for example,write stored procedure for security    
  5.     com = new SqlCommand(query, con);    
  6.     SqlDataAdapter da = new SqlDataAdapter(query, con);    
  7.     DataSet ds = new DataSet();    
  8.     da.Fill(ds);  
  9.     DataTable ChartData=ds.Tables[0];  
  10.     //storing total rows count to loop on each Record    
  11.     string[] XPointMember = new string[ChartData.Rows.Count];    
  12.     int[] YPointMember = new int[ChartData.Rows.Count];  
  13.     for (int count = 0; count < ChartData.Rows.Count;count++ )    
  14.     {    
  15.         //storing Values for X axis    
  16.         XPointMember[count] = ChartData.Rows[count]["Month"].ToString();    
  17.         //storing values for Y Axis    
  18.         YPointMember[count] = Convert.ToInt32(ChartData.Rows[count]["Orders"]);    
  19.     }    
  20.     //binding chart control    
  21.     Chart1.Series[0].Points.DataBindXY(XPointMember, YPointMember);  
  22.     //Setting width of line    
  23.     Chart1.Series[0].BorderWidth = 1;    
  24.     //setting Chart type     
  25.     Chart1.Series[0].ChartType = SeriesChartType.Line ;    
  26.     // Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;    
  27.     // Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false;    
  28.     //  Chart1.Series[0].ChartType = SeriesChartType.Spline;    
  29.     //Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;   
  30.     con.Close();  
  31. }
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.   
  12. public partial class _Default : System.Web.UI.Page  
  13. {  
  14.     private SqlConnection con;  
  15.     private SqlCommand com;  
  16.     private string constr, query;  
  17.     private void connection()  
  18.     {  
  19.         constr = ConfigurationManager.ConnectionStrings["getconn"].ToString();  
  20.         con = new SqlConnection(constr);  
  21.         con.Open();  
  22.   
  23.     }  
  24.     protected void Page_Load(object sender, EventArgs e)  
  25.     {  
  26.         if (!IsPostBack)  
  27.         {  
  28.             Bindchart();  
  29.   
  30.         }  
  31.     }  
  32.     private void Bindchart()  
  33.     {  
  34.         connection();  
  35.         query = "select *from Orderdet";//not recommended this i have written just for example,write stored procedure for security  
  36.         com = new SqlCommand(query, con);  
  37.         SqlDataAdapter da = new SqlDataAdapter(query, con);  
  38.         DataSet ds = new DataSet();  
  39.         da.Fill(ds);  
  40.        
  41.         DataTable ChartData=ds.Tables[0];  
  42.   
  43.         //storing total rows count to loop on each Record  
  44.         string[] XPointMember = new string[ChartData.Rows.Count];  
  45.         int[] YPointMember = new int[ChartData.Rows.Count];  
  46.   
  47.         for (int count = 0; count < ChartData.Rows.Count;count++ )  
  48.         {  
  49.             //storing Values for X axis  
  50.             XPointMember[count] = ChartData.Rows[count]["Month"].ToString();  
  51.             //storing values for Y Axis  
  52.             YPointMember[count] = Convert.ToInt32(ChartData.Rows[count]["Orders"]);
  53.         }  
  54.         //binding chart control  
  55.         Chart1.Series[0].Points.DataBindXY(XPointMember, YPointMember);  
  56.   
  57.         //Setting width of line  
  58.         Chart1.Series[0].BorderWidth = 1;  
  59.         //setting Chart type   
  60.         Chart1.Series[0].ChartType = SeriesChartType.Line ;  
  61.         // Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;  
  62.         // Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false;  
  63.         // Chart1.Series[0].ChartType = SeriesChartType.Spline;  
  64.         //Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;  
  65.         con.Close();  
  66.   
  67.     }  

Now, we have the entire logic to bind the chart from the database, let us run the application. The chart will look such as follows:
 
 
We can have GridLines of the chart that sometimes need to be hidden, if you want to hide them then uncomment the following code as:
  1. Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;  
  2. Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false
Now run the application, the chart will look as follows:
 
 
We can also change the thickness (width) of the lines as in the following:
  1. //Setting width of line    
  2. Chart1.Series[0].BorderWidth = 1; 
Now let's look at the Graph.
 
 
Now let us switch to a Spline chart as in the following: 
  1. //setting Chart type   
  2. Chart1.Series[0].ChartType = SeriesChartType.Spline ; 
Now run the application and the Funnel type chart will look as follows:
 
 
The preceding is the Normal Spline chart. Now let us enable the 3D style as in the following: 
  1. Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true
Now run the application and the chart will look as follows:
 
 
Now from all the preceding explanations we saw how to create and use a Line and Spline 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.