Skip to content

Add font support for chart legends #2171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Add font support for chart legends #2171

wants to merge 3 commits into from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jul 8, 2025

This PR implements font support for chart legends, allowing users to specify font properties such as family, size, color, bold, italic, underline, and strike for legend text.

Changes Made

Core Implementation

  • xmlChart.go: Added Font field to ChartLegend struct
  • drawing.go:
    • Initialize TxPr (text properties) when creating chart legends
    • Apply font settings using the existing drawChartFont function
  • chart.go: Updated documentation to include the new Font option

Testing

  • chart_test.go: Added comprehensive TestChartLegendFont test covering:
    • Normal legend with font settings
    • Legend with position: "none" (edge case)
    • Different font properties validation

Usage Example

err := f.AddChart("Sheet1", "E1", &excelize.Chart{
    Type: excelize.Col,
    Series: series,
    Title: []excelize.RichTextRun{{Text: "Chart Title"}},
    Legend: excelize.ChartLegend{
        Position: "bottom", 
        Font: excelize.Font{
            Family: "Aptos", 
            Color:  "#3E3E3E", 
            Size:   10,
            Bold:   true,
        },
    },
})

Implementation Details

The implementation follows the same pattern used for other chart text elements (data labels, axes) to ensure consistency:

  1. Text Properties Initialization: Legends now have proper TxPr structure with body properties and paragraph settings
  2. Font Application: Uses the existing drawChartFont function to apply font settings to the legend's text properties
  3. Edge Case Handling: Properly handles cases where legend position is "none" without errors

Testing Results

  • All existing tests pass
  • New test validates font functionality
  • Manual testing confirms font settings are applied correctly
  • Edge cases (no legend) handled properly

The changes are minimal and surgical, adding exactly the functionality requested without affecting existing behavior.

Fixes #2169.


💡 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.

Copilot AI and others added 2 commits July 8, 2025 14:26
Co-authored-by: xuri <2809468+xuri@users.noreply.github.com>
Co-authored-by: xuri <2809468+xuri@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Legends in charts do not support font settings Add font support for chart legends Jul 8, 2025
@Copilot Copilot AI requested a review from xuri July 8, 2025 14:30
Copilot finished work on behalf of xuri July 8, 2025 14:30
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.

Legends in charts do not support font settings
2 participants