qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
17.62k stars 1.68k forks source link

Secondary axis title is not displayed. #1926

Closed vsemichev closed 2 weeks ago

vsemichev commented 2 weeks ago

Description

I am trying to generate a combo chart with a secondary Y axis, however, the secondary Y axis is missing the title.

Steps to reproduce the issue:

Run the following code

package main

import (
    "fmt"
    "github.com/xuri/excelize/v2"
)

func main() {
    f := excelize.NewFile()
    defer func() {
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()
    data := [][]interface{}{
        {"MonYear", "Aug-23", "Sep-23", "Oct-23", "Nov-23", "Dec-23", "Jan-24", "Feb-24", "Mar-24", "Apr-24", "May-24", "Jun-24"},
        {"a", 14, 14, 14, 12, 14, 14, 13, 13, 12, 13, 4},
        {"b", 3, 2, 4, 3, 2, 3, 2, 3, 2, 1, 0},
        {"c", 44198, 44366, 44683, 44857, 44762, 44949, 45005, 45157, 45483, 45574, 45435},
    }
    for idx, col := range data {
        cell, err := excelize.CoordinatesToCellName(idx+1, 1)
        if err != nil {
            fmt.Println(err)
            return
        }
        f.SetSheetCol("Sheet1", cell, &col)
    }
    disable := false
    if err := f.AddChart("Sheet1", "F1", &excelize.Chart{
        Type:       excelize.Col,
        Dimension:  excelize.ChartDimension{Width: 1080, Height: 576},
        XAxis:      excelize.ChartAxis{Font: excelize.Font{Color: "000000"}},
        YAxis:      excelize.ChartAxis{Font: excelize.Font{Color: "000000"}, MajorGridLines: true, Title: []excelize.RichTextRun{{Text: "Primary Axis"}}},
        VaryColors: &disable,
        PlotArea:   excelize.ChartPlotArea{ShowVal: true, NumFmt: excelize.ChartNumFmt{CustomNumFmt: "#\"\""}},
        Series: []excelize.ChartSeries{
            {
                Name:              "Sheet1!$B$1",
                Categories:        "Sheet1!$A$2:$A$12",
                Values:            "Sheet1!$B$2:$B$12",
                Fill:              excelize.Fill{Type: "pattern", Color: []string{"1F77B4"}, Pattern: 1},
                DataLabelPosition: excelize.ChartDataLabelsPositionInsideEnd,
            },
            {
                Name:              "Sheet1!$C$1",
                Categories:        "Sheet1!$A$2:$A$12",
                Values:            "Sheet1!$C$2:$C$12",
                Fill:              excelize.Fill{Type: "pattern", Color: []string{"FF7F0E"}, Pattern: 1},
                DataLabelPosition: excelize.ChartDataLabelsPositionInsideEnd,
            },
        },
        Title: []excelize.RichTextRun{
            {
                Text: "Combo column and line chart",
            },
        },
    },
        &excelize.Chart{
            Type:       excelize.Line,
            Dimension:  excelize.ChartDimension{Width: 1080, Height: 576},
            YAxis:      excelize.ChartAxis{Secondary: true, Font: excelize.Font{Color: "000000"}, Title: []excelize.RichTextRun{{Font: &excelize.Font{Color: "000000"}, Text: "Secondary Axis"}}},
            VaryColors: &disable,
            PlotArea:   excelize.ChartPlotArea{ShowVal: true, NumFmt: excelize.ChartNumFmt{CustomNumFmt: "#\"\""}},
            Series: []excelize.ChartSeries{
                {
                    Name:              "Sheet1!$D$1",
                    Categories:        "Sheet1!$A$2:$A$12",
                    Values:            "Sheet1!$D$2:$D$12",
                    Fill:              excelize.Fill{Type: "pattern", Color: []string{"2CA02C"}, Pattern: 1},
                    DataLabelPosition: excelize.ChartDataLabelsPositionAbove,
                    Marker:            excelize.ChartMarker{Symbol: "none"},
                },
            },
        }); err != nil {
        fmt.Println(err)
        return
    }
    // Save spreadsheet by the given path.
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}

Describe the results you received:

Combo bar + line chart. Primary Y-axis has the title, but the secondary Y-axis does not.

Describe the results you expected:

I expect the secondary Y-axis to have a title that I provided.

Output of go version:

go version go1.22.4 darwin/arm64

Excelize version or commit ID:

v2.8.2-0.20240617135415-1a99dd4a233c

Environment details (OS, Microsoft Excel™ version, physical, etc.):

MacOS Sonoma 14.5, Microsoft Excel for Mac Version 16.86 (24060916)

xuri commented 2 weeks ago

Thanks for your issue. It seems we need remove this line of code in the drawing.go, would you like to create a pull request for help us fix that?

vsemichev commented 2 weeks ago

Hi @xuri, per your instruction I have just submitted a new pull request. Thank you for a quick response!