Skip to content

Commit bba155e

Browse files
authored
This closes #1805, support set chart axis font family, size and strike style (#1809)
- Update unit test workflow dependencies package version
1 parent a258e3d commit bba155e

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
run: env GO111MODULE=on go test -v -timeout 30m -race ./... -coverprofile=coverage.txt -covermode=atomic
3232

3333
- name: Codecov
34-
uses: codecov/codecov-action@v3
34+
uses: codecov/codecov-action@v4
3535
with:
3636
file: coverage.txt
3737
flags: unittests

chart_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func TestAddChart(t *testing.T) {
214214
sheetName, cell string
215215
opts *Chart
216216
}{
217-
{sheetName: "Sheet1", cell: "P1", opts: &Chart{Type: Col, Series: series, Format: format, Legend: ChartLegend{Position: "none", ShowLegendKey: true}, Title: []RichTextRun{{Text: "2D Column Chart"}}, PlotArea: plotArea, Border: ChartLine{Type: ChartLineNone}, ShowBlanksAs: "zero", XAxis: ChartAxis{Font: Font{Bold: true, Italic: true, Underline: "dbl", Color: "000000"}, Title: []RichTextRun{{Text: "Primary Horizontal Axis Title"}}}, YAxis: ChartAxis{Font: Font{Bold: false, Italic: false, Underline: "sng", Color: "777777"}, Title: []RichTextRun{{Text: "Primary Vertical Axis Title", Font: &Font{Color: "777777", Bold: true, Italic: true, Size: 12}}}}}},
217+
{sheetName: "Sheet1", cell: "P1", opts: &Chart{Type: Col, Series: series, Format: format, Legend: ChartLegend{Position: "none", ShowLegendKey: true}, Title: []RichTextRun{{Text: "2D Column Chart"}}, PlotArea: plotArea, Border: ChartLine{Type: ChartLineNone}, ShowBlanksAs: "zero", XAxis: ChartAxis{Font: Font{Bold: true, Italic: true, Underline: "dbl", Family: "Times New Roman", Size: 15, Strike: true, Color: "000000"}, Title: []RichTextRun{{Text: "Primary Horizontal Axis Title"}}}, YAxis: ChartAxis{Font: Font{Bold: false, Italic: false, Underline: "sng", Color: "777777"}, Title: []RichTextRun{{Text: "Primary Vertical Axis Title", Font: &Font{Color: "777777", Bold: true, Italic: true, Size: 12}}}}}},
218218
{sheetName: "Sheet1", cell: "X1", opts: &Chart{Type: ColStacked, Series: series, Format: format, Legend: legend, Title: []RichTextRun{{Text: "2D Stacked Column Chart"}}, PlotArea: plotArea, Fill: Fill{Type: "pattern", Pattern: 1}, Border: ChartLine{Type: ChartLineAutomatic}, ShowBlanksAs: "zero"}},
219219
{sheetName: "Sheet1", cell: "P16", opts: &Chart{Type: ColPercentStacked, Series: series, Format: format, Legend: legend, Title: []RichTextRun{{Text: "100% Stacked Column Chart"}}, PlotArea: plotArea, Fill: Fill{Type: "pattern", Color: []string{"EEEEEE"}, Pattern: 1}, Border: ChartLine{Type: ChartLineSolid, Width: 2}, ShowBlanksAs: "zero"}},
220220
{sheetName: "Sheet1", cell: "X16", opts: &Chart{Type: Col3DClustered, Series: series, Format: format, Legend: ChartLegend{Position: "bottom", ShowLegendKey: false}, Title: []RichTextRun{{Text: "3D Clustered Column Chart"}}, PlotArea: plotArea, ShowBlanksAs: "zero"}},

drawing.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,34 @@ func (f *File) drawPlotAreaSerAx(opts *Chart) []*cAxs {
11551155
}
11561156
}
11571157

1158+
// drawChartFont provides a function to draw the a:rPr element.
1159+
func drawChartFont(fnt *Font, r *aRPr) {
1160+
if fnt == nil {
1161+
return
1162+
}
1163+
r.B = fnt.Bold
1164+
r.I = fnt.Italic
1165+
if idx := inStrSlice(supportedDrawingUnderlineTypes, fnt.Underline, true); idx != -1 {
1166+
r.U = supportedDrawingUnderlineTypes[idx]
1167+
}
1168+
if fnt.Color != "" {
1169+
if r.SolidFill == nil {
1170+
r.SolidFill = &aSolidFill{}
1171+
}
1172+
r.SolidFill.SchemeClr = nil
1173+
r.SolidFill.SrgbClr = &attrValString{Val: stringPtr(strings.ReplaceAll(strings.ToUpper(fnt.Color), "#", ""))}
1174+
}
1175+
if fnt.Family != "" {
1176+
r.Latin.Typeface = fnt.Family
1177+
}
1178+
if fnt.Size > 0 {
1179+
r.Sz = fnt.Size * 100
1180+
}
1181+
if fnt.Strike {
1182+
r.Strike = "sngStrike"
1183+
}
1184+
}
1185+
11581186
// drawPlotAreaTitles provides a function to draw the c:title element.
11591187
func (f *File) drawPlotAreaTitles(runs []RichTextRun, vert string) *cTitle {
11601188
if len(runs) == 0 {
@@ -1163,15 +1191,7 @@ func (f *File) drawPlotAreaTitles(runs []RichTextRun, vert string) *cTitle {
11631191
title := &cTitle{Tx: cTx{Rich: &cRich{}}, Overlay: &attrValBool{Val: boolPtr(false)}}
11641192
for _, run := range runs {
11651193
r := &aR{T: run.Text}
1166-
if run.Font != nil {
1167-
r.RPr.B, r.RPr.I = run.Font.Bold, run.Font.Italic
1168-
if run.Font.Color != "" {
1169-
r.RPr.SolidFill = &aSolidFill{SrgbClr: &attrValString{Val: stringPtr(run.Font.Color)}}
1170-
}
1171-
if run.Font.Size > 0 {
1172-
r.RPr.Sz = run.Font.Size * 100
1173-
}
1174-
}
1194+
drawChartFont(run.Font, &r.RPr)
11751195
title.Tx.Rich.P = append(title.Tx.Rich.P, aP{
11761196
PPr: &aPPr{DefRPr: aRPr{}},
11771197
R: r,
@@ -1241,15 +1261,7 @@ func (f *File) drawPlotAreaTxPr(opts *ChartAxis) *cTxPr {
12411261
},
12421262
}
12431263
if opts != nil {
1244-
cTxPr.P.PPr.DefRPr.B = opts.Font.Bold
1245-
cTxPr.P.PPr.DefRPr.I = opts.Font.Italic
1246-
if idx := inStrSlice(supportedDrawingUnderlineTypes, opts.Font.Underline, true); idx != -1 {
1247-
cTxPr.P.PPr.DefRPr.U = supportedDrawingUnderlineTypes[idx]
1248-
}
1249-
if opts.Font.Color != "" {
1250-
cTxPr.P.PPr.DefRPr.SolidFill.SchemeClr = nil
1251-
cTxPr.P.PPr.DefRPr.SolidFill.SrgbClr = &attrValString{Val: stringPtr(strings.ReplaceAll(strings.ToUpper(opts.Font.Color), "#", ""))}
1252-
}
1264+
drawChartFont(&opts.Font, &cTxPr.P.PPr.DefRPr)
12531265
}
12541266
return cTxPr
12551267
}

0 commit comments

Comments
 (0)