这是我用来生成饼图的代码。我需要在饼图系列中添加百分比值。只需几种方法但没有找到解决方案。我已经附加了我的工作代码输出图像和预期图像。
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;
}
这是此代码的输出
这就是我所期待的
伙计们需要你的帮助吗?
答案 0 :(得分:4)
终于找到了解决方案。现在效果很好。
chart.Series[seriesName].XValueMember = "Name";
chart.Series[seriesName].YValueMembers = "Count";
chart.Series[seriesName].IsValueShownAsLabel = true;
答案 1 :(得分:2)
foreach (DataPoint p in YourChart.Series["YourSeriesName"].Points)
{
p.Label = "#PERCENT\n#VALX";
}
答案 2 :(得分:1)
使用双倍百分比字符的转义百分比符号。寻找下面的样本
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);
答案 3 :(得分:1)
这也许有帮助。此代码用于饼图,其中饼图中的百分比和图例中的标签(字符串):其中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;