gpt4 book ai didi

c# - EPPlus如何更改EXCEL中PIE图表的颜色

转载 作者:行者123 更新时间:2023-12-01 20:27:08 25 4
gpt4 key购买 nike

如何使用 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.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;

我想将默认颜色更改为一些明亮的颜色。

提出建议并阐明这一点。

最佳答案

受 Ernie 的回答启发,这里有一个扩展方法,用于设置折线图系列的颜色和厚度,以及一个用于设置饼图数据点颜色的未测试版本:

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");
}

用法:

lineChart.SetSeriesStyle(s, color: Color.FromArgb(0, 0, 0), thickness: 6m);
pieChart.SetDataPointStyle(dataPointIndex: 0, color: Color.FromArgb(0, 0, 0));

关于c# - EPPlus如何更改EXCEL中PIE图表的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25623324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com