drawing.go 46.3 KB
Newer Older
1
// Copyright 2016 - 2024 The excelize Authors. All rights reserved. Use of
xurime's avatar
xurime 已提交
2 3 4
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
5 6 7 8 9
// Package excelize providing a set of functions that allow you to write to and
// read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
// Supports complex components by high compatibility, and provided streaming
// API for generating or reading data from a worksheet with huge amounts of
10
// data. This library needs Go version 1.18 or later.
xurime's avatar
xurime 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24

package excelize

import (
	"bytes"
	"encoding/xml"
	"io"
	"reflect"
	"strconv"
	"strings"
)

// prepareDrawing provides a function to prepare drawing ID and XML by given
// drawingID, worksheet name and default drawingXML.
25
func (f *File) prepareDrawing(ws *xlsxWorksheet, drawingID int, sheet, drawingXML string) (int, string) {
xurime's avatar
xurime 已提交
26
	sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
27
	if ws.Drawing != nil {
28 29 30
		// The worksheet already has a picture or chart relationships, use the
		// relationships drawing ../drawings/drawing%d.xml or /xl/drawings/drawing%d.xml.
		sheetRelationshipsDrawingXML = strings.ReplaceAll(f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID), "/xl/drawings/", "../drawings/")
xurime's avatar
xurime 已提交
31
		drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml"))
32
		drawingXML = strings.ReplaceAll(sheetRelationshipsDrawingXML, "..", "xl")
xurime's avatar
xurime 已提交
33 34
	} else {
		// Add first picture for given sheet.
35 36
		sheetXMLPath, _ := f.getSheetXMLPath(sheet)
		sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(sheetXMLPath, "xl/worksheets/") + ".rels"
xurime's avatar
xurime 已提交
37 38 39 40 41 42
		rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
		f.addSheetDrawing(sheet, rID)
	}
	return drawingID, drawingXML
}

43 44
// prepareChartSheetDrawing provides a function to prepare drawing ID and XML
// by given drawingID, worksheet name and default drawingXML.
45
func (f *File) prepareChartSheetDrawing(cs *xlsxChartsheet, drawingID int, sheet string) {
46
	sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
47
	// Only allow one chart in a chartsheet.
48 49
	sheetXMLPath, _ := f.getSheetXMLPath(sheet)
	sheetRels := "xl/chartsheets/_rels/" + strings.TrimPrefix(sheetXMLPath, "xl/chartsheets/") + ".rels"
50
	rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
51
	f.addSheetNameSpace(sheet, SourceRelationship)
52
	cs.Drawing = &xlsxDrawing{
53
		RID: "rId" + strconv.Itoa(rID),
54 55 56
	}
}

xurime's avatar
xurime 已提交
57 58
// addChart provides a function to create chart as xl/charts/chart%d.xml by
// given format sets.
59
func (f *File) addChart(opts *Chart, comboCharts []*Chart) {
xurime's avatar
xurime 已提交
60 61
	count := f.countCharts()
	xlsxChartSpace := xlsxChartSpace{
62
		XMLNSa:         NameSpaceDrawingML.Value,
xurime's avatar
xurime 已提交
63 64 65 66
		Date1904:       &attrValBool{Val: boolPtr(false)},
		Lang:           &attrValString{Val: stringPtr("en-US")},
		RoundedCorners: &attrValBool{Val: boolPtr(false)},
		Chart: cChart{
67
			Title: f.drawPlotAreaTitles(opts.Title, ""),
xurime's avatar
xurime 已提交
68
			View3D: &cView3D{
69 70 71 72
				RotX:        &attrValInt{Val: intPtr(chartView3DRotX[opts.Type])},
				RotY:        &attrValInt{Val: intPtr(chartView3DRotY[opts.Type])},
				Perspective: &attrValInt{Val: intPtr(chartView3DPerspective[opts.Type])},
				RAngAx:      &attrValInt{Val: intPtr(chartView3DRAngAx[opts.Type])},
xurime's avatar
xurime 已提交
73 74 75 76 77 78 79 80 81 82 83 84
			},
			Floor: &cThicknessSpPr{
				Thickness: &attrValInt{Val: intPtr(0)},
			},
			SideWall: &cThicknessSpPr{
				Thickness: &attrValInt{Val: intPtr(0)},
			},
			BackWall: &cThicknessSpPr{
				Thickness: &attrValInt{Val: intPtr(0)},
			},
			PlotArea: &cPlotArea{},
			Legend: &cLegend{
xurime's avatar
xurime 已提交
85
				LegendPos: &attrValString{Val: stringPtr(chartLegendPosition[opts.Legend.Position])},
xurime's avatar
xurime 已提交
86 87 88 89
				Overlay:   &attrValBool{Val: boolPtr(false)},
			},

			PlotVisOnly:      &attrValBool{Val: boolPtr(false)},
90
			DispBlanksAs:     &attrValString{Val: stringPtr(opts.ShowBlanksAs)},
xurime's avatar
xurime 已提交
91 92 93 94 95 96
			ShowDLblsOverMax: &attrValBool{Val: boolPtr(false)},
		},
		SpPr: &cSpPr{
			SolidFill: &aSolidFill{
				SchemeClr: &aSchemeClr{Val: "bg1"},
			},
xurime's avatar
xurime 已提交
97
			Ln: f.drawChartLn(&opts.Border),
xurime's avatar
xurime 已提交
98 99 100 101 102 103 104 105 106 107 108 109
		},
		PrintSettings: &cPrintSettings{
			PageMargins: &cPageMargins{
				B:      0.75,
				L:      0.7,
				R:      0.7,
				T:      0.7,
				Header: 0.3,
				Footer: 0.3,
			},
		},
	}
110
	xlsxChartSpace.SpPr = f.drawShapeFill(opts.Fill, xlsxChartSpace.SpPr)
111
	plotAreaFunc := map[ChartType]func(*Chart) *cPlotArea{
xurime's avatar
xurime 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
		Area:                        f.drawBaseChart,
		AreaStacked:                 f.drawBaseChart,
		AreaPercentStacked:          f.drawBaseChart,
		Area3D:                      f.drawBaseChart,
		Area3DStacked:               f.drawBaseChart,
		Area3DPercentStacked:        f.drawBaseChart,
		Bar:                         f.drawBaseChart,
		BarStacked:                  f.drawBaseChart,
		BarPercentStacked:           f.drawBaseChart,
		Bar3DClustered:              f.drawBaseChart,
		Bar3DStacked:                f.drawBaseChart,
		Bar3DPercentStacked:         f.drawBaseChart,
		Bar3DConeClustered:          f.drawBaseChart,
		Bar3DConeStacked:            f.drawBaseChart,
		Bar3DConePercentStacked:     f.drawBaseChart,
		Bar3DPyramidClustered:       f.drawBaseChart,
		Bar3DPyramidStacked:         f.drawBaseChart,
		Bar3DPyramidPercentStacked:  f.drawBaseChart,
		Bar3DCylinderClustered:      f.drawBaseChart,
		Bar3DCylinderStacked:        f.drawBaseChart,
		Bar3DCylinderPercentStacked: f.drawBaseChart,
		Col:                         f.drawBaseChart,
		ColStacked:                  f.drawBaseChart,
		ColPercentStacked:           f.drawBaseChart,
		Col3D:                       f.drawBaseChart,
		Col3DClustered:              f.drawBaseChart,
		Col3DStacked:                f.drawBaseChart,
		Col3DPercentStacked:         f.drawBaseChart,
		Col3DCone:                   f.drawBaseChart,
		Col3DConeClustered:          f.drawBaseChart,
		Col3DConeStacked:            f.drawBaseChart,
		Col3DConePercentStacked:     f.drawBaseChart,
		Col3DPyramid:                f.drawBaseChart,
		Col3DPyramidClustered:       f.drawBaseChart,
		Col3DPyramidStacked:         f.drawBaseChart,
		Col3DPyramidPercentStacked:  f.drawBaseChart,
		Col3DCylinder:               f.drawBaseChart,
		Col3DCylinderClustered:      f.drawBaseChart,
		Col3DCylinderStacked:        f.drawBaseChart,
		Col3DCylinderPercentStacked: f.drawBaseChart,
		Doughnut:                    f.drawDoughnutChart,
		Line:                        f.drawLineChart,
154
		Line3D:                      f.drawLine3DChart,
xurime's avatar
xurime 已提交
155
		Pie:                         f.drawPieChart,
156
		Pie3D:                       f.drawPie3DChart,
157 158
		PieOfPie:                    f.drawPieOfPieChart,
		BarOfPie:                    f.drawBarOfPieChart,
xurime's avatar
xurime 已提交
159 160 161 162 163 164
		Radar:                       f.drawRadarChart,
		Scatter:                     f.drawScatterChart,
		Surface3D:                   f.drawSurface3DChart,
		WireframeSurface3D:          f.drawSurface3DChart,
		Contour:                     f.drawSurfaceChart,
		WireframeContour:            f.drawSurfaceChart,
165 166
		Bubble:                      f.drawBubbleChart,
		Bubble3D:                    f.drawBubbleChart,
xurime's avatar
xurime 已提交
167
	}
xurime's avatar
xurime 已提交
168
	if opts.Legend.Position == "none" {
169 170
		xlsxChartSpace.Chart.Legend = nil
	}
171
	xlsxChartSpace.Chart.PlotArea.SpPr = f.drawShapeFill(opts.PlotArea.Fill, xlsxChartSpace.Chart.PlotArea.SpPr)
xurime's avatar
xurime 已提交
172 173 174 175 176 177 178 179 180 181
	addChart := func(c, p *cPlotArea) {
		immutable, mutable := reflect.ValueOf(c).Elem(), reflect.ValueOf(p).Elem()
		for i := 0; i < mutable.NumField(); i++ {
			field := mutable.Field(i)
			if field.IsNil() {
				continue
			}
			immutable.FieldByName(mutable.Type().Field(i).Name).Set(field)
		}
	}
182 183
	addChart(xlsxChartSpace.Chart.PlotArea, plotAreaFunc[opts.Type](opts))
	order := len(opts.Series)
xurime's avatar
xurime 已提交
184 185 186 187 188 189 190 191 192 193 194 195
	for idx := range comboCharts {
		comboCharts[idx].order = order
		addChart(xlsxChartSpace.Chart.PlotArea, plotAreaFunc[comboCharts[idx].Type](comboCharts[idx]))
		order += len(comboCharts[idx].Series)
	}
	chart, _ := xml.Marshal(xlsxChartSpace)
	media := "xl/charts/chart" + strconv.Itoa(count+1) + ".xml"
	f.saveFileList(media, chart)
}

