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

XAxis show below? #1978

Closed
jingc1413 opened this issue Aug 23, 2024 · 4 comments
Closed

XAxis show below? #1978

jingc1413 opened this issue Aug 23, 2024 · 4 comments
Labels
duplicate This issue or pull request already exists

Comments

@jingc1413
Copy link

Description
我想让 x轴显示在下方
代码生成的图表有些在上面有些在下方

image
image
excel 上可以设置位置
我没找到如何使用,请给一些帮助,多谢

Steps to reproduce the issue:
1.
2.
3.

Describe the results you received:

Describe the results you expected:

Output of go version:
1.23.0

(paste your output here)

Excelize version or commit ID:

(paste here)

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

@xuri xuri added the needs more info This issue can't reproduce, need more info label Aug 24, 2024
@xuri
Copy link
Member

xuri commented Aug 24, 2024

Thanks for your issue. Which version of this library are you using? Could you show us a complete, standalone example program or reproducible demo? If you open an existing workbook, please provide the file attachment without confidential info.

@jingc1413
Copy link
Author

jingc1413 commented Aug 26, 2024

@xuri thank you
this is code:
model.json

package main

import (
	"encoding/json"
	"fmt"
	"github.com/xuri/excelize/v2"
	"os"
	"strconv"
	"strings"
)

const (
	upLink   = "Uplink"
	downLink = "Downlink"
)

type MetricsData struct {
	FileName        string
	SerialNumber    string
	MeasurementItem string

	Data map[string]map[string]string
}

func main() {
	M3_E62_generate()
}

