Skip to content

Commit 87a382a

Browse files
authored
Bug/s694 (#1519)
* Temp commented out fix * Added alternative for not applying override theme on first series * Added support for multiple series in template and resolved local template * Added proper support for multiple series * Fixing bug and adding support for stacked column charts * Fixed test template and minor issues
1 parent 466bfee commit 87a382a

12 files changed

+239
-45
lines changed

src/EPPlus/Drawing/Chart/ExcelBarChart.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal ExcelBarChart(ExcelDrawings drawings, XmlNode node, eChartType? type, E
3030
base(drawings, node, type, topChart, PivotTableSource, chartXml, parent)
3131
{
3232
SetChartNodeText("");
33-
if(type.HasValue) SetTypeProperties(drawings, type.Value);
33+
if(type.HasValue) SetTypeProperties(drawings, ChartType);
3434
}
3535

3636
internal ExcelBarChart(ExcelDrawings drawings, XmlNode node, Uri uriChart, Packaging.ZipPackagePart part, XmlDocument chartXml, XmlNode chartNode, ExcelGroupShape parent = null) :

src/EPPlus/Drawing/Chart/ExcelChartSeries.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Date Author Change
1818
using System.Linq;
1919
using OfficeOpenXml.Drawing.Chart.ChartEx;
2020
using OfficeOpenXml.Utils;
21+
using OfficeOpenXml.FormulaParsing.Excel.Functions.Helpers;
22+
using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
2123
namespace OfficeOpenXml.Drawing.Chart
2224
{
2325
/// <summary>
@@ -424,7 +426,15 @@ internal protected T AddSeries(string SerieAddress, string XSerieAddress, string
424426
{
425427
if (_chart.StyleManager.StylePart != null)
426428
{
427-
_chart.StyleManager.ApplySeries();
429+
if(_chart._drawings._seriesTemplateXml != null && _chart._drawings._seriesTemplateXml.Count != 0)
430+
{
431+
//If we are here we've already applied the templateXML to the series.
432+
_chart._drawings._seriesTemplateXml.RemoveAt(0);
433+
}
434+
else
435+
{
436+
_chart.StyleManager.ApplySeries();
437+
}
428438
}
429439

430440
if (_chart._legend != null && _chart._legend._entries!=null)

src/EPPlus/Drawing/Chart/ExcelChartStandardSerie.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Date Author Change
1717
using System.Linq;
1818
using OfficeOpenXml.Core.CellStore;
1919
using System.Globalization;
20+
using System.Runtime.CompilerServices;
2021
namespace OfficeOpenXml.Drawing.Chart
2122
{
2223
/// <summary>
@@ -54,16 +55,16 @@ internal ExcelChartStandardSerie(ExcelChart chart, XmlNamespaceManager ns, XmlNo
5455
_seriesPath = string.Format(_seriesPath, _seriesTopPath);
5556
_numCachePath = string.Format(_numCachePath, _seriesTopPath);
5657

57-
var np = string.Format(_xSeriesPath, _xSeriesTopPath, isPivot ? "c:multiLvlStrRef" : "c:numRef");
58-
var sp= string.Format(_xSeriesPath, _xSeriesTopPath, isPivot ? "c:multiLvlStrRef" : "c:strRef");
58+
var np = string.Format(_xSeriesParentPath, _xSeriesTopPath, isPivot ? "c:multiLvlStrRef" : "c:numRef");
59+
var sp = string.Format(_xSeriesParentPath, _xSeriesTopPath, isPivot ? "c:multiLvlStrRef" : "c:strRef");
5960

60-
if(ExistsNode(sp))
61+
if (ExistsNode(sp))
6162
{
62-
_xSeriesPath = sp;
63+
_xSeriesPath = sp + "/c:f";
6364
}
6465
else
6566
{
66-
_xSeriesPath = np;
67+
_xSeriesPath = np + "/c:f";
6768
}
6869
_seriesStrLitPath = string.Format("{0}/c:strLit", _seriesTopPath);
6970
_seriesNumLitPath = string.Format("{0}/c:numLit", _seriesTopPath);
@@ -170,6 +171,7 @@ public override string Series
170171

171172
string _xSeries=null;
172173
string _xSeriesTopPath;
174+
string _xSeriesParentPath = "{0}/{1}";
173175
string _xSeriesPath = "{0}/{1}/c:f";
174176
string _xSeriesStrLitPath, _xSeriesNumLitPath;
175177
/// <summary>
@@ -642,9 +644,13 @@ internal static XmlElement CreateSerieElement(ExcelChart chart)
642644
var ser = (XmlElement)chart._chartXmlHelper.CreateNode("c:ser", false, true);
643645

644646
//If the chart is added from a chart template, then use the chart templates series xml
645-
if (!string.IsNullOrEmpty(chart._drawings._seriesTemplateXml))
647+
if (chart._drawings._seriesTemplateXml != null)
646648
{
647-
ser.InnerXml = chart._drawings._seriesTemplateXml;
649+
if(chart._drawings._seriesTemplateXml.Count != 0)
650+
{
651+
ser.InnerXml = chart._drawings._seriesTemplateXml[0];
652+
return ser;
653+
}
648654
}
649655

650656
int idx = FindIndex(chart._topChart??chart);

src/EPPlus/Drawing/Chart/Style/ExcelChartStyleEntry.cs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,40 @@ namespace OfficeOpenXml.Drawing.Chart.Style
2525
/// </summary>
2626
public class ExcelChartStyleEntry : XmlHelper
2727
{
28-
string _fillReferencePath = "{0}/cs:fillRef";
29-
string _borderReferencePath = "{0}/cs:lnRef ";
30-
string _effectReferencePath = "{0}/cs:effectRef";
31-
string _fontReferencePath = "{0}/cs:fontRef";
28+
string _fillReferencePath = "{0}/{1}:fillRef";
29+
string _borderReferencePath = "{0}/{1}:lnRef ";
30+
string _effectReferencePath = "{0}/{1}:effectRef";
31+
string _fontReferencePath = "{0}/{1}:fontRef";
3232

33-
string _richTextPath = "{0}/cs:rich";
34-
string _fillPath = "{0}/cs:spPr";
35-
string _borderPath = "{0}/cs:spPr/a:ln";
36-
string _effectPath = "{0}/cs:spPr/a:effectLst";
37-
string _scene3DPath = "{0}/cs:spPr/a:scene3d";
38-
string _sp3DPath = "{0}/cs:spPr/a:sp3d";
33+
string _richTextPath = "{0}/{1}:rich";
34+
string _fillPath = "{0}/{1}:spPr";
35+
string _borderPath = "{0}/{1}:spPr/a:ln";
36+
string _effectPath = "{0}/{1}:spPr/a:effectLst";
37+
string _scene3DPath = "{0}/{1}:spPr/a:scene3d";
38+
string _sp3DPath = "{0}/{1}:spPr/a:sp3d";
39+
40+
string _defaultTextRunPath = "{0}/{1}:defRPr";
41+
string _defaultTextBodyPath = "{0}/{1}:bodyPr";
42+
string _prefix = "cs";
3943

40-
string _defaultTextRunPath = "{0}/cs:defRPr";
41-
string _defaultTextBodyPath = "{0}/cs:bodyPr";
4244
private readonly IPictureRelationDocument _pictureRelationDocument;
4345
internal ExcelChartStyleEntry(XmlNamespaceManager nsm, XmlNode topNode, string path, IPictureRelationDocument pictureRelationDocument) : base(nsm, topNode)
4446
{
4547
SchemaNodeOrder = new string[] { "lnRef", "fillRef", "effectRef", "fontRef", "spPr", "noFill", "solidFill", "blipFill", "gradFill", "noFill", "pattFill","ln", "defRPr" };
46-
_fillReferencePath = string.Format(_fillReferencePath, path);
47-
_borderReferencePath = string.Format(_borderReferencePath, path);
48-
_effectReferencePath = string.Format(_effectReferencePath, path);
49-
_fontReferencePath = string.Format(_fontReferencePath, path);
48+
_fillReferencePath = string.Format(_fillReferencePath, path, _prefix);
49+
_borderReferencePath = string.Format(_borderReferencePath, path, _prefix);
50+
_effectReferencePath = string.Format(_effectReferencePath, path, _prefix);
51+
_fontReferencePath = string.Format(_fontReferencePath, path, _prefix);
5052

51-
_richTextPath = string.Format(_richTextPath, path);
52-
_fillPath = string.Format(_fillPath, path);
53-
_borderPath = string.Format(_borderPath, path);
54-
_effectPath = string.Format(_effectPath, path);
55-
_scene3DPath = string.Format(_scene3DPath, path);
56-
_sp3DPath = string.Format(_sp3DPath, path);
53+
_richTextPath = string.Format(_richTextPath, path, _prefix);
54+
_fillPath = string.Format(_fillPath, path, _prefix);
55+
_borderPath = string.Format(_borderPath, path, _prefix);
56+
_effectPath = string.Format(_effectPath, path, _prefix);
57+
_scene3DPath = string.Format(_scene3DPath, path, _prefix);
58+
_sp3DPath = string.Format(_sp3DPath, path, _prefix);
5759

58-
_defaultTextRunPath = string.Format(_defaultTextRunPath, path);
59-
_defaultTextBodyPath = string.Format(_defaultTextBodyPath, path);
60+
_defaultTextRunPath = string.Format(_defaultTextRunPath, path, _prefix);
61+
_defaultTextBodyPath = string.Format(_defaultTextBodyPath, path, _prefix);
6062
_pictureRelationDocument = pictureRelationDocument;
6163
}
6264
private ExcelChartStyleReference _borderReference = null;

src/EPPlus/Drawing/Chart/Style/ExcelChartStyleManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ private ZipPackageRelationship CreateThemeOverridePart(ZipPackage p, ZipPackageP
997997

998998
foreach (var themeRel in partToCopy.GetRelationships())
999999
{
1000-
var uri = OfficeOpenXml.Utils.UriHelper.ResolvePartUri(new Uri("/xl/chart/theme1.xml", UriKind.Relative), themeRel.TargetUri);
1000+
var uri = UriHelper.ResolvePartUri(new Uri("/xl/chart/theme1.xml", UriKind.Relative), themeRel.TargetUri);
10011001
var toPart = _chart.Part.Package.CreatePart(uri, PictureStore.GetContentType(uri.OriginalString));
10021002
var imageRel = ThemeOverridePart.CreateRelationship(uri, TargetMode.Internal, themeRel.RelationshipType);
10031003
SetRelIdInThemeDoc(ThemeOverrideXml, themeRel.Id, imageRel.Id);

src/EPPlus/Drawing/ExcelDrawings.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ internal bool Comparer(byte[] compareImg)
6868
}
6969
internal ExcelPackage _package;
7070
internal Packaging.ZipPackageRelationship _drawingRelation = null;
71-
internal string _seriesTemplateXml;
71+
internal List<string> _seriesTemplateXml;
7272
internal ExcelDrawings(ExcelPackage xlPackage, ExcelWorksheet sheet)
7373
{
7474
xlPackage.Workbook.LoadAllDrawings(sheet.Name);
@@ -1204,11 +1204,17 @@ public ExcelChart AddChartFromTemplate(Stream crtxStream, string name, ExcelPivo
12041204
throw new InvalidDataException("Crtx file is corrupt.");
12051205
}
12061206
var chartXmlHelper = XmlHelperFactory.Create(NameSpaceManager, chartXml.DocumentElement);
1207-
var serNode = chartXmlHelper.GetNode("/c:chartSpace/c:chart/c:plotArea/*[substring(name(), string-length(name()) - 4) = 'Chart']/c:ser");
1208-
if (serNode != null)
1207+
var serNodes = chartXmlHelper.GetNodes("/c:chartSpace/c:chart/c:plotArea/*[substring(name(), string-length(name()) - 4) = 'Chart']/c:ser");
1208+
1209+
_seriesTemplateXml = new List<string>();
1210+
1211+
foreach(XmlNode serNode in serNodes)
12091212
{
1210-
_seriesTemplateXml = serNode.InnerXml;
1211-
serNode.ParentNode.RemoveChild(serNode);
1213+
if (serNode != null)
1214+
{
1215+
_seriesTemplateXml.Add(serNode.InnerXml);
1216+
serNode.ParentNode.RemoveChild(serNode);
1217+
}
12121218
}
12131219
XmlElement drawNode = CreateDrawingXml(eEditAs.TwoCell);
12141220
var chartType = ExcelChart.GetChartTypeFromNodeName(GetChartNodeName(chartXmlHelper));
@@ -1226,7 +1232,8 @@ public ExcelChart AddChartFromTemplate(Stream crtxStream, string name, ExcelPivo
12261232
{
12271233
chart.StyleManager.LoadThemeOverrideXml(themePart);
12281234
}
1229-
chart.StyleManager.LoadStyleXml(styleXml, chartStyle, colorsXml);
1235+
1236+
chart.StyleManager.LoadStyleAndColorsXml(styleXml, chartStyle, colorsXml);
12301237

12311238
return chart;
12321239
}

src/EPPlusTest/Drawing/Chart/DatalabelTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public static void Init(TestContext context)
3030
dSheet.Cells["E2:E10"].Value = 1;
3131
dSheet.Cells["F2:F10"].Value = 0.5;
3232

33-
3433
var pTable = cSheet.PivotTables.Add(cSheet.Cells["A1"], range, "NewPivotTable");
3534

3635
pTable.DataFields.Add(pTable.Fields["Column1"]);

src/EPPlusTest/Drawing/Chart/Styling/ChartTemplateTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void LoadChartStyle()
6868
public void AddChartFromTemplate()
6969
{
7070
var ws = _pck.Workbook.Worksheets.Add("NewChartFromTemplate");
71-
LoadTestdata(ws);
71+
LoadTestdata(ws);
7272
var chart = ws.Drawings.AddChartFromTemplate(Resources.TestLine3Crtx, "LineChart1", null);
7373
chart.Series.Add("D2:D100","A2:A100");
7474
chart.Series.Add("c2:c100", "A2:A100");

src/EPPlusTest/EPPlus.Test.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@
9999
<None Update="Resources\LineChart3.crtx">
100100
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
101101
</None>
102+
<None Update="Resources\PieChartTemplate2.crtx">
103+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
104+
</None>
105+
<None Update="Resources\StackedColumnChart.crtx">
106+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
107+
</None>
102108
<None Update="Resources\marbles.pcx">
103109
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
104110
</None>

0 commit comments

Comments
 (0)