Friday 4 May 2012

Create PIE chart in asp.net


Step 1: First Download the .NET Charting control (In 3.5), or Select the Chart control from toolbar(In 4.0) . 

Step 2: Add the reference to your project by right click on the solution explorer of your project.

Step 3: Write the below code in which event you want to draw Pie Chart.

private void CreateChart()

//Create some dummy Data

Random random = new Random();

for (int pointIndex = 0; pointIndex < 10; pointIndex++)

"Series1"].Points.AddY(random.Next(20, 100));//Set the chart type

Chart1.Series["Series1"].ChartType = SeriesChartType.Pie;// Set the bar width

Chart1.Series["Series1"]["PointWidth"] = "0.5";// Show data points labels

Chart1.Series["Series1"].IsValueShownAsLabel = true;// Set data points label style

Chart1.Series["Series1"]["BarLabelStyle"] = "Center";// Show chart as 3D

Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;// Draw chart as 3D Cylinder

Chart1.Series["Series1"]["DrawingStyle"] = "Cylinder";

Add Namespace:- 
                using System.Web.UI.DataVisualization.Charting;


If you run the project you will see you pie chart like this 
View Source:-


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.Web.UI.DataVisualization.Charting;

  


public partial class _Default : System.Web.UI.Page

{


protected void Page_Load(object sender, EventArgs e)

{


if (!IsPostBack)

{


CreateChart();

}


}


private void CreateChart()



{

//Create some dummy Data


Random random = new Random();

for (int pointIndex = 0; pointIndex < 10; pointIndex++)


 {

     Chart1.Series["Series1"].Points.AddY(random.Next(20, 100));


}  //Set the chart type

    Chart1.Series["Series1"].ChartType = SeriesChartType.Pie;// Set the Pie width

  
   Chart1.Series["Series1"]["PointWidth"] = "0.5";// Show data points labels

   Chart1.Series["Series1"].IsValueShownAsLabel = true;// Set data points label style


     Chart1.Series["Series1"]["BarLabelStyle"] = "Center";// Show chart as 3D

     Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;// Draw chart as 3D


     Chart1.Series["Series1"]["DrawingStyle"] = "Cylinder";

}


}







No comments:

Post a Comment