Skip to content
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

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

Closed
vsemichev opened this issue Jun 11, 2024 · 1 comment · Fixed by #1924
Closed
Labels
bug Something isn't working
Projects

Comments

@vsemichev
Copy link
Contributor

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:

  • Change chart type to combo - Clustered Column line on secondary axis
  • Change color for bar chart series to "1F77B4" and "FF7F0E"
  • Change line chart series color to "2CA02C"
  • Change number format for all series to '#""'
  • Change secondary Vertical Axis position to "none"
  • Add data labels to all series and unset "Show leader lines".
  • Change data label font color to white for bar chart
  • Change bar chart data label position to "Inside End"
  • Change line chart data label position to "Above"
  • Change Y axis Major grid line color to "868686"
  • Add a Linear trend line to "b" series.

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 xuri added the bug Something isn't working label Jun 16, 2024
@xuri xuri added this to Bugfix in v2.9.0 Jun 16, 2024
@xuri xuri added the in progress Working in progress label Jun 17, 2024
user1121114685 added a commit to user1121114685/excelize that referenced this issue Jun 17, 2024
@xuri xuri closed this as completed in 1a99dd4 Jun 17, 2024
@xuri xuri removed the in progress Working in progress label Jun 17, 2024
@xuri
Copy link
Member

xuri commented Jun 17, 2024

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: ";;;"}},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Bugfix
v2.9.0
Bugfix
Development

Successfully merging a pull request may close this issue.

2 participants