.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share

 Subscribe to Articles

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

Posted By :Pankaj Mishra      Posted Date :28/06/2009   Points :25   Category: ASP.NET    URL: http://www.dotnetspark.com

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.
    


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 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";

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

using 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.

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";
}
}

And the same above code in VB.NET

Imports System 
Imports System.Web 
Imports System.Web.UI 
Imports System.Web.UI.WebControls 
Imports System.Web.UI.DataVisualization.Charting 

Public Partial Class _Default 
    Inherits System.Web.UI.Page 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 
        If Not IsPostBack Then 
            CreateChart() 
        End If 
    End Sub 
    Private Sub CreateChart() 
        'Create some dummy Data 
        Dim random As New Random() 
        For pointIndex As Integer = 0 To 9 
            Chart1.Series("Series1").Points.AddY(random.[Next](20, 100)) 
        Next 
        '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" 
    End Sub 
    
End Class

You can change the data source to any mode. you can also get the data from Database and set its chart series property.

Cheers!!!
pankaj


Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
Responses
Author: Rakesh Sharma         Company URL: http://www.dotnetspark.com
Posted Date: 25/11/2009

Hi Pankaj,

Hope this will not annoyed you!! :)

I am a QA guy(not much knowledge of asp.net) Here is my requirment.

I am storing my silkTest automation results to SQL server. Now i want to display them as form of pi chart in asp page using c#.

From data base i am fetching percentage of passed test cases and failed test cases.
So want to displai Pi chart with two slices for pass in green color and for failed one in red color.

How to go fpr this.



Post Comment
You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend