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

Combo Column + Line chart with secondary invisible axis produces unexpected results. #1921

Closed vsemichev closed 2 weeks ago

vsemichev commented 3 weeks ago

Description The following book contains two charts - Top one produced by the program below, Bottom one - the expected output.

Book1.xlsx

Things I had to adjust on the default combo chat generated by Excel to match my requirements:

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},
        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, None: true},
            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: Produced

Describe the results you expected: Expected

Output of go version:

go version go1.22.3 darwin/arm64

Excelize version or commit ID:

v2.8.1

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

MacOS Sonoma 14.5, Microsoft® Excel for Mac Version 16.85 (24051214)
xuri commented 2 weeks ago

Thanks for your issue. This issue has been fixed. Please upgrade to the master branch code, and this patch will be released in the next version. Currently, this library doesn't support add trend line and set grid line color for the chart, but you can set data label color and disable secondary vertical axis by following modifications based on your code:

-PlotArea: excelize.ChartPlotArea{ShowVal: true, NumFmt: excelize.ChartNumFmt{CustomNumFmt: "#\"\""}},
+PlotArea: excelize.ChartPlotArea{ShowVal: true, NumFmt: excelize.ChartNumFmt{CustomNumFmt: "[White]#\"\""}},

-YAxis: excelize.ChartAxis{Secondary: true, None: true},
+YAxis: excelize.ChartAxis{Secondary: true, NumFmt: excelize.ChartNumFmt{CustomNumFmt: ";;;"}},