ASP.NET Charts Tutorial

  • ASP.Net chart object is a versatile control which can produce elegant graphs to cover plenty of real-life situations.
  • It is within the Namespace, System.Web.UI.DataVisualization.Charting;
  • This tutorial shows you the true potential of the chart object, when it is used with Ajax. Just keep an eye on the response time, when you choose to draw.

Pie Charts


Enter the above values and click the Draw button.

MathsPhysicsEcon:ITGeography
   

The .aspx page for the above is as follows:

<asp:Chart ID="PieChart"CssClass="imgforcontent" runat="server" ImageStorageMode="UseImageLocation" ImageLocation="~/aspchart">
<Series>
<asp:Series Name="For_Pi">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="For_Pi">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>

<asp:Table ID="Pie" runat="server">
<asp:TableHeaderRow>
<asp:TableHeaderCell>Maths</asp:TableHeaderCell>
<asp:TableHeaderCell>Physics</asp:TableHeaderCell>
<asp:TableHeaderCell>Biology</asp:TableHeaderCell>
<asp:TableHeaderCell>IT</asp:TableHeaderCell>
<asp:TableHeaderCell>English</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell><asp:TextBox Width="50px" CssClass="centralized" ID="txtMaths" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:TextBox Width="50px" CssClass="centralized" ID="txtPhysics" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:TextBox Width="50px" CssClass="centralized" ID="txtBiology" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:TextBox Width="50px" CssClass="centralized" ID="txtIT" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:TextBox Width="50px" CssClass="centralized" ID="txtEnglish" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow><asp:TableCell ColumnSpan="5" CssClass="centralized"><asp:Button ID="btnPie" onclick="btnPie_Click" runat="server" Text="Click and Draw" />   
<asp:Button ID="btnPieClear" onclick="btnpieclear" runat="server" Text="Clear" />
</asp:TableCell></asp:TableRow>
<asp:TableRow><asp:TableCell ColumnSpan="5" CssClass="centralized"><asp:CheckBox ID="chkPie" AutoPostBack="true" Text="Cut a piece?" OnCheckedChanged="onchkclick" runat="server" />
</asp:TableCell></asp:TableRow>
</asp:Table>

The corresponding C# code in .aspx.cs is as follows:

// Page Load event
protected void Page_Load(object sender, EventArgs e)
{
Chart1.Series.Clear();
// for the pie chart
double[] yval = { 70, 55, 68, 45, 30 };
// Initialize an array of strings
string[] xval = { "Maths", "Physics", "Biology", "IT", "English" };
// Bind the double array to the Y axis points of the Default data series
PieChart.Series[0].ChartType = SeriesChartType.Pie;
PieChart.Series[0].Points.DataBindXY(xval, yval);
PieChart.Series[0]["Pie"] = "30";
// Explode data point with label "Italy"
PieChart.Series[0].Points[4]["Exploded"] = "false";
// Enable 3D
PieChart.ChartAreas[0].Area3DStyle.Enable3D = true;
// Disable the Legend
//Chart1.Legends[0].Enabled = false;
}
// Draw the pie chart
protected void btnPie_Click(object sender, EventArgs e)
{
if (txtMaths.Text == "" || txtPhysics.Text == "" || txtBiology.Text == "" || txtIT.Text == "" || txtEnglish.Text == "")
{
btnPie.Text="Please enter values";
}
else
{
string[] xval = { "Maths", "Physics", "Biology", "IT", "English" };
double[] yval = { Convert.ToDouble(txtMaths.Text), Convert.ToDouble(txtPhysics.Text), Convert.ToDouble(txtBiology.Text), Convert.ToDouble(txtIT.Text), Convert.ToDouble(txtEnglish.Text) };
//double[] yval = { 90, 25, 88, 55, 60 };
PieChart.Series[0].ChartType = SeriesChartType.Pie;
PieChart.Series[0].Points.DataBindXY(xval, yval); PieChart.Series[0]["Pie"] = "30";
//PieChart.Series[0].Points[4]["Exploded"] = "true";
PieChart.ChartAreas[0].Area3DStyle.Enable3D = true;
chkPie.Checked = false;
}
}
// Clear the pie chart
protected void btnpieclear(object sender, EventArgs e)
{
txtMaths.Text = "";
txtPhysics.Text = "";
txtBiology.Text = "";
txtIT.Text = "";
txtEnglish.Text = "";
chkPie.Checked = false;
btnPie.Text = "Click and Draw";
}
// Slice the pie or reverse it
protected void onchkclick(object sender, EventArgs e)
{
Random rnd = new Random();
if (chkPie.Checked)
{
piedraw();
PieChart.Series[0].Points[rnd.Next(0, 4)]["Exploded"] = "true";
chkPie.Checked = true;
}
else
{
piedraw();
PieChart.Series[0].Points[rnd.Next(0, 4)]["Exploded"] = "false";
}
}
// Draw the pie
protected void piedraw()
{
if (txtMaths.Text == "" || txtPhysics.Text == "" || txtBiology.Text == "" || txtIT.Text == "" || txtEnglish.Text == "")
{
double[] yval = { 72, 72, 72, 72, 72 };
string[] xval = { "Maths", "Physics", "Biology", "IT", "English" };
PieChart.Series[0].ChartType = SeriesChartType.Pie;
PieChart.Series[0].Points.DataBindXY(xval, yval);
PieChart.Series[0]["Pie"] = "30";
//PieChart.Series[0].Points[4]["Exploded"] = "true";
PieChart.ChartAreas[0].Area3DStyle.Enable3D = true;
chkPie.Checked = false;
}
else
{
double[] yval = { Convert.ToDouble(txtMaths.Text), Convert.ToDouble(txtPhysics.Text), Convert.ToDouble(txtBiology.Text), Convert.ToDouble(txtIT.Text), Convert.ToDouble(txtEnglish.Text) };
string[] xval = { "Maths", "Physics", "Biology", "IT", "English" };
PieChart.Series[0].ChartType = SeriesChartType.Pie;
PieChart.Series[0].Points.DataBindXY(xval, yval);
PieChart.Series[0]["Pie"] = "30";
//PieChart.Series[0].Points[4]["Exploded"] = "true";
PieChart.ChartAreas[0].Area3DStyle.Enable3D = true;
chkPie.Checked = false;
}
}

