From c068061448545bbdb6a295ba5aec533f2455e8ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ossian=20Edstr=C3=B6m?= Date: Wed, 18 Nov 2026 09:36:17 +0100 Subject: [PATCH 01/23] Start of fix --- .../Chart/ChartTypeDrawers/ChartTypeDrawer.cs | 15 ++++---- .../Renderer/Chart/PieSliceRenderItem.cs | 35 ++++++++++++++++--- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs index 81f84b911..71faeaf6b 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs @@ -7,6 +7,7 @@ using EPPlusImageRenderer.RenderItems; using EPPlusImageRenderer.Svg; using OfficeOpenXml; +using OfficeOpenXml.DigitalSignatures; using OfficeOpenXml.Drawing; using OfficeOpenXml.Drawing.Chart; using OfficeOpenXml.Drawing.Chart.ChartEx; @@ -236,7 +237,7 @@ internal bool IsOnAxis(ExcelChartAxisStandard ax) internal static void SetFillDataPoint(ExcelChart chart, ExcelChartStandardSerie cStandardSerie, int index, RenderItemStyle style, ExcelChartDataPoint dp, ExcelChartStyleEntry entry, UserSpaceSettings spaceSettings = UserSpaceSettings.UserSpaceOnUse_Object) { var theme = chart.WorkSheet.Workbook.ThemeManager.GetOrCreateTheme(); - var color = GetVaryColor(theme, chart.StyleManager?.ColorsManager, index); + var color = GetVaryColor(theme, chart.StyleManager, index); style.SetDrawingPropertiesFill(theme, dp.Fill.IsEmpty ? cStandardSerie.Fill : dp.Fill, entry?.FillReference.Color, spaceSettings, color); style.SetDrawingPropertiesBorder(theme, dp.Border.IsEmpty ? cStandardSerie.Border : dp.Border, entry?.BorderReference.Color, dp.Border.Fill.Style != eFillStyle.NoFill, () => Color.Transparent, 0.75); @@ -245,27 +246,29 @@ internal static void SetFillDataPoint(ExcelChart chart, ExcelChartStandardSerie internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartStandardSerie cStandardSerie, int serieIndex, int index, RenderItemStyle style) { var theme = chart.WorkSheet.Workbook.ThemeManager.GetOrCreateTheme(); - if (ct.VaryColors) { + var chartStyle =(int)chart.Style; //Get the color based on the index, if no style is set. Accent1, Accent2, Accent3... - var color = GetVaryColor(theme, chart.StyleManager.ColorsManager, index); + var color = GetVaryColor(theme, chart.StyleManager, index); style.SetDrawingPropertiesFill(theme, cStandardSerie.Fill, chart.StyleManager.Style?.SeriesLine.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, color); } else { - var color = GetVaryColor(theme, chart.StyleManager?.ColorsManager, serieIndex); + var color = GetVaryColor(theme, chart.StyleManager, serieIndex); style.SetDrawingPropertiesFill(theme, cStandardSerie.Fill, chart.StyleManager.Style?.SeriesLine.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, color); } var styleFormat = DefaultChartBorder.GetDefaulDataPoint2d(theme, chart, serieIndex); style.SetDrawingPropertiesBorder(theme, cStandardSerie.Border, chart.StyleManager.Style?.SeriesLine.BorderReference.Color, cStandardSerie.Border.Fill.Style != eFillStyle.NoFill || styleFormat.width>0, () => styleFormat.color, 0.75); } - internal static Color? GetVaryColor(ExcelTheme theme, ExcelChartColorsManager colorsManager, int index) + internal static Color? GetVaryColor(ExcelTheme theme, ExcelChartStyleManager? styleManager, int index) { Color baseColor; var baseColorIndex = index % 6; - if (colorsManager == null || baseColorIndex >= colorsManager.Colors.Count) + + ExcelChartColorsManager colorsManager = styleManager?.ColorsManager; + if ((colorsManager == null || baseColorIndex >= colorsManager.Colors.Count) && styleManager.Style == null) { //TODO: Check Chart.Style here. Only when THAT is set to None should the below line happen baseColor = theme.ColorScheme.GetColorByEnum(eSchemeColor.Accent1 + baseColorIndex).GetColor(); diff --git a/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs b/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs index 43ffb4826..a33855dbb 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs @@ -17,7 +17,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Drawing; -using System.Net; +using System.Security.Cryptography; namespace EPPlus.Export.ImageRenderer.Svg.Chart { @@ -553,8 +553,10 @@ private RectRenderItem GenerateDebugPoint(BoundingBox parent, Coordinate point, internal void ImportStlyeInfo(ExcelPieChartSerie serie, ExcelPieChart chartType, int position) { + _position = position; + + var defaultFill = GetDefaultFillColor(); - var defaultFill = DefaultFillColor; if (position >= 0 && serie.DataPoints.ContainsKey(position)) { @@ -564,8 +566,21 @@ internal void ImportStlyeInfo(ExcelPieChartSerie serie, ExcelPieChart chartType, } else { - ChartTypeDrawer.SetFillSerie(Chart, chartType, serie, 0, position, _slicePath.Style); + _slicePath.Style.SetDrawingPropertiesFill(ChartRenderer.Theme, serie.Fill, chartType.StyleManager.Style?.DataPoint.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, defaultFill); + //ChartTypeDrawer.SetFillSerie(Chart, chartType, serie, 0, position, _slicePath.Style); } + //if (chartType.VaryColors) + //{ + // if (chartType.StyleManager.Style == null) + // { + // var mod5 = position % 5; + // //TODO: Only works for base-case. Add support for patterns 1,3 and 4 instead of just 2 as basecase + // defaultFill = ChartRenderer.Theme.ColorScheme.GetColorByEnum(OfficeOpenXml.Drawing.eSchemeColor.Accent1 + mod5).GetColor(); + // } + //} + ////ChartTypeDrawer.SetFillDataPoint + //_slicePath.Style.SetDrawingPropertiesFill(ChartRenderer.Theme, dp.Fill, chartType.StyleManager.Style?.DataPoint.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, defaultFill); + //_slicePath.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, dp.Border, chartType.StyleManager.Style?.DataPoint.BorderReference.Color, true, null); } internal void AppendGroupItem(GroupRenderItem group) @@ -926,9 +941,21 @@ public override void AppendRenderItems(List renderItems) throw new NotImplementedException(); } + int _position = -1; + internal override Color? GetDefaultFillColor() { - throw new NotImplementedException(); + if (Chart.VaryColors) + { + if (Chart.StyleManager.Style == null) + { + var mod5 = _position % 6; + return ChartRenderer.Theme.ColorScheme.GetColorByEnum(OfficeOpenXml.Drawing.eSchemeColor.Accent1 + mod5).GetColor(); + ////TODO: Only works for base-case. Add support for patterns 1,3 and 4 instead of just 2 as basecase + //defaultFill = ChartRenderer.Theme.ColorScheme.GetColorByEnum(OfficeOpenXml.Drawing.eSchemeColor.Accent1 + mod5).GetColor(); + } + } + return ChartRenderer.Theme.ColorScheme.GetColorByEnum(OfficeOpenXml.Drawing.eSchemeColor.Accent1).GetColor(); } internal override Color? GetDefaultBorderColor() From 066516b972964949ce4c63bd5862e42e4bed034f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ossian=20Edstr=C3=B6m?= Date: Thu, 19 Nov 2026 15:31:26 +0100 Subject: [PATCH 02/23] Figured out difficult legacy --- .../Chart/ChartTypeDrawers/ChartTypeDrawer.cs | 5 + .../Renderer/Chart/PieSliceRenderItem.cs | 14 +- .../Utils/TypeConversion/ColorConverter.cs | 4 + src/EPPlusTest/Drawing/ThemeTest.cs | 163 ++++++++++++++++++ 4 files changed, 184 insertions(+), 2 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs index e2e6027c6..2b26824c3 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs @@ -253,6 +253,9 @@ internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartSta var theme = chart.WorkSheet.Workbook.ThemeManager.GetOrCreateTheme(); if (ct.VaryColors) { + var accent1Scheme = chart.WorkSheet.Workbook.ThemeManager.CurrentTheme.ColorScheme.Accent1; + var accent1Theme = chart.WorkSheet.Workbook.ThemeManager.CurrentTheme.FormatScheme.FillStyle[0].SolidFill.Color; + var chartStyle =(int)chart.Style; //Get the color based on the index, if no style is set. Accent1, Accent2, Accent3... var color = GetVaryColor(theme, chart.StyleManager, index); @@ -293,6 +296,8 @@ internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartSta ExcelColorTransformCollection variation; if (colorsManager == null) { + //ExcelColorTransformCollectionDataPoints dpCollection = new(); + //return dpCollection.GetColor(baseColor, variationIndex, baseColorIndex); var variations = ExcelColorTransformCollection.GetDefault(); variation = variations[variationIndex % variations.Count]; } diff --git a/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs b/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs index a33855dbb..7dc0d77d7 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs @@ -7,12 +7,14 @@ using EPPlusImageRenderer; using EPPlusImageRenderer.RenderItems; using EPPlusImageRenderer.Svg; +using OfficeOpenXml.Drawing; using OfficeOpenXml.Drawing.Chart; using OfficeOpenXml.FormulaParsing.Excel.Functions.Finance; using OfficeOpenXml.FormulaParsing.Excel.Functions.MathFunctions; using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; using OfficeOpenXml.Style; using OfficeOpenXml.Utils.Drawing; +using OfficeOpenXml.Utils.TypeConversion; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -566,8 +568,16 @@ internal void ImportStlyeInfo(ExcelPieChartSerie serie, ExcelPieChart chartType, } else { - _slicePath.Style.SetDrawingPropertiesFill(ChartRenderer.Theme, serie.Fill, chartType.StyleManager.Style?.DataPoint.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, defaultFill); - //ChartTypeDrawer.SetFillSerie(Chart, chartType, serie, 0, position, _slicePath.Style); + var subtleFill = ChartRenderer.Theme.FormatScheme.FillStyle[0]; + var bg = ChartRenderer.Theme.FormatScheme.BackgroundFillStyle[0]; + var darkScheme = OfficeOpenXml.Utils.TypeConversion.ColorConverter.GetSchemeColor(ChartRenderer.Theme, eSchemeColor.Dark1); + var dark75 = OfficeOpenXml.Utils.TypeConversion.ColorConverter.ApplyTintDrawing(darkScheme, 1 - 0.75d); + + var color = OfficeOpenXml.Utils.TypeConversion.ColorConverter.ApplyTransforms(dark75, subtleFill.SolidFill.Color.Transforms); + //var test = ColorConverter. + + //_slicePath.Style.SetDrawingPropertiesFill(ChartRenderer.Theme, serie.Fill, chartType.StyleManager.Style?.DataPoint.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, defaultFill); + ChartTypeDrawer.SetFillSerie(Chart, chartType, serie, 0, position, _slicePath.Style); } //if (chartType.VaryColors) //{ diff --git a/src/EPPlus/Utils/TypeConversion/ColorConverter.cs b/src/EPPlus/Utils/TypeConversion/ColorConverter.cs index 3feec76f6..35b1222d8 100644 --- a/src/EPPlus/Utils/TypeConversion/ColorConverter.cs +++ b/src/EPPlus/Utils/TypeConversion/ColorConverter.cs @@ -277,6 +277,10 @@ internal static Color AlternativeTint(Color ret, double tint) { double blend = 1.0d - tint; //Docs state 10% input means A 10% tint is 10% of the input color combined with 90% white + //"20.1.2.3.34 tint (Tint) + //This element specifies a lighter version of its input color. A 10 % tint is 10 % of the input color combined with + //90 % white." + var r = (byte)Math.Round(ret.R * tint + (254.3d * blend)); var g = (byte)Math.Round(ret.G * tint + (254.3d * blend)); var b = (byte)Math.Round(ret.B * tint + (254.3d * blend)); diff --git a/src/EPPlusTest/Drawing/ThemeTest.cs b/src/EPPlusTest/Drawing/ThemeTest.cs index 26e2f739f..141962199 100644 --- a/src/EPPlusTest/Drawing/ThemeTest.cs +++ b/src/EPPlusTest/Drawing/ThemeTest.cs @@ -39,6 +39,7 @@ Date Author Change using System; using System.Collections.Generic; using System.Drawing; +using System.Linq; using System.Reflection; using tc = OfficeOpenXml.Utils.TypeConversion; namespace EPPlusTest.Drawing @@ -388,6 +389,17 @@ public void Shade50percent() Assert.AreEqual((double)Color.FromArgb(0x0, 0xBC, 0x0).ToArgb(), c.ToArgb()); } + + [TestMethod] + public void Shade50percentDocs() + { + //20.1.2.3.34 tint (Tint) ECMA + var color1 = Color.FromArgb(0, 255, 0); + var c = tc.ColorConverter.ApplyTintDrawing(color1, 0.5); + + Assert.AreEqual((double)Color.FromArgb(0xBC, 0xFF, 0xBC).ToArgb(), c.ToArgb()); + } + [TestMethod] public void Shade85percent() { @@ -424,6 +436,157 @@ public void Green25Apply50Light() var expectedResult = Color.FromArgb(255, 188, 188, 188); Assert.AreEqual((double)expectedResult.ToArgb(), c.ToArgb()); } + + [TestMethod] + public void ThemeTintDocs() + { + //17.2.1 'background (Document Background)' ECMA. Technically for word + //Actually accent1 but appears wrong in the docs + var accent2Theme = Color.FromArgb(255, 79, 129, 189); + var tint = tc.ColorConverter.ApplyTint(accent2Theme, 0.6d); + var lumMod = tc.ColorConverter.ApplyLumMod(accent2Theme, 0.6d, 1 - 0.6d); + + var expectedColor = Color.FromArgb(255, 149, 179, 215); + + Assert.AreEqual((double)expectedColor.ToArgb(), lumMod.ToArgb()); + } + + + [TestMethod] + public void ThemeShadeDocs() + { + //17.2.1 'background (Document Background)' ECMA. Technically for word + var accent2Theme = Color.FromArgb(255, 192, 80, 77); + var lumMod = tc.ColorConverter.ApplyLumMod(accent2Theme, 0.75d); + + //Actual expected color + //var expectedColor = Color.FromArgb(255, 148, 54, 52); + //replacement as we are incredibly close + var expectedColor = Color.FromArgb(255, 149, 55, 53); + + Assert.AreEqual((double)expectedColor.ToArgb(), lumMod.ToArgb()); + } + + //---IMPORTANT--- + + [TestMethod] + public void Apply88DarkAlt() + { + var accent1 = Color.FromArgb(255, 79, 129, 189); + var accent2 = Color.FromArgb(255, 192, 80, 77); + var c = tc.ColorConverter.ApplyTintDrawing(accent1, -0.35d); + var c2 = tc.ColorConverter.ApplyTintDrawing(accent2, -0.35d); + var expectedResult = Color.FromArgb(255, 64, 106, 156); + var eR2 = Color.FromArgb(255, 158, 65, 62); + Assert.AreEqual((double)expectedResult.ToArgb(), c.ToArgb()); + + var newResult = tc.ColorConverter.ApplyTintDrawing(Color.FromArgb(255, 79, 129, 189), 0.23d); + var newExpectedResult = Color.FromArgb(255, 147, 169, 207); + Assert.AreEqual(newExpectedResult, newResult); + + //KEY TAKE AWAYS TO MAKE FUNC WORK + //VariationIndex begins at 1 NOT 0 + //highestVariation INCREASED by 1 when REACHING 6 not the start of the next "accent1" + int variationIndex = 1; + int highestVariation = 4; + var tintValue = -70d + 140d * (variationIndex / (highestVariation + 1d)); + + var startingShade = tc.ColorConverter.ApplyTint(accent1,-(1 + tintValue)/100d); + + var lumMod = tc.ColorConverter.ApplyLumMod(accent1, 0.3); + var tint = tc.ColorConverter.ApplyTintDrawing(accent1, -0.7d); + } + + [TestMethod] + public void CalculateLEGACYDataPointsOfWholePie() + { + //Assuming 24 data points + var numVariations = 24 / 6; + + //KEY TAKE AWAYS TO MAKE FUNC WORK + //VariationIndex begins at 1 NOT 0 + //highestVariation INCREASED by 1 when REACHING 6 not the start of the next "accent1" + if (24 % 6 == 0) + { + numVariations += 1; + } + + var accent1 = Color.FromArgb(255, 79, 129, 189); + var accent2 = Color.FromArgb(255, 192, 80, 77); + var accent3 = Color.FromArgb(255, 155, 187, 89); + var accent4 = Color.FromArgb(255, 128, 100, 162); + var accent5 = Color.FromArgb(255, 75, 172, 198); + var accent6 = Color.FromArgb(255, 247, 150, 70); + + List colorList = new List() {accent1, accent2, accent3, accent4, accent5, accent6 }; + List> outputColors = new List>(); + for (int i = 1; i < numVariations; i++) + { + List currentVariations = new List(); + for (int j = 0; j < colorList.Count; j++) + { + var color = colorList[j]; + var tintValue = ( -70d + 140d * (i / (numVariations + 1d)))/100d; + + var variation = tc.ColorConverter.ApplyTintDrawing(color, tintValue); + + currentVariations.Add(variation); + } + outputColors.Add(currentVariations); + } + + var variation1 = Color.FromArgb(255, 57, 96, 142); + var variation2 = Color.FromArgb(255, 144, 58, 56); + var variation3 = Color.FromArgb(255, 116, 140, 65); + var variation4 = Color.FromArgb(255, 95, 73, 121); + var variation5 = Color.FromArgb(255, 54, 129, 149); + var variation6 = Color.FromArgb(255, 186, 112, 50); + + var variation14 = Color.FromArgb(113, 88, 143); + + Assert.AreEqual(variation1.R, outputColors[0][0].R, 1); + Assert.AreEqual(variation1.G, outputColors[0][0].G, 1); + Assert.AreEqual(variation1.B, outputColors[0][0].B, 1); + + + Assert.AreEqual(variation2.R, outputColors[0][1].R, 1); + Assert.AreEqual(variation2.G, outputColors[0][1].G, 1); + Assert.AreEqual(variation2.B, outputColors[0][1].B, 1); + + + Assert.AreEqual(variation3.R, outputColors[0][2].R, 1); + Assert.AreEqual(variation3.G, outputColors[0][2].G, 1); + Assert.AreEqual(variation3.B, outputColors[0][2].B, 1); + + + Assert.AreEqual(variation4.R, outputColors[0][3].R, 1); + Assert.AreEqual(variation4.G, outputColors[0][3].G, 1); + Assert.AreEqual(variation4.B, outputColors[0][3].B, 1); + + + Assert.AreEqual(variation5.R, outputColors[0][4].R, 1); + Assert.AreEqual(variation5.G, outputColors[0][4].G, 1); + Assert.AreEqual(variation5.B, outputColors[0][4].B, 1); + + + Assert.AreEqual(variation6.R, outputColors[0][5].R, 1); + Assert.AreEqual(variation6.G, outputColors[0][5].G, 1); + Assert.AreEqual(variation6.B, outputColors[0][5].B, 1); + + //Spot check next + Assert.AreEqual(variation14.R, outputColors[1][3].R, 1); + Assert.AreEqual(variation14.G, outputColors[1][3].G, 1); + Assert.AreEqual(variation14.B, outputColors[1][3].B, 1); + + var lastVariation = Color.FromArgb(249, 181, 144); + //Spot check last + Assert.AreEqual(lastVariation.R, outputColors[3][5].R, 1); + Assert.AreEqual(lastVariation.G, outputColors[3][5].G, 1); + Assert.AreEqual(lastVariation.B, outputColors[3][5].B, 1); + } + + //--- END IMPORTANT + [TestMethod] public void Tint60Shade60() { From 987a20902b0f292fb8e77d00fe1c2750a666d4d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4llman?= Date: Fri, 20 Nov 2026 10:31:23 +0100 Subject: [PATCH 03/23] Started vertical multi category axis support --- .../Drawing/Chart/ExcelChartAxisStandard.cs | 12 +- .../Renderer/Chart/ChartAxisRenderer.cs | 191 +++++++++++------- .../Renderer/Chart/ChartPlotareaRenderer.cs | 2 +- 3 files changed, 127 insertions(+), 78 deletions(-) diff --git a/src/EPPlus/Drawing/Chart/ExcelChartAxisStandard.cs b/src/EPPlus/Drawing/Chart/ExcelChartAxisStandard.cs index 552aab3a3..5b52efcd0 100644 --- a/src/EPPlus/Drawing/Chart/ExcelChartAxisStandard.cs +++ b/src/EPPlus/Drawing/Chart/ExcelChartAxisStandard.cs @@ -79,14 +79,14 @@ public override eAxisTickMark MajorTickMark var v = GetXmlNodeString(_majorTickMark); if (string.IsNullOrEmpty(v)) { - return eAxisTickMark.Cross; + //return eAxisTickMark.Cross; //On Epplus generated charts e.g. the test "HtmlExportWithColumnGradient" //Excel displays cross even without v or MajorTickmarkParent - //if (ExistsNode(_majorTickMarkParent)) - //{ - // return eAxisTickMark.Cross; - //} - //return eAxisTickMark.Out; + if (ExistsNode(_majorTickMarkParent)) + { + return eAxisTickMark.Cross; + } + return eAxisTickMark.Out; } else { diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs index 06c6700a0..d0e64b19f 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs @@ -17,6 +17,7 @@ Date Author Change using EPPlus.Export.Renderer; using EPPlus.Fonts.OpenType.Integration; using EPPlus.Fonts.OpenType.Integration.DataHolders; +using EPPlus.Fonts.OpenType.TextShaping; using EPPlus.Fonts.OpenType.Utils; using EPPlus.Graphics; using EPPlusImageRenderer.RenderItems; @@ -196,11 +197,6 @@ internal void UpdateValueScale() } } } - - //if (Axis.TextBody.Rotation != 0) - //{ - // Rectangle.Width += - //} } } @@ -439,7 +435,10 @@ private double GetTextWidest(ExcelChartAxisStandard ax, bool isMulti) widest = w.Width; } } - widest += (float)(AxisValuesMulti.Count - 1 * shaper.GetLineHeightInPoints(mf.Size) * 1.5); + if (ax.IsVertical) + { + widest += (float)((AxisValuesMulti[0].Length - 1) * shaper.GetLineHeightInPoints(mf.Size) * 1.5); + } } else { @@ -1152,8 +1151,13 @@ private List AddTextboxesMulti(ITextShaper shaper, float f var firstItem = (string[])AxisValuesMulti.First(); var isTopRow = true; var heightAdd = 0D; - var majorWidth = Rectangle.Width / AxisValuesMulti.Count; + var isVertical = Axis.IsVertical; + var majorSize = (isVertical ? Rectangle.Height : Rectangle.Width) / AxisValuesMulti.Count; var margin = fontSize * 0.5; //margin between lines + if(isVertical) + { + widest = GetFirstColTextWidest(shaper, fontSize, AxisValuesMulti); + } for (int j = firstItem.Length - 1; j >= 0; j--) { var emptyCells = 0; @@ -1170,37 +1174,18 @@ private List AddTextboxesMulti(ITextShaper shaper, float f } else if (isTopRow == false && tb != null) { - //center the text box in the middle of the empty cells. - var left = GetAxisItemLeft(i - emptyCells - 1); - if (i + 1 == AxisValuesMulti.Count && string.IsNullOrEmpty(AxisValuesMulti[i][j])) + if (isVertical) { - switch (Axis.LabelAlignment) + if (AdjustEmptyCellsVertical(tb, i, j, emptyCells, majorSize, widest) == false) { - case eAxisLabelAlignment.Left: - tb.Left = left; - break; - case eAxisLabelAlignment.Center: - tb.Left = left + (majorWidth * (emptyCells + 2) / 2) - (tb.Width / 2); - break; - case eAxisLabelAlignment.Right: - tb.Left = left + majorWidth * (emptyCells + 2) - tb.Width; - break; + continue; } - continue; } else { - switch (Axis.LabelAlignment) + if (AdjustEmptyCellsHorizontal(tb, i, j, emptyCells, majorSize) == false) { - case eAxisLabelAlignment.Left: - tb.Left = left; - break; - case eAxisLabelAlignment.Center: - tb.Left = left + (majorWidth * (emptyCells + 1) / 2) - (tb.Width / 2); - break; - case eAxisLabelAlignment.Right: - tb.Left = left + majorWidth * (emptyCells + 1) - tb.Width; - break; + continue; } } emptyCells = 0; @@ -1208,8 +1193,8 @@ private List AddTextboxesMulti(ITextShaper shaper, float f var v = ((object[])Values[i])[j]; var t = AxisValuesMulti[i][j]; - - var ticMarkX = GetAxisItemLeft(i); + double ticMarkX, ticMarkY; + ticMarkX = GetAxisItemLeft(i); double majorTickMarkWidth; if (i + 1 < Values.Count) { @@ -1234,7 +1219,14 @@ private List AddTextboxesMulti(ITextShaper shaper, float f float width = string.IsNullOrEmpty(t) ? 0f : shaper.Shape(t).GetWidthInPoints(fontSize); float height = string.IsNullOrEmpty(t) ? 0f : lineHeight; - var ticMarkY = GetAxisItemTop(i, width, height) + heightAdd; + if(isVertical) + { + ticMarkY = 0; //We adjust later when we now the widest label. + } + else + { + ticMarkY = GetAxisItemTop(i, width, height) + heightAdd; + } double x, y; if (LabelOrientation == eTextOrientation.Horizontal || isTopRow == false) { @@ -1298,10 +1290,10 @@ private List AddTextboxesMulti(ITextShaper shaper, float f case eAxisLabelAlignment.Left: break; case eAxisLabelAlignment.Center: - x += (majorWidth / 2) - (width / 2); + x += (majorSize / 2) - (width / 2); break; case eAxisLabelAlignment.Right: - x += majorWidth - tb.Width; + x += majorSize - tb.Width; break; } } @@ -1312,41 +1304,6 @@ private List AddTextboxesMulti(ITextShaper shaper, float f tb = new DrawingTextContainer(ChartRenderer.RenderContext, Chart, Rectangle, DefaultFonts2007Style.Axis, x, y, width, height, GetMaxWidthForEmptyRows(maxWidth, isTopRow, i, j), maxHeight); tb.SetRotation(EPPlus.DrawingRenderer.RenderItems.Textbox.RotationAnchor.Middle); - //if (isTopRow) - //{ - // if (LabelOrientation == eTextOrientation.Diagonal) - // { - // tb.Rotation = -45; - // if (Axis.ActualAxisPosition == eAxisPosition.Bottom) - // { - // tb.TextAnchor = eTextAnchor.End; - // } - // } - // else if (LabelOrientation == eTextOrientation.Vertical) - // { - // if (Axis.TextBody.VerticalText == eTextVerticalType.EastAsianVertical) - // { - // tb.Rotation = 90; - // } - // else - // { - // tb.Rotation = -90; - // } - // if (Axis.ActualAxisPosition == eAxisPosition.Bottom) - // { - // tb.TextAnchor = eTextAnchor.End; - // } - // } - // else if (LabelOrientation == eTextOrientation.Custom) - // { - // tb.Rotation = Axis.TextBody?.RotationAdjustedForTextVertical ?? 0D; - // if (Axis.ActualAxisPosition == eAxisPosition.Bottom) - // { - // tb.TextAnchor = eTextAnchor.End; - // } - // } - //} - var p = Axis.TextBody.Paragraphs.FirstOrDefault(); if (p != null) @@ -1391,6 +1348,98 @@ private List AddTextboxesMulti(ITextShaper shaper, float f return ret; } + private double GetFirstColTextWidest(ITextShaper shaper, float fontSize, List axisValuesMulti) + { + var widest = 0D; + foreach(string[] v in AxisValuesMulti) + { + var t = v[v.Length - 1]; + var w = shaper.MeasureTextInPoints(t, fontSize); + if(w > widest) + { + widest = w; + } + } + return widest; + } + + private bool AdjustEmptyCellsHorizontal(DrawingTextContainer tb, int i, int j, int emptyCells, double majorWidth) + { + //center the text box in the middle of the empty cells. + var left = GetAxisItemLeft(i - emptyCells - 1); + if (i + 1 == AxisValuesMulti.Count && string.IsNullOrEmpty(AxisValuesMulti[i][j])) + { + switch (Axis.LabelAlignment) + { + case eAxisLabelAlignment.Left: + tb.Left = left; + break; + case eAxisLabelAlignment.Center: + tb.Left = left + (majorWidth * (emptyCells + 2) / 2) - (tb.Width / 2); + break; + case eAxisLabelAlignment.Right: + tb.Left = left + majorWidth * (emptyCells + 2) - tb.Width; + break; + } + return false; + } + else + { + switch (Axis.LabelAlignment) + { + case eAxisLabelAlignment.Left: + tb.Left = left; + break; + case eAxisLabelAlignment.Center: + tb.Left = left + (majorWidth * (emptyCells + 1) / 2) - (tb.Width / 2); + break; + case eAxisLabelAlignment.Right: + tb.Left = left + majorWidth * (emptyCells + 1) - tb.Width; + break; + } + } + + return true; + } + private bool AdjustEmptyCellsVertical(DrawingTextContainer tb, int i, int j, int emptyCells, double majorHeight, double widest) + { + //center the text box in the middle of the empty cells. + var top = GetAxisItemTop(i - emptyCells - 1, tb.Width, tb.Height); + if (i + 1 == AxisValuesMulti.Count && string.IsNullOrEmpty(AxisValuesMulti[i][j])) + { + switch (Axis.LabelAlignment) + { + case eAxisLabelAlignment.Left: + tb.Top = top; + break; + case eAxisLabelAlignment.Center: + tb.Left = top + (majorHeight * (emptyCells + 2) / 2) - (tb.RotatedHeight / 2); + break; + case eAxisLabelAlignment.Right: + tb.Left = top + majorHeight * (emptyCells + 2) - tb.RotatedHeight; + break; + } + return false; + } + else + { + switch (Axis.LabelAlignment) + { + case eAxisLabelAlignment.Left: + tb.Top = top; + break; + case eAxisLabelAlignment.Center: + tb.Top = top + (majorHeight * (emptyCells + 1) / 2) - (tb.RotatedHeight / 2); + break; + case eAxisLabelAlignment.Right: + tb.Top = top + majorHeight * (emptyCells + 1) - tb.RotatedHeight; + break; + } + } + + return true; + } + private void AddGroupLines(double x, double length) { if (Axis.IsVertical) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartPlotareaRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartPlotareaRenderer.cs index 540af941f..118a88a30 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartPlotareaRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartPlotareaRenderer.cs @@ -133,7 +133,7 @@ internal double GetPlotAreaWidth(RectRenderItem rect) var rightSecondAxis = GetAxisActualByPosition(eActualAxisPosition.Right, 1); var lp = ChartRenderer.Chart.Legend?.Position; var right = ((lp == eLegendPosition.Right || lp == eLegendPosition.TopRight) && ChartRenderer.Legend != null && ChartRenderer.Legend.Overlay==false ? - ChartRenderer.Legend.Rectangle.GlobalLeft - RightMargin : + ChartRenderer.ChartArea.Rectangle.Width - ChartRenderer.Legend.Rectangle.Width - RightMargin : ChartRenderer.ChartArea.Rectangle.Width - RightMargin); var leftAxis = GetAxisActualByPosition(eActualAxisPosition.Left, 0); From 23b5f8130bafa56ea12ab85137cd29096e7b2c3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ossian=20Edstr=C3=B6m?= Date: Fri, 20 Nov 2026 16:32:58 +0100 Subject: [PATCH 04/23] Functional legacy data point variation implementation --- .../Chart/ChartTypeDrawers/ChartTypeDrawer.cs | 33 +++++++++++++ .../ChartTypeDrawers/PieChartTypeDrawer.cs | 2 +- .../Renderer/Chart/PieSliceRenderItem.cs | 20 +++++--- src/EPPlusTest/Drawing/ThemeTest.cs | 46 ++++++++++++------- 4 files changed, 77 insertions(+), 24 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs index 2b26824c3..e5ee0a026 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs @@ -248,6 +248,14 @@ internal static void SetFillDataPoint(ExcelChart chart, ExcelChartStandardSerie style.SetDrawingPropertiesBorder(theme, dp.Border.IsEmpty ? cStandardSerie.Border : dp.Border, entry?.BorderReference.Color, dp.Border.Fill.Style != eFillStyle.NoFill, () => Color.Transparent, 0.75); } + internal static void SetSingleFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartStandardSerie cStandardSerie, int variationIteration, int index, int valueCount, RenderItemStyle style) + { + var theme = chart.WorkSheet.Workbook.ThemeManager.GetOrCreateTheme(); + var baseColor = GetBaseColor(theme, chart.StyleManager, index); + var variation = DefaultDataPointPatterns.GetSingleLegacyColorVariation(variationIteration, valueCount % 6 == 0 ? valueCount / 6 + 1 : valueCount / 6, baseColor); + style.SetDrawingPropertiesFill(theme, cStandardSerie.Fill, chart.StyleManager.Style?.SeriesLine.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, variation); + } + internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartStandardSerie cStandardSerie, int serieIndex, int index, RenderItemStyle style) { var theme = chart.WorkSheet.Workbook.ThemeManager.GetOrCreateTheme(); @@ -270,6 +278,31 @@ internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartSta style.SetDrawingPropertiesBorder(theme, cStandardSerie.Border, chart.StyleManager.Style?.SeriesLine.BorderReference.Color, cStandardSerie.Border.Fill.Style != eFillStyle.NoFill || styleFormat.width>0, () => styleFormat.color, 0.75); } + internal static Color GetBaseColor(ExcelTheme theme, ExcelChartStyleManager? styleManager, int index) + { + Color baseColor; + var baseColorIndex = index % 6; + + ExcelChartColorsManager colorsManager = styleManager?.ColorsManager; + if ((colorsManager == null || baseColorIndex >= colorsManager.Colors.Count)) + { + if(styleManager.Style == null) + { + baseColor = DefaultDataPointPatterns.FallBackColors[baseColorIndex]; + } + else + { + //TODO: Check Chart.Style here. Only when THAT is set to None should the below line happen + baseColor = theme.ColorScheme.GetColorByEnum(eSchemeColor.Accent1 + baseColorIndex).GetColor(); + } + } + else + { + baseColor = colorsManager.Colors[baseColorIndex].GetColor(); + } + return baseColor; + } + internal static Color? GetVaryColor(ExcelTheme theme, ExcelChartStyleManager? styleManager, int index) { Color baseColor; diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/PieChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/PieChartTypeDrawer.cs index bbed945b4..5772c6bcc 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/PieChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/PieChartTypeDrawer.cs @@ -116,7 +116,7 @@ void InitializeSlices() for (int i = 0; i < valPercents.Count; i++) { - PieSliceRenderItem renderSlice = new PieSliceRenderItem(ChartRenderer, _groupItem, pieTransform.TransformSlices[i], pieTransform.Slices.Percentages[i], pieTransform, _startDegrees, prevDegrees); + PieSliceRenderItem renderSlice = new PieSliceRenderItem(ChartRenderer, _groupItem, pieTransform.TransformSlices[i], pieTransform.Slices.Percentages[i], pieTransform, _startDegrees, prevDegrees, valPercents.Count); prevDegrees = renderSlice.Degrees + prevDegrees; Slices.Add(renderSlice); } diff --git a/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs b/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs index 7dc0d77d7..aad85cf04 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs @@ -19,6 +19,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Drawing; +using System.Globalization; using System.Security.Cryptography; namespace EPPlus.Export.ImageRenderer.Svg.Chart @@ -305,8 +306,11 @@ double GetXOpposite(double x) RectRenderItem largestInscribedRectangleOuter; - public PieSliceRenderItem(ChartRenderer renderer, BoundingBox parent, PieSliceTransform slice, double percentOfPie, BoundingBox circleCenter, double firstSliceOffset, double prevDegrees) : base(renderer) + int totalSliceCount = 0; + + public PieSliceRenderItem(ChartRenderer renderer, BoundingBox parent, PieSliceTransform slice, double percentOfPie, BoundingBox circleCenter, double firstSliceOffset, double prevDegrees, int totalSlices) : base(renderer) { + totalSliceCount = totalSlices; DefaultFillColor = renderer.Theme.ColorScheme.Accent1.GetColor(); Rectangle.Parent = parent; _radius = slice.Circle.Radius; @@ -568,16 +572,18 @@ internal void ImportStlyeInfo(ExcelPieChartSerie serie, ExcelPieChart chartType, } else { - var subtleFill = ChartRenderer.Theme.FormatScheme.FillStyle[0]; - var bg = ChartRenderer.Theme.FormatScheme.BackgroundFillStyle[0]; - var darkScheme = OfficeOpenXml.Utils.TypeConversion.ColorConverter.GetSchemeColor(ChartRenderer.Theme, eSchemeColor.Dark1); - var dark75 = OfficeOpenXml.Utils.TypeConversion.ColorConverter.ApplyTintDrawing(darkScheme, 1 - 0.75d); + //var subtleFill = ChartRenderer.Theme.FormatScheme.FillStyle[0]; + //var bg = ChartRenderer.Theme.FormatScheme.BackgroundFillStyle[0]; + //var darkScheme = OfficeOpenXml.Utils.TypeConversion.ColorConverter.GetSchemeColor(ChartRenderer.Theme, eSchemeColor.Dark1); + //var dark75 = OfficeOpenXml.Utils.TypeConversion.ColorConverter.ApplyTintDrawing(darkScheme, 1 - 0.75d); - var color = OfficeOpenXml.Utils.TypeConversion.ColorConverter.ApplyTransforms(dark75, subtleFill.SolidFill.Color.Transforms); + //var color = OfficeOpenXml.Utils.TypeConversion.ColorConverter.ApplyTransforms(dark75, subtleFill.SolidFill.Color.Transforms); //var test = ColorConverter. //_slicePath.Style.SetDrawingPropertiesFill(ChartRenderer.Theme, serie.Fill, chartType.StyleManager.Style?.DataPoint.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, defaultFill); - ChartTypeDrawer.SetFillSerie(Chart, chartType, serie, 0, position, _slicePath.Style); + var divFactor = position / 6d; + var varIteration = (int)divFactor; + ChartTypeDrawer.SetSingleFillSerie(Chart, chartType, serie, varIteration, position, totalSliceCount, _slicePath.Style); } //if (chartType.VaryColors) //{ diff --git a/src/EPPlusTest/Drawing/ThemeTest.cs b/src/EPPlusTest/Drawing/ThemeTest.cs index 141962199..78e357111 100644 --- a/src/EPPlusTest/Drawing/ThemeTest.cs +++ b/src/EPPlusTest/Drawing/ThemeTest.cs @@ -26,6 +26,7 @@ Date Author Change ******************************************************************************* 01/27/2020 EPPlus Software AB Initial release EPPlus 5 *******************************************************************************/ +using EPPlusTest.Drawing.Style; using EPPlusTest.Properties; using Microsoft.VisualStudio.TestTools.UnitTesting; using OfficeOpenXml; @@ -497,36 +498,33 @@ public void Apply88DarkAlt() var tint = tc.ColorConverter.ApplyTintDrawing(accent1, -0.7d); } - [TestMethod] - public void CalculateLEGACYDataPointsOfWholePie() + /// + /// + /// + /// + /// usually the 6 accent colors + /// + internal List> GetLegacyColorVariations(int dataPointCount, List origColors) { - //Assuming 24 data points - var numVariations = 24 / 6; + + var numVariations = dataPointCount / origColors.Count(); //KEY TAKE AWAYS TO MAKE FUNC WORK //VariationIndex begins at 1 NOT 0 //highestVariation INCREASED by 1 when REACHING 6 not the start of the next "accent1" - if (24 % 6 == 0) + if (dataPointCount % origColors.Count() == 0) { numVariations += 1; } - var accent1 = Color.FromArgb(255, 79, 129, 189); - var accent2 = Color.FromArgb(255, 192, 80, 77); - var accent3 = Color.FromArgb(255, 155, 187, 89); - var accent4 = Color.FromArgb(255, 128, 100, 162); - var accent5 = Color.FromArgb(255, 75, 172, 198); - var accent6 = Color.FromArgb(255, 247, 150, 70); - - List colorList = new List() {accent1, accent2, accent3, accent4, accent5, accent6 }; List> outputColors = new List>(); for (int i = 1; i < numVariations; i++) { List currentVariations = new List(); - for (int j = 0; j < colorList.Count; j++) + for (int j = 0; j < origColors.Count; j++) { - var color = colorList[j]; - var tintValue = ( -70d + 140d * (i / (numVariations + 1d)))/100d; + var color = origColors[j]; + var tintValue = (-70d + 140d * (i / (numVariations + 1d))) / 100d; var variation = tc.ColorConverter.ApplyTintDrawing(color, tintValue); @@ -535,6 +533,22 @@ public void CalculateLEGACYDataPointsOfWholePie() outputColors.Add(currentVariations); } + return outputColors; + } + + [TestMethod] + public void CalculateLEGACYDataPointsOfWholePie() + { + var accent1 = Color.FromArgb(255, 79, 129, 189); + var accent2 = Color.FromArgb(255, 192, 80, 77); + var accent3 = Color.FromArgb(255, 155, 187, 89); + var accent4 = Color.FromArgb(255, 128, 100, 162); + var accent5 = Color.FromArgb(255, 75, 172, 198); + var accent6 = Color.FromArgb(255, 247, 150, 70); + + List colorList = new List() {accent1, accent2, accent3, accent4, accent5, accent6 }; + List> outputColors = GetLegacyColorVariations(24, colorList); + var variation1 = Color.FromArgb(255, 57, 96, 142); var variation2 = Color.FromArgb(255, 144, 58, 56); var variation3 = Color.FromArgb(255, 116, 140, 65); From b1630bee66cf3e00d938d901da40a27569ebd9f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ossian=20Edstr=C3=B6m?= Date: Fri, 20 Nov 2026 16:33:34 +0100 Subject: [PATCH 05/23] Added crucial missing file --- .../Defaults/DefaultDataPointPatterns.cs | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/EPPlus/Drawing/Renderer/Chart/Defaults/DefaultDataPointPatterns.cs diff --git a/src/EPPlus/Drawing/Renderer/Chart/Defaults/DefaultDataPointPatterns.cs b/src/EPPlus/Drawing/Renderer/Chart/Defaults/DefaultDataPointPatterns.cs new file mode 100644 index 000000000..fc8f7c042 --- /dev/null +++ b/src/EPPlus/Drawing/Renderer/Chart/Defaults/DefaultDataPointPatterns.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; + +namespace OfficeOpenXml.Drawing.Renderer.Chart.Defaults +{ + internal static class DefaultDataPointPatterns + { + /// + /// Excel built-in fallback colors when no style and no theme + /// + internal readonly static Color[] FallBackColors = + { Color.FromArgb(255, 79, 129, 189), + Color.FromArgb(255, 192, 80, 77), + Color.FromArgb(255, 155, 187, 89), + Color.FromArgb(255, 128, 100, 162), + Color.FromArgb(255, 75, 172, 198), + Color.FromArgb(255, 247, 150, 70) + }; + + /// + /// + /// + /// + /// usually the 6 accent colors + /// + internal static List> GetLegacyColorVariations(int dataPointCount, List origColors) + { + + var numVariations = dataPointCount / origColors.Count(); + + //KEY TAKE AWAYS TO MAKE FUNC WORK + //VariationIndex begins at 1 NOT 0 + //highestVariation INCREASED by 1 when REACHING 6 not the start of the next "accent1" + if (dataPointCount % origColors.Count() == 0) + { + numVariations += 1; + } + + List> outputColors = new List>(); + for (int i = 1; i < numVariations; i++) + { + List currentVariations = new List(); + for (int j = 0; j < origColors.Count; j++) + { + var color = origColors[j]; + var tintValue = (-70d + 140d * (i / (numVariations + 1d))) / 100d; + + var variation = OfficeOpenXml.Utils.TypeConversion.ColorConverter.ApplyTintDrawing(color, tintValue); + + currentVariations.Add(variation); + } + outputColors.Add(currentVariations); + } + + return outputColors; + } + + /// + /// + /// + /// 0-based variation index + /// Total number of variations (Should be +1 if datapoints evenly dividable with num original colors) + /// + /// + internal static Color GetSingleLegacyColorVariation(int variationIndex, int numOfVariations, Color origColor) + { + //legacy uses 1-based variation index + variationIndex += 1; + + var tintValue = (-70d + 140d * (variationIndex / (numOfVariations + 1d))) / 100d; + + return OfficeOpenXml.Utils.TypeConversion.ColorConverter.ApplyTintDrawing(origColor, tintValue); + } + } +} From c6558821a554b432e65a6ded0551e42eb4419f56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ossian=20Edstr=C3=B6m?= Date: Mon, 23 Nov 2026 10:34:32 +0100 Subject: [PATCH 06/23] Fully functional legacy --- .../Chart/ChartTypeDrawers/ChartTypeDrawer.cs | 30 ++++++++++++------- .../ChartTypeDrawers/PieChartTypeDrawer.cs | 8 +++-- .../Defaults/DefaultDataPointPatterns.cs | 1 + .../Renderer/Chart/PieSliceRenderItem.cs | 15 ++-------- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs index e5ee0a026..1e3441a8d 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs @@ -248,12 +248,19 @@ internal static void SetFillDataPoint(ExcelChart chart, ExcelChartStandardSerie style.SetDrawingPropertiesBorder(theme, dp.Border.IsEmpty ? cStandardSerie.Border : dp.Border, entry?.BorderReference.Color, dp.Border.Fill.Style != eFillStyle.NoFill, () => Color.Transparent, 0.75); } - internal static void SetSingleFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartStandardSerie cStandardSerie, int variationIteration, int index, int valueCount, RenderItemStyle style) + internal static void SetSingleFillSerieLegacy(ExcelChart chart, ExcelChart ct, ExcelChartStandardSerie cStandardSerie, int valueCount, int index, RenderItemStyle style) { var theme = chart.WorkSheet.Workbook.ThemeManager.GetOrCreateTheme(); var baseColor = GetBaseColor(theme, chart.StyleManager, index); - var variation = DefaultDataPointPatterns.GetSingleLegacyColorVariation(variationIteration, valueCount % 6 == 0 ? valueCount / 6 + 1 : valueCount / 6, baseColor); + + var currentIterationIndex = index / 6; + var totalNumberOfVariations = (valueCount / 6); + + var variation = DefaultDataPointPatterns.GetSingleLegacyColorVariation(currentIterationIndex, totalNumberOfVariations, baseColor); style.SetDrawingPropertiesFill(theme, cStandardSerie.Fill, chart.StyleManager.Style?.SeriesLine.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, variation); + + var styleFormat = DefaultChartBorder.GetDefaulDataPoint2d(theme, chart, currentIterationIndex); + style.SetDrawingPropertiesBorder(theme, cStandardSerie.Border, chart.StyleManager.Style?.SeriesLine.BorderReference.Color, cStandardSerie.Border.Fill.Style != eFillStyle.NoFill || styleFormat.width > 0, () => styleFormat.color, 0.75); } internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartStandardSerie cStandardSerie, int serieIndex, int index, RenderItemStyle style) @@ -261,9 +268,6 @@ internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartSta var theme = chart.WorkSheet.Workbook.ThemeManager.GetOrCreateTheme(); if (ct.VaryColors) { - var accent1Scheme = chart.WorkSheet.Workbook.ThemeManager.CurrentTheme.ColorScheme.Accent1; - var accent1Theme = chart.WorkSheet.Workbook.ThemeManager.CurrentTheme.FormatScheme.FillStyle[0].SolidFill.Color; - var chartStyle =(int)chart.Style; //Get the color based on the index, if no style is set. Accent1, Accent2, Accent3... var color = GetVaryColor(theme, chart.StyleManager, index); @@ -309,7 +313,7 @@ internal static Color GetBaseColor(ExcelTheme theme, ExcelChartStyleManager? sty var baseColorIndex = index % 6; ExcelChartColorsManager colorsManager = styleManager?.ColorsManager; - if ((colorsManager == null || baseColorIndex >= colorsManager.Colors.Count) && styleManager.Style == null) + if ((colorsManager == null || baseColorIndex >= colorsManager.Colors.Count)) { //TODO: Check Chart.Style here. Only when THAT is set to None should the below line happen baseColor = theme.ColorScheme.GetColorByEnum(eSchemeColor.Accent1 + baseColorIndex).GetColor(); @@ -329,10 +333,16 @@ internal static Color GetBaseColor(ExcelTheme theme, ExcelChartStyleManager? sty ExcelColorTransformCollection variation; if (colorsManager == null) { - //ExcelColorTransformCollectionDataPoints dpCollection = new(); - //return dpCollection.GetColor(baseColor, variationIndex, baseColorIndex); - var variations = ExcelColorTransformCollection.GetDefault(); - variation = variations[variationIndex % variations.Count]; + if(styleManager.Style == null) + { + var variations = ExcelColorTransformCollection.GetDefault(); + variation = variations[variationIndex % variations.Count]; + } + else + { + var variations = ExcelColorTransformCollection.GetDefault(); + variation = variations[variationIndex % variations.Count]; + } } else { diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/PieChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/PieChartTypeDrawer.cs index 5772c6bcc..fa9654717 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/PieChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/PieChartTypeDrawer.cs @@ -13,6 +13,7 @@ using OfficeOpenXml.Drawing.Chart; using OfficeOpenXml.Drawing.Renderer.Chart.ChartTypeDrawers; using OfficeOpenXml.FormulaParsing.Excel.Functions.MathFunctions; +using OfficeOpenXml.FormulaParsing.FormulaExpressions; using OfficeOpenXml.Utils.Drawing; using OfficeOpenXml.Utils.TypeConversion; using System; @@ -202,8 +203,11 @@ void ConvertSerieValuesToDoubles() var dValue = ConvertUtil.GetValueDouble(origValue, false, true); if (double.IsNaN(dValue)) { - //Ignore values that are NAN. Possibly we should throw here but Excel simply seems to skip it. - continue; + //Ignore values that are NAN. Possibly we should throw here but Excel simply seems to add as 0 size. + //Excel interestingly for numRef appears to skip blanks entirely but adds N/A errors yet treats fills as if all values are valid. + //Alternatively we could do "continue" here instead and use the catValues for the fills + //For now appears easier to add 0 + dValue = 0; } _serieValuesAsDoubles.Add(dValue); _totalOfSerieValues += dValue; diff --git a/src/EPPlus/Drawing/Renderer/Chart/Defaults/DefaultDataPointPatterns.cs b/src/EPPlus/Drawing/Renderer/Chart/Defaults/DefaultDataPointPatterns.cs index fc8f7c042..86f9e088f 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/Defaults/DefaultDataPointPatterns.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/Defaults/DefaultDataPointPatterns.cs @@ -69,6 +69,7 @@ internal static Color GetSingleLegacyColorVariation(int variationIndex, int numO { //legacy uses 1-based variation index variationIndex += 1; + numOfVariations += 1; var tintValue = (-70d + 140d * (variationIndex / (numOfVariations + 1d))) / 100d; diff --git a/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs b/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs index aad85cf04..b8ba7f092 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs @@ -568,22 +568,11 @@ internal void ImportStlyeInfo(ExcelPieChartSerie serie, ExcelPieChart chartType, { var dp = serie.DataPoints[position]; ChartTypeDrawer.SetFillDataPoint(Chart, serie, position, _slicePath.Style, dp, Chart.StyleManager.Style?.SeriesLine, UserSpaceSettings.ObjectBoundingBox); - //_slicePath.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, dp.Border, chartType.StyleManager.Style?.DataPoint.BorderReference.Color, true, null); + _slicePath.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, dp.Border, chartType.StyleManager.Style?.DataPoint.BorderReference.Color, true, null); } else { - //var subtleFill = ChartRenderer.Theme.FormatScheme.FillStyle[0]; - //var bg = ChartRenderer.Theme.FormatScheme.BackgroundFillStyle[0]; - //var darkScheme = OfficeOpenXml.Utils.TypeConversion.ColorConverter.GetSchemeColor(ChartRenderer.Theme, eSchemeColor.Dark1); - //var dark75 = OfficeOpenXml.Utils.TypeConversion.ColorConverter.ApplyTintDrawing(darkScheme, 1 - 0.75d); - - //var color = OfficeOpenXml.Utils.TypeConversion.ColorConverter.ApplyTransforms(dark75, subtleFill.SolidFill.Color.Transforms); - //var test = ColorConverter. - - //_slicePath.Style.SetDrawingPropertiesFill(ChartRenderer.Theme, serie.Fill, chartType.StyleManager.Style?.DataPoint.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, defaultFill); - var divFactor = position / 6d; - var varIteration = (int)divFactor; - ChartTypeDrawer.SetSingleFillSerie(Chart, chartType, serie, varIteration, position, totalSliceCount, _slicePath.Style); + ChartTypeDrawer.SetSingleFillSerieLegacy(Chart, chartType, serie, totalSliceCount, position, _slicePath.Style); } //if (chartType.VaryColors) //{ From 0db0bda7947824577626e3446354e8c4bbfa434c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ossian=20Edstr=C3=B6m?= Date: Mon, 23 Nov 2026 11:37:00 +0100 Subject: [PATCH 07/23] Implemented legacy for all chartTypes --- .../BarColumnChartTypeDrawer.cs | 6 +-- .../Chart/ChartTypeDrawers/ChartTypeDrawer.cs | 38 ++++++++++++++----- .../ChartTypeDrawers/LineChartTypeDrawer.cs | 21 +++++++--- .../Renderer/Chart/LegendIconRenderer.cs | 16 ++++---- .../Renderer/Chart/LineMarkerHelper.cs | 2 +- .../Renderer/Chart/PieSliceRenderItem.cs | 6 +-- .../Coloring/ExcelColorTransformCollection.cs | 35 +++++++++++++++++ 7 files changed, 95 insertions(+), 29 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/BarColumnChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/BarColumnChartTypeDrawer.cs index 8f374003c..0c6191c69 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/BarColumnChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/BarColumnChartTypeDrawer.cs @@ -394,14 +394,14 @@ private void AddBar(ExcelBarChart chartType, ExcelBarChartSerie serie, List= 0 && serie.DataPoints.ContainsKey(i)) + if (i >= 0 && serie.DataPoints.ContainsKey(i)) { var dp = serie.DataPoints[i]; - SetFillDataPoint(Chart, serie, i, rect.Style, dp, Chart.StyleManager.Style?.SeriesLine); + SetFillDataPoint(Chart, serie, i, rect.Style, dp, Chart.StyleManager.Style?.SeriesLine, serie.DataPoints.Count); } else { - SetFillSerie(Chart, chartType, serie, seriesStart + position, i, rect.Style); + SetFillSerie(Chart, chartType, serie, seriesStart + position, i, rect.Style, serie.NumberOfItems); } rect.Style.SetDrawingPropertiesEffects(ChartRenderer.Theme, serie.Effect); diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs index 1e3441a8d..bd0e11555 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs @@ -239,10 +239,22 @@ internal bool IsOnAxis(ExcelChartAxisStandard ax) { return _chartType.YAxis==ax || _chartType.XAxis==ax; } - internal static void SetFillDataPoint(ExcelChart chart, ExcelChartStandardSerie cStandardSerie, int index, RenderItemStyle style, ExcelChartDataPoint dp, ExcelChartStyleEntry entry, UserSpaceSettings spaceSettings = UserSpaceSettings.UserSpaceOnUse_Object) + + internal int GetVariationCount(ExcelChartStandardSerie cStandardSerie) { + var valValues = LoadSeriesValues(cStandardSerie.Series, cStandardSerie.NumberLiteralsY, cStandardSerie.StringLiteralsY); + var variationCount = valValues.Count / 6; + //Compare these two and take the higher count later? We should standardize loading series values more + //var catValues = LoadSeriesValues(cStandardSerie.XSeries, cStandardSerie.NumberLiteralsX, cStandardSerie.StringLiteralsX); + + return variationCount; + } + + internal static void SetFillDataPoint(ExcelChart chart, ExcelChartStandardSerie cStandardSerie, int index, RenderItemStyle style, ExcelChartDataPoint dp, ExcelChartStyleEntry entry, int numDataPoints, UserSpaceSettings spaceSettings = UserSpaceSettings.UserSpaceOnUse_Object) + { + var legacyVariationCount = numDataPoints / 6; var theme = chart.WorkSheet.Workbook.ThemeManager.GetOrCreateTheme(); - var color = GetVaryColor(theme, chart.StyleManager, index); + var color = GetVaryColor(theme, chart.StyleManager, index, legacyVariationCount); style.SetDrawingPropertiesFill(theme, dp.Fill.IsEmpty ? cStandardSerie.Fill : dp.Fill, entry?.FillReference.Color, spaceSettings, color); style.SetDrawingPropertiesBorder(theme, dp.Border.IsEmpty ? cStandardSerie.Border : dp.Border, entry?.BorderReference.Color, dp.Border.Fill.Style != eFillStyle.NoFill, () => Color.Transparent, 0.75); @@ -263,19 +275,20 @@ internal static void SetSingleFillSerieLegacy(ExcelChart chart, ExcelChart ct, E style.SetDrawingPropertiesBorder(theme, cStandardSerie.Border, chart.StyleManager.Style?.SeriesLine.BorderReference.Color, cStandardSerie.Border.Fill.Style != eFillStyle.NoFill || styleFormat.width > 0, () => styleFormat.color, 0.75); } - internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartStandardSerie cStandardSerie, int serieIndex, int index, RenderItemStyle style) + internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartStandardSerie cStandardSerie, int serieIndex, int index, RenderItemStyle style, int numValuesInSeries) { var theme = chart.WorkSheet.Workbook.ThemeManager.GetOrCreateTheme(); + var legacyVariationCount = numValuesInSeries / 6; if (ct.VaryColors) { var chartStyle =(int)chart.Style; //Get the color based on the index, if no style is set. Accent1, Accent2, Accent3... - var color = GetVaryColor(theme, chart.StyleManager, index); + var color = GetVaryColor(theme, chart.StyleManager, index, legacyVariationCount); style.SetDrawingPropertiesFill(theme, cStandardSerie.Fill, chart.StyleManager.Style?.SeriesLine.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, color); } else { - var color = GetVaryColor(theme, chart.StyleManager, serieIndex); + var color = GetVaryColor(theme, chart.StyleManager, serieIndex, legacyVariationCount); style.SetDrawingPropertiesFill(theme, cStandardSerie.Fill, chart.StyleManager.Style?.SeriesLine.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, color); } var styleFormat = DefaultChartBorder.GetDefaulDataPoint2d(theme, chart, serieIndex); @@ -307,7 +320,7 @@ internal static Color GetBaseColor(ExcelTheme theme, ExcelChartStyleManager? sty return baseColor; } - internal static Color? GetVaryColor(ExcelTheme theme, ExcelChartStyleManager? styleManager, int index) + internal static Color? GetVaryColor(ExcelTheme theme, ExcelChartStyleManager? styleManager, int index, int legacyVariationCount) { Color baseColor; var baseColorIndex = index % 6; @@ -315,8 +328,15 @@ internal static Color GetBaseColor(ExcelTheme theme, ExcelChartStyleManager? sty ExcelChartColorsManager colorsManager = styleManager?.ColorsManager; if ((colorsManager == null || baseColorIndex >= colorsManager.Colors.Count)) { - //TODO: Check Chart.Style here. Only when THAT is set to None should the below line happen - baseColor = theme.ColorScheme.GetColorByEnum(eSchemeColor.Accent1 + baseColorIndex).GetColor(); + if (styleManager.Style != null) + { + //TODO: Check Chart.Style here. Only when THAT is set to None should the below line happen + baseColor = theme.ColorScheme.GetColorByEnum(eSchemeColor.Accent1 + baseColorIndex).GetColor(); + } + else + { + baseColor = DefaultDataPointPatterns.FallBackColors[baseColorIndex]; + } } else { @@ -335,7 +355,7 @@ internal static Color GetBaseColor(ExcelTheme theme, ExcelChartStyleManager? sty { if(styleManager.Style == null) { - var variations = ExcelColorTransformCollection.GetDefault(); + var variations = ExcelColorTransformCollection.GetLegacyDefaultPattern2(legacyVariationCount); variation = variations[variationIndex % variations.Count]; } else diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/LineChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/LineChartTypeDrawer.cs index 23bc24b4e..a52b208fb 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/LineChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/LineChartTypeDrawer.cs @@ -171,6 +171,9 @@ internal override int DrawSeries(int startSerie) } } } + + int _totalDataPoints = 0; + private void AddLine(ExcelLineChart chartType, ExcelLineChartSerie serie, List xValues, List yValues, List dataPoints, int serieIndex, int startSerie) { ChartAxisRenderer yAxis, xAxis; @@ -200,6 +203,13 @@ private void AddLine(ExcelLineChart chartType, ExcelLineChartSerie serie, List(); var isNa = false; + + _totalDataPoints = serie?.DataPoints?.Count == null ? 0 : serie.DataPoints.Count; + if(_totalDataPoints == 0 && yValues.Count > 0) + { + _totalDataPoints = yValues.Count; + } + for (var i = 0; i < yValues.Count; i++) { double x; @@ -287,6 +297,7 @@ private void AddLine(ExcelLineChart chartType, ExcelLineChartSerie serie, List 0 && serie.DataPoints.ContainsKey(i)) { @@ -306,13 +317,13 @@ private void AddLine(ExcelLineChart chartType, ExcelLineChartSerie serie, List ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, serie.Index + startSerie)); + ()=> ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, serie.Index + startSerie, _totalDataPoints)); } } - lineDp.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, dp.Border, chartType.StyleManager.Style?.SeriesLine.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, serie.Index + startSerie), 3); + lineDp.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, dp.Border, chartType.StyleManager.Style?.SeriesLine.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, serie.Index + startSerie, _totalDataPoints), 3); lineDp.Style.SetDrawingPropertiesEffects(ChartRenderer.Theme, dp.Effect); dataPointOverrides.Add(lineDp); } @@ -340,7 +351,7 @@ private void AddLine(ExcelLineChart chartType, ExcelLineChartSerie serie, List ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, serie.Index), 3); + linePath.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, serie.Border, chartType.StyleManager.Style?.SeriesLine.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, serie.Index, _totalDataPoints), 3); linePath.Style.SetDrawingPropertiesEffects(ChartRenderer.Theme, serie.Effect); linePath.Style.FillColor = "none"; //No fill for line linePath.Style.StrokeMiterLimit = 4; //A much higher value of the miter limit, might cause the "spike" to get beyond the data point on the vertical scale.. @@ -436,7 +447,7 @@ private void SetMarker(ExcelLineChartSerie serie, ExcelChartMarker marker, List< } if(serie.Marker.Border.IsEmpty) { - ls.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, serie.Border, ChartRenderer.Chart.StyleManager.Style?.DataPointMarker.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, index), 0.75d); + ls.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, serie.Border, ChartRenderer.Chart.StyleManager.Style?.DataPointMarker.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, index, _totalDataPoints), 0.75d); } } markerItems.Add(ls); diff --git a/src/EPPlus/Drawing/Renderer/Chart/LegendIconRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/LegendIconRenderer.cs index 695b4f878..c706b5ff2 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/LegendIconRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/LegendIconRenderer.cs @@ -209,7 +209,7 @@ internal static void SetLineLegend(ChartRenderer chart, T parent, ExcelChart } if (ls.Marker.Border.IsEmpty) { - sls.MarkerIcon.Style.SetDrawingPropertiesBorder(chart.Theme, ls.Border, chart.Chart.StyleManager.Style?.DataPointMarker.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(chart.Theme, null, index), 0.75d); + sls.MarkerIcon.Style.SetDrawingPropertiesBorder(chart.Theme, ls.Border, chart.Chart.StyleManager.Style?.DataPointMarker.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(chart.Theme, null, index, s.NumberOfItems), 0.75d); } } } @@ -295,11 +295,11 @@ internal static void SetPieLegend(ChartRenderer chart, T parent, ExcelChart c if (i >= 0 && ps.DataPoints.ContainsKey(i)) { var dp = ps.DataPoints[i]; - ChartTypeDrawer.SetFillDataPoint(chart.Chart, ps, i, sls.SeriesIcon.Style, dp, chart.Chart.StyleManager.Style?.SeriesLine); + ChartTypeDrawer.SetFillDataPoint(chart.Chart, ps, i, sls.SeriesIcon.Style, dp, chart.Chart.StyleManager.Style?.SeriesLine, s.NumberOfItems); } else { - ChartTypeDrawer.SetFillSerie(chart.Chart, ct, ps, 0, i, sls.SeriesIcon.Style); + ChartTypeDrawer.SetFillSerie(chart.Chart, ct, ps, 0, i, sls.SeriesIcon.Style, s.NumberOfItems); } parent.SeriesIcon.Add(sls); @@ -334,7 +334,7 @@ internal static LineRenderItem GetLineSeriesIcon(ChartRenderer chart, T paren { var line = new LineRenderItem(parent.Rectangle); //Default style is NoLine NoFill - line.Style.SetDrawingPropertiesBorder(chart.Theme, cStandardSerie.Border, chart.Chart.StyleManager.Style?.SeriesLine.BorderReference.Color, cStandardSerie.Border.IsEmpty || cStandardSerie.Border.Fill.Style != eFillStyle.NoFill, () => ChartTypeDrawer.GetVaryColor(chart.Theme, null, cStandardSerie.Index), 2.25); + line.Style.SetDrawingPropertiesBorder(chart.Theme, cStandardSerie.Border, chart.Chart.StyleManager.Style?.SeriesLine.BorderReference.Color, cStandardSerie.Border.IsEmpty || cStandardSerie.Border.Fill.Style != eFillStyle.NoFill, () => ChartTypeDrawer.GetVaryColor(chart.Theme, null, cStandardSerie.Index, cStandardSerie.NumberOfItems), 2.25); double iconTop = 0, iconLeft = 0; pSls?.GetIconTopLeft(out iconTop, out iconLeft); @@ -377,11 +377,11 @@ internal static RectRenderItem GetBarSeriesIcon(ChartRenderer chart, ExcelCha if (index >= 0 && chartSerie.DataPoints.ContainsKey(index)) { var dp = chartSerie.DataPoints[index]; - ChartTypeDrawer.SetFillDataPoint(chart.Chart, chartSerie, index, item.Style, dp, chart.Chart.StyleManager.Style?.SeriesLine); + ChartTypeDrawer.SetFillDataPoint(chart.Chart, chartSerie, index, item.Style, dp, chart.Chart.StyleManager.Style?.SeriesLine, chartSerie.NumberOfItems); } else { - ChartTypeDrawer.SetFillSerie(chart.Chart, ct, chartSerie, serieIndex, index, item.Style); + ChartTypeDrawer.SetFillSerie(chart.Chart, ct, chartSerie, serieIndex, index, item.Style, chartSerie.NumberOfItems); } return item; @@ -566,11 +566,11 @@ internal static RenderItem GetSeriesIcon(ChartRenderer chartRenderer, BoundingBo if (index >= 0 && dataPoints.ContainsKey(index)) { var dp = dataPoints[index]; - ChartTypeDrawer.SetFillDataPoint(chart, s, index, item.Style, dp, chart.StyleManager.Style?.SeriesLine); + ChartTypeDrawer.SetFillDataPoint(chart, s, index, item.Style, dp, chart.StyleManager.Style?.SeriesLine, s.NumberOfItems); } else { - ChartTypeDrawer.SetFillSerie(chart, s._chart, s, index, index, item.Style); + ChartTypeDrawer.SetFillSerie(chart, s._chart, s, index, index, item.Style, s.NumberOfItems); } return item; diff --git a/src/EPPlus/Drawing/Renderer/Chart/LineMarkerHelper.cs b/src/EPPlus/Drawing/Renderer/Chart/LineMarkerHelper.cs index 2432508e2..1ace12eba 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/LineMarkerHelper.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/LineMarkerHelper.cs @@ -28,7 +28,7 @@ internal static RenderItem GetMarkerItem(ChartRenderer sc, ExcelLineChartSerie l } else if (ls.Fill.IsEmpty) { - style.SetDrawingPropertiesFillBasic(sc.Theme, ls.Fill, sc.Chart.StyleManager.Style?.DataPointMarker.FillReference.Color, UserSpaceSettings.ObjectBoundingBox, ChartTypeDrawer.GetVaryColor(sc.Theme, null, ls.Index)); + style.SetDrawingPropertiesFillBasic(sc.Theme, ls.Fill, sc.Chart.StyleManager.Style?.DataPointMarker.FillReference.Color, UserSpaceSettings.ObjectBoundingBox, ChartTypeDrawer.GetVaryColor(sc.Theme, null, ls.Index, ls.NumberOfItems)); } else { diff --git a/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs b/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs index b8ba7f092..8839789ee 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs @@ -567,12 +567,12 @@ internal void ImportStlyeInfo(ExcelPieChartSerie serie, ExcelPieChart chartType, if (position >= 0 && serie.DataPoints.ContainsKey(position)) { var dp = serie.DataPoints[position]; - ChartTypeDrawer.SetFillDataPoint(Chart, serie, position, _slicePath.Style, dp, Chart.StyleManager.Style?.SeriesLine, UserSpaceSettings.ObjectBoundingBox); - _slicePath.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, dp.Border, chartType.StyleManager.Style?.DataPoint.BorderReference.Color, true, null); + ChartTypeDrawer.SetFillDataPoint(Chart, serie, position, _slicePath.Style, dp, Chart.StyleManager.Style?.SeriesLine, serie.NumberOfItems, UserSpaceSettings.ObjectBoundingBox); + //_slicePath.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, dp.Border, chartType.StyleManager.Style?.DataPoint.BorderReference.Color, true, null); } else { - ChartTypeDrawer.SetSingleFillSerieLegacy(Chart, chartType, serie, totalSliceCount, position, _slicePath.Style); + ChartTypeDrawer.SetFillSerie(Chart, chartType, serie, totalSliceCount, position, _slicePath.Style, serie.NumberOfItems); } //if (chartType.VaryColors) //{ diff --git a/src/EPPlus/Drawing/Style/Coloring/ExcelColorTransformCollection.cs b/src/EPPlus/Drawing/Style/Coloring/ExcelColorTransformCollection.cs index e2424e1d2..48cbacabc 100644 --- a/src/EPPlus/Drawing/Style/Coloring/ExcelColorTransformCollection.cs +++ b/src/EPPlus/Drawing/Style/Coloring/ExcelColorTransformCollection.cs @@ -394,6 +394,7 @@ IEnumerator IEnumerable.GetEnumerator() } private static List _defaultVariations = null; + internal static List GetDefault() { if (_defaultVariations == null) @@ -480,6 +481,40 @@ internal static List GetDefault() return _defaultVariations; } + /// + /// Get fallback legacy fade/tint values + /// + /// Current variation or series index + /// total number of variations/series + /// + /// To our knowledge this pattern always considers the first index to be 1 rather than 0. + /// This option is future-proofing + /// + internal static double GetLegacyFadePatternTintShadePercentage(int variationIndex, int numVariations, bool isIndex1Based = true) + { + variationIndex += 1; + numVariations += 1; + + var tintValue = (-70d + 140d * (variationIndex / (numVariations + 1d))) / 100d; + + return tintValue; + } + + internal static List GetLegacyDefaultPattern2(int numVariations) + { + List legacyVariations = new List(); + + for (int i = 0; i< numVariations; i++) + { + var currVar = new ExcelColorTransformCollection(); + var tintPercentage = GetLegacyFadePatternTintShadePercentage(i, numVariations); + //Negative tint percentages are shades but underlying functions handle that + currVar.AddTint(tintPercentage); + legacyVariations.Add(currVar); + } + return legacyVariations; + } + /// /// Number of items in the collection /// From 4f3302bb93e86fc4fde4abd43270f7f3d564d911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ossian=20Edstr=C3=B6m?= Date: Mon, 23 Nov 2026 13:12:50 +0100 Subject: [PATCH 08/23] Fixed multiple legacy bugs and implemented into standard func --- .../Chart/ChartTypeDrawers/ChartTypeDrawer.cs | 98 ++++++++++--------- .../Coloring/ExcelColorTransformCollection.cs | 26 ++++- 2 files changed, 74 insertions(+), 50 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs index bd0e11555..85df8e741 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs @@ -322,69 +322,77 @@ internal static Color GetBaseColor(ExcelTheme theme, ExcelChartStyleManager? sty internal static Color? GetVaryColor(ExcelTheme theme, ExcelChartStyleManager? styleManager, int index, int legacyVariationCount) { - Color baseColor; - var baseColorIndex = index % 6; - - ExcelChartColorsManager colorsManager = styleManager?.ColorsManager; - if ((colorsManager == null || baseColorIndex >= colorsManager.Colors.Count)) + try { - if (styleManager.Style != null) - { - //TODO: Check Chart.Style here. Only when THAT is set to None should the below line happen - baseColor = theme.ColorScheme.GetColorByEnum(eSchemeColor.Accent1 + baseColorIndex).GetColor(); - } - else - { - baseColor = DefaultDataPointPatterns.FallBackColors[baseColorIndex]; - } - } - else - { - baseColor = colorsManager.Colors[baseColorIndex].GetColor(); - } - var variationIndex = index / 6; - if (variationIndex == 0) - { - return baseColor; - } - else - { - ExcelColorTransformCollection variation; - if (colorsManager == null) + Color baseColor; + var baseColorIndex = index % 6; + + ExcelChartColorsManager colorsManager = styleManager?.ColorsManager; + if ((colorsManager == null || baseColorIndex >= colorsManager.Colors.Count)) { - if(styleManager.Style == null) + if (styleManager.Style != null) { - var variations = ExcelColorTransformCollection.GetLegacyDefaultPattern2(legacyVariationCount); - variation = variations[variationIndex % variations.Count]; + //TODO: Check Chart.Style here. Only when THAT is set to None should the below line happen + baseColor = theme.ColorScheme.GetColorByEnum(eSchemeColor.Accent1 + baseColorIndex).GetColor(); } else { - var variations = ExcelColorTransformCollection.GetDefault(); - variation = variations[variationIndex % variations.Count]; + baseColor = DefaultDataPointPatterns.FallBackColors[baseColorIndex]; } } else { - if (colorsManager.Variations.Count == 0) - { - return baseColor; - } - - int colorCount = -1; + baseColor = colorsManager.Colors[baseColorIndex].GetColor(); + } - if (colorsManager.Variations.Count > colorsManager.Colors.Count) + var variationIndex = index / 6; + if (variationIndex == 0 && styleManager.Style != null) + { + return baseColor; + } + else + { + ExcelColorTransformCollection variation; + if (colorsManager == null) { - colorCount = colorsManager.Colors.Count; + if (styleManager.Style == null) + { + var variations = ExcelColorTransformCollection.GetLegacyDefaultPattern2(legacyVariationCount); + variation = variations[variationIndex % variations.Count]; + } + else + { + var variations = ExcelColorTransformCollection.GetDefault(); + variation = variations[variationIndex % variations.Count]; + } } else { - colorCount = colorsManager.Variations.Count; + if (colorsManager.Variations.Count == 0) + { + return baseColor; + } + + int colorCount = -1; + + if (colorsManager.Variations.Count > colorsManager.Colors.Count) + { + colorCount = colorsManager.Colors.Count; + } + else + { + colorCount = colorsManager.Variations.Count; + } + + variation = colorsManager.Colors[variationIndex % colorCount].Transforms; } - - variation = colorsManager.Colors[variationIndex % colorCount].Transforms; + return OfficeOpenXml.Utils.TypeConversion.ColorConverter.ApplyTransforms(baseColor, variation); } - return OfficeOpenXml.Utils.TypeConversion.ColorConverter.ApplyTransforms(baseColor, variation); + } + catch(Exception ex) + { + throw new SystemException($"{ex.Message}", ex); } } protected double GetAxisBaseY(ChartAxisRenderer catAx, ChartAxisRenderer valAx) diff --git a/src/EPPlus/Drawing/Style/Coloring/ExcelColorTransformCollection.cs b/src/EPPlus/Drawing/Style/Coloring/ExcelColorTransformCollection.cs index 48cbacabc..5ece21b9f 100644 --- a/src/EPPlus/Drawing/Style/Coloring/ExcelColorTransformCollection.cs +++ b/src/EPPlus/Drawing/Style/Coloring/ExcelColorTransformCollection.cs @@ -495,7 +495,7 @@ internal static double GetLegacyFadePatternTintShadePercentage(int variationInde variationIndex += 1; numVariations += 1; - var tintValue = (-70d + 140d * (variationIndex / (numVariations + 1d))) / 100d; + var tintValue = (-70d + 140d * ((double)variationIndex / ((double)numVariations + 1d))); return tintValue; } @@ -503,13 +503,29 @@ internal static double GetLegacyFadePatternTintShadePercentage(int variationInde internal static List GetLegacyDefaultPattern2(int numVariations) { List legacyVariations = new List(); - - for (int i = 0; i< numVariations; i++) + //must be run at least once without changing numVariations + int numIterations = numVariations + 1; + for (int i = 0; i < numIterations; i++) { var currVar = new ExcelColorTransformCollection(); var tintPercentage = GetLegacyFadePatternTintShadePercentage(i, numVariations); - //Negative tint percentages are shades but underlying functions handle that - currVar.AddTint(tintPercentage); + if (tintPercentage != 0) + { + //Negative tint percentages are shades but underlying functions handle that + if (tintPercentage < 0) + { + currVar.AddShade(100 + tintPercentage); + } + else + { + currVar.AddTint(100 - tintPercentage); + } + } + else + { + //Add nothing leaving it unchanged + currVar.AddLuminance(0); + } legacyVariations.Add(currVar); } return legacyVariations; From 23366bb56cf50d2aa369b1452584adc9bd083272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ossian=20Edstr=C3=B6m?= Date: Mon, 23 Nov 2026 13:18:29 +0100 Subject: [PATCH 09/23] Cleaned up now unneccesary methods and comments --- .../Chart/ChartTypeDrawers/ChartTypeDrawer.cs | 51 ------------------- .../Renderer/Chart/PieSliceRenderItem.cs | 13 ----- 2 files changed, 64 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs index 85df8e741..13eb32221 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs @@ -240,16 +240,6 @@ internal bool IsOnAxis(ExcelChartAxisStandard ax) return _chartType.YAxis==ax || _chartType.XAxis==ax; } - internal int GetVariationCount(ExcelChartStandardSerie cStandardSerie) - { - var valValues = LoadSeriesValues(cStandardSerie.Series, cStandardSerie.NumberLiteralsY, cStandardSerie.StringLiteralsY); - var variationCount = valValues.Count / 6; - //Compare these two and take the higher count later? We should standardize loading series values more - //var catValues = LoadSeriesValues(cStandardSerie.XSeries, cStandardSerie.NumberLiteralsX, cStandardSerie.StringLiteralsX); - - return variationCount; - } - internal static void SetFillDataPoint(ExcelChart chart, ExcelChartStandardSerie cStandardSerie, int index, RenderItemStyle style, ExcelChartDataPoint dp, ExcelChartStyleEntry entry, int numDataPoints, UserSpaceSettings spaceSettings = UserSpaceSettings.UserSpaceOnUse_Object) { var legacyVariationCount = numDataPoints / 6; @@ -259,22 +249,6 @@ internal static void SetFillDataPoint(ExcelChart chart, ExcelChartStandardSerie style.SetDrawingPropertiesFill(theme, dp.Fill.IsEmpty ? cStandardSerie.Fill : dp.Fill, entry?.FillReference.Color, spaceSettings, color); style.SetDrawingPropertiesBorder(theme, dp.Border.IsEmpty ? cStandardSerie.Border : dp.Border, entry?.BorderReference.Color, dp.Border.Fill.Style != eFillStyle.NoFill, () => Color.Transparent, 0.75); } - - internal static void SetSingleFillSerieLegacy(ExcelChart chart, ExcelChart ct, ExcelChartStandardSerie cStandardSerie, int valueCount, int index, RenderItemStyle style) - { - var theme = chart.WorkSheet.Workbook.ThemeManager.GetOrCreateTheme(); - var baseColor = GetBaseColor(theme, chart.StyleManager, index); - - var currentIterationIndex = index / 6; - var totalNumberOfVariations = (valueCount / 6); - - var variation = DefaultDataPointPatterns.GetSingleLegacyColorVariation(currentIterationIndex, totalNumberOfVariations, baseColor); - style.SetDrawingPropertiesFill(theme, cStandardSerie.Fill, chart.StyleManager.Style?.SeriesLine.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, variation); - - var styleFormat = DefaultChartBorder.GetDefaulDataPoint2d(theme, chart, currentIterationIndex); - style.SetDrawingPropertiesBorder(theme, cStandardSerie.Border, chart.StyleManager.Style?.SeriesLine.BorderReference.Color, cStandardSerie.Border.Fill.Style != eFillStyle.NoFill || styleFormat.width > 0, () => styleFormat.color, 0.75); - } - internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartStandardSerie cStandardSerie, int serieIndex, int index, RenderItemStyle style, int numValuesInSeries) { var theme = chart.WorkSheet.Workbook.ThemeManager.GetOrCreateTheme(); @@ -295,31 +269,6 @@ internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartSta style.SetDrawingPropertiesBorder(theme, cStandardSerie.Border, chart.StyleManager.Style?.SeriesLine.BorderReference.Color, cStandardSerie.Border.Fill.Style != eFillStyle.NoFill || styleFormat.width>0, () => styleFormat.color, 0.75); } - internal static Color GetBaseColor(ExcelTheme theme, ExcelChartStyleManager? styleManager, int index) - { - Color baseColor; - var baseColorIndex = index % 6; - - ExcelChartColorsManager colorsManager = styleManager?.ColorsManager; - if ((colorsManager == null || baseColorIndex >= colorsManager.Colors.Count)) - { - if(styleManager.Style == null) - { - baseColor = DefaultDataPointPatterns.FallBackColors[baseColorIndex]; - } - else - { - //TODO: Check Chart.Style here. Only when THAT is set to None should the below line happen - baseColor = theme.ColorScheme.GetColorByEnum(eSchemeColor.Accent1 + baseColorIndex).GetColor(); - } - } - else - { - baseColor = colorsManager.Colors[baseColorIndex].GetColor(); - } - return baseColor; - } - internal static Color? GetVaryColor(ExcelTheme theme, ExcelChartStyleManager? styleManager, int index, int legacyVariationCount) { try diff --git a/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs b/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs index 8839789ee..22bd766c1 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/PieSliceRenderItem.cs @@ -568,24 +568,11 @@ internal void ImportStlyeInfo(ExcelPieChartSerie serie, ExcelPieChart chartType, { var dp = serie.DataPoints[position]; ChartTypeDrawer.SetFillDataPoint(Chart, serie, position, _slicePath.Style, dp, Chart.StyleManager.Style?.SeriesLine, serie.NumberOfItems, UserSpaceSettings.ObjectBoundingBox); - //_slicePath.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, dp.Border, chartType.StyleManager.Style?.DataPoint.BorderReference.Color, true, null); } else { ChartTypeDrawer.SetFillSerie(Chart, chartType, serie, totalSliceCount, position, _slicePath.Style, serie.NumberOfItems); } - //if (chartType.VaryColors) - //{ - // if (chartType.StyleManager.Style == null) - // { - // var mod5 = position % 5; - // //TODO: Only works for base-case. Add support for patterns 1,3 and 4 instead of just 2 as basecase - // defaultFill = ChartRenderer.Theme.ColorScheme.GetColorByEnum(OfficeOpenXml.Drawing.eSchemeColor.Accent1 + mod5).GetColor(); - // } - //} - ////ChartTypeDrawer.SetFillDataPoint - //_slicePath.Style.SetDrawingPropertiesFill(ChartRenderer.Theme, dp.Fill, chartType.StyleManager.Style?.DataPoint.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, defaultFill); - //_slicePath.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, dp.Border, chartType.StyleManager.Style?.DataPoint.BorderReference.Color, true, null); } internal void AppendGroupItem(GroupRenderItem group) From c0938873cef61113b72119c252381ad584a3f229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ossian=20Edstr=C3=B6m?= Date: Mon, 23 Nov 2026 16:53:07 +0100 Subject: [PATCH 10/23] Fixed stylemanger nullref --- .../Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs index 13eb32221..f7636a21d 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs @@ -280,7 +280,7 @@ internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartSta ExcelChartColorsManager colorsManager = styleManager?.ColorsManager; if ((colorsManager == null || baseColorIndex >= colorsManager.Colors.Count)) { - if (styleManager.Style != null) + if (styleManager == null || styleManager.Style != null) { //TODO: Check Chart.Style here. Only when THAT is set to None should the below line happen baseColor = theme.ColorScheme.GetColorByEnum(eSchemeColor.Accent1 + baseColorIndex).GetColor(); @@ -296,7 +296,7 @@ internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartSta } var variationIndex = index / 6; - if (variationIndex == 0 && styleManager.Style != null) + if (variationIndex == 0 && styleManager != null && styleManager.Style != null) { return baseColor; } @@ -305,7 +305,7 @@ internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartSta ExcelColorTransformCollection variation; if (colorsManager == null) { - if (styleManager.Style == null) + if (styleManager == null || styleManager.Style == null) { var variations = ExcelColorTransformCollection.GetLegacyDefaultPattern2(legacyVariationCount); variation = variations[variationIndex % variations.Count]; From 873edb93dbd3064b273b02614dcbaa362b7b748e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4llman?= Date: Mon, 23 Nov 2026 17:22:05 +0100 Subject: [PATCH 11/23] Multi category axis on bar charts --- .../Renderer/Chart/ChartAxisRenderer.cs | 251 +++++++++++++----- .../Renderer/Chart/ChartAxisTextBoxes.cs | 1 + src/EPPlus/Drawing/Renderer/ChartRenderer.cs | 9 +- 3 files changed, 193 insertions(+), 68 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs index 1bc53b102..a8c80fd67 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs @@ -453,16 +453,6 @@ private double GetTextWidest(ExcelChartAxisStandard ax, bool isMulti) } return widest; } - //internal GetAxisWidthForPlotarea() - //{ - - //} - //internal double GetAxisHeightForPlotarea() - //{ - // var pa = ChartRenderer.Plotarea; - - // var - //} public List Values { get; @@ -521,7 +511,6 @@ public override void AppendRenderItems(List renderItems) { axisGroup.ChildObjects.Add(TickMarkGroup); } - axisGroup.ChildObjects.AddRange(MultiCategoryLineRenderItems); //renderItems.Add(axisGroup); plotareaGroup.AddChildItem(axisGroup); @@ -648,7 +637,7 @@ internal void AddTickmarksAndValues(List DefItems) } } - List MultiCategoryLineRenderItems { get; } = new List(); + internal List MultiCategoryLineRenderItems { get; } = new List(); private List GetAxisValueTextBoxes(out double widest, out double highest) { @@ -700,7 +689,14 @@ private List GetAxisValueTextBoxes(out double widest, out List ret; if (AxisValues==null) { - return AddTextboxesMulti(shaper, fontSize, lineHeight, maxWidth, maxHeight, out widest, out highest); + if(Axis.IsVertical) + { + return AddTextboxesMultiVertical(shaper, fontSize, lineHeight, maxWidth, maxHeight, out widest, out highest); + } + else + { + return AddTextboxesMultiHorizontal(shaper, fontSize, lineHeight, maxWidth, maxHeight, out widest, out highest); + } } else { @@ -1158,7 +1154,7 @@ private List AddTextboxes(ITextShaper shaper, float fontSi } return ret; } - private List AddTextboxesMulti(ITextShaper shaper, float fontSize, float lineHeight, double maxWidth, double maxHeight, out double widest, out double highest) + private List AddTextboxesMultiHorizontal(ITextShaper shaper, float fontSize, float lineHeight, double maxWidth, double maxHeight, out double widest, out double highest) { widest = highest = 0; var axisStyle = GetAxisStyleEntry(); @@ -1168,13 +1164,8 @@ private List AddTextboxesMulti(ITextShaper shaper, float f var firstItem = (string[])AxisValuesMulti.First(); var isTopRow = true; var heightAdd = 0D; - var isVertical = Axis.IsVertical; - var majorSize = (isVertical ? Rectangle.Height : Rectangle.Width) / AxisValuesMulti.Count; + var majorSize = Rectangle.Width / AxisValuesMulti.Count; var margin = fontSize * 0.5; //margin between lines - if(isVertical) - { - widest = GetFirstColTextWidest(shaper, fontSize, AxisValuesMulti); - } for (int j = firstItem.Length - 1; j >= 0; j--) { var emptyCells = 0; @@ -1191,26 +1182,22 @@ private List AddTextboxesMulti(ITextShaper shaper, float f } else if (isTopRow == false && tb != null) { - if (isVertical) - { - if (AdjustEmptyCellsVertical(tb, i, j, emptyCells, majorSize, widest) == false) - { - continue; - } - } - else + if (AdjustEmptyCellsHorizontal(tb, i, j, emptyCells, majorSize) == false) { - if (AdjustEmptyCellsHorizontal(tb, i, j, emptyCells, majorSize) == false) - { - continue; - } + continue; } emptyCells = 0; } var v = ((object[])Values[i])[j]; var t = AxisValuesMulti[i][j]; + + //An empty label has no size, as with ITextMeasurer. + float width = string.IsNullOrEmpty(t) ? 0f : shaper.Shape(t).GetWidthInPoints(fontSize); + float height = string.IsNullOrEmpty(t) ? 0f : lineHeight; + double ticMarkX, ticMarkY; + double x, y; ticMarkX = GetAxisItemLeft(i); double majorTickMarkWidth; if (i + 1 < Values.Count) @@ -1231,20 +1218,8 @@ private List AddTextboxesMulti(ITextShaper shaper, float f majorTickMarkWidth = 0; } } + ticMarkY = GetAxisItemTop(i, width, height) + heightAdd; - //An empty label has no size, as with ITextMeasurer. - float width = string.IsNullOrEmpty(t) ? 0f : shaper.Shape(t).GetWidthInPoints(fontSize); - float height = string.IsNullOrEmpty(t) ? 0f : lineHeight; - - if(isVertical) - { - ticMarkY = 0; //We adjust later when we now the widest label. - } - else - { - ticMarkY = GetAxisItemTop(i, width, height) + heightAdd; - } - double x, y; if (LabelOrientation == eTextOrientation.Horizontal || isTopRow == false) { x = ticMarkX; @@ -1301,7 +1276,7 @@ private List AddTextboxesMulti(ITextShaper shaper, float f } if (isTopRow) { - AddGroupLines(x, heightFirstRow + margin); + AddGroupLinesVertical(x, heightFirstRow + margin); switch (Axis.LabelAlignment) { case eAxisLabelAlignment.Left: @@ -1316,7 +1291,7 @@ private List AddTextboxesMulti(ITextShaper shaper, float f } else { - AddGroupLines(x, lineHeight + margin + heightAdd); + AddGroupLinesVertical(x, lineHeight + margin + heightAdd); } tb = new DrawingTextContainer(ChartRenderer.RenderContext, Chart, Rectangle, DefaultFonts2007Style.Axis, x, y, width, height, GetMaxWidthForEmptyRows(maxWidth, isTopRow, i, j), maxHeight); @@ -1349,7 +1324,6 @@ private List AddTextboxesMulti(ITextShaper shaper, float f highest = tb.Height; } ret.Add(tb); - } if(isTopRow) { @@ -1361,7 +1335,148 @@ private List AddTextboxesMulti(ITextShaper shaper, float f heightAdd += lineHeight + margin; } } - AddGroupLines(Rectangle.Width, heightAdd); + AddGroupLinesVertical(Rectangle.Width, heightAdd); + return ret; + } + private List AddTextboxesMultiVertical(ITextShaper shaper, float fontSize, float lineHeight, double maxWidth, double maxHeight, out double widest, out double highest) + { + var axisStyle = GetAxisStyleEntry(); + var ret = new List(); + int labelSkip = Axis.AxisType == eAxisType.Cat ? (int)(Axis.MajorUnit ?? 1) : 1; + var heightFirstRow = Rectangle.Height - (lineHeight * AxisValuesMulti[0].Length - 1); + var firstItem = (string[])AxisValuesMulti.First(); + var isTopRow = true; + var isVertical = Axis.IsVertical; + var majorSize = (isVertical ? Rectangle.Height : Rectangle.Width) / AxisValuesMulti.Count; + var margin = fontSize * 0.5; //margin between lines + + widest = GetFirstColTextWidest(shaper, fontSize, AxisValuesMulti); + var widthAdd = widest+RightMargin; + highest = 0; + for (int j = firstItem.Length - 1; j >= 0; j--) + { + var emptyCells = 0; + DrawingTextContainer tb = null; + for (var i = 0; i < AxisValuesMulti.Count; i++) + { + if (i % labelSkip != 0) continue; + var items = AxisValuesMulti[i]; + + if (isTopRow == false && i + 1 < AxisValuesMulti.Count && string.IsNullOrEmpty(AxisValuesMulti[i][j])) + { + emptyCells++; + continue; + } + else if (isTopRow == false && tb != null) + { + if (AdjustEmptyCellsVertical(tb, i, j, emptyCells, majorSize, widest) == false) + { + continue; + } + emptyCells = 0; + } + + var v = ((object[])Values[i])[j]; + var t = AxisValuesMulti[i][j]; + + //An empty label has no size, as with ITextMeasurer. + float width = string.IsNullOrEmpty(t) ? 0f : shaper.Shape(t).GetWidthInPoints(fontSize); + float height = string.IsNullOrEmpty(t) ? 0f : lineHeight; + + double ticMarkX, ticMarkY; + double x, y; + ticMarkY = GetAxisItemTop(i, width, height); + + ticMarkX = j * (lineHeight + LeftMargin); + //if (isTopRow) + //{ + x = ticMarkX; + y = ticMarkY; + //} + //else + //{ + //if (tb == null) + //{ + //x = ticMarkX; + //y = ticMarkY; + //} + //else + //{ + // x = tb.Left; + // y = tb.Top; + //} + //double cos = Math.Cos(MathHelper.Radians(rot)); + //double sin = Math.Sin(MathHelper.Radians(rot)); + + //if (LabelOrientation == eTextOrientation.Diagonal || (LabelOrientation == eTextOrientation.Custom && rot != 90)) + //{ + // if (width < MajorTickMarkPositions) + // { + // x = ticMarkX; + // } + // else + // { + // x = ticMarkX - ((height / 2) * cos); + // } + + // var midPointText = height / 2; + // var halfHeightAfterRot = Math.Abs(((width / 2) * sin)); + // var heightPastY = halfHeightAfterRot - midPointText; + + // y = ticMarkY - heightPastY; + //} + //else if ((rot == 90 || rot == -90)) + //{ + // x = ticMarkX; + // y = ticMarkY - (width / 2); + //} + //else + //{ + // x = ticMarkX - (height / 2); + // if (Axis.ActualAxisPosition == eAxisPosition.Bottom) + // { + // y = ticMarkY + BottomMargin + 4; + // } + // else //Top + // { + // y = ticMarkY - TopMargin - 4; + // } + //} + //} + //} + + tb = new DrawingTextContainer(ChartRenderer.RenderContext, Chart, Rectangle, DefaultFonts2007Style.Axis, x, y, width, height, GetMaxWidthForEmptyRows(maxWidth, isTopRow, i, j), maxHeight); + tb.SetRotation(EPPlus.DrawingRenderer.RenderItems.Textbox.RotationAnchor.Middle); + if (isTopRow == false) + { + tb.TextBody.Rotation = -90; + } + + var p = Axis.TextBody.Paragraphs.FirstOrDefault(); + + if (p != null) + { + if (p.HorizontalAlignment != eTextAlignment.Center && Axis.AxisType != eAxisType.Val && (Axis.AxisPosition == eAxisPosition.Bottom || Axis.AxisPosition == eAxisPosition.Top)) + { + //Horizontal axises are always center aligned visually + //Should be broken out as input to ImportParagraph instead of changing the base item + p.HorizontalAlignment = eTextAlignment.Center; + } + tb.ImportParagraph(p, 0, t); + } + else + { + tb.AddText(t); + } + + tb.GetStyleBg().SetDrawingPropertiesFill(ChartRenderer.Theme, Axis.Fill, axisStyle?.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Global, DefaultFillColor); + ret.Add(tb); + AddGroupLinesHorizontal(y+majorSize, widthAdd); + } + widthAdd += lineHeight + LeftMargin; + isTopRow = false; + } + AddGroupLinesHorizontal(0, widthAdd- lineHeight - LeftMargin); return ret; } @@ -1421,19 +1536,20 @@ private bool AdjustEmptyCellsHorizontal(DrawingTextContainer tb, int i, int j, i private bool AdjustEmptyCellsVertical(DrawingTextContainer tb, int i, int j, int emptyCells, double majorHeight, double widest) { //center the text box in the middle of the empty cells. - var top = GetAxisItemTop(i - emptyCells - 1, tb.Width, tb.Height); + var top = majorHeight * (AxisValuesMulti.Count-i); //GetAxisItemTop(i - emptyCells - 1, tb.Width, tb.Height); if (i + 1 == AxisValuesMulti.Count && string.IsNullOrEmpty(AxisValuesMulti[i][j])) { + top -= majorHeight; switch (Axis.LabelAlignment) { case eAxisLabelAlignment.Left: - tb.Top = top; + tb.Top = top + majorHeight * (emptyCells + 2); break; case eAxisLabelAlignment.Center: - tb.Left = top + (majorHeight * (emptyCells + 2) / 2) - (tb.RotatedHeight / 2); + tb.Top = top + (majorHeight * (emptyCells + 2) / 2) + (tb.TextBody.RotatedHeight / 2); break; case eAxisLabelAlignment.Right: - tb.Left = top + majorHeight * (emptyCells + 2) - tb.RotatedHeight; + tb.Top = top + tb.TextBody.RotatedHeight; break; } return false; @@ -1443,13 +1559,13 @@ private bool AdjustEmptyCellsVertical(DrawingTextContainer tb, int i, int j, int switch (Axis.LabelAlignment) { case eAxisLabelAlignment.Left: - tb.Top = top; + tb.Top = top + majorHeight * (emptyCells + 1); break; case eAxisLabelAlignment.Center: - tb.Top = top + (majorHeight * (emptyCells + 1) / 2) - (tb.RotatedHeight / 2); + tb.Top = top + (majorHeight * (emptyCells + 1) / 2) + (tb.TextBody.RotatedHeight / 2); break; case eAxisLabelAlignment.Right: - tb.Top = top + majorHeight * (emptyCells + 1) - tb.RotatedHeight; + tb.Top = top + majorHeight * (emptyCells + 1) - tb.TextBody.RotatedHeight; break; } } @@ -1457,20 +1573,29 @@ private bool AdjustEmptyCellsVertical(DrawingTextContainer tb, int i, int j, int return true; } - private void AddGroupLines(double x, double length) + private void AddGroupLinesVertical(double x, double length) { - if (Axis.IsVertical) + var li = new LineRenderItem(Rectangle) { X1 = x, X2 = x, Y1 = 0, Y2 = length }; + li.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, Axis.Border, ChartRenderer.Chart.StyleManager.Style?.Title.BorderReference.Color, Axis.Border.IsEmpty == true || Axis.Border.Fill.Style != eFillStyle.NoFill, GetDefaultBorderColor, 0.75); + MultiCategoryLineRenderItems.Add(li); + } + private void AddGroupLinesHorizontal(double y, double length) + { + double x1, x2; + if(Axis.ActualAxisPosition==eAxisPosition.Left) { - + x1 = Rectangle.Width - length; + x2 = Rectangle.Width; } else { - var li = new LineRenderItem(Rectangle) { X1 = x, X2 = x, Y1 = 0, Y2 = length }; - li.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, Axis.Border, ChartRenderer.Chart.StyleManager.Style?.Title.BorderReference.Color, Axis.Border.IsEmpty == true || Axis.Border.Fill.Style != eFillStyle.NoFill, GetDefaultBorderColor, 0.75); - MultiCategoryLineRenderItems.Add(li); + x1 = 0; + x2 = length; } + var li = new LineRenderItem(Rectangle) { X1 = x1, X2 = x2, Y1 = y, Y2 = y }; + li.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, Axis.Border, ChartRenderer.Chart.StyleManager.Style?.Title.BorderReference.Color, Axis.Border.IsEmpty == true || Axis.Border.Fill.Style != eFillStyle.NoFill, GetDefaultBorderColor, 0.75); + MultiCategoryLineRenderItems.Add(li); } - private double GetMaxWidthForEmptyRows(double maxWidth, bool isTopRow, int startIndex, int col) { if (isTopRow) return maxWidth; diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisTextBoxes.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisTextBoxes.cs index a9da2990e..54cbdb969 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisTextBoxes.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisTextBoxes.cs @@ -57,6 +57,7 @@ public override void AppendRenderItems(List renderItems) { tb.Parent = axisTxtBoxGroup; } + axisTxtBoxGroup.ChildObjects.AddRange(_axis.MultiCategoryLineRenderItems); renderItems.Add(axisTxtBoxGroup); } diff --git a/src/EPPlus/Drawing/Renderer/ChartRenderer.cs b/src/EPPlus/Drawing/Renderer/ChartRenderer.cs index 53b587874..6810a0511 100644 --- a/src/EPPlus/Drawing/Renderer/ChartRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/ChartRenderer.cs @@ -683,11 +683,6 @@ public bool AppendItems() { Plotarea?.AppendRenderItems(RenderItems); - HorizontalAxis?.AppendRenderItems(RenderItems); - VerticalAxis?.AppendRenderItems(RenderItems); - SecondHorizontalAxis?.AppendRenderItems(RenderItems); - SecondVerticalAxis?.AppendRenderItems(RenderItems); - if (Plotarea != null) { //TODO: Add typeDrawers group? @@ -698,6 +693,10 @@ public bool AppendItems() drawer.AppendRenderItems(RenderItems); } } + HorizontalAxis?.AppendRenderItems(RenderItems); + VerticalAxis?.AppendRenderItems(RenderItems); + SecondHorizontalAxis?.AppendRenderItems(RenderItems); + SecondVerticalAxis?.AppendRenderItems(RenderItems); HorizontalAxis?.Textboxes?.AppendRenderItems(RenderItems); VerticalAxis?.Textboxes?.AppendRenderItems(RenderItems); From cfabd00f2002dae07b5c724be1ab2ad25e50cdd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4llman?= Date: Tue, 24 Nov 2026 08:16:21 +0100 Subject: [PATCH 12/23] Fixed a high axis label issue --- src/EPPlus/Drawing/Renderer/ChartRenderer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/ChartRenderer.cs b/src/EPPlus/Drawing/Renderer/ChartRenderer.cs index 6810a0511..70ac598f6 100644 --- a/src/EPPlus/Drawing/Renderer/ChartRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/ChartRenderer.cs @@ -297,12 +297,12 @@ private void PlaceHorizontalAxis(ChartAxisRenderer horizontalAxis, bool isSecond case eTickLabelPosition.High: //if (ax.AxisPosition == eAxisPosition.Bottom) //{ - horizontalAxis.Rectangle.Top = Plotarea.Group.GlobalTop; + horizontalAxis.Rectangle.Top = Plotarea.Group.GlobalTop - horizontalAxis.Rectangle.Height; horizontalAxis.AdjustPosBySize = true; //} //else //{ - // horizontalAxis.Rectangle.Top = Plotarea.Group.GlobalTop+Plotarea.Rectangle.Height; + // horizontalAxis.Rectangle.Top = Plotarea.Group.GlobalBottom; // horizontalAxis.AdjustPosBySize = false; //} break; From 550a97140716730f09482b63808a83049da81378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ossian=20Edstr=C3=B6m?= Date: Tue, 24 Nov 2026 13:13:25 +0100 Subject: [PATCH 13/23] Fix for legacy lines --- .../Chart/ChartTypeDrawers/ChartTypeDrawer.cs | 13 +++++++------ .../Chart/ChartTypeDrawers/LineChartTypeDrawer.cs | 10 +++++----- .../Drawing/Renderer/Chart/LegendIconRenderer.cs | 12 ++++++++---- .../Drawing/Renderer/Chart/LineMarkerHelper.cs | 2 +- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs index f7636a21d..646f070f5 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/ChartTypeDrawer.cs @@ -244,32 +244,33 @@ internal static void SetFillDataPoint(ExcelChart chart, ExcelChartStandardSerie { var legacyVariationCount = numDataPoints / 6; var theme = chart.WorkSheet.Workbook.ThemeManager.GetOrCreateTheme(); - var color = GetVaryColor(theme, chart.StyleManager, index, legacyVariationCount); + var color = GetVaryColor(theme, chart.StyleManager, index, legacyVariationCount, true); style.SetDrawingPropertiesFill(theme, dp.Fill.IsEmpty ? cStandardSerie.Fill : dp.Fill, entry?.FillReference.Color, spaceSettings, color); style.SetDrawingPropertiesBorder(theme, dp.Border.IsEmpty ? cStandardSerie.Border : dp.Border, entry?.BorderReference.Color, dp.Border.Fill.Style != eFillStyle.NoFill, () => Color.Transparent, 0.75); } internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartStandardSerie cStandardSerie, int serieIndex, int index, RenderItemStyle style, int numValuesInSeries) { + var isSingleSeries = ct.Series.Count == 1; var theme = chart.WorkSheet.Workbook.ThemeManager.GetOrCreateTheme(); var legacyVariationCount = numValuesInSeries / 6; if (ct.VaryColors) { var chartStyle =(int)chart.Style; //Get the color based on the index, if no style is set. Accent1, Accent2, Accent3... - var color = GetVaryColor(theme, chart.StyleManager, index, legacyVariationCount); + var color = GetVaryColor(theme, chart.StyleManager, index, legacyVariationCount, isSingleSeries); style.SetDrawingPropertiesFill(theme, cStandardSerie.Fill, chart.StyleManager.Style?.SeriesLine.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, color); } else { - var color = GetVaryColor(theme, chart.StyleManager, serieIndex, legacyVariationCount); + var color = GetVaryColor(theme, chart.StyleManager, serieIndex, legacyVariationCount, isSingleSeries); style.SetDrawingPropertiesFill(theme, cStandardSerie.Fill, chart.StyleManager.Style?.SeriesLine.FillReference.Color, UserSpaceSettings.UserSpaceOnUse_Object, color); } var styleFormat = DefaultChartBorder.GetDefaulDataPoint2d(theme, chart, serieIndex); style.SetDrawingPropertiesBorder(theme, cStandardSerie.Border, chart.StyleManager.Style?.SeriesLine.BorderReference.Color, cStandardSerie.Border.Fill.Style != eFillStyle.NoFill || styleFormat.width>0, () => styleFormat.color, 0.75); } - internal static Color? GetVaryColor(ExcelTheme theme, ExcelChartStyleManager? styleManager, int index, int legacyVariationCount) + internal static Color? GetVaryColor(ExcelTheme theme, ExcelChartStyleManager? styleManager, int index, int legacyVariationCount, bool isSingleSeries) { try { @@ -280,7 +281,7 @@ internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartSta ExcelChartColorsManager colorsManager = styleManager?.ColorsManager; if ((colorsManager == null || baseColorIndex >= colorsManager.Colors.Count)) { - if (styleManager == null || styleManager.Style != null) + if (styleManager != null && styleManager.Style != null) { //TODO: Check Chart.Style here. Only when THAT is set to None should the below line happen baseColor = theme.ColorScheme.GetColorByEnum(eSchemeColor.Accent1 + baseColorIndex).GetColor(); @@ -296,7 +297,7 @@ internal static void SetFillSerie(ExcelChart chart, ExcelChart ct, ExcelChartSta } var variationIndex = index / 6; - if (variationIndex == 0 && styleManager != null && styleManager.Style != null) + if (variationIndex == 0 && styleManager != null && styleManager.Style != null && isSingleSeries == false) { return baseColor; } diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/LineChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/LineChartTypeDrawer.cs index a52b208fb..bcd149a2b 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/LineChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/LineChartTypeDrawer.cs @@ -317,13 +317,13 @@ private void AddLine(ExcelLineChart chartType, ExcelLineChartSerie serie, List ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, serie.Index + startSerie, _totalDataPoints)); + ()=> ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, serie.Index + startSerie, _totalDataPoints/6, true)); } } - lineDp.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, dp.Border, chartType.StyleManager.Style?.SeriesLine.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, serie.Index + startSerie, _totalDataPoints), 3); + lineDp.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, dp.Border, chartType.StyleManager.Style?.SeriesLine.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, serie.Index + startSerie, _totalDataPoints / 6, chartType.Series.Count == 1), 3); lineDp.Style.SetDrawingPropertiesEffects(ChartRenderer.Theme, dp.Effect); dataPointOverrides.Add(lineDp); } @@ -351,7 +351,7 @@ private void AddLine(ExcelLineChart chartType, ExcelLineChartSerie serie, List ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, serie.Index, _totalDataPoints), 3); + linePath.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, serie.Border, chartType.StyleManager.Style?.SeriesLine.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, serie.Index, chartType.Series.Count() / 6, true), 3); linePath.Style.SetDrawingPropertiesEffects(ChartRenderer.Theme, serie.Effect); linePath.Style.FillColor = "none"; //No fill for line linePath.Style.StrokeMiterLimit = 4; //A much higher value of the miter limit, might cause the "spike" to get beyond the data point on the vertical scale.. @@ -447,7 +447,7 @@ private void SetMarker(ExcelLineChartSerie serie, ExcelChartMarker marker, List< } if(serie.Marker.Border.IsEmpty) { - ls.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, serie.Border, ChartRenderer.Chart.StyleManager.Style?.DataPointMarker.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, index, _totalDataPoints), 0.75d); + ls.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, serie.Border, ChartRenderer.Chart.StyleManager.Style?.DataPointMarker.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, index, _totalDataPoints, false), 0.75d); } } markerItems.Add(ls); diff --git a/src/EPPlus/Drawing/Renderer/Chart/LegendIconRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/LegendIconRenderer.cs index c706b5ff2..c84b22572 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/LegendIconRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/LegendIconRenderer.cs @@ -160,7 +160,8 @@ internal static void SetLineLegend(ChartRenderer chart, T parent, ExcelChart { var ls = (ExcelLineChartSerie)s; - var si = GetLineSeriesIcon(chart, parent, ls, pSls, entryWidth, entryHeight); + var numSeries = ct.Series.Count() == 1 ? s.NumberOfItems : ct.Series.Count(); + var si = GetLineSeriesIcon(chart, parent, ls, pSls, entryWidth, entryHeight, numSeries); sls.SeriesIcon = si; var tbLeft = si.X1 + maxIconLength + MarginIconText; @@ -209,7 +210,7 @@ internal static void SetLineLegend(ChartRenderer chart, T parent, ExcelChart } if (ls.Marker.Border.IsEmpty) { - sls.MarkerIcon.Style.SetDrawingPropertiesBorder(chart.Theme, ls.Border, chart.Chart.StyleManager.Style?.DataPointMarker.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(chart.Theme, null, index, s.NumberOfItems), 0.75d); + sls.MarkerIcon.Style.SetDrawingPropertiesBorder(chart.Theme, ls.Border, chart.Chart.StyleManager.Style?.DataPointMarker.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(chart.Theme, null, index, s.NumberOfItems, numSeries == 1), 0.75d); } } } @@ -330,11 +331,14 @@ internal static void SetPieLegend(ChartRenderer chart, T parent, ExcelChart c sls = null; } - internal static LineRenderItem GetLineSeriesIcon(ChartRenderer chart, T parent, ExcelChartStandardSerie cStandardSerie, DrawingLegendSerie pSls, double entryWidth, double entryHeight) where T : ChartDrawingObjectWithBackground, ILegendKeyContainer + internal static LineRenderItem GetLineSeriesIcon(ChartRenderer chart, T parent, ExcelChartStandardSerie cStandardSerie, DrawingLegendSerie pSls, double entryWidth, double entryHeight, int numSeries) where T : ChartDrawingObjectWithBackground, ILegendKeyContainer { var line = new LineRenderItem(parent.Rectangle); //Default style is NoLine NoFill - line.Style.SetDrawingPropertiesBorder(chart.Theme, cStandardSerie.Border, chart.Chart.StyleManager.Style?.SeriesLine.BorderReference.Color, cStandardSerie.Border.IsEmpty || cStandardSerie.Border.Fill.Style != eFillStyle.NoFill, () => ChartTypeDrawer.GetVaryColor(chart.Theme, null, cStandardSerie.Index, cStandardSerie.NumberOfItems), 2.25); + var totalVariations = numSeries == 1 ? cStandardSerie.NumberOfItems : numSeries; + totalVariations = totalVariations / 6; + + line.Style.SetDrawingPropertiesBorder(chart.Theme, cStandardSerie.Border, chart.Chart.StyleManager.Style?.SeriesLine.BorderReference.Color, cStandardSerie.Border.IsEmpty || cStandardSerie.Border.Fill.Style != eFillStyle.NoFill, () => ChartTypeDrawer.GetVaryColor(chart.Theme, null, cStandardSerie.Index, totalVariations, numSeries == 1), 2.25); double iconTop = 0, iconLeft = 0; pSls?.GetIconTopLeft(out iconTop, out iconLeft); diff --git a/src/EPPlus/Drawing/Renderer/Chart/LineMarkerHelper.cs b/src/EPPlus/Drawing/Renderer/Chart/LineMarkerHelper.cs index 1ace12eba..29dd54c3c 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/LineMarkerHelper.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/LineMarkerHelper.cs @@ -28,7 +28,7 @@ internal static RenderItem GetMarkerItem(ChartRenderer sc, ExcelLineChartSerie l } else if (ls.Fill.IsEmpty) { - style.SetDrawingPropertiesFillBasic(sc.Theme, ls.Fill, sc.Chart.StyleManager.Style?.DataPointMarker.FillReference.Color, UserSpaceSettings.ObjectBoundingBox, ChartTypeDrawer.GetVaryColor(sc.Theme, null, ls.Index, ls.NumberOfItems)); + style.SetDrawingPropertiesFillBasic(sc.Theme, ls.Fill, sc.Chart.StyleManager.Style?.DataPointMarker.FillReference.Color, UserSpaceSettings.ObjectBoundingBox, ChartTypeDrawer.GetVaryColor(sc.Theme, null, ls.Index, ls.NumberOfItems, sc.Chart.Series.Count == 1)); } else { From 2374f94f34aceee34b3b1215d8e52ff9b1e3dd7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ossian=20Edstr=C3=B6m?= Date: Tue, 24 Nov 2026 13:14:30 +0100 Subject: [PATCH 14/23] Extra fix --- .../Renderer/Chart/ChartTypeDrawers/LineChartTypeDrawer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/LineChartTypeDrawer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/LineChartTypeDrawer.cs index bcd149a2b..78a5496a5 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/LineChartTypeDrawer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTypeDrawers/LineChartTypeDrawer.cs @@ -447,7 +447,7 @@ private void SetMarker(ExcelLineChartSerie serie, ExcelChartMarker marker, List< } if(serie.Marker.Border.IsEmpty) { - ls.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, serie.Border, ChartRenderer.Chart.StyleManager.Style?.DataPointMarker.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, index, _totalDataPoints, false), 0.75d); + ls.Style.SetDrawingPropertiesBorder(ChartRenderer.Theme, serie.Border, ChartRenderer.Chart.StyleManager.Style?.DataPointMarker.BorderReference.Color, true, () => ChartTypeDrawer.GetVaryColor(ChartRenderer.Theme, null, index, _totalDataPoints / 6, false), 0.75d); } } markerItems.Add(ls); From ea5ffe5a821594f18b4203b0e776d3ff426c991c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4llman?= Date: Tue, 24 Nov 2026 13:20:47 +0100 Subject: [PATCH 15/23] Changed axis to use plotarea width/hight when deciding lables --- .../Drawing/Chart/ExcelChartAxisStandard.cs | 7 +-- src/EPPlus/Drawing/Chart/ExcelChartSerie.cs | 35 ++++++++----- .../Renderer/Chart/ChartAxisRenderer.cs | 2 +- .../Renderer/Chart/ChartDataTableRenderer.cs | 2 +- src/EPPlus/ExcelRange.cs | 3 ++ src/EPPlus/ExcelRangeBase.cs | 51 +++++++++++++++++++ 6 files changed, 81 insertions(+), 19 deletions(-) diff --git a/src/EPPlus/Drawing/Chart/ExcelChartAxisStandard.cs b/src/EPPlus/Drawing/Chart/ExcelChartAxisStandard.cs index 45efb3733..1cda8be04 100644 --- a/src/EPPlus/Drawing/Chart/ExcelChartAxisStandard.cs +++ b/src/EPPlus/Drawing/Chart/ExcelChartAxisStandard.cs @@ -1096,6 +1096,7 @@ internal void GetSeriesValues(out bool isCount, out bool isMultiLevel, out List< isMultiLevel = false; foreach (var ct in _chart.PlotArea.ChartTypes) { + var showHidden = ct.ShowHiddenData; foreach (var serie in ct.Series) { var l = new List(); @@ -1110,7 +1111,7 @@ internal void GetSeriesValues(out bool isCount, out bool isMultiLevel, out List< { if (ct.XAxis.Id == Id || ct.YAxis.Id == Id) { - serie.AddFromSerie(l, true, true, new string[] { serie.HeaderAddress?.Address, serie.GetHeaderText(ix) }, out isMultiLevel); + serie.AddFromSerie(l, true, true, new string[] { serie.HeaderAddress?.Address, serie.GetHeaderText(ix) }, showHidden, out isMultiLevel); } else { @@ -1124,11 +1125,11 @@ internal void GetSeriesValues(out bool isCount, out bool isMultiLevel, out List< if (ct.YAxis.Id == Id) { //AddFromSerie(l, serie.Series, serie.NumberLiteralsY, serie.StringLiteralsY, false, null, pl); - serie.AddFromSerie(l, false, false, null, out isMultiLevel); + serie.AddFromSerie(l, false, false, null, showHidden, out isMultiLevel); } else if (ct.XAxis.Id == Id) { - serie.AddFromSerie(l, true, false, null, out isMultiLevel); + serie.AddFromSerie(l, true, false, null, showHidden, out isMultiLevel); } else { diff --git a/src/EPPlus/Drawing/Chart/ExcelChartSerie.cs b/src/EPPlus/Drawing/Chart/ExcelChartSerie.cs index 3c380a008..602590949 100644 --- a/src/EPPlus/Drawing/Chart/ExcelChartSerie.cs +++ b/src/EPPlus/Drawing/Chart/ExcelChartSerie.cs @@ -329,13 +329,13 @@ private string GetAddressValue(ExcelAddressBase address) return ExcelErrorValue.Values.Ref; } - internal List GetValues(bool useX, bool useString, out bool isMultiLevel) + internal List GetValues(bool useX, bool useString, bool showHidden, out bool isMultiLevel) { var l = new List(); - AddFromSerie(l, useX, useString, null, out isMultiLevel); + AddFromSerie(l, useX, useString, null, showHidden, out isMultiLevel); return l; } - internal void AddFromSerie(List list, bool useX, bool useText, string[] headerInfo, out bool isMultiLevel, bool doSort=false) + internal void AddFromSerie(List list, bool useX, bool useText, string[] headerInfo, bool showHidden, out bool isMultiLevel, bool doSort=false) { isMultiLevel = false; var values = new List[] {new List(), new List()}; @@ -345,12 +345,12 @@ internal void AddFromSerie(List list, bool useX, bool useText, string[] if (useX) { var max = GetCount(Series, NumberLiteralsY, StringLiteralsY)??0; - AddSerieValues(list, useText, headerInfo, ref isMultiLevel, XSeries, NumberLiteralsX, StringLiteralsX, max); + AddSerieValues(list, useText, headerInfo, ref isMultiLevel, XSeries, NumberLiteralsX, StringLiteralsX, max, showHidden); } else { var max = GetCount(XSeries, NumberLiteralsX, StringLiteralsX) ?? 0; - AddSerieValues(list, useText, headerInfo, ref isMultiLevel, Series, NumberLiteralsY, StringLiteralsY, max); + AddSerieValues(list, useText, headerInfo, ref isMultiLevel, Series, NumberLiteralsY, StringLiteralsY, max, showHidden); } } else @@ -358,8 +358,8 @@ internal void AddFromSerie(List list, bool useX, bool useText, string[] bool isMultiX=false, isMultiY=false; var maxY = GetCount(Series, NumberLiteralsY, StringLiteralsY) ?? 0; var maxX = GetCount(XSeries, NumberLiteralsX, StringLiteralsX) ?? 0; - AddSerieValues(values[1], useText, headerInfo, ref isMultiX, Series, NumberLiteralsY, StringLiteralsY, maxX); - AddSerieValues(values[0], useText, headerInfo, ref isMultiY, XSeries, NumberLiteralsX, StringLiteralsX, maxY); + AddSerieValues(values[1], useText, headerInfo, ref isMultiX, Series, NumberLiteralsY, StringLiteralsY, maxX, showHidden); + AddSerieValues(values[0], useText, headerInfo, ref isMultiY, XSeries, NumberLiteralsX, StringLiteralsX, maxY, showHidden); var sorted = values[0] .Zip(values[1], (k, v) => (Key: k, Value: v)) @@ -392,10 +392,8 @@ private object GetValue(object v) } } - private void AddSerieValues(List list, bool useText, string[] headerInfo, ref bool isMultiLevel, string address, double[] numberLiterals, string[] stringLiterals, int maxOtherSeries) + private void AddSerieValues(List list, bool useText, string[] headerInfo, ref bool isMultiLevel, string address, double[] numberLiterals, string[] stringLiterals, int maxOtherSeries, bool showHidden) { - var isStacked = false;//_chart.IsTypeStacked() && prevList != null; - var count = Math.Min(GetCount(address, numberLiterals, stringLiterals) ?? maxOtherSeries, maxOtherSeries); if (numberLiterals?.Length > 0) @@ -430,7 +428,7 @@ private void AddSerieValues(List list, bool useText, string[] headerInfo } else { - GetInternalValues(list, useText, headerInfo, a, count, out isMultiLevel); + GetInternalValues(list, useText, headerInfo, a, count, showHidden, out isMultiLevel); } } } @@ -453,12 +451,12 @@ private void AddSerieValues(List list, bool useText, string[] headerInfo return null; } - private void GetInternalValues(List list, bool useText, string[] headerInfo, ExcelAddressBase a, int? count, out bool isMultiLevel) + private void GetInternalValues(List list, bool useText, string[] headerInfo, ExcelAddressBase a, int? count, bool showHidden, out bool isMultiLevel) { var ws = _chart.WorkSheet.Workbook.Worksheets[a.WorkSheetName]; if (ws != null) { - GetValuesFromWorksheet(list, useText, headerInfo, a, ws, count, out isMultiLevel); + GetValuesFromWorksheet(list, useText, headerInfo, a, ws, count, showHidden, out isMultiLevel); } else { @@ -466,9 +464,14 @@ private void GetInternalValues(List list, bool useText, string[] headerI } } - private void GetValuesFromWorksheet(List list, bool useText, string[] headerInfo, ExcelAddressBase a, ExcelWorksheet ws, int? count, out bool isMultiLevel) + private void GetValuesFromWorksheet(List list, bool useText, string[] headerInfo, ExcelAddressBase a, ExcelWorksheet ws, int? count, bool showHidden, out bool isMultiLevel) { var range = ws.Cells[a.Address]; + if (showHidden==false && range.IsHidden) + { + isMultiLevel = false; + return; + } var i = 0; isMultiLevel = range.Rows > 1 && range.Columns > 1; object o; @@ -487,6 +490,10 @@ private void GetValuesFromWorksheet(List list, bool useText, string[] he for (var c = 0; c < range.Columns; c++) { var v = range.Offset(r, c, 1, 1); + if(showHidden==false && v.IsHidden) + { + continue; + } if (useText) { if (headerInfo == null) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs index a8c80fd67..0531455b0 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs @@ -108,7 +108,7 @@ internal ChartAxisRenderer(ChartRenderer sc, ExcelChartAxisStandard ax) : base(s internal void UpdateValueScale() { - Values = GetAxisValue(Axis, ChartRenderer.ChartArea.Rectangle, out double? min, out double? max, out double? majorUnit, out eTimeUnit? dateUnit, out eTextOrientation orientation); + Values = GetAxisValue(Axis, ChartRenderer.Plotarea.Rectangle, out double? min, out double? max, out double? majorUnit, out eTimeUnit? dateUnit, out eTextOrientation orientation); if (_isMulti) { AxisValues = null; diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartDataTableRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartDataTableRenderer.cs index fde5c40d9..66bb854a5 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartDataTableRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartDataTableRenderer.cs @@ -186,7 +186,7 @@ internal ChartDataTableRenderer(ChartRenderer svgChart) : base(svgChart) } var rows = AddSerieValues(svgChart, _maxWidth, _maxHeight, serie); pSls = sls; - var formattedValues = serie.GetValues(false, true, out bool isMultiLevel); + var formattedValues = serie.GetValues(false, true, ct.ShowHiddenData, out bool isMultiLevel); var l=new List(); foreach (var v in formattedValues) { diff --git a/src/EPPlus/ExcelRange.cs b/src/EPPlus/ExcelRange.cs index d5676cb4f..cfe69f697 100644 --- a/src/EPPlus/ExcelRange.cs +++ b/src/EPPlus/ExcelRange.cs @@ -10,6 +10,9 @@ Date Author Change ************************************************************************************************* 01/27/2020 EPPlus Software AB Initial release EPPlus 5 *************************************************************************************************/ +using OfficeOpenXml.Core.CellStore; +using OfficeOpenXml.FormulaParsing.Excel.Functions.DateAndTime; +using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; using System; using System.Xml.Serialization; diff --git a/src/EPPlus/ExcelRangeBase.cs b/src/EPPlus/ExcelRangeBase.cs index 868ea0437..5006bb6fa 100644 --- a/src/EPPlus/ExcelRangeBase.cs +++ b/src/EPPlus/ExcelRangeBase.cs @@ -2953,5 +2953,56 @@ internal ExcelTable GetIntersectingTable() } return _worksheet.Tables.GetIntersectingRanges(this).Select(x => x.Value).FirstOrDefault(); } + internal bool IsHidden + { + get + { + if (_fromRow == _toRow) + { + if (IsRowHidden) + { + return true; + } + return IsColumnHidden; + } + else + { + return IsColumnHidden || IsRowHidden; + } + } + } + + private bool IsColumnHidden + { + get + { + ExcelValue ev = default; + for (int col = _fromCol; col <= _toCol; col++) + { + if (Worksheet._values.Exists(0, col, ref ev) == false || ((ExcelColumn)ev._value).Hidden == false) + { + return false; + } + } + + return true; + } + } + private bool IsRowHidden + { + get + { + ExcelValue ev = default; + for (int row = _fromRow; row <= _toRow; row++) + { + if (Worksheet._values.Exists(row, 0, ref ev) == false || ((RowInternal)ev._value).Hidden == false) + { + return false; + } + } + + return true; + } + } } } From ba4e332069d8d3feaea475017b401771c86b1fa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4llman?= Date: Tue, 24 Nov 2026 13:28:52 +0100 Subject: [PATCH 16/23] Changed label margins --- src/EPPlus/Drawing/ExcelDrawing.cs | 2 +- .../Chart/Axis/CategoryAxisScaleCalculator.cs | 6 +- .../Renderer/Chart/ChartAxisRenderer.cs | 91 +++---------------- 3 files changed, 15 insertions(+), 84 deletions(-) diff --git a/src/EPPlus/Drawing/ExcelDrawing.cs b/src/EPPlus/Drawing/ExcelDrawing.cs index ecab9561c..bc8f02b2c 100644 --- a/src/EPPlus/Drawing/ExcelDrawing.cs +++ b/src/EPPlus/Drawing/ExcelDrawing.cs @@ -2610,7 +2610,7 @@ public string ToSvg() { return ToSvg(null); } - /// git statu + /// /// Converts the drawing to a SVG image. /// This is currently only supported for shapes, line-, column-, bar- and pie- charts. /// diff --git a/src/EPPlus/Drawing/Renderer/Chart/Axis/CategoryAxisScaleCalculator.cs b/src/EPPlus/Drawing/Renderer/Chart/Axis/CategoryAxisScaleCalculator.cs index af07ab121..8ae624732 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/Axis/CategoryAxisScaleCalculator.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/Axis/CategoryAxisScaleCalculator.cs @@ -60,7 +60,7 @@ internal static AxisScale CalculateHorizontalAxisByWidth(ref List values //if (FitAsVerticalDiagonalText(displayValues.Count, interval, width, margin, plotAreaWidth)) //Check diagonal //{ var width = fontSize * Math.Sqrt(2); - var margin = fontSize * 0.5; + var margin = fontSize * 0.2; if (FitAsVerticalDiagonalText(displayValues.Count, interval, width, margin, plotAreaWidth)) //Check diagonal { return new AxisScale() @@ -90,7 +90,7 @@ internal static AxisScale CalculateHorizontalAxisByWidth(ref List values else { var width = fontSize * Math.Sqrt(2); - var margin = fontSize * 0.5; + var margin = fontSize * 0.2; if (options.IsMulti || FitAsVerticalDiagonalText(displayValues.Count, interval, width, margin, plotAreaWidth)) //Check diagonal { if (FitAsHorizontalText(displayValues, interval, shaper, fontSize, plotAreaWidth, options.IsMulti)) //Check horizontal @@ -197,7 +197,7 @@ internal static AxisScale CalculateVerticalAxisByHeight(ref List values, private static bool FitAsHorizontalText(List displayValues, int interval, ITextShaper shaper, float fontSize, double plotAreaWidth, bool isMultLevel) { - var margin = fontSize * 0.3; + var margin = fontSize * 0.1; double maxWidth; var pos = interval; if (isMultLevel) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs index 0531455b0..b54651e11 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs @@ -744,13 +744,13 @@ private List GetAxisValueTextBoxes(out double widest, out { switch (lblAlignment) { - case OfficeOpenXml.eAxisLabelAlignment.Left: + case eAxisLabelAlignment.Left: tb.Left -= (tb.Width + majorWidth) / 2; break; - case OfficeOpenXml.eAxisLabelAlignment.Center: + case eAxisLabelAlignment.Center: tb.Left -= tb.Width / 2; break; - case OfficeOpenXml.eAxisLabelAlignment.Right: + case eAxisLabelAlignment.Right: tb.Left += (tb.Width + majorWidth) / 2; break; } @@ -763,28 +763,14 @@ private List GetAxisValueTextBoxes(out double widest, out switch (lblAlignment) { - case OfficeOpenXml.eAxisLabelAlignment.Left: + case eAxisLabelAlignment.Left: break; - case OfficeOpenXml.eAxisLabelAlignment.Center: + case eAxisLabelAlignment.Center: tb.Left += majorWidth / 2 - tb.Width / 2; break; case OfficeOpenXml.eAxisLabelAlignment.Right: - //tb.TextBody.MaxWidth = 50000d; - //foreach (var item in tb.TextBody.Paragraphs) - //{ - // //Re-calculates wrap (so that it's one line) - // item.HorizontalAlignment = item.HorizontalAlignment; - //} - - tb.Left += majorWidth - tb.Width; - //foreach (var item in tb.TextBody.Paragraphs) - //{ - // //Re-calculates wrap (so that it's one line) - // item.HorizontalAlignment = item.HorizontalAlignment; - //} - break; } } @@ -806,10 +792,10 @@ private List GetAxisValueTextBoxes(out double widest, out { majorWidth = Rectangle.Width / count; } - foreach (var tb in ret) - { - //tb.Left += majorWidth / 2; - } + //foreach (var tb in ret) + //{ + // //tb.Left += majorWidth / 2; + //} } } else if(LabelOrientation == eTextOrientation.Custom) @@ -1386,64 +1372,9 @@ private List AddTextboxesMultiVertical(ITextShaper shaper, double ticMarkX, ticMarkY; double x, y; ticMarkY = GetAxisItemTop(i, width, height); - ticMarkX = j * (lineHeight + LeftMargin); - //if (isTopRow) - //{ - x = ticMarkX; - y = ticMarkY; - //} - //else - //{ - //if (tb == null) - //{ - //x = ticMarkX; - //y = ticMarkY; - //} - //else - //{ - // x = tb.Left; - // y = tb.Top; - //} - //double cos = Math.Cos(MathHelper.Radians(rot)); - //double sin = Math.Sin(MathHelper.Radians(rot)); - - //if (LabelOrientation == eTextOrientation.Diagonal || (LabelOrientation == eTextOrientation.Custom && rot != 90)) - //{ - // if (width < MajorTickMarkPositions) - // { - // x = ticMarkX; - // } - // else - // { - // x = ticMarkX - ((height / 2) * cos); - // } - - // var midPointText = height / 2; - // var halfHeightAfterRot = Math.Abs(((width / 2) * sin)); - // var heightPastY = halfHeightAfterRot - midPointText; - - // y = ticMarkY - heightPastY; - //} - //else if ((rot == 90 || rot == -90)) - //{ - // x = ticMarkX; - // y = ticMarkY - (width / 2); - //} - //else - //{ - // x = ticMarkX - (height / 2); - // if (Axis.ActualAxisPosition == eAxisPosition.Bottom) - // { - // y = ticMarkY + BottomMargin + 4; - // } - // else //Top - // { - // y = ticMarkY - TopMargin - 4; - // } - //} - //} - //} + x = ticMarkX; + y = ticMarkY; tb = new DrawingTextContainer(ChartRenderer.RenderContext, Chart, Rectangle, DefaultFonts2007Style.Axis, x, y, width, height, GetMaxWidthForEmptyRows(maxWidth, isTopRow, i, j), maxHeight); tb.SetRotation(EPPlus.DrawingRenderer.RenderItems.Textbox.RotationAnchor.Middle); From 1be8a8d488d9a3fa24d44bb7bb2acccf9a965df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ossian=20Edstr=C3=B6m?= Date: Tue, 24 Nov 2026 13:43:18 +0100 Subject: [PATCH 17/23] Fixed second vertical axis position --- src/EPPlus/Drawing/Renderer/Chart/ChartTitleRenderer.cs | 4 ++-- src/EPPlus/Drawing/Renderer/ChartRenderer.cs | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartTitleRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartTitleRenderer.cs index 3ae7e0e9e..7b4448b50 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartTitleRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartTitleRenderer.cs @@ -214,9 +214,9 @@ internal void InitTextBox(double maxWidth, double maxHeight, MeasurementFont def { TextBox = new DrawingTextBox(ChartRenderer.RenderContext, _svgChart.Drawing, _svgChart.ChartArea.Rectangle, defaultFont, maxWidth, maxHeight); TextBox.TextBody.Style.FillColor = "#" + ChartRenderer.InbuiltStyleFontColor.Value.ToColorString(); - if (_title.Rotation != 0) + if (_title.TextBody.Rotation.HasValue && _title.TextBody.Rotation != 0) { - TextBox.Rotation = _title.Rotation; + TextBox.Rotation = _title.TextBody.Rotation.Value; } if (_title.TextBody.Paragraphs.Count > 0) { diff --git a/src/EPPlus/Drawing/Renderer/ChartRenderer.cs b/src/EPPlus/Drawing/Renderer/ChartRenderer.cs index 53b587874..cc0d2deed 100644 --- a/src/EPPlus/Drawing/Renderer/ChartRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/ChartRenderer.cs @@ -486,7 +486,14 @@ private void PlaceVerticalAxisTitle(ChartAxisRenderer verticalAxis) { var sinRot = Math.Abs(Math.Sin(MathHelper.Radians(verticalAxis.Title.TextBox.Rotation))); var cosRot = Math.Abs(Math.Cos(MathHelper.Radians(verticalAxis.Title.TextBox.Rotation))); - verticalAxis.Title.TextBox.Top = Plotarea.Group.Top + (Plotarea.Rectangle.Height / 2) - ((verticalAxis.Title.TextBox.Height * cosRot + verticalAxis.Title.TextBox.Width * sinRot) / 2); + + if(verticalAxis.Title.TextBox.Rotation < 0) + { + verticalAxis.Title.TextBox.Top = Plotarea.Group.Top + (Plotarea.Rectangle.Height / 2) - ((verticalAxis.Title.TextBox.Height * cosRot + verticalAxis.Title.TextBox.Width * sinRot) / 2); + } + { + verticalAxis.Title.TextBox.Top = Plotarea.Group.Top + (Plotarea.Rectangle.Height / 2) + ((verticalAxis.Title.TextBox.Height * cosRot + verticalAxis.Title.TextBox.Width * sinRot) / 2); + } if (verticalAxis.Axis.AxisPosition == eAxisPosition.Left) { From d0c6c517a57b55ad2f1549df88fd7801a2f52ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ossian=20Edstr=C3=B6m?= Date: Tue, 24 Nov 2026 14:03:59 +0100 Subject: [PATCH 18/23] Fixed vaHeight when labelPosition == eTickLabelPosition.None --- src/EPPlus/Drawing/Renderer/Chart/ChartPlotareaRenderer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartPlotareaRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartPlotareaRenderer.cs index 540af941f..2855d9ca6 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartPlotareaRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartPlotareaRenderer.cs @@ -94,7 +94,7 @@ internal double GetPlotAreaHeight(RectRenderItem rect) { var bottomAxis = GetAxisActualByPosition(eActualAxisPosition.Bottom, 0); double vaHeight = 0; - if (bottomAxis!=null) + if (bottomAxis!=null && bottomAxis.Axis.LabelPosition != eTickLabelPosition.None) { var bottomSecondAxis = GetAxisActualByPosition(eActualAxisPosition.Bottom, 1); if(bottomSecondAxis==null) @@ -105,7 +105,7 @@ internal double GetPlotAreaHeight(RectRenderItem rect) vaHeight = secAxis.Title?.Rectangle?.Height??0D; } } - + vaHeight += (bottomAxis.Rectangle?.Height ?? 0D) + (bottomAxis.Title?.GetHeightOverlay(0) ?? 0D) + (bottomAxis.DisplayUnitTitle?.TextBox.GetActualHeight() ?? 0D) + (bottomSecondAxis?.Rectangle?.Height ?? 0D); } else From 9a40dfa2ca6401bdbd52299daf65d84f49ce9089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4llman?= Date: Tue, 24 Nov 2026 14:40:23 +0100 Subject: [PATCH 19/23] Fixed chart series with count. --- src/EPPlus/Drawing/ExcelDrawing.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/EPPlus/Drawing/ExcelDrawing.cs b/src/EPPlus/Drawing/ExcelDrawing.cs index bc8f02b2c..30eb134e2 100644 --- a/src/EPPlus/Drawing/ExcelDrawing.cs +++ b/src/EPPlus/Drawing/ExcelDrawing.cs @@ -2640,7 +2640,6 @@ public string ToSvg(Action options) } throw new NotSupportedException("Only line-, column-, bar- and pie charts and shapes can be rendered to svg"); } - } } \ No newline at end of file From 279261fabdcf76d561bb3d590dda0d19d36053f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4llman?= Date: Tue, 24 Nov 2026 14:44:24 +0100 Subject: [PATCH 20/23] Count fix --- src/EPPlus/Drawing/Chart/ExcelChartSerie.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EPPlus/Drawing/Chart/ExcelChartSerie.cs b/src/EPPlus/Drawing/Chart/ExcelChartSerie.cs index 602590949..145ab4535 100644 --- a/src/EPPlus/Drawing/Chart/ExcelChartSerie.cs +++ b/src/EPPlus/Drawing/Chart/ExcelChartSerie.cs @@ -344,12 +344,12 @@ internal void AddFromSerie(List list, bool useX, bool useText, string[] { if (useX) { - var max = GetCount(Series, NumberLiteralsY, StringLiteralsY)??0; + var max = GetCount(Series, NumberLiteralsY, StringLiteralsY) ?? 0; AddSerieValues(list, useText, headerInfo, ref isMultiLevel, XSeries, NumberLiteralsX, StringLiteralsX, max, showHidden); } else { - var max = GetCount(XSeries, NumberLiteralsX, StringLiteralsX) ?? 0; + var max = GetCount(XSeries, NumberLiteralsX, StringLiteralsX) ?? GetCount(Series, NumberLiteralsY, StringLiteralsY)??0; AddSerieValues(list, useText, headerInfo, ref isMultiLevel, Series, NumberLiteralsY, StringLiteralsY, max, showHidden); } } From 52ab8f82a65799cf2f6259cf9962d5fe46ddfd3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4llman?= Date: Tue, 24 Nov 2026 15:48:10 +0100 Subject: [PATCH 21/23] Fixed year axis label issue --- src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs index b54651e11..aa8350a21 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs @@ -599,7 +599,7 @@ internal void AddTickmarksAndValues(List DefItems) } else if (ChartRenderer.HorizontalAxis == this || ChartRenderer.HorizontalAxis.Axis.AxisTextboxPosition != Axis.AxisTextboxPosition) { - Rectangle.Top -= Rectangle.Height; + //Rectangle.Top -= Rectangle.Height; } else if (Axis.AxisTextboxPosition == eActualAxisPosition.Top) { From b391f11d2d600335575bdbba18886d2e004dd85f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4llman?= Date: Wed, 25 Nov 2026 07:59:58 +0100 Subject: [PATCH 22/23] Fixed axis label cerntering --- src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs index aa8350a21..74ca61c17 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs @@ -865,7 +865,7 @@ private List AddTextboxes(ITextShaper shaper, float fontSi else { //There is only one axisValue as we are somehow both the first and last value - majorTickMarkWidth = 0; + majorTickMarkWidth = maxWidth; } } @@ -919,7 +919,7 @@ private List AddTextboxes(ITextShaper shaper, float fontSi } else { - x = ticMarkX - width / 2; + x = ticMarkX/* - width / 2*/; y = ticMarkY; } } From 4c510d61e39ca56b98d32521fdd0038fe187e6f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4llman?= Date: Thu, 26 Nov 2026 13:24:45 +0100 Subject: [PATCH 23/23] Fixed several issues with svg chart rendering --- .../Renderer/Chart/ChartAxisRenderer.cs | 57 +++++++++++++++++-- .../Renderer/Chart/ChartLegendRenderer.cs | 4 +- src/EPPlus/Drawing/Renderer/ChartRenderer.cs | 7 +-- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs index 74ca61c17..03f04820b 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartAxisRenderer.cs @@ -686,6 +686,10 @@ private List GetAxisValueTextBoxes(out double widest, out } } + if(Axis.AxisType==eAxisType.Val) + { + maxWidth = Rectangle.Width; //We dont want to wrap value axis labels as they are numeric. + } List ret; if (AxisValues==null) { @@ -886,7 +890,7 @@ private List AddTextboxes(ITextShaper shaper, float fontSi double x, y; double availableTextLength; - if (Axis.IsVertical) + if (Axis.IsVertical || AutoAxisType==eAxisType.Val) { availableTextLength = maxWidth; } @@ -908,7 +912,8 @@ private List AddTextboxes(ITextShaper shaper, float fontSi if (Axis.IsVertical) { x = ticMarkX; - if (ChartRenderer.Chart.IsTypeBar()) + var ct = ChartRenderer.Chart.PlotArea.ChartTypes.First(x => x.XAxis?.Id == Axis.Id || x.YAxis?.Id == Axis.Id); + if ((ct??ChartRenderer.Chart).IsTypeBar()) { y = ticMarkY; } @@ -919,7 +924,7 @@ private List AddTextboxes(ITextShaper shaper, float fontSi } else { - x = ticMarkX/* - width / 2*/; + x = ticMarkX - width / 2; y = ticMarkY; } } @@ -938,9 +943,16 @@ private List AddTextboxes(ITextShaper shaper, float fontSi if (rot == 90 || rot == -90 || LabelOrientation == eTextOrientation.Vertical) { - if(Axis.IsVertical == false) + if (Axis.IsVertical == false) { - x = xPosToMiddle - (width/2); + if (Axis.AxisType == eAxisType.Cat) + { + x = xPosToMiddle - (width / 2); + } + else + { + x = ticMarkX - (width / 2); + } y = ticMarkY + descent; } else @@ -2631,5 +2643,40 @@ internal eTimeUnit GetBaseUnit() } return eTimeUnit.Days; } + + internal double GetAxisPosition(double cv) + { + if(cv <= Min) + { + if (Axis.CrossingAxis.ActualAxisPosition == eAxisPosition.Bottom) + { + return ChartRenderer.Plotarea.Rectangle.Height; + } + else if (Axis.CrossingAxis.ActualAxisPosition == eAxisPosition.Right) + { + return ChartRenderer.Plotarea.Rectangle.Width; + } + else + { + return 0; + } + } + else if (cv >= Max) + { + if (Axis.ActualAxisPosition == eAxisPosition.Top) + { + return ChartRenderer.Plotarea.Rectangle.Height; + } + else if (Axis.ActualAxisPosition == eAxisPosition.Left) + { + return ChartRenderer.Plotarea.Rectangle.Width; + } + else + { + return 0; + } + } + return GetPositionInPlotarea(cv); + } } } \ No newline at end of file diff --git a/src/EPPlus/Drawing/Renderer/Chart/ChartLegendRenderer.cs b/src/EPPlus/Drawing/Renderer/Chart/ChartLegendRenderer.cs index 2d2ec6e5e..930c394f3 100644 --- a/src/EPPlus/Drawing/Renderer/Chart/ChartLegendRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/Chart/ChartLegendRenderer.cs @@ -430,7 +430,7 @@ internal DrawingLegendSerie SetLegendSeries(double entryWidth, double entryHeigh } else { - var totSeriesCount = Chart.PlotArea.ChartTypes.Sum(x => x.Series.Count)-1; + //var totSeriesCount = Chart.PlotArea.ChartTypes.Sum(x => x.Series.Count)-1; int entryIx = 0; int prevCtSeriesCount = 0; index = 0; @@ -443,7 +443,7 @@ internal DrawingLegendSerie SetLegendSeries(double entryWidth, double entryHeigh { ix = ct.Series.Count - 1; end = -1; - index = totSeriesCount-prevCtSeriesCount; + index = ix+prevCtSeriesCount; } else { diff --git a/src/EPPlus/Drawing/Renderer/ChartRenderer.cs b/src/EPPlus/Drawing/Renderer/ChartRenderer.cs index a8447b4a8..b8809d848 100644 --- a/src/EPPlus/Drawing/Renderer/ChartRenderer.cs +++ b/src/EPPlus/Drawing/Renderer/ChartRenderer.cs @@ -216,11 +216,11 @@ private void PlaceHorizontalAxis(ChartAxisRenderer horizontalAxis, bool isSecond var cv = ax.CrossesAt.Value; if (isSecondary) { - axisY = SecondVerticalAxis.GetPositionInPlotarea(Math.Max(Math.Min(cv, SecondHorizontalAxis.Min), SecondVerticalAxis.Max)); + axisY = SecondVerticalAxis.GetAxisPosition(cv); } else { - axisY = VerticalAxis.GetPositionInPlotarea(Math.Min(Math.Max(cv, VerticalAxis.Min), VerticalAxis.Max)); + axisY = VerticalAxis.GetAxisPosition(cv); } horizontalAxis.Line.Y1 = horizontalAxis.Line.Y2 = axisY; } @@ -231,7 +231,7 @@ private void PlaceHorizontalAxis(ChartAxisRenderer horizontalAxis, bool isSecond { if(VerticalAxis.Min < 0) { - pos = VerticalAxis.GetPositionInPlotarea(0); + pos = VerticalAxis.GetAxisPosition(0); } else { @@ -348,7 +348,6 @@ private void PlaceVerticalAxis(ChartAxisRenderer verticalAxis, bool isSecondary) verticalAxis.Line?.Y2 = (float)verticalAxis.Rectangle.Height; var axisPos = verticalAxis.Axis.ActualAxisPosition; var ax = verticalAxis.Axis; - if (ax.CrossesAt.HasValue) { double axisX;