Skip to content

Commit cc1d3fe

Browse files
rohanallisonrohanthewiz
authored andcommitted
Add stacked bar chart
1 parent 90998bf commit cc1d3fe

File tree

3 files changed

+91
-71
lines changed

3 files changed

+91
-71
lines changed

Diff for: chart.go

+89-71
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,62 @@ import (
99

1010
// This section defines the currently supported chart types.
1111
const (
12-
Bar = "bar"
13-
Bar3D = "bar3D"
14-
Doughnut = "doughnut"
15-
Line = "line"
16-
Pie = "pie"
17-
Pie3D = "pie3D"
18-
Radar = "radar"
19-
Scatter = "scatter"
12+
Bar = "bar"
13+
BarStacked = "barStacked"
14+
Bar3D = "bar3D"
15+
Doughnut = "doughnut"
16+
Line = "line"
17+
Pie = "pie"
18+
Pie3D = "pie3D"
19+
Radar = "radar"
20+
Scatter = "scatter"
2021
)
2122

2223
// This section defines the default value of chart properties.
2324
var (
2425
chartView3DRotX = map[string]int{
25-
Bar: 0,
26-
Bar3D: 15,
27-
Doughnut: 0,
28-
Line: 0,
29-
Pie: 0,
30-
Pie3D: 30,
31-
Radar: 0,
32-
Scatter: 0,
26+
Bar: 0,
27+
BarStacked: 0,
28+
Bar3D: 15,
29+
Doughnut: 0,
30+
Line: 0,
31+
Pie: 0,
32+
Pie3D: 30,
33+
Radar: 0,
34+
Scatter: 0,
3335
}
3436
chartView3DRotY = map[string]int{
35-
Bar: 0,
36-
Bar3D: 20,
37-
Doughnut: 0,
38-
Line: 0,
39-
Pie: 0,
40-
Pie3D: 0,
41-
Radar: 0,
42-
Scatter: 0,
37+
Bar: 0,
38+
BarStacked: 0,
39+
Bar3D: 20,
40+
Doughnut: 0,
41+
Line: 0,
42+
Pie: 0,
43+
Pie3D: 0,
44+
Radar: 0,
45+
Scatter: 0,
4346
}
4447
chartView3DDepthPercent = map[string]int{
45-
Bar: 100,
46-
Bar3D: 100,
47-
Doughnut: 100,
48-
Line: 100,
49-
Pie: 100,
50-
Pie3D: 100,
51-
Radar: 100,
52-
Scatter: 100,
48+
Bar: 100,
49+
BarStacked: 100,
50+
Bar3D: 100,
51+
Doughnut: 100,
52+
Line: 100,
53+
Pie: 100,
54+
Pie3D: 100,
55+
Radar: 100,
56+
Scatter: 100,
5357
}
5458
chartView3DRAngAx = map[string]int{
55-
Bar: 0,
56-
Bar3D: 1,
57-
Doughnut: 0,
58-
Line: 0,
59-
Pie: 0,
60-
Pie3D: 0,
61-
Radar: 0,
62-
Scatter: 0}
59+
Bar: 0,
60+
BarStacked: 0,
61+
Bar3D: 1,
62+
Doughnut: 0,
63+
Line: 0,
64+
Pie: 0,
65+
Pie3D: 0,
66+
Radar: 0,
67+
Scatter: 0}
6368
chartLegendPosition = map[string]string{
6469
"bottom": "b",
6570
"left": "l",
@@ -128,16 +133,17 @@ func parseFormatChartSet(formatSet string) *formatChart {
128133
//
129134
// The following shows the type of chart supported by excelize:
130135
//
131-
// Type | Chart
132-
// ----------+----------------
133-
// bar | bar chart
134-
// bar3D | 3D bar chart
135-
// doughnut | doughnut chart
136-
// line | line chart
137-
// pie | pie chart
138-
// pie3D | 3D pie chart
139-
// radar | radar chart
140-
// scatter | scatter chart
136+
// Type | Chart
137+
// ------------+----------------
138+
// bar | bar chart
139+
// barStacked | stacked bar chart
140+
// bar3D | 3D bar chart
141+
// doughnut | doughnut chart
142+
// line | line chart
143+
// pie | pie chart
144+
// pie3D | 3D pie chart
145+
// radar | radar chart
146+
// scatter | scatter chart
141147
//
142148
// In Excel a chart series is a collection of information that defines which data is plotted such as values, axis labels and formatting.
143149
//
@@ -383,14 +389,15 @@ func (f *File) addChart(formatSet *formatChart) {
383389
},
384390
}
385391
plotAreaFunc := map[string]func(*formatChart) *cPlotArea{
386-
Bar: f.drawBarChart,
387-
Bar3D: f.drawBarChart,
388-
Doughnut: f.drawDoughnutChart,
389-
Line: f.drawLineChart,
390-
Pie3D: f.drawPie3DChart,
391-
Pie: f.drawPieChart,
392-
Radar: f.drawRadarChart,
393-
Scatter: f.drawScatterChart,
392+
Bar: f.drawBarChart,
393+
BarStacked: f.drawBarChart,
394+
Bar3D: f.drawBarChart,
395+
Doughnut: f.drawDoughnutChart,
396+
Line: f.drawLineChart,
397+
Pie3D: f.drawPie3DChart,
398+
Pie: f.drawPieChart,
399+
Radar: f.drawRadarChart,
400+
Scatter: f.drawScatterChart,
394401
}
395402
xlsxChartSpace.Chart.PlotArea = plotAreaFunc[formatSet.Type](formatSet)
396403

@@ -399,7 +406,7 @@ func (f *File) addChart(formatSet *formatChart) {
399406
f.saveFileList(media, string(chart))
400407
}
401408

402-
// drawBarChart provides function to draw the c:plotArea element for bar and
409+
// drawBarChart provides function to draw the c:plotArea element for bar, barStacked and
403410
// bar3D chart by given format sets.
404411
func (f *File) drawBarChart(formatSet *formatChart) *cPlotArea {
405412
c := cCharts{
@@ -419,16 +426,27 @@ func (f *File) drawBarChart(formatSet *formatChart) *cPlotArea {
419426
{Val: 753999904},
420427
},
421428
}
429+
if formatSet.Type == "barStacked" {
430+
c.Grouping.Val = "stacked"
431+
c.Overlap = &attrValInt{Val: 100}
432+
}
433+
catAx := f.drawPlotAreaCatAx()
434+
valAx := f.drawPlotAreaValAx()
422435
charts := map[string]*cPlotArea{
423436
"bar": {
424437
BarChart: &c,
425-
CatAx: f.drawPlotAreaCatAx(),
426-
ValAx: f.drawPlotAreaValAx(),
438+
CatAx: catAx,
439+
ValAx: valAx,
440+
},
441+
"barStacked": {
442+
BarChart: &c,
443+
CatAx: catAx,
444+
ValAx: valAx,
427445
},
428446
"bar3D": {
429447
Bar3DChart: &c,
430-
CatAx: f.drawPlotAreaCatAx(),
431-
ValAx: f.drawPlotAreaValAx(),
448+
CatAx: catAx,
449+
ValAx: valAx,
432450
},
433451
}
434452
return charts[formatSet.Type]
@@ -590,7 +608,7 @@ func (f *File) drawChartSeriesSpPr(i int, formatSet *formatChart) *cSpPr {
590608
},
591609
},
592610
}
593-
chartSeriesSpPr := map[string]*cSpPr{Bar: nil, Bar3D: nil, Doughnut: nil, Line: spPrLine, Pie: nil, Pie3D: nil, Radar: nil, Scatter: spPrScatter}
611+
chartSeriesSpPr := map[string]*cSpPr{Bar: nil, BarStacked: nil, Bar3D: nil, Doughnut: nil, Line: spPrLine, Pie: nil, Pie3D: nil, Radar: nil, Scatter: spPrScatter}
594612
return chartSeriesSpPr[formatSet.Type]
595613
}
596614

