Display percentage values in Pie Chart using C# and VB.Net in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

Display percentage in pie chart and text in legend describing what each color represent within chart

Here is the ASP code for my chart: 

<asp:Chart ID="Chart1" BorderlineColor="Black" BorderlineDashStyle="Solid" Visible="true"
    ImageType="Png" runat="server" Height="600px" Width="600px" PaletteCustomColors="128, 255, 128; 255, 128, 0">
    <Titles>
        <asp:Title TextStyle="Frame">
        </asp:Title>
    </Titles>
    <Legends>
        <asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default"
            LegendStyle="Row">
        </asp:Legend>
    </Legends>
    <Series>
        <asp:Series Name="Series1" ChartType="Pie" YValuesPerPoint="2">
        </asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea IsSameFontSizeForAllAxes="true" BorderWidth="0" Name="ChartArea1">
            <Area3DStyle Enable3D="true" />
        </asp:ChartArea>
    </ChartAreas>
</asp:Chart>

 Below is the code behind to populate my Pie chart:

Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("###").ConnectionString)
    Using cmd As New SqlCommand("SELECT COUNT(MedicalResultsID) AS 'Total'  FROM tblMedicalResults WHERE MONTH(DateTested) = MONTH(GETDATE()) AND completed = 1 ", conn)
        Dim series As Series = PieChartMonthToDate.Series("Series1")
        conn.Open()
        Dim rdr As SqlDataReader = cmd.ExecuteReader()
        While rdr.Read()
            series.Points.AddXY(rdr("Total").ToString(), rdr("Total".ToString()))
            PieChartMonthToDate.Series("Series1").Label = "#PERCENT{P2}"
            Chart1.Series("Series1").LegendText = "etert"
            PieChartMonthToDate.Series("Series1").LegendText = "ertVALYeger"
        End While
        rdr.Close()
    End Using
    Using command As New SqlCommand("SELECT  COUNT(MedicalResultsID) AS 'Total' FROM tblMedicalResults WHERE MONTH(DateTested) = MONTH(GETDATE()) AND completed = 0 ", conn)
        Dim series As Series = PieChartMonthToDate.Series("Series1")
        Dim dr As SqlDataReader = command.ExecuteReader()
        While dr.Read()
            series.Points.AddXY(dr("Total").ToString(), dr("Total".ToString()))
        End While
        dr.Close()
    End Using
End Using

 

Posted one month ago

Hi Tevin,

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

HTML

<asp:Chart ID="PieChartMonthToDate" BorderlineColor="Black" BorderlineDashStyle="Solid"
    Visible="true" ImageType="Png" runat="server" Height="400px" Width="400px" PaletteCustomColors="128, 255, 128; 255, 128, 0">
    <Titles>
        <asp:Title TextStyle="Frame">
        </asp:Title>
    </Titles>
    <Legends>
        <asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default"
            LegendStyle="Column">
        </asp:Legend>
    </Legends>
    <Series>
        <asp:Series Name="Series1" ChartType="Pie" YValuesPerPoint="2">
        </asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea IsSameFontSizeForAllAxes="true" BorderWidth="0" Name="ChartArea1">
            <Area3DStyle Enable3D="true" />
        </asp:ChartArea>
    </ChartAreas>
</asp:Chart>

Namespaces

C#

using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.DataVisualization.Charting;

VB.Net

Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.UI.DataVisualization.Charting

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("select shipcity, count(orderid) Total from orders where shipcountry = 'Brazil' group by shipcity", conn))
            {
                Series series = PieChartMonthToDate.Series["Series1"];
                conn.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    series.Points.AddXY(rdr["shipcity"].ToString(), rdr["Total".ToString()]);
                    PieChartMonthToDate.Series["Series1"].Label = "#PERCENT{P2}";
                    PieChartMonthToDate.Series["Series1"].LegendText = "#VALX";
                    PieChartMonthToDate.Legends[0].LegendStyle = LegendStyle.Column;
                    PieChartMonthToDate.Legends[0].Docking = Docking.Right;
                    PieChartMonthToDate.Legends[0].Alignment = System.Drawing.StringAlignment.Center;
                }
                rdr.Close();
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConString").ConnectionString)
            Using cmd As New SqlCommand("select shipcity, count(orderid) Total from orders where shipcountry = 'USA' group by shipcity", conn)
                Dim series As Series = PieChartMonthToDate.Series("Series1")
                conn.Open()
                Dim rdr As SqlDataReader = cmd.ExecuteReader()
                While rdr.Read()
                    series.Points.AddXY(rdr("shipcity").ToString(), rdr("Total".ToString()))
                    PieChartMonthToDate.Series("Series1").Label = "#PERCENT{P2}"
                    PieChartMonthToDate.Series("Series1").LegendText = "#VALX"
                    PieChartMonthToDate.Legends(0).LegendStyle = LegendStyle.Column
                    PieChartMonthToDate.Legends(0).Docking = Docking.Right
                    PieChartMonthToDate.Legends(0).Alignment = System.Drawing.StringAlignment.Center
                End While
                rdr.Close()
            End Using
        End Using
    End If
End Sub

Screenshot