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

Same base code produces different fill colors for different version 2.8.1& 2.8.0 #1949

Closed
sstjerne opened this issue Jul 11, 2024 · 4 comments

Comments

@sstjerne
Copy link

sstjerne commented Jul 11, 2024

Description

Hey maintainer

After upgrade to latest excelize version, 2.8.1, we're noticed that the fill color for the charTypes Col, Doughnut and StackedBar are producing different results. Please take a look the below screenshot.

Steps to reproduce the issue:

Here the base code using to insert the charts, those are two methods, the first is for chart Doughnut using reference, and the second collecting the values from cells for charts Col & StackedBar.

Colors = [][]string{
	{"#005996"},
	{"#0077C8"},
	{"#66ADDE"},
	{"#66D1EE"},
	{"#66DBD1"},
	{"#E888D1"},
	{"#BEA1E0"},
	{"#9363CC"},
	{"#493166"},
	{"#CCE4F4"},
}

//chartType Doughnut, uses chart series from reference
func InsertChartByReference(f *excelize.File, sheetName string, dataStartCol, dataStartRow, dataEndRow int, chartType excelize.ChartType) []excelize.ChartSeries {

	categoriCol, _ := excelize.ColumnNumberToName(1)
	valueCol, _ := excelize.ColumnNumberToName(dataStartCol)

	chartSeries := []excelize.ChartSeries{
		{
			Name:       "Amount",
			Categories: fmt.Sprintf("%s!$%s$%d:$%s$%d", sheetName, categoriCol, dataStartRow, categoriCol, dataEndRow),
			Values:     fmt.Sprintf("%s!$%s$%d:$%s$%d", sheetName, valueCol, dataStartRow, valueCol, dataEndRow),
			Fill: excelize.Fill{
				Color: Colors[0],
			},
		},
	}

	err1 := f.AddChart(sheetName, cell, &excelize.Chart{
		Type: chartType,
		Format: excelize.GraphicOptions{
			OffsetX:     15,
			OffsetY:     10,
			Positioning: "absolute",
		},
		PlotArea: chartPlotArea,
		Title: []excelize.RichTextRun{
			{
				Text: chartTitle,
			},
		},
		XAxis: xAxis,
		YAxis: yAxis,
		Dimension: excelize.ChartDimension{
			Height: uint(height),
			Width:  uint(width),
		},
		Legend: chartLegend,
		Series: chartSeries,
	})

}

// Chart for Col & StackedBar
func InsertChartByCollectValues(f *excelize.File, sheetName string, dataStartCol, dataStartRow, dataEndRow int, chartType excelize.ChartType) []excelize.ChartSeries {

	dataMap := make(map[string]float64)
	categories := make([]string, (dataEndRow - dataStartRow + 1))
	values := make([]string, (dataEndRow - dataStartRow + 1))
	i := dataStartRow
	j := 0
	for i <= dataEndRow {
		categoryCell, _ := excelize.CoordinatesToCellName(1, i)
		dataCell, _ := excelize.CoordinatesToCellName(dataCol, i)
		category, _ := f.GetCellValue(sheet, categoryCell)
		value, _ := f.GetCellValue(sheet, dataCell)
		i++
		categories[j] = category
		values[j] = value
		valueFlt, _ := strconv.ParseFloat(value, 64)
		dataMap[category] = valueFlt
		j++
	}

	categoriesStr := strings.Join(categories, "\",\"")
	categoriesStr = "{\"" + categoriesStr + "\"}"

	valuesStr := strings.Join(values, ",")
	valuesStr = "{" + valuesStr + "}"


	chartSeries := []excelize.ChartSeries{
		{
			Name:       "Amount",
			Categories: categoriesStr,
			Values:     valuesStr,
			Fill: excelize.Fill{
				Color: Colors[0],
			},
		},
	}

	err1 := f.AddChart(sheetName, cell, &excelize.Chart{
		Type: chartType,
		Format: excelize.GraphicOptions{
			OffsetX:     15,
			OffsetY:     10,
			Positioning: "absolute",
		},
		PlotArea: chartPlotArea,
		Title: []excelize.RichTextRun{
			{
				Text: chartTitle,
			},
		},
		XAxis: xAxis,
		YAxis: yAxis,
		Dimension: excelize.ChartDimension{
			Height: uint(height),
			Width:  uint(width),
		},
		Legend: chartLegend,
		Series: chartSeries,
	})
}

Describe the results you received:

We received the chart with multiple colors when the chartType is Col or StackedBar, and in case the chartType is a Doughnut the color is just one. This is using excelize 2.8.1

Chart Col:
image (2)

Chart StackedBar:
image (3)

Chart Doughnut:
image (7)

Describe the results you expected:
We expect one color when the chartType is a Col or StackedBar, and multicolors when the chartType is a Doughnut. This is using excelize 2.8.0.

Chart Col:
image (5)

Chart StackedBar:
image (4)

Chart Doughnut:
image (6)

Output of go version:

 go1.18.10 linux/amd64

Excelize version or commit ID:

2.8.1

Environment details (OS, Microsoft Excel™ version, physical, etc.):
macOS Version 14.3 (23D56) & Microsoft Version 16.86

@xuri
Copy link
Member

xuri commented Jul 12, 2024

Thanks for your issue. Could you show us a complete, standalone example program (with main function) or reproducible demo?

@xuri xuri added the needs more info This issue can't reproduce, need more info label Jul 12, 2024
@sstjerne
Copy link
Author

Hi Xuri, sure, below the standalone example, and also the two spreadsheet generates. Thanks

Steps to reproduce the issue:

