Skip to content

Conversation

Copy link

Copilot AI commented Oct 7, 2025

Problem

When configuring X-Axis parameters on LINE charts using Add-ExcelChart, the XMajorUnit and XMinorUnit parameters were being ignored, even though XMinValue and XMaxValue worked correctly. This issue affected line charts and other chart types that use category axes.

# This would set min/max but ignore MajorUnit and MinorUnit
Add-ExcelChart -WorkSheet $Sheet -ChartType "LINE" -Title "Chart" `
    -YRange "A2:G2" -XRange "A1:G1" `
    -XMinValue 0 -XMaxValue 45 `
    -XMajorUnit 10 -XMinorUnit 5  # These were being ignored

Root Cause

LINE charts use a category axis (catAx) for the X-axis in the underlying Excel Open XML format, while the Y-axis uses a value axis (valAx). When setting XMajorUnit and XMinorUnit through the EPPlus API, these values are stored in memory but EPPlus doesn't serialize them to the XML for category axes—only value axes have these properties properly written to the file.

Solution

This PR adds code to manually insert the <c:majorUnit> and <c:minorUnit> XML elements into the category axis node when these parameters are specified. The fix:

  • Only activates when XMajorUnit or XMinorUnit parameters are provided
  • Checks if the chart uses a category axis (as opposed to a value axis)
  • Manually creates and inserts the required XML elements with proper namespace handling
  • Properly suppresses pipeline output to maintain compatibility with -PassThru
  • Handles both creating new elements and updating existing ones

Changes

  • Public/Add-ExcelChart.ps1: Added XML manipulation code (51 lines) to set majorUnit/minorUnit on category axes
  • __tests__/Add-ExcelChart-XAxis.Tests.ps1: New comprehensive test suite (8 tests) covering various scenarios

Testing

All tests passing:

  • ✅ 8 new tests for X-axis configuration (both with and without parameters)
  • ✅ 14 existing chart-related tests continue to pass
  • ✅ Verified with the exact example from the issue report
  • ✅ Tested across multiple chart types (Line, LineMarkers, ColumnClustered, ColumnStacked, BarClustered, Area, XYScatter)

Affected Chart Types

This fix applies to chart types that use category axes:

  • Line, LineMarkers
  • ColumnClustered, ColumnStacked
  • BarClustered, BarStacked
  • Area
  • And similar chart types

Chart types using value axes for X-axis (XYScatter, etc.) were not affected by the bug and continue to work as before.

Example

# Now works correctly!
Add-ExcelChart -WorkSheet $Sheet -ChartType "LINE" `
    -Title "Properly Configured Chart" `
    -YRange "A2:G2" -XRange "A1:G1" `
    -XMinValue 0 -XMaxValue 40 `
    -XMajorUnit 10 -XMinorUnit 5 `  # ✅ Now properly applied
    -YMinValue 0 -YMaxValue 40 `
    -YMajorUnit 10 -YMinorUnit 5

Fixes #[issue_number]

Original prompt

This section details on the original issue you should resolve

<issue_title>Configuring X-Axis on a line chart</issue_title>
<issue_description>The Add-ExcelChart cmdlet has various parameters to configure the X and Y axis. While configuring the Y-axis seems to work as expected, the X-axis behaves quite strangely as the example below shows.
`
$XL = Open-ExcelPackage -Path "Example2.xlsx" -Create
$Sheet = Add-WorkSheet -ExcelPackage $XL -WorksheetName "Example2"

for ($I = 1;$I -lt 8;$I++)
{
$Sheet.Cells[1,$I].Value = @(5, 10, 15, 20, 25, 30, 35)[$I - 1]
}
for ($I = 1;$I -lt 8;$I++)
{
$Sheet.Cells[2,$I].Value = @(18, 11, 23, 17, 12, 32, 12)[$I - 1]
}

$Chart = Add-ExcelChart -WorkSheet $Sheet -Row 8 -Column 1 -ChartType "LINE" -Title "Very Fancy Title" -passthru -YRange "A2:G2" -YMinValue 5 -YMaxValue 45 -YMajorUnit 10 -YMinorUnit 5
-XRange "A1:G1" #-XMinValue 0 -XMaxValue 45 -XMajorUnit 10 -XMinorUnit 5
#-YRange "A2:G2" -YMinValue 10 -YMaxValue 50 `
#-XRange "A1:G1" -XMinValue 0 -XMaxValue 50

Close-ExcelPackage -ExcelPackage $XL -Show
`

I've included a couple of lines of code that are commented out. These show some other parameters that have an effect but not a desirable one.

Having looked at the code for Add-ExcelChart I am fairly sure that the problem is in the EPPlus library but I thought I should mention it in case it is something that can be fixed elsewhere.

Best wishes.....</issue_description>

<agent_instructions>review, suggest fixes</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #1722

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copilot AI changed the title [WIP] Fix x-axis configuration for line chart Fix X-Axis MajorUnit and MinorUnit configuration for line charts Oct 7, 2025
Copilot AI requested a review from dfinke October 7, 2025 16:14
Copilot finished work on behalf of dfinke October 7, 2025 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Configuring X-Axis on a line chart

2 participants