Search code examples
excelc#-4.0chartsepplusepplus-4

EPPlus - How to add "Series Lines" in Pivot Chart (ColumnStacked)


I am using EPPlus library to generate pivot chart in excel. I created the chart but don't know how I can add "Series Lines" to it. The arrow in the below image indicates series lines.

Chart - Series Lines

Here is the sample code.

var wsBar = pck.Workbook.Worksheets.Add("Bar");
--dataRange = Data from "Data" worksheet.
var pivotTable1 = wsBar.PivotTables.Add(wsBar.Cells["Z100"], dataRange, "pivotTable1");

var dataFieldBar1 = pivotTable1.DataFields.Add(pivotTable1.Fields[22]);
dataFieldBar1.Format = "$ #,###.00";
pivotTable1.DataOnRows = true;

pivotTable1.RowFields.Add(pivotTable1.Fields[15]);

pivotTable1.ColumnFields.Add(pivotTable1.Fields[12]);

pivotTable1.PageFields.Add(pivotTable1.Fields[7]);

var columnchart = wsBar.Drawings.AddChart("ColumnChart", eChartType.ColumnStacked, pivotTable1);

columnchart.SetPosition(0, 0, 0, 0);
columnchart.SetSize(600, 300);

Any help is highly appreciated.


Solution

  • Dont think EPPlus has that as an option so it would be some kind of XML manipulation without another library:

    var chartXml = columnchart.ChartXml;
    var nsm = new XmlNamespaceManager(chartXml.NameTable);
    
    var nsuri = chartXml.DocumentElement.NamespaceURI;
    nsm.AddNamespace("c", nsuri);
    
    var serNode = chartXml.SelectSingleNode("c:chartSpace/c:chart/c:plotArea/c:barChart", nsm);
    var serLinesNode = chartXml.CreateNode(XmlNodeType.Element, "serLines", nsuri);
    serNode.AppendChild(serLinesNode);