func M3_E62_generate() {
	var modelDeviceList = make(map[string]map[string]*MetricsData)
	jsonData, _ := os.ReadFile("model.json")
	e := json.Unmarshal(jsonData, &modelDeviceList)
	if e != nil {
		return
	}

	for devType, v := range modelDeviceList {
		f := excelize.NewFile()
		sheetName := "test"
		f.NewSheet(sheetName)
		{
			f.DeleteSheet("Sheet1")
		}

		f.SetCellValue(sheetName, "A2", "Measurement item")
		f.SetCellValue(sheetName, "B2", "Measurement point")
		f.MergeCell(sheetName, "A3", "A10")
		f.MergeCell(sheetName, "A11", "A18")
		f.SetColWidth(sheetName, "A", "B", 18)

		alignmentStyle := &excelize.Style{
			Alignment: &excelize.Alignment{
				Horizontal: "center",
				Vertical:   "center",
			},
		}
		alignmentId, _ := f.NewStyle(alignmentStyle)
		f.SetCellStyle(sheetName, "A3", "A10", alignmentId)
		f.SetCellStyle(sheetName, "A11", "A18", alignmentId)

		for i := 0; i < 16; i++ {
			if i < 8 {
				cellkey := fmt.Sprintf("%c%d", 'B', i+2+1)
				cellval := fmt.Sprintf("RU ANT%d", i+1)
				f.SetCellValue(sheetName, cellkey, cellval)
			} else {
				cellkey := fmt.Sprintf("%c%d", 'B', i+2+1)
				cellval := fmt.Sprintf("AU CH%d", i+1-8)
				f.SetCellValue(sheetName, cellkey, cellval)
			}
		}

		idx := 1
		for serialNumber, vv := range v {
			pos := fmt.Sprintf("%c%d", 'B'+idx, 2)
			// set sn
			f.SetCellValue(sheetName, pos, serialNumber)

			urowIndex := 0
			drowIndex := 0
			for measture, vitem := range vv.Data {
				seps := strings.Split(measture, ":")
				if len(seps) < 2 {
					fmt.Printf("measture key error, %s, %s, %s\n", devType, serialNumber, measture)
					continue
				}

				urowflag, drowflag := false, false
				for _, pointv := range vitem {
					if seps[0] == upLink {
						f.SetCellValue(sheetName, "A3", seps[1])
						pointCellKey := fmt.Sprintf("%c%d", 'B'+idx, urowIndex+3)
						pointValue, errConv := strconv.ParseFloat(pointv, 64)
						if errConv != nil {
							continue
						}
						f.SetCellValue(sheetName, pointCellKey, pointValue)
						urowflag = true
					} else if seps[0] == downLink {
						f.SetCellValue(sheetName, "A11", seps[1])
						pointCellKey := fmt.Sprintf("%c%d", 'B'+idx, drowIndex+3+8)
						pointValue, errConv := strconv.ParseFloat(pointv, 64)
						if errConv != nil {
							fmt.Printf("convert number error, %v, %v, %v, %v", sheetName, serialNumber, measture, pointv)
							continue
						}
						f.SetCellValue(sheetName, pointCellKey, pointValue)
						drowflag = true
					}
					if urowflag {
						urowIndex++
					}
					if drowflag {
						drowIndex++
					}
				}

			}
			idx = idx + 1
		}
		{
			columnLength := 0
			for range v {
				columnLength++
			}
			// chart
			colChart := 0
			rowindex := 0
			rowCLCR := 0
			for i := 1; i <= 16; i++ {
				cellKey := fmt.Sprintf("C%d", i+2)
				cellVal, err := f.GetCellValue(sheetName, cellKey)
				if err != nil {
					continue
				}
				if len(cellVal) < 1 {
					continue
				}
				rowindex++

				posKey := ""
				chartVal := ""

				if rowindex%3 != 0 {
					fmt.Printf("111 %% != zero, i=%d, rowindex=%d, col=%d\n", i, rowindex, (rowindex-1)*8+1)
					posKey, e = excelize.CoordinatesToCellName((rowindex-1)*8+1, 20+rowCLCR*8) // A1
					if e != nil {
						fmt.Printf("111 e error, %v\n", e)
					}
				} else {
					colChart++
					rowCLCR++
					rowindex = 1
					fmt.Printf("222 %% == zero, i=%d, rowindex=%d, col=%d\n", i, rowindex, (rowindex-1)*8+1)
					posKey, e = excelize.CoordinatesToCellName((rowindex-1)*8+1, 20+rowCLCR*8) // A1
					if e != nil {
						fmt.Printf("222 e error, %v\n", e)
					}
					rowindex = 0
				}
				chartVal = fmt.Sprintf("%s!$%c$%d:$%c$%d", sheetName, 'C', i+2, 'C'+columnLength-1, i+2)
				if err := f.AddChart(sheetName, posKey, &excelize.Chart{
					Type: excelize.Line,
					//VaryColors: &disable,
					Series: []excelize.ChartSeries{
						{
							Name:   "",
							Values: chartVal,
							Line: excelize.ChartLine{
								Smooth: false,
								Width:  1.5,
							},
						},
					},
					Legend: excelize.ChartLegend{
						Position: "none",
					},
					Title: []excelize.RichTextRun{
						{
							Text: "Maximum Output Power",
						},
					},
					PlotArea: excelize.ChartPlotArea{
						ShowCatName:     false,
						ShowLeaderLines: true,
						ShowPercent:     true,
						ShowSerName:     false,
						ShowVal: false,
						//NumFmt: excelize.ChartNumFmt{CustomNumFmt: "#\"\""},
					},
					YAxis: excelize.ChartAxis{
						MajorGridLines: true,
						Font: excelize.Font{
							Color: "#000000",
						},
					},
					XAxis: excelize.ChartAxis{
						Font: excelize.Font{
							Color: "#000000",
						},
					},
					Dimension: excelize.ChartDimension{
						Height: 160,
					},
					ShowBlanksAs: "zero",
				}); err != nil {
					fmt.Printf("add 2d line chart error, %s, %s, %s, %v\n", devType, posKey, chartVal, err)
					continue
				}
				fmt.Printf("add 2d line chart %s, %s, %s, %v\n", devType, posKey, chartVal, err)
			}
		}
		//Measurement Item -> Measurement point -> Measurement result
		if err := f.SaveAs(fmt.Sprintf("%s.xlsx", devType)); err != nil {
			fmt.Println(err)
			fmt.Printf("create report file error, %s, %v\n", devType, err)
			return
		}
	}
}

@jingc1413
Copy link
Author

jingc1413 commented Aug 26, 2024

There is another problem here
image
when sheetName contains symbol "-", The generated report chart is wrong,
symbol "_" is normal
image

@xuri
Copy link
Member

xuri commented Aug 26, 2024

Thanks for your feedback. This issue is duplicated to issue #1944. Please add a single quote for worksheet name in chart data range reference if the worksheet name contains symbol "-". For example:

-chartVal = fmt.Sprintf("%s!$%c$%d:$%c$%d", sheetName, 'C', i+2, 'C'+columnLength-1, i+2)
+chartVal = fmt.Sprintf("'%s'!$%c$%d:$%c$%d", sheetName, 'C', i+2, 'C'+columnLength-1, i+2)

I'll close this, if you have any questions, please let me know, and you can reopen this anytime.

@xuri xuri closed this as completed Aug 26, 2024
@xuri xuri added duplicate This issue or pull request already exists and removed needs more info This issue can't reproduce, need more info labels Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants