From 431c31029e28a2acb27759acaffb2de285d063f8 Mon Sep 17 00:00:00 2001 From: Patrick Wang Date: Thu, 11 Jul 2024 14:39:16 +0800 Subject: [PATCH] This closes #1944, add new TickLabelPosition field in the ChartAxis data type (#1946) - Introduce new exported ChartTickLabelPositionType enumeration - Update unit tests --- chart.go | 21 ++++++++++++++++++++- chart_test.go | 2 +- drawing.go | 8 ++++---- xmlChart.go | 29 +++++++++++++++-------------- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/chart.go b/chart.go index a1078f7..b6d4504 100644 --- a/chart.go +++ b/chart.go @@ -90,6 +90,19 @@ const ( ChartLineAutomatic ) +// ChartTickLabelPositionType is the type of supported chart tick label position +// types. +type ChartTickLabelPositionType byte + +// This section defines the supported chart tick label position types +// enumeration. +const ( + ChartTickLabelNextToAxis ChartTickLabelPositionType = iota + ChartTickLabelHigh + ChartTickLabelLow + ChartTickLabelNone +) + // This section defines the default value of chart properties. var ( chartView3DRotX = map[ChartType]int{ @@ -484,7 +497,13 @@ var ( true: "r", false: "l", } - valTickLblPos = map[ChartType]string{ + tickLblPosVal = map[ChartTickLabelPositionType]string{ + ChartTickLabelNextToAxis: "nextTo", + ChartTickLabelHigh: "high", + ChartTickLabelLow: "low", + ChartTickLabelNone: "none", + } + tickLblPosNone = map[ChartType]string{ Contour: "none", WireframeContour: "none", } diff --git a/chart_test.go b/chart_test.go index 0d870ad..c847978 100644 --- a/chart_test.go +++ b/chart_test.go @@ -237,7 +237,7 @@ func TestAddChart(t *testing.T) { {sheetName: "Sheet2", cell: "P1", opts: &Chart{Type: Line3D, Series: series2, Format: format, Legend: ChartLegend{Position: "top", ShowLegendKey: false}, Title: []RichTextRun{{Text: "3D Line Chart"}}, PlotArea: plotArea, ShowBlanksAs: "zero", XAxis: ChartAxis{MajorGridLines: true, MinorGridLines: true, TickLabelSkip: 1, NumFmt: ChartNumFmt{CustomNumFmt: "General"}}, YAxis: ChartAxis{MajorGridLines: true, MinorGridLines: true, MajorUnit: 1, NumFmt: ChartNumFmt{CustomNumFmt: "General"}}}}, {sheetName: "Sheet2", cell: "X1", opts: &Chart{Type: Scatter, Series: series, Format: format, Legend: ChartLegend{Position: "bottom", ShowLegendKey: false}, Title: []RichTextRun{{Text: "Scatter Chart"}}, PlotArea: plotArea, ShowBlanksAs: "zero"}}, {sheetName: "Sheet2", cell: "P16", opts: &Chart{Type: Doughnut, Series: series3, Format: format, Legend: ChartLegend{Position: "right", ShowLegendKey: false}, Title: []RichTextRun{{Text: "Doughnut Chart"}}, PlotArea: ChartPlotArea{ShowBubbleSize: false, ShowCatName: false, ShowLeaderLines: false, ShowPercent: true, ShowSerName: false, ShowVal: false}, ShowBlanksAs: "zero", HoleSize: 30}}, - {sheetName: "Sheet2", cell: "X16", opts: &Chart{Type: Line, Series: series2, Format: format, Legend: ChartLegend{Position: "top", ShowLegendKey: false}, Title: []RichTextRun{{Text: "Line Chart"}}, PlotArea: plotArea, ShowBlanksAs: "zero", XAxis: ChartAxis{MajorGridLines: true, MinorGridLines: true, TickLabelSkip: 1}, YAxis: ChartAxis{MajorGridLines: true, MinorGridLines: true, MajorUnit: 1}}}, + {sheetName: "Sheet2", cell: "X16", opts: &Chart{Type: Line, Series: series2, Format: format, Legend: ChartLegend{Position: "top", ShowLegendKey: false}, Title: []RichTextRun{{Text: "Line Chart"}}, PlotArea: plotArea, ShowBlanksAs: "zero", XAxis: ChartAxis{MajorGridLines: true, MinorGridLines: true, TickLabelSkip: 1, TickLabelPosition: ChartTickLabelLow}, YAxis: ChartAxis{MajorGridLines: true, MinorGridLines: true, MajorUnit: 1}}}, {sheetName: "Sheet2", cell: "P32", opts: &Chart{Type: Pie3D, Series: series3, Format: format, Legend: ChartLegend{Position: "bottom", ShowLegendKey: false}, Title: []RichTextRun{{Text: "3D Column Chart"}}, PlotArea: plotArea, ShowBlanksAs: "zero"}}, {sheetName: "Sheet2", cell: "X32", opts: &Chart{Type: Pie, Series: series3, Format: format, Legend: ChartLegend{Position: "bottom", ShowLegendKey: false}, Title: []RichTextRun{{Text: "Pie Chart"}}, PlotArea: ChartPlotArea{ShowBubbleSize: true, ShowCatName: false, ShowLeaderLines: false, ShowPercent: true, ShowSerName: false, ShowVal: false, NumFmt: ChartNumFmt{CustomNumFmt: "0.00%;0;;"}}, ShowBlanksAs: "gap"}}, // bar series chart diff --git a/drawing.go b/drawing.go index bbd75ec..95d5366 100644 --- a/drawing.go +++ b/drawing.go @@ -1001,7 +1001,7 @@ func (f *File) drawPlotAreaCatAx(pa *cPlotArea, opts *Chart) []*cAxs { MajorTickMark: &attrValString{Val: stringPtr("none")}, MinorTickMark: &attrValString{Val: stringPtr("none")}, Title: f.drawPlotAreaTitles(opts.XAxis.Title, ""), - TickLblPos: &attrValString{Val: stringPtr("nextTo")}, + TickLblPos: &attrValString{Val: stringPtr(tickLblPosVal[opts.XAxis.TickLabelPosition])}, SpPr: f.drawPlotAreaSpPr(), TxPr: f.drawPlotAreaTxPr(&opts.XAxis), CrossAx: &attrValInt{Val: intPtr(100000001)}, @@ -1063,7 +1063,7 @@ func (f *File) drawPlotAreaValAx(pa *cPlotArea, opts *Chart) []*cAxs { }, MajorTickMark: &attrValString{Val: stringPtr("none")}, MinorTickMark: &attrValString{Val: stringPtr("none")}, - TickLblPos: &attrValString{Val: stringPtr("nextTo")}, + TickLblPos: &attrValString{Val: stringPtr(tickLblPosVal[opts.YAxis.TickLabelPosition])}, SpPr: f.drawPlotAreaSpPr(), TxPr: f.drawPlotAreaTxPr(&opts.YAxis), CrossAx: &attrValInt{Val: intPtr(100000000)}, @@ -1079,7 +1079,7 @@ func (f *File) drawPlotAreaValAx(pa *cPlotArea, opts *Chart) []*cAxs { if opts.YAxis.MinorGridLines { ax.MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()} } - if pos, ok := valTickLblPos[opts.Type]; ok { + if pos, ok := tickLblPosNone[opts.Type]; ok { ax.TickLblPos.Val = stringPtr(pos) } if opts.YAxis.MajorUnit != 0 { @@ -1115,7 +1115,7 @@ func (f *File) drawPlotAreaSerAx(opts *Chart) []*cAxs { }, Delete: &attrValBool{Val: boolPtr(opts.YAxis.None)}, AxPos: &attrValString{Val: stringPtr(catAxPos[opts.XAxis.ReverseOrder])}, - TickLblPos: &attrValString{Val: stringPtr("nextTo")}, + TickLblPos: &attrValString{Val: stringPtr(tickLblPosVal[opts.YAxis.TickLabelPosition])}, SpPr: f.drawPlotAreaSpPr(), TxPr: f.drawPlotAreaTxPr(nil), CrossAx: &attrValInt{Val: intPtr(100000001)}, diff --git a/xmlChart.go b/xmlChart.go index c5d601e..273edac 100644 --- a/xmlChart.go +++ b/xmlChart.go @@ -530,20 +530,21 @@ type ChartNumFmt struct { // ChartAxis directly maps the format settings of the chart axis. type ChartAxis struct { - None bool - MajorGridLines bool - MinorGridLines bool - MajorUnit float64 - TickLabelSkip int - ReverseOrder bool - Secondary bool - Maximum *float64 - Minimum *float64 - Font Font - LogBase float64 - NumFmt ChartNumFmt - Title []RichTextRun - axID int + None bool + MajorGridLines bool + MinorGridLines bool + MajorUnit float64 + TickLabelPosition ChartTickLabelPositionType + TickLabelSkip int + ReverseOrder bool + Secondary bool + Maximum *float64 + Minimum *float64 + Font Font + LogBase float64 + NumFmt ChartNumFmt + Title []RichTextRun + axID int } // ChartDimension directly maps the dimension of the chart.