Skip to content

Commit 5399572

Browse files
authored
This closes #1786, support set fill color of the chart (#1788)
- Add a new field Fill in Chart, ChartPlotArea, and ChartMarker struct - Support set solid color or transparent fill for chart area, plot area, and maker
1 parent 4eb3486 commit 5399572

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

chart_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ func TestAddChart(t *testing.T) {
158158
{Name: "Sheet1!$A$34", Categories: "Sheet1!$B$29:$D$29", Values: "Sheet1!$B$34:$D$34"},
159159
{Name: "Sheet1!$A$35", Categories: "Sheet1!$B$29:$D$29", Values: "Sheet1!$B$35:$D$35"},
160160
{Name: "Sheet1!$A$36", Categories: "Sheet1!$B$29:$D$29", Values: "Sheet1!$B$36:$D$36"},
161-
{Name: "Sheet1!$A$37", Categories: "Sheet1!$B$29:$D$29", Values: "Sheet1!$B$37:$D$37"},
161+
{
162+
Name: "Sheet1!$A$37", Categories: "Sheet1!$B$29:$D$29", Values: "Sheet1!$B$37:$D$37",
163+
Marker: ChartMarker{
164+
Fill: Fill{Type: "pattern", Color: []string{"FFFF00"}, Pattern: 1},
165+
},
166+
},
162167
}
163168
series2 := []ChartSeries{
164169
{
@@ -203,14 +208,15 @@ func TestAddChart(t *testing.T) {
203208
ShowPercent: true,
204209
ShowSerName: true,
205210
ShowVal: true,
211+
Fill: Fill{Type: "pattern", Pattern: 1},
206212
}
207213
for _, c := range []struct {
208214
sheetName, cell string
209215
opts *Chart
210216
}{
211217
{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}}}}}},
212-
{sheetName: "Sheet1", cell: "X1", opts: &Chart{Type: ColStacked, Series: series, Format: format, Legend: legend, Title: []RichTextRun{{Text: "2D Stacked Column Chart"}}, PlotArea: plotArea, Border: ChartLine{Type: ChartLineAutomatic}, ShowBlanksAs: "zero"}},
213-
{sheetName: "Sheet1", cell: "P16", opts: &Chart{Type: ColPercentStacked, Series: series, Format: format, Legend: legend, Title: []RichTextRun{{Text: "100% Stacked Column Chart"}}, PlotArea: plotArea, Border: ChartLine{Type: ChartLineSolid, Width: 2}, ShowBlanksAs: "zero"}},
218+
{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"}},
219+
{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"}},
214220
{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"}},
215221
{sheetName: "Sheet1", cell: "P30", opts: &Chart{Type: Col3DStacked, Series: series, Format: format, Legend: legend, Title: []RichTextRun{{Text: "3D Stacked Column Chart"}}, PlotArea: plotArea, ShowBlanksAs: "zero"}},
216222
{sheetName: "Sheet1", cell: "X30", opts: &Chart{Type: Col3DPercentStacked, Series: series, Format: format, Legend: legend, Title: []RichTextRun{{Text: "3D 100% Stacked Column Chart"}}, PlotArea: plotArea, ShowBlanksAs: "zero"}},

drawing.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func (f *File) addChart(opts *Chart, comboCharts []*Chart) {
107107
},
108108
},
109109
}
110+
xlsxChartSpace.SpPr = f.drawShapeFill(opts.Fill, xlsxChartSpace.SpPr)
110111
plotAreaFunc := map[ChartType]func(*Chart) *cPlotArea{
111112
Area: f.drawBaseChart,
112113
AreaStacked: f.drawBaseChart,
@@ -167,6 +168,7 @@ func (f *File) addChart(opts *Chart, comboCharts []*Chart) {
167168
if opts.Legend.Position == "none" {
168169
xlsxChartSpace.Chart.Legend = nil
169170
}
171+
xlsxChartSpace.Chart.PlotArea.SpPr = f.drawShapeFill(opts.PlotArea.Fill, xlsxChartSpace.Chart.PlotArea.SpPr)
170172
addChart := func(c, p *cPlotArea) {
171173
immutable, mutable := reflect.ValueOf(c).Elem(), reflect.ValueOf(p).Elem()
172174
for i := 0; i < mutable.NumField(); i++ {
@@ -727,19 +729,28 @@ func (f *File) drawChartSeries(opts *Chart) *[]cSer {
727729
return &ser
728730
}
729731

732+
// drawShapeFill provides a function to draw the a:solidFill element by given
733+
// fill format sets.
734+
func (f *File) drawShapeFill(fill Fill, spPr *cSpPr) *cSpPr {
735+
if fill.Type == "pattern" && fill.Pattern == 1 {
736+
if spPr == nil {
737+
spPr = &cSpPr{}
738+
}
739+
if len(fill.Color) == 1 {
740+
spPr.SolidFill = &aSolidFill{SrgbClr: &attrValString{Val: stringPtr(strings.TrimPrefix(fill.Color[0], "#"))}}
741+
return spPr
742+
}
743+
spPr.SolidFill = nil
744+
spPr.NoFill = stringPtr("")
745+
}
746+
return spPr
747+
}
748+
730749
// drawChartSeriesSpPr provides a function to draw the c:spPr element by given
731750
// format sets.
732751
func (f *File) drawChartSeriesSpPr(i int, opts *Chart) *cSpPr {
733-
var srgbClr *attrValString
734-
var schemeClr *aSchemeClr
735-
736-
if color := opts.Series[i].Fill.Color; len(color) == 1 {
737-
srgbClr = &attrValString{Val: stringPtr(strings.TrimPrefix(color[0], "#"))}
738-
} else {
739-
schemeClr = &aSchemeClr{Val: "accent" + strconv.Itoa((opts.order+i)%6+1)}
740-
}
741-
742-
spPr := &cSpPr{SolidFill: &aSolidFill{SchemeClr: schemeClr, SrgbClr: srgbClr}}
752+
spPr := &cSpPr{SolidFill: &aSolidFill{SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa((opts.order+i)%6+1)}}}
753+
spPr = f.drawShapeFill(opts.Series[i].Fill, spPr)
743754
spPrScatter := &cSpPr{
744755
Ln: &aLn{
745756
W: 25400,
@@ -748,20 +759,17 @@ func (f *File) drawChartSeriesSpPr(i int, opts *Chart) *cSpPr {
748759
}
749760
spPrLine := &cSpPr{
750761
Ln: &aLn{
751-
W: f.ptToEMUs(opts.Series[i].Line.Width),
752-
Cap: "rnd", // rnd, sq, flat
753-
SolidFill: &aSolidFill{
754-
SchemeClr: schemeClr,
755-
SrgbClr: srgbClr,
756-
},
762+
W: f.ptToEMUs(opts.Series[i].Line.Width),
763+
Cap: "rnd", // rnd, sq, flat
764+
SolidFill: spPr.SolidFill,
757765
},
758766
}
759767
if chartSeriesSpPr, ok := map[ChartType]*cSpPr{
760768
Line: spPrLine, Scatter: spPrScatter,
761769
}[opts.Type]; ok {
762770
return chartSeriesSpPr
763771
}
764-
if srgbClr != nil {
772+
if spPr.SolidFill.SrgbClr != nil {
765773
return spPr
766774
}
767775
return nil
@@ -857,6 +865,7 @@ func (f *File) drawChartSeriesMarker(i int, opts *Chart) *cMarker {
857865
},
858866
}
859867
}
868+
marker.SpPr = f.drawShapeFill(opts.Series[i].Marker.Fill, marker.SpPr)
860869
chartSeriesMarker := map[ChartType]*cMarker{Scatter: marker, Line: marker}
861870
return chartSeriesMarker[opts.Type]
862871
}

xmlChart.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ type ChartPlotArea struct {
561561
ShowPercent bool
562562
ShowSerName bool
563563
ShowVal bool
564+
Fill Fill
564565
NumFmt ChartNumFmt
565566
}
566567

@@ -576,6 +577,7 @@ type Chart struct {
576577
XAxis ChartAxis
577578
YAxis ChartAxis
578579
PlotArea ChartPlotArea
580+
Fill Fill
579581
Border ChartLine
580582
ShowBlanksAs string
581583
BubbleSize int
@@ -591,6 +593,7 @@ type ChartLegend struct {
591593

592594
// ChartMarker directly maps the format settings of the chart marker.
593595
type ChartMarker struct {
596+
Fill Fill
594597
Symbol string
595598
Size int
596599
}

0 commit comments

Comments
 (0)