-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
How to set the font for DataLabel in ChartSeries? #2052
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
Labels
enhancement
New feature or request
Comments
Thanks for your issue. I added a new field 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)
}
}()
sheetName := "Sheet1"
index, err := f.NewSheet(sheetName)
if err != nil {
fmt.Println(err)
return
}
f.SetActiveSheet(index)
data := [][]interface{}{
{"RL6547E", "Octo", "Nove", nil, "WW46", "WW47", "WW48", "WW49", "WW50"},
{"Input", 1469, 1190, nil, 200, 300, 123, 243, 324},
{"Output", 1469, 1190, nil, 200, 300, 123, 243, 324},
{"Final Yield", 0.9767, 0.9799, nil, 0.9765, 0.9774, 0.9814, 0.9635, 0.9903},
{"Yield Limit", 0.9500, 0.9500, nil, 0.9500, 0.9500, 0.9500, 0.9500, 0.9500},
}
for idx, row := range data {
cell, err := excelize.CoordinatesToCellName(1, idx+1)
if err != nil {
fmt.Println(err)
return
}
if err := f.SetSheetRow("Sheet1", cell, &row); err != nil {
fmt.Println(err)
return
}
}
colLen := len(data[0])
rowLen := len(data)
maxColAscii := CalcAscii(colLen - 1)
// maxRowAscii := CalcAscii(rowLen)
enable, disable := true, false
barChart := excelize.Chart{
Dimension: excelize.ChartDimension{
Height: 350,
Width: 700,
},
Type: excelize.Col,
Series: []excelize.ChartSeries{
{
Name: fmt.Sprintf("%s!$A$2", sheetName),
Categories: fmt.Sprintf("%s!$B$1:$%s$1", sheetName, maxColAscii),
Values: fmt.Sprintf("%s!$B$2:$%s$2", sheetName, maxColAscii),
DataLabelPosition: excelize.ChartDataLabelsPositionAbove,
},
{
Name: fmt.Sprintf("%s!$A$3", sheetName),
Categories: fmt.Sprintf("%s!$B$1:$%s$1", sheetName, maxColAscii),
Values: fmt.Sprintf("%s!$B$3:$%s$3", sheetName, maxColAscii),
DataLabelPosition: excelize.ChartDataLabelsPositionAbove,
},
},
Format: excelize.GraphicOptions{
ScaleX: 1,
ScaleY: 1,
OffsetX: 15,
OffsetY: 10,
PrintObject: &enable,
LockAspectRatio: false,
Locked: &disable,
},
Title: []excelize.RichTextRun{
{
Text: fmt.Sprintf("%s CP Yield Performance", data[0][0]),
},
},
Legend: excelize.ChartLegend{
Position: "bottom",
ShowLegendKey: false,
},
PlotArea: excelize.ChartPlotArea{
ShowCatName: false,
ShowLeaderLines: false,
ShowPercent: false,
ShowSerName: false,
ShowVal: true,
},
ShowBlanksAs: "gap",
YAxis: excelize.ChartAxis{
MajorGridLines: true,
Secondary: true,
Font: excelize.Font{
Color: "#000000",
Size: 15,
},
},
}
lineChart := excelize.Chart{
Type: excelize.Line,
Series: []excelize.ChartSeries{
{
Name: fmt.Sprintf("%s!$A$4", sheetName),
Categories: fmt.Sprintf("%s!$B$1:$%s$1", sheetName, maxColAscii),
Values: fmt.Sprintf("%s!$B$4:$%s$4", sheetName, maxColAscii),
DataLabelPosition: excelize.ChartDataLabelsPositionCenter,
Marker: excelize.ChartMarker{
Symbol: "none", Size: 9,
},
+ DataLabel: excelize.ChartDataLabel{
+ Font: excelize.Font{
+ Size: 22,
+ },
+ Fill: excelize.Fill{Type: "pattern", Color: []string{"C7EECF"}, Pattern: 1},
+ },
},
{
Name: fmt.Sprintf("%s!$A$5", sheetName),
Categories: fmt.Sprintf("%s!$B$1:$%s$1", sheetName, maxColAscii),
Values: fmt.Sprintf("%s!$B$5:$%s$5", sheetName, maxColAscii),
DataLabelPosition: excelize.ChartDataLabelsPositionCenter,
Marker: excelize.ChartMarker{
Symbol: "none", Size: 9,
},
},
},
Legend: excelize.ChartLegend{
Position: "bottom",
ShowLegendKey: false,
},
Format: excelize.GraphicOptions{
ScaleX: 1,
ScaleY: 1,
OffsetX: 15,
OffsetY: 10,
PrintObject: &enable,
LockAspectRatio: false,
Locked: &disable,
},
PlotArea: excelize.ChartPlotArea{
ShowCatName: false,
ShowLeaderLines: false,
ShowPercent: false,
ShowSerName: false,
ShowVal: true,
NumFmt: excelize.ChartNumFmt{
CustomNumFmt: "0.00%",
},
},
ShowBlanksAs: "gap",
YAxis: excelize.ChartAxis{
MajorGridLines: true,
Secondary: true,
Font: excelize.Font{
Color: "#000000",
Size: 10,
},
},
}
chartStartCell, err := excelize.CoordinatesToCellName(1, rowLen+1)
if err != nil {
fmt.Println(err)
return
}
if err := f.AddChart("Sheet1", chartStartCell,
&barChart, &lineChart,
); err != nil {
fmt.Println(err)
return
}
if err := f.SaveAs("TestCharts.xlsx"); err != nil {
fmt.Println(err)
}
}
func CalcAscii(num int) string {
startAscii := 65 // A
return string(rune(startAscii + num))
} This feature will be released on the next version. |
This feature really helps a lot, Thanks! |
You're welcome. If you have enjoyed the productivity of using my open-source projects, consider sponsorship though GitHub sponsors, thanks. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
When generate Chart, the default FontSize of ChartSeries is 10, which is a little larger. Is there any way that I can change the Font, like: FontSize, FontColor, etc. ?
Below is an example:
Steps to reproduce the issue:
Describe the results you received:
I cannot find any option in ChartSeries
Describe the results you expected:
Change the font like XAxis or YAxis
Output of
go version
:1.23.1
Excelize version or commit ID:
github.com/xuri/excelize/v2 v2.9.0
Environment details (OS, Microsoft Excel™ version, physical, etc.):
OS: Windows 11
Excel version: Microsoft Office Home and Student 2019
The text was updated successfully, but these errors were encountered: