C# EPPlus如何在EXCEL中更改饼图的颜色
如何使用EPPlus编程更改Excel饼图的默认颜色 下面是我的代码C# EPPlus如何在EXCEL中更改饼图的颜色,c#,epplus,C#,Epplus,如何使用EPPlus编程更改Excel饼图的默认颜色 下面是我的代码 var pieChart = worksheet.Drawings.AddChart("piechart", eChartType.Pie3D) as ExcelPieChart; //Set top left corner to row 1 column 2 pieChart.SetPosition(18, 0, 0, 0); pieChart.SetS
var pieChart = worksheet.Drawings.AddChart("piechart", eChartType.Pie3D) as ExcelPieChart;
//Set top left corner to row 1 column 2
pieChart.SetPosition(18, 0, 0, 0);
pieChart.SetSize(350, 300);
pieChart.Series.Add(ExcelRange.GetAddress(12, 2, 15, 2),ExcelRange.GetAddress(12, 1, 15, 1) );
pieChart.Legend.Position = eLegendPosition.Bottom;
pieChart.Legend.Border.Fill.Color = Color.Green;
pieChart.Legend.Border.LineStyle = eLineStyle.Solid;
pieChart.Legend.Border.Fill.Style = eFillStyle.SolidFill;
pieChart.Title.Text = "Current Status";
pieChart.DataLabel.ShowCategory = false;
pieChart.DataLabel.ShowPercent = true;
我想将默认颜色更改为一些明亮的颜色
建议并解释一下这个问题。当我遇到类似的问题时,我想我会回馈一下。简而言之,EEPlus不支持更改单个切片的颜色,因此我不得不依赖xml操作。不漂亮,需要对输出的数据有很好的了解-需要知道期望的切片数。但它可以工作,这应该适用于除3D以外的其他饼图类型 下面是一个测试方法,演示了。针对EPP 4.0.1执行此操作,但应与以前的版本同样适用:
[TestMethod]
public void Change_3DPieChart_Color()
{
const string PIE_PATH = "c:chartSpace/c:chart/c:plotArea/c:pie3DChart/c:ser";
var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
if (existingFile.Exists)
existingFile.Delete();
using (var package = new ExcelPackage(existingFile))
{
var workbook = package.Workbook;
var worksheet = workbook.Worksheets.Add("newsheet");
//Some data
worksheet.Cells["A12"].Value = "wer";
worksheet.Cells["A13"].Value = "sdf";
worksheet.Cells["A14"].Value = "wer";
worksheet.Cells["A15"].Value = "ghgh";
worksheet.Cells["B12"].Value = 53;
worksheet.Cells["B13"].Value = 36;
worksheet.Cells["B14"].Value = 43;
worksheet.Cells["B15"].Value = 86;
//Create the pie
var pieChart = (ExcelPieChart) worksheet.Drawings.AddChart("piechart", eChartType.Pie3D);
//Set top left corner to row 1 column 2
pieChart.SetPosition(18, 0, 0, 0);
pieChart.SetSize(350, 300);
pieChart.Series.Add(ExcelCellBase.GetAddress(12, 2, 15, 2), ExcelCellBase.GetAddress(12, 1, 15, 1));
pieChart.Legend.Position = eLegendPosition.Bottom;
pieChart.Legend.Border.Fill.Color = Color.Green;
pieChart.Legend.Border.LineStyle = eLineStyle.Solid;
pieChart.Legend.Border.Fill.Style = eFillStyle.SolidFill;
pieChart.Title.Text = "Current Status";
pieChart.DataLabel.ShowCategory = false;
pieChart.DataLabel.ShowPercent = true;
//Get the nodes
var ws = pieChart.WorkSheet;
var nsm = ws.Drawings.NameSpaceManager;
var nschart = nsm.LookupNamespace("c");
var nsa = nsm.LookupNamespace("a");
var node = pieChart.ChartXml.SelectSingleNode(PIE_PATH, nsm);
var doc = pieChart.ChartXml;
//Add the node
var rand = new Random();
for (var i = 0; i < 4; i++)
{
//Create the data point node
var dPt = doc.CreateElement("dPt", nschart);
var idx = dPt.AppendChild(doc.CreateElement("idx", nschart));
var valattrib = idx.Attributes.Append(doc.CreateAttribute("val"));
valattrib.Value = i.ToString(CultureInfo.InvariantCulture);
node.AppendChild(dPt);
//Add the solid fill node
var spPr = doc.CreateElement("spPr", nschart);
var solidFill = spPr.AppendChild(doc.CreateElement("solidFill", nsa));
var srgbClr = solidFill.AppendChild(doc.CreateElement("srgbClr", nsa));
valattrib = srgbClr.Attributes.Append(doc.CreateAttribute("val"));
//Set the color
var color = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
valattrib.Value = ColorTranslator.ToHtml(color).Replace("#", String.Empty);
dPt.AppendChild(spPr);
}
package.Save();
}
}
[TestMethod]
公共无效更改\u 3DPieChart\u颜色()
{
const string PIE_PATH=“c:chartSpace/c:chart/c:plotArea/c:pie3DChart/c:ser”;
var existingFile=new FileInfo(@“c:\temp\temp.xlsx”);
if(existingFile.Exists)
existingFile.Delete();
使用(var package=new ExcelPackage(existingFile))
{
var工作簿=package.workbook;
var工作表=工作簿。工作表。添加(“新闻表”);
//一些数据
工作表.单元格[“A12”].Value=“wer”;
工作表.单元格[“A13”].Value=“sdf”;
工作表。单元格[“A14”]。Value=“wer”;
工作表.单元格[“A15”].Value=“GHGHGH”;
工作表。单元格[“B12”]。值=53;
工作表。单元格[“B13”]。值=36;
工作表。单元格[“B14”]。值=43;
工作表。单元格[“B15”]。值=86;
//创建饼图
var pieChart=(ExcelPieChart)worker.Drawings.AddChart(“pieChart”,eChartType.Pie3D);
//将左上角设置为第1行第2列
设置位置(18,0,0,0);
pieChart.设置尺寸(350300);
添加(ExcelCellBase.GetAddress(12,2,15,2),ExcelCellBase.GetAddress(12,1,15,1));
pieChart.Legend.Position=eLegendPosition.Bottom;
pieChart.Legend.Border.Fill.Color=Color.Green;
pieChart.Legend.Border.LineStyle=eLineStyle.Solid;
pieChart.Legend.Border.Fill.Style=eFillStyle.SolidFill;
pieChart.Title.Text=“当前状态”;
pieChart.DataLabel.ShowCategory=false;
pieChart.DataLabel.ShowPercent=true;
//获取节点
var ws=pieChart.WorkSheet;
var nsm=ws.Drawings.NameSpaceManager;
var nschart=nsm.LookupNamespace(“c”);
var nsa=nsm.LookupNamespace(“a”);
var node=pieChart.ChartXml.SelectSingleNode(饼图路径,nsm);
var doc=pieChart.ChartXml;
//添加节点
var rand=new Random();
对于(变量i=0;i<4;i++)
{
//创建数据点节点
var dPt=doc.CreateElement(“dPt”,nschart);
var idx=dPt.AppendChild(doc.CreateElement(“idx”,nschart));
var valattrib=idx.Attributes.Append(doc.CreateAttribute(“val”);
valattrib.Value=i.ToString(CultureInfo.InvariantCulture);
子节点(dPt);
//添加实体填充节点
var spPr=doc.CreateElement(“spPr”,nschart);
var solidFill=spPr.AppendChild(doc.CreateElement(“solidFill”,nsa));
var srgbClr=solidFill.AppendChild(doc.CreateElement(“srgbClr”,nsa));
valattrib=srgbClr.Attributes.Append(doc.CreateAttribute(“val”);
//设置颜色
var color=color.FromArgb(rand.Next(256)、rand.Next(256)、rand.Next(256));
valattrib.Value=ColorTranslator.ToHtml(color.Replace(“#”),String.Empty;
儿童精神分裂症(spPr);
}
package.Save();
}
}
受厄尼答案的启发,以下是一种用于设置折线图系列颜色和厚度的扩展方法,以及一种用于设置饼图数据点颜色的非测试版本:
public static void SetSeriesStyle(this ExcelLineChart chart, ExcelChartSerie series, Color color, decimal? thickness = null) {
if (thickness < 0) throw new ArgumentOutOfRangeException("thickness");
var i = 0;
var found = false;
foreach (var s in chart.Series) {
if (s == series) {
found = true;
break;
}
++i;
}
if (!found) throw new InvalidOperationException("series not found.");
//Get the nodes
var nsm = chart.WorkSheet.Drawings.NameSpaceManager;
var nschart = nsm.LookupNamespace("c");
var nsa = nsm.LookupNamespace("a");
var node = chart.ChartXml.SelectSingleNode(@"c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser[c:idx[@val='" + i.ToString(CultureInfo.InvariantCulture) + "']]", nsm);
var doc = chart.ChartXml;
//Add the solid fill node
var spPr = doc.CreateElement("c:spPr", nschart);
var ln = spPr.AppendChild(doc.CreateElement("a:ln", nsa));
if (thickness.HasValue) {
var w = ln.Attributes.Append(doc.CreateAttribute("w"));
w.Value = Math.Round(thickness.Value * 12700).ToString(CultureInfo.InvariantCulture);
var cap = ln.Attributes.Append(doc.CreateAttribute("cap"));
cap.Value = "rnd";
}
var solidFill = ln.AppendChild(doc.CreateElement("a:solidFill", nsa));
var srgbClr = solidFill.AppendChild(doc.CreateElement("a:srgbClr", nsa));
var valattrib = srgbClr.Attributes.Append(doc.CreateAttribute("val"));
//Set the color
valattrib.Value = color.ToHex().Substring(1);
node.AppendChild(spPr);
}
public static void SetDataPointStyle(this ExcelPieChart chart, int dataPointIndex, Color color) {
//Get the nodes
var nsm = chart.WorkSheet.Drawings.NameSpaceManager;
var nschart = nsm.LookupNamespace("c");
var nsa = nsm.LookupNamespace("a");
var node = chart.ChartXml.SelectSingleNode("c:chartSpace/c:chart/c:plotArea/c:pieChart/c:ser", nsm);
var doc = chart.ChartXml;
//Add the node
//Create the data point node
var dPt = doc.CreateElement("c:dPt", nschart);
var idx = dPt.AppendChild(doc.CreateElement("c:idx", nschart));
var valattrib = idx.Attributes.Append(doc.CreateAttribute("val"));
valattrib.Value = dataPointIndex.ToString(CultureInfo.InvariantCulture);
node.AppendChild(dPt);
//Add the solid fill node
var spPr = doc.CreateElement("c:spPr", nschart);
var solidFill = spPr.AppendChild(doc.CreateElement("a:solidFill", nsa));
var srgbClr = solidFill.AppendChild(doc.CreateElement("a:srgbClr", nsa));
valattrib = srgbClr.Attributes.Append(doc.CreateAttribute("val"));
//Set the color
valattrib.Value = color.ToHex().Substring(1);
dPt.AppendChild(spPr);
}
public static String ToHex(this Color c) {
return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}
这是一条很晚的评论,但是PieChart的xml有一个输入错误。应该是
c:pieChart
而不是c:lineChart
,但是扩展性很好。谢谢@Steven,我已经解决了这个问题。
lineChart.SetSeriesStyle(s, color: Color.FromArgb(0, 0, 0), thickness: 6m);
pieChart.SetDataPointStyle(dataPointIndex: 0, color: Color.FromArgb(0, 0, 0));