由厄尼的答案啓發,這裏是一個擴展方法,用於設置折線圖系列顏色和厚度的作品,和非測試版本設置餅圖數據點的顏色:
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));
晚發表評論,但有一個錯字在t他爲PieChart的XML。應該是'c:pieChart'而不是'c:lineChart',但是很好的擴展。 – Steven 2018-02-19 20:38:14
謝謝@Steven,我已經解決了這個問題。 – 2018-02-20 15:09:53