Normal Distribution Calculator

The days of annoying tables are over; the values of probability are just a click away. Just enter the upper 'Z' value in the box.

 

 

Enter Z value here:

 

 

 

The .aspx page for the above is as follows:

<asp:Chart ID="Chart1" runat="server" BackColor="lemonchiffon"
BorderlineColor="Beige" BorderSkin-BackColor="Beige" width="400px"
Height="300px" ImageStorageMode="UseImageLocation" ImageLocation="~/aspchart"
BackImageAlignment="BottomLeft" BackImageTransparentColor="128, 255, 255">
<Series>
<asp:Series Name="Series1">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</ChartAreas>
<BorderSkin SkinStyle="Emboss" />
</asp:Chart>

The .aspx page for the above is as follows:

protected void btnNormalDistribution_Click(object sender, EventArgs e)
{
Chart1.Series.Clear();
if (string.IsNullOrEmpty(TextBox2.Text))
{
lblNormalDistribution.Text = "Please enter the data";
return;
}
double i = 0;
Series series = new Series("Spline");
series.ChartType = SeriesChartType.SplineArea;
series.BorderWidth = 2;
series.ShadowOffset = 1;
// Add series into the chart's series collection
double f = Math.Sqrt(1 / 2 * Math.PI);
double x = Convert.ToDouble(TextBox2.Text);
double T = 1 / (1 + 0.2316419 * Math.Abs(x));
double D = 0.3989423 * Math.Exp(-x * x / 2);
double Prob = D * T * (0.3193815 + T * (-0.3565638 + T * (1.781478 + T * (-1.821256 + T * 1.330274))));
lblNormalDistribution.Text = "P(0<= Z <= " + x + " ) = " + Math.Round(10000 * (0.5 - Prob)) / 10000;
for (i = 0; i <= x; i++)
{
series.Points.AddXY(i, Math.Sqrt(1 / (2 * Math.PI)) * Math.Exp(-Math.Pow(i, 2) / 2));
}
Chart1.ChartAreas[0].AxisY.Minimum = 0;
Chart1.ChartAreas[0].AxisY.Maximum = 0.5;
Chart1.ChartAreas[0].AxisX.Minimum = -5;
Chart1.ChartAreas[0].AxisX.Maximum = 5;
Chart1.ChartAreas[0].AxisX.MajorGrid.Interval = 1;
Chart1.ChartAreas[0].AxisX.MajorTickMark.Interval = 1;
Chart1.ChartAreas[0].AxisY.MajorGrid.Interval = 0.1;
Chart1.ChartAreas[0].AxisY.MajorTickMark.Interval = 0.1;
Chart1.ChartAreas[0].AxisY.LineColor = System.Drawing.Color.Red;
Chart1.ChartAreas[0].AxisX.LineColor = System.Drawing.Color.Red;
Chart1.Series.Add(series);
}
protected void btnClearNormal_Click(object sender, EventArgs e)
{
lblNormalDistribution.Text = "";
TextBox2.Text = "";
}

 

 

 

Resources at Fingertips

There is a significant selection of tutorials here, covering ASP.Net, HTML5, CSS3 and JavaScript. They are categorized clearly for you to access them easily on any device - desktops/laptops, smartphones and laptops.
There are quite a few of them on HTML5 Canvas and CSS3. In addition, there are tutorials on JavaScript and ASP.Net too.

Email: 

Stand Out - from the crowd

students

"There's no such thing as a free lunch."

The best things in nature are free with no strings attached - fresh air, breathtakingly warm sunshine, scene of meadow on the horizon...

Vivax Solutions, while mimicking nature, offers a huge set of tutorials along with interactive tools for free.

Please use them and excel in the sphere of science education.

Everything is free; not even registration is required.

 

 

Recommended Reading

 

The best book to master HTML5 canvas animations. The author tells you how to manipulate the canvas element with JavaScript in a progressive way - starting from the easier to harder worked examples. It is ideal for anyone who wants to be a game developer in HTML5.

Advertisements

Advertisement