How to create a Pie chart using ASP.Net and C#

First Step: Web Config file

The Web.Config file needs to be modified to contain the httpHandler and controls. These configurations enable the asp:Chart tag which is used in the Default.aspx file.

In httpHandlers add below code:


<add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization,
Version=3.5.0.0, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35" />






In controls add below code:

<add tagPrefix="asp"
namespace="System.Web.UI.DataVisualization.Charting"
assembly="System.Web.DataVisualization, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" />





Second Step: Default.aspx file:

we will add the chart, titles, legend, series and chart areas.


<asp:Chart ID="Chart1" runat="server" Width="500px"  Height="500px" IsMapAreaAttributesEncoded="True">

<Series>
<asp:Series ChartArea="myChartArea" Name="Series1" ChartType="Pie" XValueMember="Status" YValueMembers="Total" XValueType="String" YValueType="Int32">
</asp:Series>
</Series>

<ChartAreas>
<asp:ChartArea Name="myChartArea"  Area3DStyle-Enable3D="true" >
<AxisY Title="Status">                                          </AxisY>
<AxisX Title="Process">
</AxisX>
<Area3DStyle Enable3D="True" Inclination="20" />
</asp:ChartArea>
</ChartAreas> </asp:Chart>



And lastly, we will add the code to the code-behind file to populate and configure the chart.


 private void LoadChartOptions()
        {

            Chart1.Legends.Add(new Legend("WorkOrder"));
            Chart1.Legends[0].TableStyle = LegendTableStyle.Auto;
            Chart1.Legends[0].Docking = Docking.Bottom;
            Chart1.Series[0].Label = "#AXISLABEL -> #VALY{D}";

            Chart1.Series[0]["PieLabelStyle"] = "Inside";
            Chart1.Series[0]["PieLabelStyle"] = "Disabled";
            Chart1.Series[0].BackSecondaryColor = System.Drawing.Color.Blue;
            Chart1.Series[0]["PieLineColor"] = "Black";
            Chart1.Series[0]["PieDrawingStyle"] = "Concave";
            Chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
            Chart1.ChartAreas[0].Area3DStyle.Rotation = 30;

        }





Comments

Popular posts from this blog

How to retain the data annotations or class during modify a table in the database

Interview Questions

AJAX CONTROL