To create the spreadsheet with v2.8.1

  1. go clean -cache -r -i -x
  2. go clean -modcache
  3. Modify go.mod in order to set github.com/xuri/excelize/v2 v2.8.1
  4. go mod tidy
  5. go run main.go

To create spreadsheet with v2.8.0

  1. go clean -cache -r -i -x
  2. go clean -modcache
  3. Modify go.mod in order to set github.com/xuri/excelize/v2 v2.8.0
  4. go mod tidy
  5. go run main.go
package main

import (
	"fmt"

	"github.com/xuri/excelize/v2"
)

var (
	ColorGrid = [][]string{
		{"#005996"},
		{"#0077C8"},
		{"#66ADDE"},
	}
)

func main() {

	f := excelize.NewFile()
	defer func() {
		if err := f.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	AddChart(f, "Doughnut", excelize.Doughnut)
	AddChart(f, "StackedBar", excelize.BarStacked)
	AddChart(f, "Col", excelize.Col)

	// Save workbook
	if err := f.SaveAs("Demo_excelize_v2.8.1.xlsx"); err != nil {
		fmt.Println(err)
	}
}

func AddChart(f *excelize.File, sheetName string, chartType excelize.ChartType) {

	f.NewSheet(sheetName)

	for idx, row := range [][]interface{}{
		{nil, "Apple"},
		{"Small", 2},
		{"Normal", 5},
		{"Large", 6},
	} {
		cell, err := excelize.CoordinatesToCellName(1, idx+1)
		if err != nil {
			fmt.Println(err)
			return
		}
		if err := f.SetSheetRow(sheetName, cell, &row); err != nil {
			fmt.Println(err)
			return
		}
	}
	if err := f.AddChart(sheetName, "E1", &excelize.Chart{
		Type: chartType,
		Series: []excelize.ChartSeries{
			{
				Name:       fmt.Sprintf("%s!$A$2", sheetName),
				Categories: fmt.Sprintf("%s!$A$2:$A$4", sheetName),
				Values:     fmt.Sprintf("%s!$B$2:$B$4", sheetName),
				Fill: excelize.Fill{
					Color: ColorGrid[0],
				},
			},
		},
		Format: excelize.GraphicOptions{
			OffsetX: 15,
			OffsetY: 10,
		},
		Legend: excelize.ChartLegend{
			Position: "right",
		},
		Title: []excelize.RichTextRun{
			{
				Text: "Fruit Doughnut Chart",
			},
		},
		PlotArea: excelize.ChartPlotArea{
			ShowCatName:     false,
			ShowLeaderLines: false,
			ShowPercent:     true,
			ShowSerName:     false,
			ShowVal:         false,
		},
		ShowBlanksAs: "zero",
	}); err != nil {
		fmt.Println(err)
		return
	}

}

Demo_excelize_v2.8.1.xlsx
Demo_excelize_v2.8.0.xlsx

@xuri
Copy link
Member

xuri commented Jul 13, 2024

Thanks for your feedback. This change introduce by pull request #1788 since version 5399572. If you're using v2.8.1 and later, please speficy Type and Pattern field in the Fill options, for example:

package main

import (
	"fmt"

	"github.com/xuri/excelize/v2"
)

var (
	ColorGrid = [][]string{
		{"#005996"},
		{"#0077C8"},
		{"#66ADDE"},
	}
)

func main() {

	f := excelize.NewFile()
	defer func() {
		if err := f.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	AddChart(f, "Doughnut", excelize.Doughnut)
	AddChart(f, "StackedBar", excelize.BarStacked)
	AddChart(f, "Col", excelize.Col)

	// Save workbook
	if err := f.SaveAs("Demo_excelize_master.xlsx"); err != nil {
		fmt.Println(err)
	}
}

func AddChart(f *excelize.File, sheetName string, chartType excelize.ChartType) {

	f.NewSheet(sheetName)

	for idx, row := range [][]interface{}{
		{nil, "Apple"},
		{"Small", 2},
		{"Normal", 5},
		{"Large", 6},
	} {
		cell, err := excelize.CoordinatesToCellName(1, idx+1)
		if err != nil {
			fmt.Println(err)
			return
		}
		if err := f.SetSheetRow(sheetName, cell, &row); err != nil {
			fmt.Println(err)
			return
		}
	}
	if err := f.AddChart(sheetName, "E1", &excelize.Chart{
		Type: chartType,
		Series: []excelize.ChartSeries{
			{
				Name:       fmt.Sprintf("%s!$A$2", sheetName),
				Categories: fmt.Sprintf("%s!$A$2:$A$4", sheetName),
				Values:     fmt.Sprintf("%s!$B$2:$B$4", sheetName),
				Fill: excelize.Fill{
					Color:   ColorGrid[0],
+					Pattern: 1,
+					Type:    "pattern",
				},
			},
		},
		Format: excelize.GraphicOptions{
			OffsetX: 15,
			OffsetY: 10,
		},
		Legend: excelize.ChartLegend{
			Position: "right",
		},
		Title: []excelize.RichTextRun{
			{
				Text: "Fruit Doughnut Chart",
			},
		},
		PlotArea: excelize.ChartPlotArea{
			ShowCatName:     false,
			ShowLeaderLines: false,
			ShowPercent:     true,
			ShowSerName:     false,
			ShowVal:         false,
		},
		ShowBlanksAs: "zero",
	}); err != nil {
		fmt.Println(err)
		return
	}

}

@xuri xuri removed the needs more info This issue can't reproduce, need more info label Jul 13, 2024
@xuri
Copy link
Member

xuri commented Jul 16, 2024

I've closed this issue. If you have any questions, please let me know, and you can reopen this anytime.

@xuri xuri closed this as completed Jul 16, 2024
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

No branches or pull requests

2 participants