Reputation: 938
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 ?
Upvotes: 5
Views: 13543
Reputation: 21
this maybe help. This code is for a Pie Chart with percentage inside the Pie and labels(strings) in the legends: Where datosSeries = List
string[] labels = datosSeries.Select(o => o.Week).ToArray();
float[] data = datosSeries.Select(o => o.ProfitSerie).ToArray();
chart2.Series.Add("Series1");
chart2.Series[0].Points.DataBindXY(labels, data);
chart2.Series[0].ChartType = SeriesChartType.Pie;
chart2.Series[0]["PieLabelStyle"] = "Outside";
chart2.Series[0].BorderWidth = 1;
chart2.Series[0].BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
chart2.Series[0].Label = "#PERCENT{P2}";
chart2.Legends.Add(new Legend("Default"));
// Add Color column
LegendCellColumn firstColumn = new LegendCellColumn();
firstColumn.ColumnType = LegendCellColumnType.SeriesSymbol;
firstColumn.HeaderText = "";
chart2.Legends["Default"].CellColumns.Add(firstColumn);
// Add name cell column
LegendCellColumn percentColumn = new LegendCellColumn();
percentColumn.Text = "#VALX";
percentColumn.HeaderText = "Tipo de Gastos";
percentColumn.Name = "nameColumn";
chart2.Legends["Default"].CellColumns.Add(percentColumn);
//Format the legend
chart2.Legends["Default"].LegendStyle = LegendStyle.Table;
chart2.Legends["Default"].TableStyle = LegendTableStyle.Tall;
chart2.Legends["Default"].DockedToChartArea = "ChartArea1";
chart2.Legends["Default"].IsDockedInsideChartArea = false;
chart2.Legends["Default"].Docking = Docking.Bottom;
Upvotes: 2
Reputation: 18956
escape percentage sign with double percentage char. look for the sample below
Chart1.Series.Add(series);
Series series1 = Chart1.Series[0];
series1.Points.DataBindXY(xValues, yValues);
Title ti = new Title()
ti.TextStyle = TextStyle.Emboss;
ti.Text = ((yValues[0] / 100) * 100) + @"%%";//double Percentage sign
ti.Font = new Font("Times New Roman", 40, FontStyle.Bold);
ti.Position.X = 50;
ti.Position.Y = 50;
Chart1.Titles.Add(ti);
Upvotes: 2
Reputation: 74
foreach (DataPoint p in YourChart.Series["YourSeriesName"].Points)
{
p.Label = "#PERCENT\n#VALX";
}
Upvotes: 5
Reputation: 938
Finally found solution. Now works well.
chart.Series[seriesName].XValueMember = "Name";
chart.Series[seriesName].YValueMembers = "Count";
chart.Series[seriesName].IsValueShownAsLabel = true;
Upvotes: 4