c# - 图表上的 Epplus 误差线

标签 c# epplus

使用 Epplus,有没有办法在图表系列上添加误差线?

我没有发现任何关于 Epplus 能够在任何地方处理误差线的提及,而且在图表或任何单独的系列中似乎没有任何属性或方法表明您可以访问/创建误差线。

我正在尝试重新创建此图表,其中我只剩下误差线:

enter image description here

最佳答案

我已经提交了一个带有适当代码的拉取请求来处理 EPPlus 中的错误栏,但是如果有人需要在该请求被接受之前执行此操作,那么您可以使用以下代码手动执行此操作:

public static void AddErrorBars(ExcelBarChart chart, string customFullRange)
{
    var nsmgr = new XmlNamespaceManager(chart.ChartXml.NameTable);
    nsmgr.AddNamespace("c", "http://schemas.openxmlformats.org/drawingml/2006/chart");
    nsmgr.AddNamespace("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
    nsmgr.AddNamespace("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
    nsmgr.AddNamespace("c16r2", "http://schemas.microsoft.com/office/drawing/2015/06/chart");

    XNamespace ns_c = "http://schemas.openxmlformats.org/drawingml/2006/chart";
    XNamespace ns_a = "http://schemas.openxmlformats.org/drawingml/2006/main";

    var xDoc = XDocument.Parse(chart.ChartXml.InnerXml);

    var cSer = xDoc.XPathSelectElement("/c:chartSpace/c:chart/c:plotArea/c:barChart/c:ser", nsmgr);

    cSer.Add(
        new XElement(ns_c + "errBars",

            new XElement(ns_c + "errBarType",
                new XAttribute("val", "plus")
            ),

            new XElement(ns_c + "errValType",
                new XAttribute("val", "cust")
            ),

            new XElement(ns_c + "noEndCap",
                new XAttribute("val", "0")
            ),

            new XElement(ns_c + "plus",
                new XElement(ns_c + "numRef",
                    new XElement(ns_c + "f", customFullRange)
                )
            )
        )
    );

    chart.ChartXml.LoadXml(xDoc.ToString());
}

关于c# - 图表上的 Epplus 误差线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34743539/

相关文章:

c# - EPPlus 超链接到另一个工作表中的单元格

c# - EPPlus 格式单元格为 "Accounting"数字

c# - EPPlus 缺少依赖项

c# - 是否可以在 Windows docker 容器中运行的 dotnet 核心应用程序中使用 EPPlus?

c# - 如何强制 Visual Studio *真的*停止执行?

c# - 如何在 rdlc 中的表标题顶部而不是左侧显示按列分组

c# - 传递独立于模型 MVC/C# 的 ID

c# - 如何在 MVC .Net 中的 session 超时后重定向?

c# - 无法访问已处置的对象。对象名称 : 'System.Net.HttpListener'

c# - 使用 EPPlus (asp.net) 在 excel 中转换为整数