@@ -619,7 +637,7 @@ func (f *File) drawChartSeriesDPt(i int, formatSet *formatChart) []*cDPt {
619637
},
620638
},
621639
}}
622-
chartSeriesDPt := map[string][]*cDPt{Bar: nil, Bar3D: nil, Doughnut: nil, Line: nil, Pie: dpt, Pie3D: dpt, Radar: nil, Scatter: nil}
640+
chartSeriesDPt := map[string][]*cDPt{Bar: nil, BarStacked: nil, Bar3D: nil, Doughnut: nil, Line: nil, Pie: dpt, Pie3D: dpt, Radar: nil, Scatter: nil}
623641
return chartSeriesDPt[formatSet.Type]
624642
}
625643

@@ -631,7 +649,7 @@ func (f *File) drawChartSeriesCat(v formatChartSeries, formatSet *formatChart) *
631649
F: v.Categories,
632650
},
633651
}
634-
chartSeriesCat := map[string]*cCat{Bar: cat, Bar3D: cat, Doughnut: cat, Line: cat, Pie: cat, Pie3D: cat, Radar: cat, Scatter: nil}
652+
chartSeriesCat := map[string]*cCat{Bar: cat, BarStacked: cat, Bar3D: cat, Doughnut: cat, Line: cat, Pie: cat, Pie3D: cat, Radar: cat, Scatter: nil}
635653
return chartSeriesCat[formatSet.Type]
636654
}
637655

