Skip to content

Commit 0f19d7f

Browse files
committed
This closes #2117, support add data table for chart
- Add ShowDataTable and ShowDataTableKeys fields in the ChartPlotArea data type - Update unit tests
1 parent 55cf0d4 commit 0f19d7f

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

chart.go

+8
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,14 @@ func (opts *Chart) parseTitle() {
863863
// ShowCatName: Specifies that the category name shall be shown in the data
864864
// label. The 'ShowCatName' property is optional. The default value is true.
865865
//
866+
// ShowDataTable: Used for add data table under chart, depending on the chart
867+
// type, only available for area, bar, column and line series type charts. The
868+
// 'ShowDataTable' property is optional. The default value is false.
869+
//
870+
// ShowDataTableKeys: Used for add legend key in data table, only works on
871+
// 'ShowDataTable' is enabled. The 'ShowDataTableKeys' property is optional.
872+
// The default value is false.
873+
//
866874
// ShowLeaderLines: Specifies leader lines shall be shown for data labels. The
867875
// 'ShowLeaderLines' property is optional. The default value is false.
868876
//

chart_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func TestAddChart(t *testing.T) {
293293
{"I1", Doughnut, "Clustered Column - Doughnut Chart"},
294294
}
295295
for _, props := range clusteredColumnCombo {
296-
assert.NoError(t, f.AddChart("Combo Charts", props[0].(string), &Chart{Type: Col, Series: series[:4], Format: format, Legend: legend, Title: []RichTextRun{{Text: props[2].(string)}}, PlotArea: ChartPlotArea{ShowBubbleSize: true, ShowCatName: false, ShowLeaderLines: false, ShowPercent: true, ShowSerName: true, ShowVal: true}}, &Chart{Type: props[1].(ChartType), Series: series[4:], Format: format, Legend: legend, PlotArea: ChartPlotArea{ShowBubbleSize: true, ShowCatName: false, ShowLeaderLines: false, ShowPercent: true, ShowSerName: true, ShowVal: true}, YAxis: ChartAxis{Secondary: true}}))
296+
assert.NoError(t, f.AddChart("Combo Charts", props[0].(string), &Chart{Type: Col, Series: series[:4], Format: format, Legend: legend, Title: []RichTextRun{{Text: props[2].(string)}}, PlotArea: ChartPlotArea{ShowBubbleSize: true, ShowCatName: false, ShowDataTable: true, ShowDataTableKeys: true, ShowLeaderLines: false, ShowPercent: true, ShowSerName: true, ShowVal: true}}, &Chart{Type: props[1].(ChartType), Series: series[4:], Format: format, Legend: legend, PlotArea: ChartPlotArea{ShowBubbleSize: true, ShowCatName: false, ShowLeaderLines: false, ShowPercent: true, ShowSerName: true, ShowVal: true}, YAxis: ChartAxis{Secondary: true}}))
297297
}
298298
stackedAreaCombo := map[string][]interface{}{
299299
"A16": {Line, "Stacked Area - Line Chart"},

drawing.go

+14
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func (f *File) addChart(opts *Chart, comboCharts []*Chart) {
169169
xlsxChartSpace.Chart.Legend = nil
170170
}
171171
xlsxChartSpace.Chart.PlotArea.SpPr = f.drawShapeFill(opts.PlotArea.Fill, xlsxChartSpace.Chart.PlotArea.SpPr)
172+
xlsxChartSpace.Chart.PlotArea.DTable = f.drawPlotAreaDTable(opts)
172173
addChart := func(c, p *cPlotArea) {
173174
immutable, mutable := reflect.ValueOf(c).Elem(), reflect.ValueOf(p).Elem()
174175
for i := 0; i < mutable.NumField(); i++ {
@@ -1232,6 +1233,19 @@ func (f *File) drawPlotAreaTitles(runs []RichTextRun, vert string) *cTitle {
12321233
return title
12331234
}
12341235

1236+
// drawPlotAreaDTable provides a function to draw the c:dTable element.
1237+
func (f *File) drawPlotAreaDTable(opts *Chart) *cDTable {
1238+
if _, ok := plotAreaChartGrouping[opts.Type]; ok && opts.PlotArea.ShowDataTable {
1239+
return &cDTable{
1240+
ShowHorzBorder: &attrValBool{Val: boolPtr(true)},
1241+
ShowVertBorder: &attrValBool{Val: boolPtr(true)},
1242+
ShowOutline: &attrValBool{Val: boolPtr(true)},
1243+
ShowKeys: &attrValBool{Val: boolPtr(opts.PlotArea.ShowDataTableKeys)},
1244+
}
1245+
}
1246+
return nil
1247+
}
1248+
12351249
// drawPlotAreaSpPr provides a function to draw the c:spPr element.
12361250
func (f *File) drawPlotAreaSpPr() *cSpPr {
12371251
return &cSpPr{

xmlChart.go

+23-9
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ type aRPr struct {
215215
Cs *aCs `xml:"a:cs"`
216216
}
217217

218+
// cDTable (Data Table) directly maps the dTable element.
219+
type cDTable struct {
220+
ShowHorzBorder *attrValBool `xml:"showHorzBorder"`
221+
ShowVertBorder *attrValBool `xml:"showVertBorder"`
222+
ShowOutline *attrValBool `xml:"showOutline"`
223+
ShowKeys *attrValBool `xml:"showKeys"`
224+
SpPr *cSpPr `xml:"spPr"`
225+
TxPr *cTxPr `xml:"txPr"`
226+
ExtLst *xlsxExtLst `xml:"extLst"`
227+
}
228+
218229
// cSpPr (Shape Properties) directly maps the spPr element. This element
219230
// specifies the visual shape properties that can be applied to a shape. These
220231
// properties include the shape fill, outline, geometry, effects, and 3D
@@ -319,6 +330,7 @@ type cPlotArea struct {
319330
CatAx []*cAxs `xml:"catAx"`
320331
ValAx []*cAxs `xml:"valAx"`
321332
SerAx []*cAxs `xml:"serAx"`
333+
DTable *cDTable `xml:"dTable"`
322334
SpPr *cSpPr `xml:"spPr"`
323335
}
324336

@@ -559,15 +571,17 @@ type ChartDimension struct {
559571

560572
// ChartPlotArea directly maps the format settings of the plot area.
561573
type ChartPlotArea struct {
562-
SecondPlotValues int
563-
ShowBubbleSize bool
564-
ShowCatName bool
565-
ShowLeaderLines bool
566-
ShowPercent bool
567-
ShowSerName bool
568-
ShowVal bool
569-
Fill Fill
570-
NumFmt ChartNumFmt
574+
SecondPlotValues int
575+
ShowBubbleSize bool
576+
ShowCatName bool
577+
ShowDataTable bool
578+
ShowDataTableKeys bool
579+
ShowLeaderLines bool
580+
ShowPercent bool
581+
ShowSerName bool
582+
ShowVal bool
583+
Fill Fill
584+
NumFmt ChartNumFmt
571585
}
572586

573587
// Chart directly maps the format settings of the chart.

0 commit comments

Comments
 (0)