Skip to content

Commit e2bd08c

Browse files
committed
Make DeleteChart delete multiple charts located on the same cell
1 parent 0bb2455 commit e2bd08c

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

chart.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,9 @@ func parseFormatChartSet(formatSet string) (*formatChart, error) {
694694
//
695695
// Set chart size by dimension property. The dimension property is optional. The default width is 480, and height is 290.
696696
//
697-
// combo: Specifies the create a chart that combines two art types in a single
698-
// chart. For example, create a clustered column - line chart with data
699-
// Sheet1!$E$1:$L$15:
697+
// combo: Specifies the create a chart that combines two or more chart types
698+
// in a single chart. For example, create a clustered column - line chart with
699+
// data Sheet1!$E$1:$L$15:
700700
//
701701
// package main
702702
//
@@ -782,10 +782,11 @@ func (f *File) DeleteChart(sheet, cell string) (err error) {
782782
}
783783
drawingXML := strings.Replace(f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID), "..", "xl", -1)
784784
wsDr, _ = f.drawingParser(drawingXML)
785-
for idx, anchor := range wsDr.TwoCellAnchor {
786-
if err = nil; anchor.From != nil && anchor.Pic == nil {
787-
if anchor.From.Col == col && anchor.From.Row == row {
785+
for idx := 0; idx < len(wsDr.TwoCellAnchor); idx++ {
786+
if err = nil; wsDr.TwoCellAnchor[idx].From != nil && wsDr.TwoCellAnchor[idx].Pic == nil {
787+
if wsDr.TwoCellAnchor[idx].From.Col == col && wsDr.TwoCellAnchor[idx].From.Row == row {
788788
wsDr.TwoCellAnchor = append(wsDr.TwoCellAnchor[:idx], wsDr.TwoCellAnchor[idx+1:]...)
789+
idx--
789790
}
790791
}
791792
}
@@ -795,26 +796,18 @@ func (f *File) DeleteChart(sheet, cell string) (err error) {
795796
// deleteChart provides a function to delete chart graphic frame by given by
796797
// given coordinates.
797798
func (f *File) deleteChart(col, row int, drawingXML string, wsDr *xlsxWsDr) (err error) {
798-
var (
799-
deWsDr *decodeWsDr
800-
deTwoCellAnchor *decodeTwoCellAnchor
801-
)
802-
deWsDr = new(decodeWsDr)
803-
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(drawingXML)))).
804-
Decode(deWsDr); err != nil && err != io.EOF {
805-
err = fmt.Errorf("xml decode error: %s", err)
806-
return
807-
}
808-
for idx, anchor := range deWsDr.TwoCellAnchor {
799+
var deTwoCellAnchor *decodeTwoCellAnchor
800+
for idx := 0; idx < len(wsDr.TwoCellAnchor); idx++ {
809801
deTwoCellAnchor = new(decodeTwoCellAnchor)
810-
if err = f.xmlNewDecoder(bytes.NewReader([]byte("<decodeTwoCellAnchor>" + anchor.Content + "</decodeTwoCellAnchor>"))).
802+
if err = f.xmlNewDecoder(bytes.NewReader([]byte("<decodeTwoCellAnchor>" + wsDr.TwoCellAnchor[idx].GraphicFrame + "</decodeTwoCellAnchor>"))).
811803
Decode(deTwoCellAnchor); err != nil && err != io.EOF {
812804
err = fmt.Errorf("xml decode error: %s", err)
813805
return
814806
}
815807
if err = nil; deTwoCellAnchor.From != nil && deTwoCellAnchor.Pic == nil {
816-
if anchor.From.Col == col && anchor.From.Row == row {
808+
if deTwoCellAnchor.From.Col == col && deTwoCellAnchor.From.Row == row {
817809
wsDr.TwoCellAnchor = append(wsDr.TwoCellAnchor[:idx], wsDr.TwoCellAnchor[idx+1:]...)
810+
idx--
818811
}
819812
}
820813
}

drawing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ func (f *File) drawPie3DChart(formatSet *formatChart) *cPlotArea {
563563
// pie chart by given format sets.
564564
func (f *File) drawPieOfPieChart(formatSet *formatChart) *cPlotArea {
565565
return &cPlotArea{
566-
PieChart: &cCharts{
566+
OfPieChart: &cCharts{
567567
OfPieType: &attrValString{
568568
Val: stringPtr("pie"),
569569
},
@@ -580,7 +580,7 @@ func (f *File) drawPieOfPieChart(formatSet *formatChart) *cPlotArea {
580580
// pie chart by given format sets.
581581
func (f *File) drawBarOfPieChart(formatSet *formatChart) *cPlotArea {
582582
return &cPlotArea{
583-
PieChart: &cCharts{
583+
OfPieChart: &cCharts{
584584
OfPieType: &attrValString{
585585
Val: stringPtr("bar"),
586586
},

xmlChart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,10 @@ type cCharts struct {
331331
RadarStyle *attrValString `xml:"radarStyle"`
332332
ScatterStyle *attrValString `xml:"scatterStyle"`
333333
OfPieType *attrValString `xml:"ofPieType"`
334-
SerLines *attrValString `xml:"serLines"`
335334
VaryColors *attrValBool `xml:"varyColors"`
336335
Wireframe *attrValBool `xml:"wireframe"`
337336
Ser *[]cSer `xml:"ser"`
337+
SerLines *attrValString `xml:"serLines"`
338338
DLbls *cDLbls `xml:"dLbls"`
339339
Shape *attrValString `xml:"shape"`
340340
HoleSize *attrValInt `xml:"holeSize"`

0 commit comments

Comments
 (0)