使用EPPlus创建的Excel图表中的图例颜色不正确

时间:2016-04-09 17:42:31

标签: c# excel epplus

我想知道是否有办法手动设置图例条目颜色。

我将系列颜色更改为自定义颜色,但图例颜色未更新。见图:

Incorrect legend color

1 个答案:

答案 0 :(得分:5)

以下是OP在评论中链接的代码的改进版本。这个版本以你想要的颜色(而不是只使用随机颜色)传递给SetChartPointsColor,它会将所有点颜色设置为相同以及为图例创建和输入:

public static void SetChartPointsColor(this ExcelChart chart, int serieNumber, Color color)
{
    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;

    //Add reference to the color for the legend and data points
    var srgbClr = chartXml.CreateNode(XmlNodeType.Element, "srgbClr", nsa);
    var att = chartXml.CreateAttribute("val");
    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);

    serieNode.AppendChild(spPr);
}

像这样使用:

using (var pck = new ExcelPackage(fileInfo))
{
    var workbook = pck.Workbook;
    var worksheet = workbook.Worksheets.Add("Sheet1");
    worksheet.Cells.LoadFromDataTable(datatable, true);

    var chart = worksheet.Drawings.AddChart("chart test", eChartType.ColumnStacked);
    chart.Series.Add(worksheet.Cells["B2:B11"], worksheet.Cells["A2:A11"]);
    chart.Series.Add(worksheet.Cells["C2:C11"], worksheet.Cells["A2:A11"]);

    var rand = new Random();
    var color = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));

    chart.SetChartPointsColor(0, color);
    color = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
    chart.SetChartPointsColor(1, color);

    pck.Save();
}

在输出中给出:

Example Output

对评论的回应

对于一个系列,它会有所不同:

public static void SetLineChartColor(this ExcelChart chart, int serieNumber, Color color)
{
    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:lineChart/c:ser[c:idx[@val='{serieNumber}']]", nsm);
    var serie = chart.Series[serieNumber];
    var points = serie.Series.Length;

    //Add reference to the color for the legend
    var srgbClr = chartXml.CreateNode(XmlNodeType.Element, "srgbClr", nsa);
    var att = chartXml.CreateAttribute("val");
    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 ln = chartXml.CreateNode(XmlNodeType.Element, "ln", nsa);
    ln.AppendChild(solidFill);

    var spPr = chartXml.CreateNode(XmlNodeType.Element, "spPr", nsuri);
    spPr.AppendChild(ln);

    serieNode.AppendChild(spPr);
}