From debda511985841d33a88b46d61c3d353e5550bd8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 7 Oct 2025 16:08:10 +0000 Subject: [PATCH] Fix X-Axis MajorUnit and MinorUnit for category axes in line charts Co-authored-by: dfinke <67258+dfinke@users.noreply.github.com> --- Public/Add-ExcelChart.ps1 | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/Public/Add-ExcelChart.ps1 b/Public/Add-ExcelChart.ps1 index 88a0d46..edc4178 100644 --- a/Public/Add-ExcelChart.ps1 +++ b/Public/Add-ExcelChart.ps1 @@ -113,6 +113,57 @@ function Add-ExcelChart { if ($null -ne $XMinValue) { $chart.XAxis.MinValue = $XMinValue } if ($null -ne $XMaxValue) { $chart.XAxis.MaxValue = $XMaxValue } if ($XAxisNumberformat) { $chart.XAxis.Format = (Expand-NumberFormat $XAxisNumberformat) } + + # Fix for category axis (used in line charts, etc.): EPPlus doesn't serialize MajorUnit/MinorUnit to XML for category axes + # We need to manually add these elements to the XML if they were specified + if ($XMajorUnit -or $XMinorUnit) { + $chartXml = $chart.ChartXml + $nsManager = New-Object System.Xml.XmlNamespaceManager($chartXml.NameTable) + $null = $nsManager.AddNamespace("c", "http://schemas.openxmlformats.org/drawingml/2006/chart") + $catAx = $chartXml.SelectSingleNode("//c:catAx", $nsManager) + + if ($catAx) { + # Category axis exists - need to add majorUnit/minorUnit elements + if ($XMajorUnit) { + $existingMajorUnit = $catAx.SelectSingleNode("c:majorUnit", $nsManager) + if (-not $existingMajorUnit) { + $majorUnitElement = $chartXml.CreateElement("c", "majorUnit", "http://schemas.openxmlformats.org/drawingml/2006/chart") + $null = $majorUnitElement.SetAttribute("val", $XMajorUnit) + # Insert after scaling element or at the beginning + $scalingNode = $catAx.SelectSingleNode("c:scaling", $nsManager) + if ($scalingNode) { + $null = $catAx.InsertAfter($majorUnitElement, $scalingNode) + } else { + $null = $catAx.PrependChild($majorUnitElement) + } + } else { + $null = $existingMajorUnit.SetAttribute("val", $XMajorUnit) + } + } + + if ($XMinorUnit) { + $existingMinorUnit = $catAx.SelectSingleNode("c:minorUnit", $nsManager) + if (-not $existingMinorUnit) { + $minorUnitElement = $chartXml.CreateElement("c", "minorUnit", "http://schemas.openxmlformats.org/drawingml/2006/chart") + $null = $minorUnitElement.SetAttribute("val", $XMinorUnit) + # Insert after majorUnit if it exists, otherwise after scaling + $majorUnitNode = $catAx.SelectSingleNode("c:majorUnit", $nsManager) + if ($majorUnitNode) { + $null = $catAx.InsertAfter($minorUnitElement, $majorUnitNode) + } else { + $scalingNode = $catAx.SelectSingleNode("c:scaling", $nsManager) + if ($scalingNode) { + $null = $catAx.InsertAfter($minorUnitElement, $scalingNode) + } else { + $null = $catAx.PrependChild($minorUnitElement) + } + } + } else { + $null = $existingMinorUnit.SetAttribute("val", $XMinorUnit) + } + } + } + } if ($YAxisTitleText) { $chart.YAxis.Title.Text = $YAxisTitleText