@@ -187,24 +187,12 @@ func (f *File) AddChart(sheet, cell, format string) {
187
187
drawingID := f .countDrawings () + 1
188
188
chartID := f .countCharts () + 1
189
189
drawingXML := "xl/drawings/drawing" + strconv .Itoa (drawingID ) + ".xml"
190
- sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv .Itoa (drawingID ) + ".xml"
191
-
192
- var drawingRID int
193
- if xlsx .Drawing != nil {
194
- // The worksheet already has a picture or chart relationships, use the relationships drawing ../drawings/drawing%d.xml.
195
- sheetRelationshipsDrawingXML = f .getSheetRelationshipsTargetByID (sheet , xlsx .Drawing .RID )
196
- drawingID , _ = strconv .Atoi (strings .TrimSuffix (strings .TrimPrefix (sheetRelationshipsDrawingXML , "../drawings/drawing" ), ".xml" ))
197
- drawingXML = strings .Replace (sheetRelationshipsDrawingXML , ".." , "xl" , - 1 )
198
- } else {
199
- // Add first picture for given sheet.
200
- rID := f .addSheetRelationships (sheet , SourceRelationshipDrawingML , sheetRelationshipsDrawingXML , "" )
201
- f .addSheetDrawing (sheet , rID )
202
- }
203
- drawingRID = f .addDrawingRelationships (drawingID , SourceRelationshipChart , "../charts/chart" + strconv .Itoa (chartID )+ ".xml" )
190
+ drawingID , drawingXML = f .prepareDrawing (xlsx , drawingID , sheet , drawingXML )
191
+ drawingRID := f .addDrawingRelationships (drawingID , SourceRelationshipChart , "../charts/chart" + strconv .Itoa (chartID )+ ".xml" )
204
192
f .addDrawingChart (sheet , drawingXML , cell , 480 , 290 , drawingRID , & formatSet .Format )
205
193
f .addChart (formatSet )
206
- f .addChartContentTypePart (chartID )
207
- f .addDrawingContentTypePart (drawingID )
194
+ f .addContentTypePart (chartID , "chart" )
195
+ f .addContentTypePart (drawingID , "drawings" )
208
196
}
209
197
210
198
// countCharts provides function to get chart files count storage in the
@@ -219,19 +207,21 @@ func (f *File) countCharts() int {
219
207
return count
220
208
}
221
209
222
- // addChartContentTypePart provides function to add chart part relationships in
223
- // the file [Content_Types].xml by given chart index.
224
- func (f * File ) addChartContentTypePart (index int ) {
225
- content := f .contentTypesReader ()
226
- for _ , v := range content .Overrides {
227
- if v .PartName == "/xl/charts/chart" + strconv .Itoa (index )+ ".xml" {
228
- return
229
- }
210
+ // prepareDrawing provides function to prepare drawing ID and XML by given
211
+ // drawingID, worksheet index and default drawingXML.
212
+ func (f * File ) prepareDrawing (xlsx * xlsxWorksheet , drawingID int , sheet , drawingXML string ) (int , string ) {
213
+ sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv .Itoa (drawingID ) + ".xml"
214
+ if xlsx .Drawing != nil {
215
+ // The worksheet already has a picture or chart relationships, use the relationships drawing ../drawings/drawing%d.xml.
216
+ sheetRelationshipsDrawingXML = f .getSheetRelationshipsTargetByID (sheet , xlsx .Drawing .RID )
217
+ drawingID , _ = strconv .Atoi (strings .TrimSuffix (strings .TrimPrefix (sheetRelationshipsDrawingXML , "../drawings/drawing" ), ".xml" ))
218
+ drawingXML = strings .Replace (sheetRelationshipsDrawingXML , ".." , "xl" , - 1 )
219
+ } else {
220
+ // Add first picture for given sheet.
221
+ rID := f .addSheetRelationships (sheet , SourceRelationshipDrawingML , sheetRelationshipsDrawingXML , "" )
222
+ f .addSheetDrawing (sheet , rID )
230
223
}
231
- content .Overrides = append (content .Overrides , xlsxOverride {
232
- PartName : "/xl/charts/chart" + strconv .Itoa (index ) + ".xml" ,
233
- ContentType : "application/vnd.openxmlformats-officedocument.drawingml.chart+xml" ,
234
- })
224
+ return drawingID , drawingXML
235
225
}
236
226
237
227
// addChart provides function to create chart as xl/charts/chart%d.xml by given
@@ -364,7 +354,7 @@ func (f *File) addChart(formatSet *formatChart) {
364
354
}
365
355
plotAreaFunc := map [string ]func (* formatChart ) * cPlotArea {
366
356
Bar : f .drawBarChart ,
367
- Bar3D : f .drawBar3DChart ,
357
+ Bar3D : f .drawBarChart ,
368
358
Doughnut : f .drawDoughnutChart ,
369
359
Line : f .drawLineChart ,
370
360
Pie3D : f .drawPie3DChart ,
@@ -379,56 +369,39 @@ func (f *File) addChart(formatSet *formatChart) {
379
369
f .saveFileList (media , string (chart ))
380
370
}
381
371
382
- // drawBarChart provides function to draw the c:plotArea element for bar chart
383
- // by given format sets.
372
+ // drawBarChart provides function to draw the c:plotArea element for bar and
373
+ // bar3D chart by given format sets.
384
374
func (f * File ) drawBarChart (formatSet * formatChart ) * cPlotArea {
385
- return & cPlotArea {
386
- BarChart : & cCharts {
387
- BarDir : & attrValString {
388
- Val : "col" ,
389
- },
390
- Grouping : & attrValString {
391
- Val : "clustered" ,
392
- },
393
- VaryColors : & attrValBool {
394
- Val : true ,
395
- },
396
- Ser : f .drawChartSeries (formatSet ),
397
- DLbls : f .drawChartDLbls (formatSet ),
398
- AxID : []* attrValInt {
399
- {Val : 754001152 },
400
- {Val : 753999904 },
401
- },
375
+ c := cCharts {
376
+ BarDir : & attrValString {
377
+ Val : "col" ,
378
+ },
379
+ Grouping : & attrValString {
380
+ Val : "clustered" ,
381
+ },
382
+ VaryColors : & attrValBool {
383
+ Val : true ,
384
+ },
385
+ Ser : f .drawChartSeries (formatSet ),
386
+ DLbls : f .drawChartDLbls (formatSet ),
387
+ AxID : []* attrValInt {
388
+ {Val : 754001152 },
389
+ {Val : 753999904 },
402
390
},
403
- CatAx : f .drawPlotAreaCatAx (),
404
- ValAx : f .drawPlotAreaValAx (),
405
391
}
406
- }
407
-
408
- // drawBar3DChart provides function to draw the c:plotArea element for 3D bar
409
- // chart by given format sets.
410
- func (f * File ) drawBar3DChart (formatSet * formatChart ) * cPlotArea {
411
- return & cPlotArea {
412
- Bar3DChart : & cCharts {
413
- BarDir : & attrValString {
414
- Val : "col" ,
415
- },
416
- Grouping : & attrValString {
417
- Val : "clustered" ,
418
- },
419
- VaryColors : & attrValBool {
420
- Val : true ,
421
- },
422
- Ser : f .drawChartSeries (formatSet ),
423
- DLbls : f .drawChartDLbls (formatSet ),
424
- AxID : []* attrValInt {
425
- {Val : 754001152 },
426
- {Val : 753999904 },
427
- },
392
+ charts := map [string ]* cPlotArea {
393
+ "bar" : & cPlotArea {
394
+ BarChart : & c ,
395
+ CatAx : f .drawPlotAreaCatAx (),
396
+ ValAx : f .drawPlotAreaValAx (),
397
+ },
398
+ "bar3D" : & cPlotArea {
399
+ Bar3DChart : & c ,
400
+ CatAx : f .drawPlotAreaCatAx (),
401
+ ValAx : f .drawPlotAreaValAx (),
428
402
},
429
- CatAx : f .drawPlotAreaCatAx (),
430
- ValAx : f .drawPlotAreaValAx (),
431
403
}
404
+ return charts [formatSet .Type ]
432
405
}
433
406
434
407
// drawDoughnutChart provides function to draw the c:plotArea element for
@@ -711,15 +684,7 @@ func (f *File) drawChartDLbls(formatSet *formatChart) *cDLbls {
711
684
// drawChartSeriesDLbls provides function to draw the c:dLbls element by given
712
685
// format sets.
713
686
func (f * File ) drawChartSeriesDLbls (formatSet * formatChart ) * cDLbls {
714
- dLbls := & cDLbls {
715
- ShowLegendKey : & attrValBool {Val : formatSet .Legend .ShowLegendKey },
716
- ShowVal : & attrValBool {Val : formatSet .Plotarea .ShowVal },
717
- ShowCatName : & attrValBool {Val : formatSet .Plotarea .ShowCatName },
718
- ShowSerName : & attrValBool {Val : formatSet .Plotarea .ShowSerName },
719
- ShowBubbleSize : & attrValBool {Val : formatSet .Plotarea .ShowBubbleSize },
720
- ShowPercent : & attrValBool {Val : formatSet .Plotarea .ShowPercent },
721
- ShowLeaderLines : & attrValBool {Val : formatSet .Plotarea .ShowLeaderLines },
722
- }
687
+ dLbls := f .drawChartDLbls (formatSet )
723
688
chartSeriesDLbls := map [string ]* cDLbls {Bar : dLbls , Bar3D : dLbls , Doughnut : dLbls , Line : dLbls , Pie : dLbls , Pie3D : dLbls , Radar : dLbls , Scatter : nil }
724
689
return chartSeriesDLbls [formatSet .Type ]
725
690
}
@@ -837,21 +802,11 @@ func (f *File) drawPlotAreaTxPr() *cTxPr {
837
802
}
838
803
}
839
804
840
- // addDrawingChart provides function to add chart graphic frame by given sheet,
841
- // drawingXML, cell, width, height, relationship index and format sets.
842
- func (f * File ) addDrawingChart (sheet , drawingXML , cell string , width , height , rID int , formatSet * formatPicture ) {
843
- cell = strings .ToUpper (cell )
844
- fromCol := string (strings .Map (letterOnlyMapF , cell ))
845
- fromRow , _ := strconv .Atoi (strings .Map (intOnlyMapF , cell ))
846
- row := fromRow - 1
847
- col := titleToNumber (fromCol )
848
- width = int (float64 (width ) * formatSet .XScale )
849
- height = int (float64 (height ) * formatSet .YScale )
850
- colStart , rowStart , _ , _ , colEnd , rowEnd , x2 , y2 := f .positionObjectPixels (sheet , col , row , formatSet .OffsetX , formatSet .OffsetY , width , height )
851
- content := xlsxWsDr {}
852
- content .A = NameSpaceDrawingML
853
- content .Xdr = NameSpaceDrawingMLSpreadSheet
854
- cNvPrID := 1
805
+ // drawingParser provides function to parse drawingXML. In order to solve the
806
+ // problem that the label structure is changed after serialization and
807
+ // deserialization, two different structures: decodeWsDr and encodeWsDr are
808
+ // defined.
809
+ func (f * File ) drawingParser (drawingXML string , cNvPrID int , content * xlsxWsDr ) {
855
810
_ , ok := f .XLSX [drawingXML ]
856
811
if ok { // Append Model
857
812
decodeWsDr := decodeWsDr {}
@@ -870,6 +825,24 @@ func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rI
870
825
})
871
826
}
872
827
}
828
+ }
829
+
830
+ // addDrawingChart provides function to add chart graphic frame by given sheet,
831
+ // drawingXML, cell, width, height, relationship index and format sets.
832
+ func (f * File ) addDrawingChart (sheet , drawingXML , cell string , width , height , rID int , formatSet * formatPicture ) {
833
+ cell = strings .ToUpper (cell )
834
+ fromCol := string (strings .Map (letterOnlyMapF , cell ))
835
+ fromRow , _ := strconv .Atoi (strings .Map (intOnlyMapF , cell ))
836
+ row := fromRow - 1
837
+ col := titleToNumber (fromCol )
838
+ width = int (float64 (width ) * formatSet .XScale )
839
+ height = int (float64 (height ) * formatSet .YScale )
840
+ colStart , rowStart , _ , _ , colEnd , rowEnd , x2 , y2 := f .positionObjectPixels (sheet , col , row , formatSet .OffsetX , formatSet .OffsetY , width , height )
841
+ content := xlsxWsDr {}
842
+ content .A = NameSpaceDrawingML
843
+ content .Xdr = NameSpaceDrawingMLSpreadSheet
844
+ cNvPrID := 1
845
+ f .drawingParser (drawingXML , cNvPrID , & content )
873
846
twoCellAnchor := xdrCellAnchor {}
874
847
twoCellAnchor .EditAs = "oneCell"
875
848
from := xlsxFrom {}
0 commit comments