Skip to content

Commit 38f11c4

Browse files
committed
Support set dash line and marker border type of charts
- Add Dash and Fill fields in the ChartLine data type - Add Border field in the ChartMarker data type - Add new ChartDashType data type - Support setting dash type of line charts - Update unit tests
1 parent 4ac39a8 commit 38f11c4

File tree

6 files changed

+70
-27
lines changed

6 files changed

+70
-27
lines changed

chart.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@ const (
8080
Bubble3D
8181
)
8282

83+
// ChartDashType is the type of supported chart dash types.
84+
type ChartDashType byte
85+
86+
// This section defines the currently supported chart dash types enumeration.
87+
const (
88+
ChartDashUnset ChartDashType = iota
89+
ChartDashSolid
90+
ChartDashDot
91+
ChartDashDash
92+
ChartDashLgDash
93+
ChartDashSashDot
94+
ChartDashLgDashDot
95+
ChartDashLgDashDotDot
96+
ChartDashSysDash
97+
ChartDashSysDot
98+
ChartDashSysDashDot
99+
ChartDashSysDashDotDot
100+
)
101+
83102
// ChartLineType is the type of supported chart line types.
84103
type ChartLineType byte
85104

chart_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,13 @@ func TestAddChart(t *testing.T) {
163163
{
164164
Name: "Sheet1!$A$37", Categories: "Sheet1!$B$29:$D$29", Values: "Sheet1!$B$37:$D$37",
165165
Marker: ChartMarker{
166-
Fill: Fill{Type: "pattern", Color: []string{"FFFF00"}, Pattern: 1},
166+
Fill: Fill{Type: "pattern", Color: []string{"FFFFFF"}, Pattern: 1},
167+
Border: ChartLine{
168+
Type: ChartLineSolid,
169+
Dash: ChartDashDot,
170+
Fill: Fill{Type: "pattern", Color: []string{"#FFFF00"}, Pattern: 1},
171+
Width: 1,
172+
},
167173
},
168174
},
169175
}
@@ -179,7 +185,7 @@ func TestAddChart(t *testing.T) {
179185
{Name: "Sheet1!$A$34", Categories: "Sheet1!$B$29:$D$29", Values: "Sheet1!$B$34:$D$34"},
180186
{Name: "Sheet1!$A$35", Categories: "Sheet1!$B$29:$D$29", Values: "Sheet1!$B$35:$D$35"},
181187
{Name: "Sheet1!$A$36", Categories: "Sheet1!$B$29:$D$29", Values: "Sheet1!$B$36:$D$36"},
182-
{Name: "Sheet1!$A$37", Categories: "Sheet1!$B$29:$D$29", Values: "Sheet1!$B$37:$D$37", Line: ChartLine{Width: 0.25}},
188+
{Name: "Sheet1!$A$37", Categories: "Sheet1!$B$29:$D$29", Values: "Sheet1!$B$37:$D$37", Line: ChartLine{Dash: ChartDashDash, Width: 0.25}},
183189
}
184190
series3 := []ChartSeries{{Name: "Sheet1!$A$30", Categories: "Sheet1!$A$30:$D$37", Values: "Sheet1!$B$30:$B$37"}}
185191
series4 := []ChartSeries{

drawing.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,9 @@ func (f *File) drawChartSeriesSpPr(i int, opts *Chart) *cSpPr {
816816
SolidFill: spPr.SolidFill,
817817
},
818818
}
819+
if opts.Series[i].Line.Dash != ChartDashUnset {
820+
solid.Ln.PrstDash = &attrValString{Val: stringPtr(chartDashTypes[opts.Series[i].Line.Dash])}
821+
}
819822
noLn := &cSpPr{Ln: &aLn{NoFill: &attrValString{}}}
820823
if chartSeriesSpPr, ok := map[ChartType]map[ChartLineType]*cSpPr{
821824
Line: {ChartLineUnset: solid, ChartLineSolid: solid, ChartLineNone: noLn, ChartLineAutomatic: solid},
@@ -903,23 +906,14 @@ func (f *File) drawChartSeriesMarker(i int, opts *Chart) *cMarker {
903906
marker.Size = &attrValInt{Val: size}
904907
}
905908
if i < 6 {
906-
marker.SpPr = &cSpPr{
907-
SolidFill: &aSolidFill{
908-
SchemeClr: &aSchemeClr{
909-
Val: "accent" + strconv.Itoa(i+1),
910-
},
911-
},
912-
Ln: &aLn{
913-
W: 9252,
914-
SolidFill: &aSolidFill{
915-
SchemeClr: &aSchemeClr{
916-
Val: "accent" + strconv.Itoa(i+1),
917-
},
918-
},
919-
},
920-
}
909+
marker.SpPr = &cSpPr{SolidFill: &aSolidFill{
910+
SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
911+
}, Ln: &aLn{W: 9252}}
921912
}
922913
marker.SpPr = f.drawShapeFill(opts.Series[i].Marker.Fill, marker.SpPr)
914+
if marker.SpPr != nil && marker.SpPr.Ln != nil {
915+
marker.SpPr.Ln = f.drawChartLn(&opts.Series[i].Marker.Border)
916+
}
923917
chartSeriesMarker := map[ChartType]*cMarker{Scatter: marker, Line: marker}
924918
return chartSeriesMarker[opts.Type]
925919
}
@@ -1322,19 +1316,24 @@ func (f *File) drawChartLn(opts *ChartLine) *aLn {
13221316
Cmpd: "sng",
13231317
Algn: "ctr",
13241318
}
1319+
if opts.Dash != ChartDashUnset {
1320+
ln.PrstDash = &attrValString{Val: stringPtr(chartDashTypes[opts.Dash])}
1321+
}
13251322
switch opts.Type {
13261323
case ChartLineSolid:
1327-
ln.SolidFill = &aSolidFill{
1328-
SchemeClr: &aSchemeClr{
1329-
Val: "tx1",
1330-
LumMod: &attrValInt{
1331-
Val: intPtr(15000),
1332-
},
1333-
LumOff: &attrValInt{
1334-
Val: intPtr(85000),
1324+
ln.SolidFill = f.drawShapeFill(opts.Fill, &cSpPr{
1325+
SolidFill: &aSolidFill{
1326+
SchemeClr: &aSchemeClr{
1327+
Val: "tx1",
1328+
LumMod: &attrValInt{
1329+
Val: intPtr(15000),
1330+
},
1331+
LumOff: &attrValInt{
1332+
Val: intPtr(85000),
1333+
},
13351334
},
13361335
},
1337-
}
1336+
}).SolidFill
13381337
return ln
13391338
case ChartLineNone:
13401339
ln.NoFill = &attrValString{}

rows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ func (f *File) GetRowVisible(sheet string, row int) (bool, error) {
585585
}
586586

587587
// SetRowOutlineLevel provides a function to set outline level number of a
588-
// single row by given worksheet name and row number. The range of 'level'
588+
// single row by given worksheet name and row number. The range of 'level'
589589
// parameter value from 1 to 7. For example, outline row 2 in Sheet1 to level 1:
590590
//
591591
// err := f.SetRowOutlineLevel("Sheet1", 2, 1)

templates.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,21 @@ var chartDataLabelsPositionTypes = map[ChartDataLabelPositionType]string{
255255
ChartDataLabelsPositionAbove: "t",
256256
}
257257

258+
// chartDashTypes defined supported preset chart dash types.
259+
var chartDashTypes = map[ChartDashType]string{
260+
ChartDashSolid: "solid",
261+
ChartDashDot: "dot",
262+
ChartDashDash: "dash",
263+
ChartDashLgDash: "lgDash",
264+
ChartDashSashDot: "dashDot",
265+
ChartDashLgDashDot: "lgDashDot",
266+
ChartDashLgDashDotDot: "lgDashDotDot",
267+
ChartDashSysDash: "sysDash",
268+
ChartDashSysDot: "sysDot",
269+
ChartDashSysDashDot: "sysDashDot",
270+
ChartDashSysDashDotDot: "sysDashDotDot",
271+
}
272+
258273
// supportedChartDataLabelsPosition defined supported chart data labels position
259274
// types for each type of chart.
260275
var supportedChartDataLabelsPosition = map[ChartType][]ChartDataLabelPositionType{

xmlChart.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ type aLn struct {
266266
NoFill *attrValString `xml:"a:noFill"`
267267
Round string `xml:"a:round,omitempty"`
268268
SolidFill *aSolidFill `xml:"a:solidFill"`
269+
PrstDash *attrValString `xml:"a:prstDash"`
269270
}
270271

271272
// cTxPr (Text Properties) directly maps the txPr element. This element
@@ -614,6 +615,7 @@ type ChartLegend struct {
614615

615616
// ChartMarker directly maps the format settings of the chart marker.
616617
type ChartMarker struct {
618+
Border ChartLine
617619
Fill Fill
618620
Symbol string
619621
Size int
@@ -622,6 +624,8 @@ type ChartMarker struct {
622624
// ChartLine directly maps the format settings of the chart line.
623625
type ChartLine struct {
624626
Type ChartLineType
627+
Dash ChartDashType
628+
Fill Fill
625629
Smooth bool
626630
Width float64
627631
}

0 commit comments

Comments
 (0)