This
article will show you how you can create a bar chart using asp.net chart
control in asp.net and c#. This will take data in datatable from database.
Some of my
previous articles are as follows: Asp.Net
StackedBar Chart From DataBase Table Using C#.Net and VB.Net, Asp.Net
Bar Chart From DataBase Table Using C#.Net and VB.Net, Asp.Net
FastLine Chart From DataBase Table Using C#.Net and VB.Net, Asp.Net
StepLine Chart From DataBase Table Using C#.Net and VB.Net, Asp.Net
Spline Chart From DataBase Table Using C#.Net and VB.Net, Asp.Net
Line Chart From DataBase Table Using C#.Net and VB.Net, Asp.Net
Bubble Chart From DataBase Table Using C#.Net and VB.Net.
So for this
article first we will create a new asp.net application and add an asp.net chart
control. And after that we will select the chart type as bar chart.
Your page
code will look as shown below:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ColumnChart.aspx.cs" Inherits="WebApplication7.ColumnChart " %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Chart ID="Chart1" runat="server" Width="500px" Palette="Excel">
<Series>
<asp:Series Name="Growth" XValueMember="Country" YValueMembers="Growth"
IsVisibleInLegend="true"
IsValueShownAsLabel="true" YValuesPerPoint="2">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
<AxisX LineColor="Gray">
<MajorGrid LineColor="Gray" />
</AxisX>
<AxisY LineColor="Gray">
<MajorGrid LineColor="Gray" />
</AxisY>
<Area3DStyle Enable3D="false" LightStyle="Realistic"></Area3DStyle>
</asp:ChartArea>
</ChartAreas>
<Legends>
<asp:Legend>
</asp:Legend>
</Legends>
</asp:Chart>
</div>
</form>
</body>
</html>
|
After this
we will create a datatable for passing the data to the chart to display.
using System;
using
System.Collections.Generic;
using System.Data;
using
System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
namespace WebApplication7
{
public partial class ColumnChart:
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable _objdt = new DataTable();
_objdt
= GetDataForChart();
BindDataToChartcontrol(_objdt);
}
///
/// This function will bind
data to chart control
///
public void
BindDataToChartcontrol(DataTable _objdt)
{
Chart1.DataSource = _objdt;
Chart1.DataBind();
}
///
/// This method will provide
data
/// In this methos you can
fatch data from DB and pass it to chart control
///
///
public DataTable GetDataForChart()
{
DataTable _objdt = new DataTable();
SqlConnection con =
new SqlConnection("--Your connectionstring--");
SqlDataAdapter da = new SqlDataAdapter("Select * from Country", con);
da.Fill(_objdt);
return _objdt;
}
}
}
|
Now we have
done run the application to check the output.
0 comments:
Please let me know your view