@@ -643,7 +661,7 @@ func (f *File) drawChartSeriesVal(v formatChartSeries, formatSet *formatChart) *
643661
F: v.Values,
644662
},
645663
}
646-
chartSeriesVal := map[string]*cVal{Bar: val, Bar3D: val, Doughnut: val, Line: val, Pie: val, Pie3D: val, Radar: val, Scatter: nil}
664+
chartSeriesVal := map[string]*cVal{Bar: val, BarStacked: val, Bar3D: val, Doughnut: val, Line: val, Pie: val, Pie3D: val, Radar: val, Scatter: nil}
647665
return chartSeriesVal[formatSet.Type]
648666
}
649667

@@ -669,7 +687,7 @@ func (f *File) drawChartSeriesMarker(i int, formatSet *formatChart) *cMarker {
669687
},
670688
},
671689
}
672-
chartSeriesMarker := map[string]*cMarker{Bar: nil, Bar3D: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: marker}
690+
chartSeriesMarker := map[string]*cMarker{Bar: nil, BarStacked: nil, Bar3D: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: marker}
673691
return chartSeriesMarker[formatSet.Type]
674692
}
675693

@@ -681,7 +699,7 @@ func (f *File) drawChartSeriesXVal(v formatChartSeries, formatSet *formatChart)
681699
F: v.Categories,
682700
},
683701
}
684-
chartSeriesXVal := map[string]*cCat{Bar: nil, Bar3D: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: cat}
702+
chartSeriesXVal := map[string]*cCat{Bar: nil, BarStacked: nil, Bar3D: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: cat}
685703
return chartSeriesXVal[formatSet.Type]
686704
}
687705

@@ -693,7 +711,7 @@ func (f *File) drawChartSeriesYVal(v formatChartSeries, formatSet *formatChart)
693711
F: v.Values,
694712
},
695713
}
696-
chartSeriesYVal := map[string]*cVal{Bar: nil, Bar3D: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: val}
714+
chartSeriesYVal := map[string]*cVal{Bar: nil, BarStacked: nil, Bar3D: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: val}
697715
return chartSeriesYVal[formatSet.Type]
698716
}
699717

@@ -715,7 +733,7 @@ func (f *File) drawChartDLbls(formatSet *formatChart) *cDLbls {
715733
// format sets.
716734
func (f *File) drawChartSeriesDLbls(formatSet *formatChart) *cDLbls {
717735
dLbls := f.drawChartDLbls(formatSet)
718-
chartSeriesDLbls := map[string]*cDLbls{Bar: dLbls, Bar3D: dLbls, Doughnut: dLbls, Line: dLbls, Pie: dLbls, Pie3D: dLbls, Radar: dLbls, Scatter: nil}
736+
chartSeriesDLbls := map[string]*cDLbls{Bar: dLbls, BarStacked: dLbls, Bar3D: dLbls, Doughnut: dLbls, Line: dLbls, Pie: dLbls, Pie3D: dLbls, Radar: dLbls, Scatter: nil}
719737
return chartSeriesDLbls[formatSet.Type]
720738
}
721739

Diff for: excelize_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ func TestAddChart(t *testing.T) {
817817
xlsx.AddChart("Sheet1", "X30", `{"type":"pie","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit Pie Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":false,"show_val":false},"show_blanks_as":"gap"}`)
818818
xlsx.AddChart("Sheet2", "P1", `{"type":"radar","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"top_right","show_legend_key":false},"title":{"name":"Fruit Radar Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"span"}`)
819819
xlsx.AddChart("Sheet2", "X1", `{"type":"scatter","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit Scatter Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
820+
xlsx.AddChart("Sheet2", "P16", `{"type":"barStacked","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"Fruit Bar Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
820821
// Save xlsx file by the given path.
821822
err = xlsx.SaveAs("./test/Workbook_addchart.xlsx")
822823
if err != nil {

Diff for: xmlChart.go

+1
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ type cCharts struct {
316316
DLbls *cDLbls `xml:"c:dLbls"`
317317
HoleSize *attrValInt `xml:"c:holeSize"`
318318
Smooth *attrValBool `xml:"c:smooth"`
319+
Overlap *attrValInt `xml:"c:overlap"`
319320
AxID []*attrValInt `xml:"c:axId"`
320321
}
321322

0 commit comments

Comments
 (0)