Wednesday, March 22, 2017

Create PIE chart in asp.net using Microsoft .NET Charting control 3.5

In this article we will create PIE chart using Microsoft .NET Charting control 3.5. In some application we need
to display pie chart as a part of report and using Microsoft Charting control its very easy, and more over
this .NET Charting control is free to use and distribute.
Step 1: First Download the .NET Charting control from here.
Setp 2: Add the reference to your project by right click on the solution explorer of your project.
Setp 3: Write the below code in which event you want to draw Pie Chart.
private void CreateChart()
//Create some dummy DataRandom random = new Random();
for (int pointIndex = 0; pointIndex < 10; pointIndex++)
"Series1"].Points.AddY(random.Next(20, 100));
//Set the chart typeChart1.Series["Series1"].ChartType = SeriesChartType.Pie;// Set the bar widthChart1.Series["Series1"]["PointWidth"] = "0.5";// Show data points labelsChart1.Series["Series1"].IsValueShownAsLabel = true;// Set data points label styleChart1.Series["Series1"]["BarLabelStyle"] = "Center";// Show chart as 3DChart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;// Draw chart as 3D CylinderChart1.Series["Series1"]["DrawingStyle"] = "Cylinder";
Note: For more information about dowloading and installing information refer this article .NET Framework 3.5 ASP.Net Chart Control Example
Dont forget to add namespace
1using System.Web.UI.DataVisualization.Charting;

If you run the project you will see you pie chart like this

Full Code of your .cs file using C# Example. In the below example we have calling the method in the page load event.
01using System;
02using System.Web;
03using System.Web.UI;
04using System.Web.UI.WebControls;
05using System.Web.UI.DataVisualization.Charting;
06  
07public partial class _Default : System.Web.UI.Page
08{
09protected void Page_Load(object sender, EventArgs e)
10{
11if (!IsPostBack)
12{
13CreateChart();
14}
15}
16private void CreateChart()
17{
18//Create some dummy Data
19Random random = new Random();
20for (int pointIndex = 0; pointIndex < 10; pointIndex++)
21{
22Chart1.Series["Series1"].Points.AddY(random.Next(20, 100));
23}//Set the chart type
24Chart1.Series["Series1"].ChartType = SeriesChartType.Pie;// Set the Pie width
25Chart1.Series["Series1"]["PointWidth"] = "0.5";// Show data points labels
26Chart1.Series["Series1"].IsValueShownAsLabel = true;// Set data points label style
27Chart1.Series["Series1"]["BarLabelStyle"] = "Center";// Show chart as 3D
28Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;// Draw chart as 3D
29Chart1.Series["Series1"]["DrawingStyle"] = "Cylinder";
30}
31}
And the same above code in VB.NET
01Imports System
02Imports System.Web
03Imports System.Web.UI
04Imports System.Web.UI.WebControls
05Imports System.Web.UI.DataVisualization.Charting
06
07Public Partial Class _Default
08    Inherits System.Web.UI.Page
09    Protected Sub Page_Load(ByVal sender As ObjectByVal As EventArgs)
10        If Not IsPostBack Then
11            CreateChart()
12        End If
13    End Sub
14    Private Sub CreateChart()
15        'Create some dummy Data
16        Dim random As New Random()
17        For pointIndex As Integer = 0 To 9
18            Chart1.Series("Series1").Points.AddY(random.[Next](20, 100))
19        Next
20        'Set the chart type
21        Chart1.Series("Series1").ChartType = SeriesChartType.Pie
22        ' Set the PIE width
23        Chart1.Series("Series1")("PointWidth") = "0.5"
24        ' Show data points labels
25        Chart1.Series("Series1").IsValueShownAsLabel = True
26        ' Set data points label style
27        Chart1.Series("Series1")("BarLabelStyle") = "Center"
28        ' Show chart as 3D
29        Chart1.ChartAreas("ChartArea1").Area3DStyle.Enable3D = True
30        ' Draw chart as 3D
31        Chart1.Series("Series1")("DrawingStyle") = "Cylinder"
32    End Sub
33     
34End Class
You can change the data source to any mode. you can also get the data from Database and set its chart series property.
------------------------------------------------------------------------------------------------------------

Source: http://www.dotnetspark.com/kb/656-create-pie-chart-asp-net-using-microsoft.aspx 

No comments:

HOW TO RECOVER THE SQL DATABASE FROM SUSPECT MODE (Or) HOW TO BRING BACK THE SUSPECTED DATABASE TO THE NORMAL MODE When you logi...