传奇的色彩是不正确的Excel图表使用EPPlus创建 [英] Legend color is incorrect in excel chart created using EPPlus

查看:1113
本文介绍了传奇的色彩是不正确的Excel图表使用EPPlus创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有手动设置图例项颜色的一种方式。

I am wondering if there is a way to manually set the legend entry colors.

我改变了颜色系列到自定义颜色,但传说色彩没有更新。见图片:

I changed the series colors to a custom color, but the legend color is not updating. See image:

推荐答案

下面就是OP链接到注释代码的改进版本。这个版本中,你在颜色传给你想要的(而不是它只是采用随机的)为 SetChartPointsColor ,这将设置所有点的颜色相同,以及创建和入境传说:

Here is an improved version of the code that the OP linked to in the comments. This version you pass in the color you want (instead of it just using random ones) to SetChartPointsColor and it will set all point colors to the same as well as create and entry for the legend:

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



给出了这样的输出:

Gives this in the output:

意见回馈

对于线系列这将是一个有点不同:

For a line series it would be a little different:

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

这篇关于传奇的色彩是不正确的Excel图表使用EPPlus创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