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
This is what i expect
Guys need your help to do that ?
foreach (DataPoint p in YourChart.Series["YourSeriesName"].Points)
{
p.Label = "#PERCENT\n#VALX";
}
Finally found solution. Now works well.
chart.Series[seriesName].XValueMember = "Name";
chart.Series[seriesName].YValueMembers = "Count";
chart.Series[seriesName].IsValueShownAsLabel = true;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With