-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Comments
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. |
@xuri thank you 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
}
}
}
|
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. |
Description
我想让 x轴显示在下方
代码生成的图表有些在上面有些在下方
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
Excelize version or commit ID:
Environment details (OS, Microsoft Excel™ version, physical, etc.):
The text was updated successfully, but these errors were encountered: