How add percentage value in piechart?

Eranga Lakmal Perera picture Eranga Lakmal Perera · Aug 6, 2014 · Viewed 8.8k times · Source

This is the code which i used to generate pie chart . I need to add percentage value to piechart series.Tries few ways but did not find solution.I have attached my working code output image and expected image .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.DataVisualization.Charting;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Xml;
using System.Collections;
 public bool GetPieChart(string XML , string Path)
    {
        try
        {
        // create chart instance
        var chart = new Chart();



        // set chart height and width
        chart.Height = 500;
        chart.Width = 600;

        // Add legend to chart
        chart.Legends.Add(new Legend() { Name = "Legend" });
        chart.Legends[0].Font = new Font("Verdana", 10);
        chart.Legends[0].Docking = Docking.Bottom;

        ArrayList xAxisData = new ArrayList();

        ArrayList yAxisData = new ArrayList();

        XmlDocument xml = new XmlDocument();
        xml.LoadXml(XML);

        XmlNodeList multiLine = xml.DocumentElement.SelectNodes("/Report/PieChart/series");

        // Set Chart Title
        string title = xml.SelectSingleNode("/Report").Attributes["Name"].Value.ToString();
        chart.Titles.Add(title);
        chart.Titles[0].Font = new Font("Verdana", 12);

        // Set chart properties
        var chartArea = new ChartArea();
        chart.ChartAreas.Add(chartArea);

        Series series;

        // Add data to the chart

        foreach (XmlNode seriesNode in multiLine)
        {
            xAxisData.Clear();

            yAxisData.Clear();

            series = new Series();

            string seriesName = seriesNode.Attributes["name"].Value.ToString();


            XmlNodeList detailsList = seriesNode.SelectNodes("datapoint");

            foreach (XmlNode detailNode in detailsList)
            {
                xAxisData.Add(detailNode.Attributes["Label"].Value.ToString());

                int value;

                string st = detailNode.Attributes["value"].Value.ToString();

                int.TryParse(st, out value);

                yAxisData.Add(value);
            }

            // set series properties
            series.Name = seriesName;
            series.ChartType = SeriesChartType.Pie;

            chart.Series.Add(series);
            chart.Series[seriesName].Points.DataBindXY(xAxisData, yAxisData);
            chart.Series[seriesName]["PieLabelStyle"] = "Outside";
            chart.Series[seriesName]["PieLineColor"] = "Black";
        }

        chart.SaveImage(Path, ChartImageFormat.Jpeg);

        }
        catch (Exception ex)
        {

        }

        return true;
    }

This is the out put from this code

enter image description here

This is what i expect

enter image description here

Guys need your help to do that ?

Answer

Eranga Lakmal Perera picture Eranga Lakmal Perera · Aug 6, 2014

Finally found solution. Now works well.

            chart.Series[seriesName].XValueMember = "Name";
            chart.Series[seriesName].YValueMembers = "Count";
            chart.Series[seriesName].IsValueShownAsLabel = true;