目标是将边框应用于所有数据栏,并有条件地设置同一系列中单个数据栏的填充颜色。我可以做一个或另一个,但不能两者都做。我可以使用
Border.Width
和
Border.Fill.Color
,但当我有条件地为数据点/条上色时,边框样式将消失(从
SO: EPPlus ColumnStacked chart data point colors
).
如果
SetChartPointColors()
被注释掉后,图表将保持数据栏边框。如果我跑
SetCharPointColors()
,即使在运行之前边界也会消失
边框。宽度
和
边框.Fill.Color
被称为。
我解压缩了*.xlsx并尝试比较这两个chart01.xml,但仍然导致文件损坏。
ExcelBarChart excelBarChart = (ExcelBarChart)ws.Drawings.AddChart("ebc", eChartType.ColumnClustered);
excelBarChart.GapWidth = 5;
ExcelRange x_range = ws.Cells["A1:A10"];
ExcelRange y_range = ws.Cells["B1:B10"];
excelBarChart.Series.Add(y_range, x_range);
// SetChartPointColors(excelBarChart, 0);
excelBarChart.Series[0].Border.Width = 1;
excelBarChart.Series[0].Border.Fill.Color = System.Drawing.Color.Black;
SetChartPointColors():
public void SetChartPointColors(ExcelChart chart, int serieNumber)
{
var chartXml = chart.ChartXml;
var nsa = chart.WorkSheet.Drawings.NameSpaceManager.LookupNamespace("a");
var nsuri = chartXml.DocumentElement.NamespaceURI;
var nsm = new XmlNamespaceManager(chartXml.NameTable);
nsm.AddNamespace("a", nsa);
nsm.AddNamespace("c", nsuri);
var serieNode = chart.ChartXml.SelectSingleNode(@"c:chartSpace/c:chart/c:plotArea/c:barChart/c:ser[c:idx[@val='" + serieNumber + "']]", nsm);
var serie = chart.Series[serieNumber];
var points = serie.Series.Length;
for (var i = 0; i < points; i++)
{
var dPt = chartXml.CreateNode(XmlNodeType.Element, "dPt", nsuri);
var idx = chartXml.CreateNode(XmlNodeType.Element, "idx", nsuri);
var att = chartXml.CreateAttribute("val", nsuri);
att.Value = i.ToString();
idx.Attributes.Append(att);
dPt.AppendChild(idx);
var srgbClr = chartXml.CreateNode(XmlNodeType.Element, "srgbClr", nsa);
att = chartXml.CreateAttribute("val");
var color = System.Drawing.Color.Green;
// outIdx is a List<int> that contains index to be colored red
if (outIdx.Contains(i))
{
color = System.Drawing.Color.Red;
}
att.Value = $"{color.R:X2}{color.G:X2}{color.B:X2}";
srgbClr.Attributes.Append(att);
var solidFill = chartXml.CreateNode(XmlNodeType.Element, "solidFill", nsa);
solidFill.AppendChild(srgbClr);
var spPr = chartXml.CreateNode(XmlNodeType.Element, "spPr", nsuri);
spPr.AppendChild(solidFill);
dPt.AppendChild(spPr);
serieNode.AppendChild(dPt);
}
}