// drawBaseChart provides a function to draw the c:plotArea element for bar,
// and column series charts by given format sets.
196
func (f *File) drawBaseChart(opts *Chart) *cPlotArea {
xurime's avatar
xurime 已提交
197 198 199 200 201
	c := cCharts{
		BarDir: &attrValString{
			Val: stringPtr("col"),
		},
		Grouping: &attrValString{
202
			Val: stringPtr(plotAreaChartGrouping[opts.Type]),
xurime's avatar
xurime 已提交
203 204
		},
		VaryColors: &attrValBool{
205
			Val: opts.VaryColors,
xurime's avatar
xurime 已提交
206
		},
207 208 209 210
		Ser:     f.drawChartSeries(opts),
		Shape:   f.drawChartShape(opts),
		DLbls:   f.drawChartDLbls(opts),
		AxID:    f.genAxID(opts),
xurime's avatar
xurime 已提交
211 212 213
		Overlap: &attrValInt{Val: intPtr(100)},
	}
	var ok bool
214
	if *c.BarDir.Val, ok = plotAreaChartBarDir[opts.Type]; !ok {
xurime's avatar
xurime 已提交
215 216
		c.BarDir = nil
	}
217
	if *c.Overlap.Val, ok = plotAreaChartOverlap[opts.Type]; !ok {
xurime's avatar
xurime 已提交
218 219
		c.Overlap = nil
	}
220 221
	catAx := f.drawPlotAreaCatAx(opts)
	valAx := f.drawPlotAreaValAx(opts)
222 223
	charts := map[ChartType]*cPlotArea{
		Area: {
xurime's avatar
xurime 已提交
224 225 226 227
			AreaChart: &c,
			CatAx:     catAx,
			ValAx:     valAx,
		},
228
		AreaStacked: {
xurime's avatar
xurime 已提交
229 230 231 232
			AreaChart: &c,
			CatAx:     catAx,
			ValAx:     valAx,
		},
233
		AreaPercentStacked: {
xurime's avatar
xurime 已提交
234 235 236 237
			AreaChart: &c,
			CatAx:     catAx,
			ValAx:     valAx,
		},
238
		Area3D: {
xurime's avatar
xurime 已提交
239 240 241 242
			Area3DChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
243
		Area3DStacked: {
xurime's avatar
xurime 已提交
244 245 246 247
			Area3DChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
248
		Area3DPercentStacked: {
xurime's avatar
xurime 已提交
249 250 251 252
			Area3DChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
253
		Bar: {
xurime's avatar
xurime 已提交
254 255 256 257
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
258
		BarStacked: {
xurime's avatar
xurime 已提交
259 260 261 262
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
263
		BarPercentStacked: {
xurime's avatar
xurime 已提交
264 265 266 267
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
268
		Bar3DClustered: {
xurime's avatar
xurime 已提交
269 270 271 272
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
273
		Bar3DStacked: {
xurime's avatar
xurime 已提交
274 275 276 277
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
278
		Bar3DPercentStacked: {
xurime's avatar
xurime 已提交
279 280 281 282
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
283
		Bar3DConeClustered: {
xurime's avatar
xurime 已提交
284 285 286 287
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
288
		Bar3DConeStacked: {
xurime's avatar
xurime 已提交
289 290 291 292
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
293
		Bar3DConePercentStacked: {
xurime's avatar
xurime 已提交
294 295 296 297
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
298
		Bar3DPyramidClustered: {
xurime's avatar
xurime 已提交
299 300 301 302
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
303
		Bar3DPyramidStacked: {
xurime's avatar
xurime 已提交
304 305 306 307
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
308
		Bar3DPyramidPercentStacked: {
xurime's avatar
xurime 已提交
309 310 311 312
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
313
		Bar3DCylinderClustered: {
xurime's avatar
xurime 已提交
314 315 316 317
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
318
		Bar3DCylinderStacked: {
xurime's avatar
xurime 已提交
319 320 321 322
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
323
		Bar3DCylinderPercentStacked: {
xurime's avatar
xurime 已提交
324 325 326 327
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
328
		Col: {
xurime's avatar
xurime 已提交
329 330 331 332
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
333
		ColStacked: {
xurime's avatar
xurime 已提交
334 335 336 337
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
338
		ColPercentStacked: {
xurime's avatar
xurime 已提交
339 340 341 342
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
343
		Col3D: {
xurime's avatar
xurime 已提交
344 345 346 347
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
348
		Col3DClustered: {
xurime's avatar
xurime 已提交
349 350 351 352
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
353
		Col3DStacked: {
xurime's avatar
xurime 已提交
354 355 356 357
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
358
		Col3DPercentStacked: {
xurime's avatar
xurime 已提交
359 360 361 362
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
363
		Col3DCone: {
xurime's avatar
xurime 已提交
364 365 366 367
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
368
		Col3DConeClustered: {
xurime's avatar
xurime 已提交
369 370 371 372
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
373
		Col3DConeStacked: {
xurime's avatar
xurime 已提交
374 375 376 377
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
378
		Col3DConePercentStacked: {
xurime's avatar
xurime 已提交
379 380 381 382
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
383
		Col3DPyramid: {
xurime's avatar
xurime 已提交
384 385 386 387
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
388
		Col3DPyramidClustered: {
xurime's avatar
xurime 已提交
389 390 391 392
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
393
		Col3DPyramidStacked: {
xurime's avatar
xurime 已提交
394 395 396 397
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
398
		Col3DPyramidPercentStacked: {
xurime's avatar
xurime 已提交
399 400 401 402
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
403
		Col3DCylinder: {
xurime's avatar
xurime 已提交
404 405 406 407
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
408
		Col3DCylinderClustered: {
xurime's avatar
xurime 已提交
409 410 411 412
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
413
		Col3DCylinderStacked: {
xurime's avatar
xurime 已提交
414 415 416 417
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
418
		Col3DCylinderPercentStacked: {
xurime's avatar
xurime 已提交
419 420 421 422
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
423
		Bubble: {
xurime's avatar
xurime 已提交
424 425 426 427
			BubbleChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
428
		Bubble3D: {
xurime's avatar
xurime 已提交
429 430 431 432 433
			BubbleChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
	}
434
	return charts[opts.Type]
xurime's avatar
xurime 已提交
435 436 437 438
}

// drawDoughnutChart provides a function to draw the c:plotArea element for
// doughnut chart by given format sets.
439
func (f *File) drawDoughnutChart(opts *Chart) *cPlotArea {
440
	holeSize := 75
441 442
	if opts.HoleSize > 0 && opts.HoleSize <= 90 {
		holeSize = opts.HoleSize
443 444
	}

xurime's avatar
xurime 已提交
445 446 447
	return &cPlotArea{
		DoughnutChart: &cCharts{
			VaryColors: &attrValBool{
448
				Val: opts.VaryColors,
xurime's avatar
xurime 已提交
449
			},
450
			Ser:      f.drawChartSeries(opts),
451
			HoleSize: &attrValInt{Val: intPtr(holeSize)},
xurime's avatar
xurime 已提交
452 453 454 455 456 457
		},
	}
}

// drawLineChart provides a function to draw the c:plotArea element for line
// chart by given format sets.
458
func (f *File) drawLineChart(opts *Chart) *cPlotArea {
xurime's avatar
xurime 已提交
459 460 461
	return &cPlotArea{
		LineChart: &cCharts{
			Grouping: &attrValString{
462
				Val: stringPtr(plotAreaChartGrouping[opts.Type]),
xurime's avatar
xurime 已提交
463 464 465 466
			},
			VaryColors: &attrValBool{
				Val: boolPtr(false),
			},
467 468
			Ser:   f.drawChartSeries(opts),
			DLbls: f.drawChartDLbls(opts),
469
			AxID:  f.genAxID(opts),
xurime's avatar
xurime 已提交
470
		},
471 472
		CatAx: f.drawPlotAreaCatAx(opts),
		ValAx: f.drawPlotAreaValAx(opts),
xurime's avatar
xurime 已提交
473 474 475
	}
}

476 477
// drawLine3DChart provides a function to draw the c:plotArea element for line
// chart by given format sets.
478
func (f *File) drawLine3DChart(opts *Chart) *cPlotArea {
479 480 481 482 483 484 485 486 487 488
	return &cPlotArea{
		Line3DChart: &cCharts{
			Grouping: &attrValString{
				Val: stringPtr(plotAreaChartGrouping[opts.Type]),
			},
			VaryColors: &attrValBool{
				Val: boolPtr(false),
			},
			Ser:   f.drawChartSeries(opts),
			DLbls: f.drawChartDLbls(opts),
489
			AxID:  f.genAxID(opts),
490 491 492 493 494 495
		},
		CatAx: f.drawPlotAreaCatAx(opts),
		ValAx: f.drawPlotAreaValAx(opts),
	}
}

xurime's avatar
xurime 已提交
496 497
// drawPieChart provides a function to draw the c:plotArea element for pie
// chart by given format sets.
498
func (f *File) drawPieChart(opts *Chart) *cPlotArea {
xurime's avatar
xurime 已提交
499 500 501
	return &cPlotArea{
		PieChart: &cCharts{
			VaryColors: &attrValBool{
502
				Val: opts.VaryColors,
xurime's avatar
xurime 已提交
503
			},
504
			Ser: f.drawChartSeries(opts),
xurime's avatar
xurime 已提交
505 506 507 508 509 510
		},
	}
}

// drawPie3DChart provides a function to draw the c:plotArea element for 3D
// pie chart by given format sets.
511
func (f *File) drawPie3DChart(opts *Chart) *cPlotArea {
xurime's avatar
xurime 已提交
512 513 514
	return &cPlotArea{
		Pie3DChart: &cCharts{
			VaryColors: &attrValBool{
515
				Val: opts.VaryColors,
xurime's avatar
xurime 已提交
516
			},
517
			Ser: f.drawChartSeries(opts),
xurime's avatar
xurime 已提交
518 519 520 521 522 523
		},
	}
}

// drawPieOfPieChart provides a function to draw the c:plotArea element for
// pie chart by given format sets.
524
func (f *File) drawPieOfPieChart(opts *Chart) *cPlotArea {
525 526 527 528
	var splitPos *attrValInt
	if opts.PlotArea.SecondPlotValues > 0 {
		splitPos = &attrValInt{Val: intPtr(opts.PlotArea.SecondPlotValues)}
	}
xurime's avatar
xurime 已提交
529
	return &cPlotArea{
530
		OfPieChart: &cCharts{
xurime's avatar
xurime 已提交
531 532 533 534
			OfPieType: &attrValString{
				Val: stringPtr("pie"),
			},
			VaryColors: &attrValBool{
535
				Val: opts.VaryColors,
xurime's avatar
xurime 已提交
536
			},
537
			Ser:      f.drawChartSeries(opts),
538
			SplitPos: splitPos,
xurime's avatar
xurime 已提交
539 540 541 542 543 544 545
			SerLines: &attrValString{},
		},
	}
}

// drawBarOfPieChart provides a function to draw the c:plotArea element for
// pie chart by given format sets.
546
func (f *File) drawBarOfPieChart(opts *Chart) *cPlotArea {
547 548 549 550
	var splitPos *attrValInt
	if opts.PlotArea.SecondPlotValues > 0 {
		splitPos = &attrValInt{Val: intPtr(opts.PlotArea.SecondPlotValues)}
	}
xurime's avatar
xurime 已提交
551
	return &cPlotArea{
552
		OfPieChart: &cCharts{
xurime's avatar
xurime 已提交
553 554 555 556
			OfPieType: &attrValString{
				Val: stringPtr("bar"),
			},
			VaryColors: &attrValBool{
557
				Val: opts.VaryColors,
xurime's avatar
xurime 已提交
558
			},
559
			SplitPos: splitPos,
560
			Ser:      f.drawChartSeries(opts),
xurime's avatar
xurime 已提交
561 562 563 564 565 566 567
			SerLines: &attrValString{},
		},
	}
}

// drawRadarChart provides a function to draw the c:plotArea element for radar
// chart by given format sets.
568
func (f *File) drawRadarChart(opts *Chart) *cPlotArea {
xurime's avatar
xurime 已提交
569 570 571 572 573 574 575 576
	return &cPlotArea{
		RadarChart: &cCharts{
			RadarStyle: &attrValString{
				Val: stringPtr("marker"),
			},
			VaryColors: &attrValBool{
				Val: boolPtr(false),
			},
577 578
			Ser:   f.drawChartSeries(opts),
			DLbls: f.drawChartDLbls(opts),
579
			AxID:  f.genAxID(opts),
xurime's avatar
xurime 已提交
580
		},
581 582
		CatAx: f.drawPlotAreaCatAx(opts),
		ValAx: f.drawPlotAreaValAx(opts),
xurime's avatar
xurime 已提交
583 584 585 586 587
	}
}

// drawScatterChart provides a function to draw the c:plotArea element for
// scatter chart by given format sets.
588
func (f *File) drawScatterChart(opts *Chart) *cPlotArea {
xurime's avatar
xurime 已提交
589 590 591 592 593 594 595 596
	return &cPlotArea{
		ScatterChart: &cCharts{
			ScatterStyle: &attrValString{
				Val: stringPtr("smoothMarker"), // line,lineMarker,marker,none,smooth,smoothMarker
			},
			VaryColors: &attrValBool{
				Val: boolPtr(false),
			},
597 598
			Ser:   f.drawChartSeries(opts),
			DLbls: f.drawChartDLbls(opts),
599
			AxID:  f.genAxID(opts),
xurime's avatar
xurime 已提交
600
		},
601 602
		CatAx: f.drawPlotAreaCatAx(opts),
		ValAx: f.drawPlotAreaValAx(opts),
xurime's avatar
xurime 已提交
603 604 605 606 607
	}
}

// drawSurface3DChart provides a function to draw the c:surface3DChart element by
// given format sets.
608
func (f *File) drawSurface3DChart(opts *Chart) *cPlotArea {
xurime's avatar
xurime 已提交
609 610
	plotArea := &cPlotArea{
		Surface3DChart: &cCharts{
611
			Ser: f.drawChartSeries(opts),
xurime's avatar
xurime 已提交
612
			AxID: []*attrValInt{
613 614 615
				{Val: intPtr(100000000)},
				{Val: intPtr(100000001)},
				{Val: intPtr(100000005)},
xurime's avatar
xurime 已提交
616 617
			},
		},
618 619 620
		CatAx: f.drawPlotAreaCatAx(opts),
		ValAx: f.drawPlotAreaValAx(opts),
		SerAx: f.drawPlotAreaSerAx(opts),
xurime's avatar
xurime 已提交
621
	}
622
	if opts.Type == WireframeSurface3D {
xurime's avatar
xurime 已提交
623 624 625 626 627 628 629
		plotArea.Surface3DChart.Wireframe = &attrValBool{Val: boolPtr(true)}
	}
	return plotArea
}

// drawSurfaceChart provides a function to draw the c:surfaceChart element by
// given format sets.
630
func (f *File) drawSurfaceChart(opts *Chart) *cPlotArea {
xurime's avatar
xurime 已提交
631 632
	plotArea := &cPlotArea{
		SurfaceChart: &cCharts{
633
			Ser: f.drawChartSeries(opts),
xurime's avatar
xurime 已提交
634
			AxID: []*attrValInt{
635 636 637
				{Val: intPtr(100000000)},
				{Val: intPtr(100000001)},
				{Val: intPtr(100000005)},
xurime's avatar
xurime 已提交
638 639
			},
		},
640 641 642
		CatAx: f.drawPlotAreaCatAx(opts),
		ValAx: f.drawPlotAreaValAx(opts),
		SerAx: f.drawPlotAreaSerAx(opts),
xurime's avatar
xurime 已提交
643
	}
644
	if opts.Type == WireframeContour {
xurime's avatar
xurime 已提交
645 646 647 648 649
		plotArea.SurfaceChart.Wireframe = &attrValBool{Val: boolPtr(true)}
	}
	return plotArea
}

650 651 652 653 654 655 656 657 658 659
// drawBubbleChart provides a function to draw the c:bubbleChart element by
// given format sets.
func (f *File) drawBubbleChart(opts *Chart) *cPlotArea {
	plotArea := &cPlotArea{
		BubbleChart: &cCharts{
			VaryColors: &attrValBool{
				Val: opts.VaryColors,
			},
			Ser:   f.drawChartSeries(opts),
			DLbls: f.drawChartDLbls(opts),
660
			AxID:  f.genAxID(opts),
661 662 663
		},
		ValAx: []*cAxs{f.drawPlotAreaCatAx(opts)[0], f.drawPlotAreaValAx(opts)[0]},
	}
664 665 666
	if opts.BubbleSize > 0 && opts.BubbleSize <= 300 {
		plotArea.BubbleChart.BubbleScale = &attrValFloat{Val: float64Ptr(float64(opts.BubbleSize))}
	}
667 668 669
	return plotArea
}

xurime's avatar
xurime 已提交
670 671
// drawChartShape provides a function to draw the c:shape element by given
// format sets.
672
func (f *File) drawChartShape(opts *Chart) *attrValString {
673
	shapes := map[ChartType]string{
xurime's avatar
xurime 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
		Bar3DConeClustered:          "cone",
		Bar3DConeStacked:            "cone",
		Bar3DConePercentStacked:     "cone",
		Bar3DPyramidClustered:       "pyramid",
		Bar3DPyramidStacked:         "pyramid",
		Bar3DPyramidPercentStacked:  "pyramid",
		Bar3DCylinderClustered:      "cylinder",
		Bar3DCylinderStacked:        "cylinder",
		Bar3DCylinderPercentStacked: "cylinder",
		Col3DCone:                   "cone",
		Col3DConeClustered:          "cone",
		Col3DConeStacked:            "cone",
		Col3DConePercentStacked:     "cone",
		Col3DPyramid:                "pyramid",
		Col3DPyramidClustered:       "pyramid",
		Col3DPyramidStacked:         "pyramid",
		Col3DPyramidPercentStacked:  "pyramid",
		Col3DCylinder:               "cylinder",
		Col3DCylinderClustered:      "cylinder",
		Col3DCylinderStacked:        "cylinder",
		Col3DCylinderPercentStacked: "cylinder",
	}
696
	if shape, ok := shapes[opts.Type]; ok {
xurime's avatar
xurime 已提交
697 698 699 700 701 702 703
		return &attrValString{Val: stringPtr(shape)}
	}
	return nil
}

// drawChartSeries provides a function to draw the c:ser element by given
// format sets.
704
func (f *File) drawChartSeries(opts *Chart) *[]cSer {
705
	var ser []cSer
706
	for k := range opts.Series {
xurime's avatar
xurime 已提交
707
		ser = append(ser, cSer{
708 709
			IDx:   &attrValInt{Val: intPtr(k + opts.order)},
			Order: &attrValInt{Val: intPtr(k + opts.order)},
xurime's avatar
xurime 已提交
710 711
			Tx: &cTx{
				StrRef: &cStrRef{
712
					F: opts.Series[k].Name,
xurime's avatar
xurime 已提交
713 714
				},
			},
715 716 717
			SpPr:             f.drawChartSeriesSpPr(k, opts),
			Marker:           f.drawChartSeriesMarker(k, opts),
			DPt:              f.drawChartSeriesDPt(k, opts),
718
			DLbls:            f.drawChartSeriesDLbls(k, opts),
719
			InvertIfNegative: &attrValBool{Val: boolPtr(false)},
720 721 722 723 724 725 726
			Cat:              f.drawChartSeriesCat(opts.Series[k], opts),
			Smooth:           &attrValBool{Val: boolPtr(opts.Series[k].Line.Smooth)},
			Val:              f.drawChartSeriesVal(opts.Series[k], opts),
			XVal:             f.drawChartSeriesXVal(opts.Series[k], opts),
			YVal:             f.drawChartSeriesYVal(opts.Series[k], opts),
			BubbleSize:       f.drawCharSeriesBubbleSize(opts.Series[k], opts),
			Bubble3D:         f.drawCharSeriesBubble3D(opts),
xurime's avatar
xurime 已提交
727 728 729 730 731
		})
	}
	return &ser
}

732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
// drawShapeFill provides a function to draw the a:solidFill element by given
// fill format sets.
func (f *File) drawShapeFill(fill Fill, spPr *cSpPr) *cSpPr {
	if fill.Type == "pattern" && fill.Pattern == 1 {
		if spPr == nil {
			spPr = &cSpPr{}
		}
		if len(fill.Color) == 1 {
			spPr.SolidFill = &aSolidFill{SrgbClr: &attrValString{Val: stringPtr(strings.TrimPrefix(fill.Color[0], "#"))}}
			return spPr
		}
		spPr.SolidFill = nil
		spPr.NoFill = stringPtr("")
	}
	return spPr
}

xurime's avatar
xurime 已提交
749 750
// drawChartSeriesSpPr provides a function to draw the c:spPr element by given
// format sets.
751
func (f *File) drawChartSeriesSpPr(i int, opts *Chart) *cSpPr {
752 753
	spPr := &cSpPr{SolidFill: &aSolidFill{SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa((opts.order+i)%6+1)}}}
	spPr = f.drawShapeFill(opts.Series[i].Fill, spPr)
xurime's avatar
xurime 已提交
754 755 756
	spPrScatter := &cSpPr{
		Ln: &aLn{
			W:      25400,
xurime's avatar
xurime 已提交
757
			NoFill: &attrValString{},
xurime's avatar
xurime 已提交
758 759 760 761
		},
	}
	spPrLine := &cSpPr{
		Ln: &aLn{
762 763 764
			W:         f.ptToEMUs(opts.Series[i].Line.Width),
			Cap:       "rnd", // rnd, sq, flat
			SolidFill: spPr.SolidFill,
xurime's avatar
xurime 已提交
765 766
		},
	}
767
	if chartSeriesSpPr, ok := map[ChartType]*cSpPr{
768 769 770 771
		Line: spPrLine, Scatter: spPrScatter,
	}[opts.Type]; ok {
		return chartSeriesSpPr
	}
772
	if spPr.SolidFill.SrgbClr != nil {
773 774 775
		return spPr
	}
	return nil
xurime's avatar
xurime 已提交
776 777 778 779
}

// drawChartSeriesDPt provides a function to draw the c:dPt element by given
// data index and format sets.
780
func (f *File) drawChartSeriesDPt(i int, opts *Chart) []*cDPt {
xurime's avatar
xurime 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
	dpt := []*cDPt{{
		IDx:      &attrValInt{Val: intPtr(i)},
		Bubble3D: &attrValBool{Val: boolPtr(false)},
		SpPr: &cSpPr{
			SolidFill: &aSolidFill{
				SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
			},
			Ln: &aLn{
				W:   25400,
				Cap: "rnd",
				SolidFill: &aSolidFill{
					SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
				},
			},
			Sp3D: &aSp3D{
				ContourW: 25400,
				ContourClr: &aContourClr{
					SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
				},
			},
		},
	}}
803
	chartSeriesDPt := map[ChartType][]*cDPt{Pie: dpt, Pie3D: dpt}
804
	return chartSeriesDPt[opts.Type]
xurime's avatar
xurime 已提交
805 806 807 808
}

// drawChartSeriesCat provides a function to draw the c:cat element by given
// chart series and format sets.
809
func (f *File) drawChartSeriesCat(v ChartSeries, opts *Chart) *cCat {
xurime's avatar
xurime 已提交
810 811 812 813 814
	cat := &cCat{
		StrRef: &cStrRef{
			F: v.Categories,
		},
	}
815
	chartSeriesCat := map[ChartType]*cCat{Scatter: nil, Bubble: nil, Bubble3D: nil}
816
	if _, ok := chartSeriesCat[opts.Type]; ok || v.Categories == "" {
xurime's avatar
xurime 已提交
817 818 819 820 821 822 823
		return nil
	}
	return cat
}

// drawChartSeriesVal provides a function to draw the c:val element by given
// chart series and format sets.
824
func (f *File) drawChartSeriesVal(v ChartSeries, opts *Chart) *cVal {
xurime's avatar
xurime 已提交
825 826 827 828 829
	val := &cVal{
		NumRef: &cNumRef{
			F: v.Values,
		},
	}
830
	chartSeriesVal := map[ChartType]*cVal{Scatter: nil, Bubble: nil, Bubble3D: nil}
831
	if _, ok := chartSeriesVal[opts.Type]; ok {
xurime's avatar
xurime 已提交
832 833 834 835 836 837 838
		return nil
	}
	return val
}

// drawChartSeriesMarker provides a function to draw the c:marker element by
// given data index and format sets.
839
func (f *File) drawChartSeriesMarker(i int, opts *Chart) *cMarker {
840
	defaultSymbol := map[ChartType]*attrValString{Scatter: {Val: stringPtr("circle")}}
xurime's avatar
xurime 已提交
841
	marker := &cMarker{
842
		Symbol: defaultSymbol[opts.Type],
xurime's avatar
xurime 已提交
843 844
		Size:   &attrValInt{Val: intPtr(5)},
	}
845
	if symbol := stringPtr(opts.Series[i].Marker.Symbol); *symbol != "" {
846 847
		marker.Symbol = &attrValString{Val: symbol}
	}
848
	if size := intPtr(opts.Series[i].Marker.Size); *size != 0 {
849 850
		marker.Size = &attrValInt{Val: size}
	}
xurime's avatar
xurime 已提交
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
	if i < 6 {
		marker.SpPr = &cSpPr{
			SolidFill: &aSolidFill{
				SchemeClr: &aSchemeClr{
					Val: "accent" + strconv.Itoa(i+1),
				},
			},
			Ln: &aLn{
				W: 9252,
				SolidFill: &aSolidFill{
					SchemeClr: &aSchemeClr{
						Val: "accent" + strconv.Itoa(i+1),
					},
				},
			},
		}
	}
868
	marker.SpPr = f.drawShapeFill(opts.Series[i].Marker.Fill, marker.SpPr)
869
	chartSeriesMarker := map[ChartType]*cMarker{Scatter: marker, Line: marker}
870
	return chartSeriesMarker[opts.Type]
xurime's avatar
xurime 已提交
871 872 873 874
}

// drawChartSeriesXVal provides a function to draw the c:xVal element by given
// chart series and format sets.
875
func (f *File) drawChartSeriesXVal(v ChartSeries, opts *Chart) *cCat {
xurime's avatar
xurime 已提交
876 877 878 879 880
	cat := &cCat{
		StrRef: &cStrRef{
			F: v.Categories,
		},
	}
881
	chartSeriesXVal := map[ChartType]*cCat{Scatter: cat, Bubble: cat, Bubble3D: cat}
882
	return chartSeriesXVal[opts.Type]
xurime's avatar
xurime 已提交
883 884 885 886
}

// drawChartSeriesYVal provides a function to draw the c:yVal element by given
// chart series and format sets.
887
func (f *File) drawChartSeriesYVal(v ChartSeries, opts *Chart) *cVal {
xurime's avatar
xurime 已提交
888 889 890 891 892
	val := &cVal{
		NumRef: &cNumRef{
			F: v.Values,
		},
	}
893
	chartSeriesYVal := map[ChartType]*cVal{Scatter: val, Bubble: val, Bubble3D: val}
894
	return chartSeriesYVal[opts.Type]
xurime's avatar
xurime 已提交
895 896 897 898
}

// drawCharSeriesBubbleSize provides a function to draw the c:bubbleSize
// element by given chart series and format sets.
899
func (f *File) drawCharSeriesBubbleSize(v ChartSeries, opts *Chart) *cVal {
900
	if _, ok := map[ChartType]bool{Bubble: true, Bubble3D: true}[opts.Type]; !ok {
xurime's avatar
xurime 已提交
901 902
		return nil
	}
903 904 905 906
	fVal := v.Values
	if v.Sizes != "" {
		fVal = v.Sizes
	}
xurime's avatar
xurime 已提交
907 908
	return &cVal{
		NumRef: &cNumRef{
909
			F: fVal,
xurime's avatar
xurime 已提交
910 911 912 913 914 915
		},
	}
}

// drawCharSeriesBubble3D provides a function to draw the c:bubble3D element
// by given format sets.
916
func (f *File) drawCharSeriesBubble3D(opts *Chart) *attrValBool {
917
	if _, ok := map[ChartType]bool{Bubble3D: true}[opts.Type]; !ok {
xurime's avatar
xurime 已提交
918 919 920 921 922
		return nil
	}
	return &attrValBool{Val: boolPtr(true)}
}

923 924 925 926 927 928 929 930 931 932 933 934 935
// drawChartNumFmt provides a function to draw the c:numFmt element by given
// data labels format sets.
func (f *File) drawChartNumFmt(labels ChartNumFmt) *cNumFmt {
	var numFmt *cNumFmt
	if labels.CustomNumFmt != "" || labels.SourceLinked {
		numFmt = &cNumFmt{
			FormatCode:   labels.CustomNumFmt,
			SourceLinked: labels.SourceLinked,
		}
	}
	return numFmt
}

xurime's avatar
xurime 已提交
936 937
// drawChartDLbls provides a function to draw the c:dLbls element by given
// format sets.
938
func (f *File) drawChartDLbls(opts *Chart) *cDLbls {
xurime's avatar
xurime 已提交
939
	return &cDLbls{
940
		NumFmt:          f.drawChartNumFmt(opts.PlotArea.NumFmt),
941
		ShowLegendKey:   &attrValBool{Val: boolPtr(opts.Legend.ShowLegendKey)},
942 943 944 945 946 947
		ShowVal:         &attrValBool{Val: boolPtr(opts.PlotArea.ShowVal)},
		ShowCatName:     &attrValBool{Val: boolPtr(opts.PlotArea.ShowCatName)},
		ShowSerName:     &attrValBool{Val: boolPtr(opts.PlotArea.ShowSerName)},
		ShowBubbleSize:  &attrValBool{Val: boolPtr(opts.PlotArea.ShowBubbleSize)},
		ShowPercent:     &attrValBool{Val: boolPtr(opts.PlotArea.ShowPercent)},
		ShowLeaderLines: &attrValBool{Val: boolPtr(opts.PlotArea.ShowLeaderLines)},
xurime's avatar
xurime 已提交
948 949 950
	}
}

951 952 953 954 955 956 957 958 959 960 961 962
// inSupportedChartDataLabelsPositionType provides a method to check if an
// element is present in an array, and return the index of its location,
// otherwise return -1.
func inSupportedChartDataLabelsPositionType(a []ChartDataLabelPositionType, x ChartDataLabelPositionType) int {
	for idx, n := range a {
		if x == n {
			return idx
		}
	}
	return -1
}

xurime's avatar
xurime 已提交
963 964
// drawChartSeriesDLbls provides a function to draw the c:dLbls element by
// given format sets.
965
func (f *File) drawChartSeriesDLbls(i int, opts *Chart) *cDLbls {
966
	dLbls := f.drawChartDLbls(opts)
967
	chartSeriesDLbls := map[ChartType]*cDLbls{
968
		Scatter: nil, Surface3D: nil, WireframeSurface3D: nil, Contour: nil, WireframeContour: nil,
969
	}
970
	if _, ok := chartSeriesDLbls[opts.Type]; ok {
xurime's avatar
xurime 已提交
971 972
		return nil
	}
973 974 975 976 977
	if types, ok := supportedChartDataLabelsPosition[opts.Type]; ok && opts.Series[i].DataLabelPosition != ChartDataLabelsPositionUnset {
		if inSupportedChartDataLabelsPositionType(types, opts.Series[i].DataLabelPosition) != -1 {
			dLbls.DLblPos = &attrValString{Val: stringPtr(chartDataLabelsPositionTypes[opts.Series[i].DataLabelPosition])}
		}
	}
xurime's avatar
xurime 已提交
978 979 980 981
	return dLbls
}

// drawPlotAreaCatAx provides a function to draw the c:catAx element.
982
func (f *File) drawPlotAreaCatAx(opts *Chart) []*cAxs {
983 984
	maxVal := &attrValFloat{Val: opts.XAxis.Maximum}
	minVal := &attrValFloat{Val: opts.XAxis.Minimum}
985
	if opts.XAxis.Maximum == nil {
986
		maxVal = nil
xurime's avatar
xurime 已提交
987
	}
988
	if opts.XAxis.Minimum == nil {
989
		minVal = nil
990
	}
xurime's avatar
xurime 已提交
991 992
	axs := []*cAxs{
		{
993
			AxID: &attrValInt{Val: intPtr(100000000)},
xurime's avatar
xurime 已提交
994
			Scaling: &cScaling{
995
				Orientation: &attrValString{Val: stringPtr(orientation[opts.XAxis.ReverseOrder])},
996 997
				Max:         maxVal,
				Min:         minVal,
xurime's avatar
xurime 已提交
998
			},
999 1000 1001
			Delete:        &attrValBool{Val: boolPtr(opts.XAxis.None)},
			AxPos:         &attrValString{Val: stringPtr(catAxPos[opts.XAxis.ReverseOrder])},
			NumFmt:        &cNumFmt{FormatCode: "General"},
xurime's avatar
xurime 已提交
1002 1003
			MajorTickMark: &attrValString{Val: stringPtr("none")},
			MinorTickMark: &attrValString{Val: stringPtr("none")},
1004
			Title:         f.drawPlotAreaTitles(opts.XAxis.Title, ""),
xurime's avatar
xurime 已提交
1005 1006
			TickLblPos:    &attrValString{Val: stringPtr("nextTo")},
			SpPr:          f.drawPlotAreaSpPr(),
1007
			TxPr:          f.drawPlotAreaTxPr(&opts.YAxis),
1008
			CrossAx:       &attrValInt{Val: intPtr(100000001)},
xurime's avatar
xurime 已提交
1009 1010 1011 1012 1013 1014 1015
			Crosses:       &attrValString{Val: stringPtr("autoZero")},
			Auto:          &attrValBool{Val: boolPtr(true)},
			LblAlgn:       &attrValString{Val: stringPtr("ctr")},
			LblOffset:     &attrValInt{Val: intPtr(100)},
			NoMultiLvlLbl: &attrValBool{Val: boolPtr(false)},
		},
	}
1016 1017 1018
	if numFmt := f.drawChartNumFmt(opts.XAxis.NumFmt); numFmt != nil {
		axs[0].NumFmt = numFmt
	}
1019
	if opts.XAxis.MajorGridLines {
xurime's avatar
xurime 已提交
1020 1021
		axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
	}
1022
	if opts.XAxis.MinorGridLines {
xurime's avatar
xurime 已提交
1023 1024
		axs[0].MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
	}
1025 1026
	if opts.XAxis.TickLabelSkip != 0 {
		axs[0].TickLblSkip = &attrValInt{Val: intPtr(opts.XAxis.TickLabelSkip)}
xurime's avatar
xurime 已提交
1027
	}
1028 1029 1030 1031 1032
	if opts.order > 0 && opts.YAxis.Secondary {
		axs = append(axs, &cAxs{
			AxID: &attrValInt{Val: intPtr(opts.XAxis.axID)},
			Scaling: &cScaling{
				Orientation: &attrValString{Val: stringPtr(orientation[opts.XAxis.ReverseOrder])},
1033 1034
				Max:         maxVal,
				Min:         minVal,
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
			},
			Delete:        &attrValBool{Val: boolPtr(true)},
			AxPos:         &attrValString{Val: stringPtr("b")},
			MajorTickMark: &attrValString{Val: stringPtr("none")},
			MinorTickMark: &attrValString{Val: stringPtr("none")},
			TickLblPos:    &attrValString{Val: stringPtr("nextTo")},
			SpPr:          f.drawPlotAreaSpPr(),
			TxPr:          f.drawPlotAreaTxPr(&opts.YAxis),
			CrossAx:       &attrValInt{Val: intPtr(opts.YAxis.axID)},
			Auto:          &attrValBool{Val: boolPtr(true)},
			LblAlgn:       &attrValString{Val: stringPtr("ctr")},
			LblOffset:     &attrValInt{Val: intPtr(100)},
			NoMultiLvlLbl: &attrValBool{Val: boolPtr(false)},
		})
	}
xurime's avatar
xurime 已提交
1050 1051 1052 1053
	return axs
}

// drawPlotAreaValAx provides a function to draw the c:valAx element.
1054
func (f *File) drawPlotAreaValAx(opts *Chart) []*cAxs {
1055 1056
	maxVal := &attrValFloat{Val: opts.YAxis.Maximum}
	minVal := &attrValFloat{Val: opts.YAxis.Minimum}
1057
	if opts.YAxis.Maximum == nil {
1058
		maxVal = nil
xurime's avatar
xurime 已提交
1059
	}
1060
	if opts.YAxis.Minimum == nil {
1061
		minVal = nil
1062
	}
1063
	var logBase *attrValFloat
1064 1065
	if opts.YAxis.LogBase >= 2 && opts.YAxis.LogBase <= 1000 {
		logBase = &attrValFloat{Val: float64Ptr(opts.YAxis.LogBase)}
1066
	}
xurime's avatar
xurime 已提交
1067 1068
	axs := []*cAxs{
		{
1069
			AxID: &attrValInt{Val: intPtr(100000001)},
xurime's avatar
xurime 已提交
1070
			Scaling: &cScaling{
1071
				LogBase:     logBase,
1072
				Orientation: &attrValString{Val: stringPtr(orientation[opts.YAxis.ReverseOrder])},
1073 1074
				Max:         maxVal,
				Min:         minVal,
xurime's avatar
xurime 已提交
1075
			},
1076 1077
			Delete: &attrValBool{Val: boolPtr(opts.YAxis.None)},
			AxPos:  &attrValString{Val: stringPtr(valAxPos[opts.YAxis.ReverseOrder])},
1078
			Title:  f.drawPlotAreaTitles(opts.YAxis.Title, "horz"),
xurime's avatar
xurime 已提交
1079
			NumFmt: &cNumFmt{
1080
				FormatCode: chartValAxNumFmtFormatCode[opts.Type],
xurime's avatar
xurime 已提交
1081 1082 1083 1084 1085
			},
			MajorTickMark: &attrValString{Val: stringPtr("none")},
			MinorTickMark: &attrValString{Val: stringPtr("none")},
			TickLblPos:    &attrValString{Val: stringPtr("nextTo")},
			SpPr:          f.drawPlotAreaSpPr(),
1086
			TxPr:          f.drawPlotAreaTxPr(&opts.XAxis),
1087
			CrossAx:       &attrValInt{Val: intPtr(100000000)},
xurime's avatar
xurime 已提交
1088
			Crosses:       &attrValString{Val: stringPtr("autoZero")},
1089
			CrossBetween:  &attrValString{Val: stringPtr(chartValAxCrossBetween[opts.Type])},
xurime's avatar
xurime 已提交
1090 1091
		},
	}
1092 1093 1094
	if numFmt := f.drawChartNumFmt(opts.YAxis.NumFmt); numFmt != nil {
		axs[0].NumFmt = numFmt
	}
1095
	if opts.YAxis.MajorGridLines {
xurime's avatar
xurime 已提交
1096 1097
		axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
	}
1098
	if opts.YAxis.MinorGridLines {
xurime's avatar
xurime 已提交
1099 1100
		axs[0].MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
	}
1101
	if pos, ok := valTickLblPos[opts.Type]; ok {
xurime's avatar
xurime 已提交
1102 1103
		axs[0].TickLblPos.Val = stringPtr(pos)
	}
1104 1105
	if opts.YAxis.MajorUnit != 0 {
		axs[0].MajorUnit = &attrValFloat{Val: float64Ptr(opts.YAxis.MajorUnit)}
xurime's avatar
xurime 已提交
1106
	}
1107 1108 1109 1110 1111
	if opts.order > 0 && opts.YAxis.Secondary {
		axs = append(axs, &cAxs{
			AxID: &attrValInt{Val: intPtr(opts.YAxis.axID)},
			Scaling: &cScaling{
				Orientation: &attrValString{Val: stringPtr(orientation[opts.YAxis.ReverseOrder])},
1112 1113
				Max:         maxVal,
				Min:         minVal,
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
			},
			Delete:        &attrValBool{Val: boolPtr(false)},
			AxPos:         &attrValString{Val: stringPtr("r")},
			MajorTickMark: &attrValString{Val: stringPtr("none")},
			MinorTickMark: &attrValString{Val: stringPtr("none")},
			TickLblPos:    &attrValString{Val: stringPtr("nextTo")},
			SpPr:          f.drawPlotAreaSpPr(),
			TxPr:          f.drawPlotAreaTxPr(&opts.XAxis),
			CrossAx:       &attrValInt{Val: intPtr(opts.XAxis.axID)},
			Crosses:       &attrValString{Val: stringPtr("max")},
			CrossBetween:  &attrValString{Val: stringPtr(chartValAxCrossBetween[opts.Type])},
		})
	}
xurime's avatar
xurime 已提交
1127 1128 1129 1130
	return axs
}

// drawPlotAreaSerAx provides a function to draw the c:serAx element.
1131
func (f *File) drawPlotAreaSerAx(opts *Chart) []*cAxs {
1132 1133
	maxVal := &attrValFloat{Val: opts.YAxis.Maximum}
	minVal := &attrValFloat{Val: opts.YAxis.Minimum}
1134
	if opts.YAxis.Maximum == nil {
1135
		maxVal = nil
xurime's avatar
xurime 已提交
1136
	}
1137
	if opts.YAxis.Minimum == nil {
1138
		minVal = nil
1139
	}
xurime's avatar
xurime 已提交
1140 1141
	return []*cAxs{
		{
1142
			AxID: &attrValInt{Val: intPtr(100000005)},
xurime's avatar
xurime 已提交
1143
			Scaling: &cScaling{
1144
				Orientation: &attrValString{Val: stringPtr(orientation[opts.YAxis.ReverseOrder])},
1145 1146
				Max:         maxVal,
				Min:         minVal,
xurime's avatar
xurime 已提交
1147
			},
1148 1149
			Delete:     &attrValBool{Val: boolPtr(opts.YAxis.None)},
			AxPos:      &attrValString{Val: stringPtr(catAxPos[opts.XAxis.ReverseOrder])},
xurime's avatar
xurime 已提交
1150 1151
			TickLblPos: &attrValString{Val: stringPtr("nextTo")},
			SpPr:       f.drawPlotAreaSpPr(),
1152
			TxPr:       f.drawPlotAreaTxPr(nil),
1153
			CrossAx:    &attrValInt{Val: intPtr(100000001)},
xurime's avatar
xurime 已提交
1154 1155 1156 1157
		},
	}
}

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
// drawChartFont provides a function to draw the a:rPr element.
func drawChartFont(fnt *Font, r *aRPr) {
	if fnt == nil {
		return
	}
	r.B = fnt.Bold
	r.I = fnt.Italic
	if idx := inStrSlice(supportedDrawingUnderlineTypes, fnt.Underline, true); idx != -1 {
		r.U = supportedDrawingUnderlineTypes[idx]
	}
	if fnt.Color != "" {
		if r.SolidFill == nil {
			r.SolidFill = &aSolidFill{}
		}
		r.SolidFill.SchemeClr = nil
		r.SolidFill.SrgbClr = &attrValString{Val: stringPtr(strings.ReplaceAll(strings.ToUpper(fnt.Color), "#", ""))}
	}
	if fnt.Family != "" {
		r.Latin.Typeface = fnt.Family
	}
	if fnt.Size > 0 {
		r.Sz = fnt.Size * 100
	}
	if fnt.Strike {
		r.Strike = "sngStrike"
	}
}

1186 1187 1188 1189 1190 1191 1192 1193
// drawPlotAreaTitles provides a function to draw the c:title element.
func (f *File) drawPlotAreaTitles(runs []RichTextRun, vert string) *cTitle {
	if len(runs) == 0 {
		return nil
	}
	title := &cTitle{Tx: cTx{Rich: &cRich{}}, Overlay: &attrValBool{Val: boolPtr(false)}}
	for _, run := range runs {
		r := &aR{T: run.Text}
1194
		drawChartFont(run.Font, &r.RPr)
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
		title.Tx.Rich.P = append(title.Tx.Rich.P, aP{
			PPr:        &aPPr{DefRPr: aRPr{}},
			R:          r,
			EndParaRPr: &aEndParaRPr{Lang: "en-US", AltLang: "en-US"},
		})
	}
	if vert == "horz" {
		title.Tx.Rich.BodyPr = aBodyPr{Rot: -5400000, Vert: vert}
	}
	return title
}

xurime's avatar
xurime 已提交
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
// drawPlotAreaSpPr provides a function to draw the c:spPr element.
func (f *File) drawPlotAreaSpPr() *cSpPr {
	return &cSpPr{
		Ln: &aLn{
			W:    9525,
			Cap:  "flat",
			Cmpd: "sng",
			Algn: "ctr",
			SolidFill: &aSolidFill{
				SchemeClr: &aSchemeClr{
					Val:    "tx1",
					LumMod: &attrValInt{Val: intPtr(15000)},
					LumOff: &attrValInt{Val: intPtr(85000)},
				},
			},
		},
	}
}

// drawPlotAreaTxPr provides a function to draw the c:txPr element.
1227
func (f *File) drawPlotAreaTxPr(opts *ChartAxis) *cTxPr {
1228
	cTxPr := &cTxPr{
xurime's avatar
xurime 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
		BodyPr: aBodyPr{
			Rot:              -60000000,
			SpcFirstLastPara: true,
			VertOverflow:     "ellipsis",
			Vert:             "horz",
			Wrap:             "square",
			Anchor:           "ctr",
			AnchorCtr:        true,
		},
		P: aP{
			PPr: &aPPr{
				DefRPr: aRPr{
					Sz:       900,
					B:        false,
					I:        false,
					U:        "none",
					Strike:   "noStrike",
					Kern:     1200,
					Baseline: 0,
					SolidFill: &aSolidFill{
						SchemeClr: &aSchemeClr{
							Val:    "tx1",
							LumMod: &attrValInt{Val: intPtr(15000)},
							LumOff: &attrValInt{Val: intPtr(85000)},
						},
					},
1255
					Latin: &xlsxCTTextFont{Typeface: "+mn-lt"},
xurime's avatar
xurime 已提交
1256 1257 1258 1259 1260 1261 1262
					Ea:    &aEa{Typeface: "+mn-ea"},
					Cs:    &aCs{Typeface: "+mn-cs"},
				},
			},
			EndParaRPr: &aEndParaRPr{Lang: "en-US"},
		},
	}
1263
	if opts != nil {
1264
		drawChartFont(&opts.Font, &cTxPr.P.PPr.DefRPr)
1265 1266
	}
	return cTxPr
xurime's avatar
xurime 已提交
1267 1268
}

xurime's avatar
xurime 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
// drawChartLn provides a function to draw the a:ln element.
func (f *File) drawChartLn(opts *ChartLine) *aLn {
	ln := &aLn{
		W:    f.ptToEMUs(opts.Width),
		Cap:  "flat",
		Cmpd: "sng",
		Algn: "ctr",
	}
	switch opts.Type {
	case ChartLineSolid:
		ln.SolidFill = &aSolidFill{
			SchemeClr: &aSchemeClr{
				Val: "tx1",
				LumMod: &attrValInt{
					Val: intPtr(15000),
				},
				LumOff: &attrValInt{
					Val: intPtr(85000),
				},
			},
		}
		return ln
	case ChartLineNone:
		ln.NoFill = &attrValString{}
		return ln
	default:
		return nil
	}
}

xurime's avatar
xurime 已提交
1299 1300 1301 1302
// drawingParser provides a function to parse drawingXML. In order to solve
// the problem that the label structure is changed after serialization and
// deserialization, two different structures: decodeWsDr and encodeWsDr are
// defined.
1303
func (f *File) drawingParser(path string) (*xlsxWsDr, int, error) {
xurime's avatar
xurime 已提交
1304 1305 1306 1307
	var (
		err error
		ok  bool
	)
1308 1309
	_, ok = f.Drawings.Load(path)
	if !ok {
1310 1311 1312 1313 1314
		content := xlsxWsDr{
			NS:  NameSpaceDrawingMLSpreadSheet.Value,
			Xdr: NameSpaceDrawingMLSpreadSheet.Value,
			A:   NameSpaceDrawingML.Value,
		}
1315
		if _, ok = f.Pkg.Load(path); ok { // Append Model
xurime's avatar
xurime 已提交
1316 1317 1318
			decodeWsDr := decodeWsDr{}
			if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(path)))).
				Decode(&decodeWsDr); err != nil && err != io.EOF {
1319
				return nil, 0, err
xurime's avatar
xurime 已提交
1320 1321
			}
			content.R = decodeWsDr.R
1322 1323 1324 1325 1326 1327
			for _, v := range decodeWsDr.AlternateContent {
				content.AlternateContent = append(content.AlternateContent, &xlsxAlternateContent{
					Content: v.Content,
					XMLNSMC: SourceRelationshipCompatibility.Value,
				})
			}
xurime's avatar
xurime 已提交
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
			for _, v := range decodeWsDr.OneCellAnchor {
				content.OneCellAnchor = append(content.OneCellAnchor, &xdrCellAnchor{
					EditAs:       v.EditAs,
					GraphicFrame: v.Content,
				})
			}
			for _, v := range decodeWsDr.TwoCellAnchor {
				content.TwoCellAnchor = append(content.TwoCellAnchor, &xdrCellAnchor{
					EditAs:       v.EditAs,
					GraphicFrame: v.Content,
				})
			}
		}
1341
		f.Drawings.Store(path, &content)
xurime's avatar
xurime 已提交
1342
	}
xurime's avatar
xurime 已提交
1343 1344 1345 1346
	var wsDr *xlsxWsDr
	if drawing, ok := f.Drawings.Load(path); ok && drawing != nil {
		wsDr = drawing.(*xlsxWsDr)
	}
1347 1348
	wsDr.mu.Lock()
	defer wsDr.mu.Unlock()
1349
	return wsDr, len(wsDr.OneCellAnchor) + len(wsDr.TwoCellAnchor) + 2, nil
xurime's avatar
xurime 已提交
1350 1351 1352 1353
}

// addDrawingChart provides a function to add chart graphic frame by given
// sheet, drawingXML, cell, width, height, relationship index and format sets.
xurime's avatar
xurime 已提交
1354
func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rID int, opts *GraphicOptions) error {
xurime's avatar
xurime 已提交
1355 1356 1357 1358
	col, row, err := CellNameToCoordinates(cell)
	if err != nil {
		return err
	}
xurime's avatar
xurime 已提交
1359 1360
	width = int(float64(width) * opts.ScaleX)
	height = int(float64(height) * opts.ScaleY)
1361
	colStart, rowStart, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, col, row, opts.OffsetX, opts.OffsetY, width, height)
1362 1363 1364 1365
	content, cNvPrID, err := f.drawingParser(drawingXML)
	if err != nil {
		return err
	}
xurime's avatar
xurime 已提交
1366
	twoCellAnchor := xdrCellAnchor{}
1367
	twoCellAnchor.EditAs = opts.Positioning
xurime's avatar
xurime 已提交
1368 1369
	from := xlsxFrom{}
	from.Col = colStart
1370
	from.ColOff = opts.OffsetX * EMU
xurime's avatar
xurime 已提交
1371
	from.Row = rowStart
1372
	from.RowOff = opts.OffsetY * EMU
xurime's avatar
xurime 已提交
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
	to := xlsxTo{}
	to.Col = colEnd
	to.ColOff = x2 * EMU
	to.Row = rowEnd
	to.RowOff = y2 * EMU
	twoCellAnchor.From = &from
	twoCellAnchor.To = &to

	graphicFrame := xlsxGraphicFrame{
		NvGraphicFramePr: xlsxNvGraphicFramePr{
			CNvPr: &xlsxCNvPr{
				ID:   cNvPrID,
				Name: "Chart " + strconv.Itoa(cNvPrID),
			},
		},
		Graphic: &xlsxGraphic{
			GraphicData: &xlsxGraphicData{
1390
				URI: NameSpaceDrawingMLChart.Value,
xurime's avatar
xurime 已提交
1391
				Chart: &xlsxChart{
1392
					C:   NameSpaceDrawingMLChart.Value,
1393
					R:   SourceRelationship.Value,
xurime's avatar
xurime 已提交
1394 1395 1396 1397 1398 1399 1400 1401
					RID: "rId" + strconv.Itoa(rID),
				},
			},
		},
	}
	graphic, _ := xml.Marshal(graphicFrame)
	twoCellAnchor.GraphicFrame = string(graphic)
	twoCellAnchor.ClientData = &xdrClientData{
1402 1403
		FLocksWithSheet:  *opts.Locked,
		FPrintsWithSheet: *opts.PrintObject,
xurime's avatar
xurime 已提交
1404 1405
	}
	content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
1406
	f.Drawings.Store(drawingXML, content)
xurime's avatar
xurime 已提交
1407 1408
	return err
}
1409

1410 1411 1412
// addSheetDrawingChart provides a function to add chart graphic frame for
// chartsheet by given sheet, drawingXML, width, height, relationship index
// and format sets.
xurime's avatar
xurime 已提交
1413
func (f *File) addSheetDrawingChart(drawingXML string, rID int, opts *GraphicOptions) error {
1414 1415 1416 1417
	content, cNvPrID, err := f.drawingParser(drawingXML)
	if err != nil {
		return err
	}
1418
	absoluteAnchor := xdrCellAnchor{
1419
		EditAs: opts.Positioning,
1420
		Pos:    &xlsxPoint2D{},
1421
		Ext:    &aExt{},
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
	}

	graphicFrame := xlsxGraphicFrame{
		NvGraphicFramePr: xlsxNvGraphicFramePr{
			CNvPr: &xlsxCNvPr{
				ID:   cNvPrID,
				Name: "Chart " + strconv.Itoa(cNvPrID),
			},
		},
		Graphic: &xlsxGraphic{
			GraphicData: &xlsxGraphicData{
1433
				URI: NameSpaceDrawingMLChart.Value,
1434
				Chart: &xlsxChart{
1435
					C:   NameSpaceDrawingMLChart.Value,
1436
					R:   SourceRelationship.Value,
1437 1438 1439 1440 1441 1442 1443 1444
					RID: "rId" + strconv.Itoa(rID),
				},
			},
		},
	}
	graphic, _ := xml.Marshal(graphicFrame)
	absoluteAnchor.GraphicFrame = string(graphic)
	absoluteAnchor.ClientData = &xdrClientData{
1445 1446
		FLocksWithSheet:  *opts.Locked,
		FPrintsWithSheet: *opts.PrintObject,
1447 1448
	}
	content.AbsoluteAnchor = append(content.AbsoluteAnchor, &absoluteAnchor)
1449
	f.Drawings.Store(drawingXML, content)
1450
	return err
1451 1452
}

1453 1454
// deleteDrawing provides a function to delete the chart graphic frame and
// returns deleted embed relationships ID (for unique picture cell anchor) by
1455
// given coordinates and graphic type.
1456
func (f *File) deleteDrawing(col, row int, drawingXML, drawingType string) (string, error) {
1457
	var (
1458
		err             error
1459 1460
		rID             string
		rIDs            []string
1461
		wsDr            *xlsxWsDr
1462
		deTwoCellAnchor *decodeCellAnchor
1463 1464 1465 1466 1467
	)
	xdrCellAnchorFuncs := map[string]func(anchor *xdrCellAnchor) bool{
		"Chart": func(anchor *xdrCellAnchor) bool { return anchor.Pic == nil },
		"Pic":   func(anchor *xdrCellAnchor) bool { return anchor.Pic != nil },
	}
1468 1469 1470
	decodeCellAnchorFuncs := map[string]func(anchor *decodeCellAnchor) bool{
		"Chart": func(anchor *decodeCellAnchor) bool { return anchor.Pic == nil },
		"Pic":   func(anchor *decodeCellAnchor) bool { return anchor.Pic != nil },
1471
	}
1472
	onAnchorCell := func(c, r int) bool { return c == col && r == row }
1473
	if wsDr, _, err = f.drawingParser(drawingXML); err != nil {
1474
		return rID, err
1475
	}
1476 1477
	for idx := 0; idx < len(wsDr.TwoCellAnchor); idx++ {
		if err = nil; wsDr.TwoCellAnchor[idx].From != nil && xdrCellAnchorFuncs[drawingType](wsDr.TwoCellAnchor[idx]) {
1478 1479
			if onAnchorCell(wsDr.TwoCellAnchor[idx].From.Col, wsDr.TwoCellAnchor[idx].From.Row) {
				rID, _ = extractEmbedRID(wsDr.TwoCellAnchor[idx].Pic, nil, rIDs)
1480 1481
				wsDr.TwoCellAnchor = append(wsDr.TwoCellAnchor[:idx], wsDr.TwoCellAnchor[idx+1:]...)
				idx--
1482
				continue
1483
			}
1484
			_, rIDs = extractEmbedRID(wsDr.TwoCellAnchor[idx].Pic, nil, rIDs)
1485 1486 1487
		}
	}
	for idx := 0; idx < len(wsDr.TwoCellAnchor); idx++ {
1488 1489
		deTwoCellAnchor = new(decodeCellAnchor)
		if err = f.xmlNewDecoder(strings.NewReader("<decodeCellAnchor>" + wsDr.TwoCellAnchor[idx].GraphicFrame + "</decodeCellAnchor>")).
1490
			Decode(deTwoCellAnchor); err != nil && err != io.EOF {
1491
			return rID, err
1492
		}
1493
		if err = nil; deTwoCellAnchor.From != nil && decodeCellAnchorFuncs[drawingType](deTwoCellAnchor) {
1494 1495
			if onAnchorCell(deTwoCellAnchor.From.Col, deTwoCellAnchor.From.Row) {
				rID, _ = extractEmbedRID(nil, deTwoCellAnchor.Pic, rIDs)
1496 1497
				wsDr.TwoCellAnchor = append(wsDr.TwoCellAnchor[:idx], wsDr.TwoCellAnchor[idx+1:]...)
				idx--
1498
				continue
1499
			}
1500
			_, rIDs = extractEmbedRID(nil, deTwoCellAnchor.Pic, rIDs)
1501 1502
		}
	}
1503 1504 1505
	if inStrSlice(rIDs, rID, true) != -1 {
		rID = ""
	}
1506
	f.Drawings.Store(drawingXML, wsDr)
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
	return rID, err
}

// extractEmbedRID returns embed relationship ID and all relationship ID lists
// for giving cell anchor.
func extractEmbedRID(pic *xlsxPic, decodePic *decodePic, rIDs []string) (string, []string) {
	if pic != nil {
		rIDs = append(rIDs, pic.BlipFill.Blip.Embed)
		return pic.BlipFill.Blip.Embed, rIDs
	}
	if decodePic != nil {
		rIDs = append(rIDs, decodePic.BlipFill.Blip.Embed)
		return decodePic.BlipFill.Blip.Embed, rIDs
	}
	return "", rIDs
}

// deleteDrawingRels provides a function to delete relationships in
// xl/drawings/_rels/drawings%d.xml.rels by giving drawings relationships path
// and relationship ID.
func (f *File) deleteDrawingRels(rels, rID string) {
	drawingRels, _ := f.relsReader(rels)
	if drawingRels == nil {
		drawingRels = &xlsxRelationships{}
	}
	drawingRels.mu.Lock()
	defer drawingRels.mu.Unlock()
	for k, v := range drawingRels.Relationships {
		if v.ID == rID {
			drawingRels.Relationships = append(drawingRels.Relationships[:k], drawingRels.Relationships[k+1:]...)
		}
	}
	f.Relationships.Store(rels, drawingRels)
1540
}
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550

// genAxID provides a function to generate ID for primary and secondary
// horizontal or vertical axis.
func (f *File) genAxID(opts *Chart) []*attrValInt {
	opts.XAxis.axID, opts.YAxis.axID = 100000000, 100000001
	if opts.order > 0 && opts.YAxis.Secondary {
		opts.XAxis.axID, opts.YAxis.axID = 100000003, 100000004
	}
	return []*attrValInt{{Val: intPtr(opts.XAxis.axID)}, {Val: intPtr(opts.YAxis.axID)}}
}