drawing.go 45.7 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
xurime's avatar
xurime 已提交
10
// data. This library needs Go version 1.16 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
	plotAreaFunc := map[ChartType]func(*Chart) *cPlotArea{
xurime's avatar
xurime 已提交
111 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
		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,
153
		Line3D:                      f.drawLine3DChart,
xurime's avatar
xurime 已提交
154
		Pie:                         f.drawPieChart,
155
		Pie3D:                       f.drawPie3DChart,
156 157
		PieOfPie:                    f.drawPieOfPieChart,
		BarOfPie:                    f.drawBarOfPieChart,
xurime's avatar
xurime 已提交
158 159 160 161 162 163
		Radar:                       f.drawRadarChart,
		Scatter:                     f.drawScatterChart,
		Surface3D:                   f.drawSurface3DChart,
		WireframeSurface3D:          f.drawSurface3DChart,
		Contour:                     f.drawSurfaceChart,
		WireframeContour:            f.drawSurfaceChart,
164 165
		Bubble:                      f.drawBubbleChart,
		Bubble3D:                    f.drawBubbleChart,
xurime's avatar
xurime 已提交
166
	}
xurime's avatar
xurime 已提交
167
	if opts.Legend.Position == "none" {
168 169
		xlsxChartSpace.Chart.Legend = nil
	}
xurime's avatar
xurime 已提交
170 171 172 173 174 175 176 177 178 179
	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)
		}
	}
180 181
	addChart(xlsxChartSpace.Chart.PlotArea, plotAreaFunc[opts.Type](opts))
	order := len(opts.Series)
xurime's avatar
xurime 已提交
182 183 184 185 186 187 188 189 190 191 192 193
	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.
194
func (f *File) drawBaseChart(opts *Chart) *cPlotArea {
xurime's avatar
xurime 已提交
195 196 197 198 199
	c := cCharts{
		BarDir: &attrValString{
			Val: stringPtr("col"),
		},
		Grouping: &attrValString{
200
			Val: stringPtr(plotAreaChartGrouping[opts.Type]),
xurime's avatar
xurime 已提交
201 202
		},
		VaryColors: &attrValBool{
203
			Val: opts.VaryColors,
xurime's avatar
xurime 已提交
204
		},
205 206 207 208
		Ser:     f.drawChartSeries(opts),
		Shape:   f.drawChartShape(opts),
		DLbls:   f.drawChartDLbls(opts),
		AxID:    f.genAxID(opts),
xurime's avatar
xurime 已提交
209 210 211
		Overlap: &attrValInt{Val: intPtr(100)},
	}
	var ok bool
212
	if *c.BarDir.Val, ok = plotAreaChartBarDir[opts.Type]; !ok {
xurime's avatar
xurime 已提交
213 214
		c.BarDir = nil
	}
215
	if *c.Overlap.Val, ok = plotAreaChartOverlap[opts.Type]; !ok {
xurime's avatar
xurime 已提交
216 217
		c.Overlap = nil
	}
218 219
	catAx := f.drawPlotAreaCatAx(opts)
	valAx := f.drawPlotAreaValAx(opts)
220 221
	charts := map[ChartType]*cPlotArea{
		Area: {
xurime's avatar
xurime 已提交
222 223 224 225
			AreaChart: &c,
			CatAx:     catAx,
			ValAx:     valAx,
		},
226
		AreaStacked: {
xurime's avatar
xurime 已提交
227 228 229 230
			AreaChart: &c,
			CatAx:     catAx,
			ValAx:     valAx,
		},
231
		AreaPercentStacked: {
xurime's avatar
xurime 已提交
232 233 234 235
			AreaChart: &c,
			CatAx:     catAx,
			ValAx:     valAx,
		},
236
		Area3D: {
xurime's avatar
xurime 已提交
237 238 239 240
			Area3DChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
241
		Area3DStacked: {
xurime's avatar
xurime 已提交
242 243 244 245
			Area3DChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
246
		Area3DPercentStacked: {
xurime's avatar
xurime 已提交
247 248 249 250
			Area3DChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
251
		Bar: {
xurime's avatar
xurime 已提交
252 253 254 255
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
256
		BarStacked: {
xurime's avatar
xurime 已提交
257 258 259 260
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
261
		BarPercentStacked: {
xurime's avatar
xurime 已提交
262 263 264 265
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
266
		Bar3DClustered: {
xurime's avatar
xurime 已提交
267 268 269 270
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
271
		Bar3DStacked: {
xurime's avatar
xurime 已提交
272 273 274 275
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
276
		Bar3DPercentStacked: {
xurime's avatar
xurime 已提交
277 278 279 280
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
281
		Bar3DConeClustered: {
xurime's avatar
xurime 已提交
282 283 284 285
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
286
		Bar3DConeStacked: {
xurime's avatar
xurime 已提交
287 288 289 290
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
291
		Bar3DConePercentStacked: {
xurime's avatar
xurime 已提交
292 293 294 295
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
296
		Bar3DPyramidClustered: {
xurime's avatar
xurime 已提交
297 298 299 300
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
301
		Bar3DPyramidStacked: {
xurime's avatar
xurime 已提交
302 303 304 305
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
306
		Bar3DPyramidPercentStacked: {
xurime's avatar
xurime 已提交
307 308 309 310
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
311
		Bar3DCylinderClustered: {
xurime's avatar
xurime 已提交
312 313 314 315
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
316
		Bar3DCylinderStacked: {
xurime's avatar
xurime 已提交
317 318 319 320
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
321
		Bar3DCylinderPercentStacked: {
xurime's avatar
xurime 已提交
322 323 324 325
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
326
		Col: {
xurime's avatar
xurime 已提交
327 328 329 330
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
331
		ColStacked: {
xurime's avatar
xurime 已提交
332 333 334 335
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
336
		ColPercentStacked: {
xurime's avatar
xurime 已提交
337 338 339 340
			BarChart: &c,
			CatAx:    catAx,
			ValAx:    valAx,
		},
341
		Col3D: {
xurime's avatar
xurime 已提交
342 343 344 345
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
346
		Col3DClustered: {
xurime's avatar
xurime 已提交
347 348 349 350
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
351
		Col3DStacked: {
xurime's avatar
xurime 已提交
352 353 354 355
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
356
		Col3DPercentStacked: {
xurime's avatar
xurime 已提交
357 358 359 360
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
361
		Col3DCone: {
xurime's avatar
xurime 已提交
362 363 364 365
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
366
		Col3DConeClustered: {
xurime's avatar
xurime 已提交
367 368 369 370
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
371
		Col3DConeStacked: {
xurime's avatar
xurime 已提交
372 373 374 375
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
376
		Col3DConePercentStacked: {
xurime's avatar
xurime 已提交
377 378 379 380
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
381
		Col3DPyramid: {
xurime's avatar
xurime 已提交
382 383 384 385
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
386
		Col3DPyramidClustered: {
xurime's avatar
xurime 已提交
387 388 389 390
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
391
		Col3DPyramidStacked: {
xurime's avatar
xurime 已提交
392 393 394 395
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
396
		Col3DPyramidPercentStacked: {
xurime's avatar
xurime 已提交
397 398 399 400
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
401
		Col3DCylinder: {
xurime's avatar
xurime 已提交
402 403 404 405
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
406
		Col3DCylinderClustered: {
xurime's avatar
xurime 已提交
407 408 409 410
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
411
		Col3DCylinderStacked: {
xurime's avatar
xurime 已提交
412 413 414 415
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
416
		Col3DCylinderPercentStacked: {
xurime's avatar
xurime 已提交
417 418 419 420
			Bar3DChart: &c,
			CatAx:      catAx,
			ValAx:      valAx,
		},
421
		Bubble: {
xurime's avatar
xurime 已提交
422 423 424 425
			BubbleChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
426
		Bubble3D: {
xurime's avatar
xurime 已提交
427 428 429 430 431
			BubbleChart: &c,
			CatAx:       catAx,
			ValAx:       valAx,
		},
	}
432
	return charts[opts.Type]
xurime's avatar
xurime 已提交
433 434 435 436
}

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

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

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

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

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

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

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

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

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

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

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

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

648 649 650 651 652 653 654 655 656 657
// 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),
658
			AxID:  f.genAxID(opts),
659 660 661
		},
		ValAx: []*cAxs{f.drawPlotAreaCatAx(opts)[0], f.drawPlotAreaValAx(opts)[0]},
	}
662 663 664
	if opts.BubbleSize > 0 && opts.BubbleSize <= 300 {
		plotArea.BubbleChart.BubbleScale = &attrValFloat{Val: float64Ptr(float64(opts.BubbleSize))}
	}
665 666 667
	return plotArea
}

xurime's avatar
xurime 已提交
668 669
// drawChartShape provides a function to draw the c:shape element by given
// format sets.
670
func (f *File) drawChartShape(opts *Chart) *attrValString {
671
	shapes := map[ChartType]string{
xurime's avatar
xurime 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
		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",
	}
694
	if shape, ok := shapes[opts.Type]; ok {
xurime's avatar
xurime 已提交
695 696 697 698 699 700 701
		return &attrValString{Val: stringPtr(shape)}
	}
	return nil
}

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

// drawChartSeriesSpPr provides a function to draw the c:spPr element by given
// format sets.
732
func (f *File) drawChartSeriesSpPr(i int, opts *Chart) *cSpPr {
733 734 735
	var srgbClr *attrValString
	var schemeClr *aSchemeClr

736 737
	if color := opts.Series[i].Fill.Color; len(color) == 1 {
		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}}
xurime's avatar
xurime 已提交
743 744 745
	spPrScatter := &cSpPr{
		Ln: &aLn{
			W:      25400,
xurime's avatar
xurime 已提交
746
			NoFill: &attrValString{},
xurime's avatar
xurime 已提交
747 748 749 750
		},
	}
	spPrLine := &cSpPr{
		Ln: &aLn{
751
			W:   f.ptToEMUs(opts.Series[i].Line.Width),
xurime's avatar
xurime 已提交
752
			Cap: "rnd", // rnd, sq, flat
753
			SolidFill: &aSolidFill{
754 755
				SchemeClr: schemeClr,
				SrgbClr:   srgbClr,
756
			},
xurime's avatar
xurime 已提交
757 758
		},
	}
759
	if chartSeriesSpPr, ok := map[ChartType]*cSpPr{
760 761 762 763 764 765 766 767
		Line: spPrLine, Scatter: spPrScatter,
	}[opts.Type]; ok {
		return chartSeriesSpPr
	}
	if srgbClr != nil {
		return spPr
	}
	return nil
xurime's avatar
xurime 已提交
768 769 770 771
}

// drawChartSeriesDPt provides a function to draw the c:dPt element by given
// data index and format sets.
772
func (f *File) drawChartSeriesDPt(i int, opts *Chart) []*cDPt {
xurime's avatar
xurime 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
	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)},
				},
			},
		},
	}}
795
	chartSeriesDPt := map[ChartType][]*cDPt{Pie: dpt, Pie3D: dpt}
796
	return chartSeriesDPt[opts.Type]
xurime's avatar
xurime 已提交
797 798 799 800
}

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

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

// drawChartSeriesMarker provides a function to draw the c:marker element by
// given data index and format sets.
831
func (f *File) drawChartSeriesMarker(i int, opts *Chart) *cMarker {
832
	defaultSymbol := map[ChartType]*attrValString{Scatter: {Val: stringPtr("circle")}}
xurime's avatar
xurime 已提交
833
	marker := &cMarker{
834
		Symbol: defaultSymbol[opts.Type],
xurime's avatar
xurime 已提交
835 836
		Size:   &attrValInt{Val: intPtr(5)},
	}
837
	if symbol := stringPtr(opts.Series[i].Marker.Symbol); *symbol != "" {
838 839
		marker.Symbol = &attrValString{Val: symbol}
	}
840
	if size := intPtr(opts.Series[i].Marker.Size); *size != 0 {
841 842
		marker.Size = &attrValInt{Val: size}
	}
xurime's avatar
xurime 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
	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),
					},
				},
			},
		}
	}
860
	chartSeriesMarker := map[ChartType]*cMarker{Scatter: marker, Line: marker}
861
	return chartSeriesMarker[opts.Type]
xurime's avatar
xurime 已提交
862 863 864 865
}

// drawChartSeriesXVal provides a function to draw the c:xVal element by given
// chart series and format sets.
866
func (f *File) drawChartSeriesXVal(v ChartSeries, opts *Chart) *cCat {
xurime's avatar
xurime 已提交
867 868 869 870 871
	cat := &cCat{
		StrRef: &cStrRef{
			F: v.Categories,
		},
	}
872
	chartSeriesXVal := map[ChartType]*cCat{Scatter: cat, Bubble: cat, Bubble3D: cat}
873
	return chartSeriesXVal[opts.Type]
xurime's avatar
xurime 已提交
874 875 876 877
}

// drawChartSeriesYVal provides a function to draw the c:yVal element by given
// chart series and format sets.
878
func (f *File) drawChartSeriesYVal(v ChartSeries, opts *Chart) *cVal {
xurime's avatar
xurime 已提交
879 880 881 882 883
	val := &cVal{
		NumRef: &cNumRef{
			F: v.Values,
		},
	}
884
	chartSeriesYVal := map[ChartType]*cVal{Scatter: val, Bubble: val, Bubble3D: val}
885
	return chartSeriesYVal[opts.Type]
xurime's avatar
xurime 已提交
886 887 888 889
}

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

// drawCharSeriesBubble3D provides a function to draw the c:bubble3D element
// by given format sets.
907
func (f *File) drawCharSeriesBubble3D(opts *Chart) *attrValBool {
908
	if _, ok := map[ChartType]bool{Bubble3D: true}[opts.Type]; !ok {
xurime's avatar
xurime 已提交
909 910 911 912 913
		return nil
	}
	return &attrValBool{Val: boolPtr(true)}
}

914 915 916 917 918 919 920 921 922 923 924 925 926
// 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 已提交
927 928
// drawChartDLbls provides a function to draw the c:dLbls element by given
// format sets.
929
func (f *File) drawChartDLbls(opts *Chart) *cDLbls {
xurime's avatar
xurime 已提交
930
	return &cDLbls{
931
		NumFmt:          f.drawChartNumFmt(opts.PlotArea.NumFmt),
932
		ShowLegendKey:   &attrValBool{Val: boolPtr(opts.Legend.ShowLegendKey)},
933 934 935 936 937 938
		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 已提交
939 940 941
	}
}

942 943 944 945 946 947 948 949 950 951 952 953
// 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 已提交
954 955
// drawChartSeriesDLbls provides a function to draw the c:dLbls element by
// given format sets.
956
func (f *File) drawChartSeriesDLbls(i int, opts *Chart) *cDLbls {
957
	dLbls := f.drawChartDLbls(opts)
958
	chartSeriesDLbls := map[ChartType]*cDLbls{
959
		Scatter: nil, Surface3D: nil, WireframeSurface3D: nil, Contour: nil, WireframeContour: nil,
960
	}
961
	if _, ok := chartSeriesDLbls[opts.Type]; ok {
xurime's avatar
xurime 已提交
962 963
		return nil
	}
964 965 966 967 968
	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 已提交
969 970 971 972
	return dLbls
}

// drawPlotAreaCatAx provides a function to draw the c:catAx element.
973
func (f *File) drawPlotAreaCatAx(opts *Chart) []*cAxs {
974 975 976
	max := &attrValFloat{Val: opts.XAxis.Maximum}
	min := &attrValFloat{Val: opts.XAxis.Minimum}
	if opts.XAxis.Maximum == nil {
xurime's avatar
xurime 已提交
977 978
		max = nil
	}
979
	if opts.XAxis.Minimum == nil {
980 981
		min = nil
	}
xurime's avatar
xurime 已提交
982 983
	axs := []*cAxs{
		{
984
			AxID: &attrValInt{Val: intPtr(100000000)},
xurime's avatar
xurime 已提交
985
			Scaling: &cScaling{
986
				Orientation: &attrValString{Val: stringPtr(orientation[opts.XAxis.ReverseOrder])},
xurime's avatar
xurime 已提交
987 988 989
				Max:         max,
				Min:         min,
			},
990 991 992
			Delete:        &attrValBool{Val: boolPtr(opts.XAxis.None)},
			AxPos:         &attrValString{Val: stringPtr(catAxPos[opts.XAxis.ReverseOrder])},
			NumFmt:        &cNumFmt{FormatCode: "General"},
xurime's avatar
xurime 已提交
993 994
			MajorTickMark: &attrValString{Val: stringPtr("none")},
			MinorTickMark: &attrValString{Val: stringPtr("none")},
995
			Title:         f.drawPlotAreaTitles(opts.XAxis.Title, ""),
xurime's avatar
xurime 已提交
996 997
			TickLblPos:    &attrValString{Val: stringPtr("nextTo")},
			SpPr:          f.drawPlotAreaSpPr(),
998
			TxPr:          f.drawPlotAreaTxPr(&opts.YAxis),
999
			CrossAx:       &attrValInt{Val: intPtr(100000001)},
xurime's avatar
xurime 已提交
1000 1001 1002 1003 1004 1005 1006
			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)},
		},
	}
1007 1008 1009
	if numFmt := f.drawChartNumFmt(opts.XAxis.NumFmt); numFmt != nil {
		axs[0].NumFmt = numFmt
	}
1010
	if opts.XAxis.MajorGridLines {
xurime's avatar
xurime 已提交
1011 1012
		axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
	}
1013
	if opts.XAxis.MinorGridLines {
xurime's avatar
xurime 已提交
1014 1015
		axs[0].MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
	}
1016 1017
	if opts.XAxis.TickLabelSkip != 0 {
		axs[0].TickLblSkip = &attrValInt{Val: intPtr(opts.XAxis.TickLabelSkip)}
xurime's avatar
xurime 已提交
1018
	}
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
	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])},
				Max:         max,
				Min:         min,
			},
			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 已提交
1041 1042 1043 1044
	return axs
}

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

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

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
// 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}
		if run.Font != nil {
			r.RPr.B, r.RPr.I = run.Font.Bold, run.Font.Italic
			if run.Font.Color != "" {
				r.RPr.SolidFill = &aSolidFill{SrgbClr: &attrValString{Val: stringPtr(run.Font.Color)}}
			}
			if run.Font.Size > 0 {
				r.RPr.Sz = run.Font.Size * 100
			}
		}
		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 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
// 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.
1198
func (f *File) drawPlotAreaTxPr(opts *ChartAxis) *cTxPr {
1199
	cTxPr := &cTxPr{
xurime's avatar
xurime 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
		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)},
						},
					},
1226
					Latin: &xlsxCTTextFont{Typeface: "+mn-lt"},
xurime's avatar
xurime 已提交
1227 1228 1229 1230 1231 1232 1233
					Ea:    &aEa{Typeface: "+mn-ea"},
					Cs:    &aCs{Typeface: "+mn-cs"},
				},
			},
			EndParaRPr: &aEndParaRPr{Lang: "en-US"},
		},
	}
1234
	if opts != nil {
1235 1236 1237
		cTxPr.P.PPr.DefRPr.B = opts.Font.Bold
		cTxPr.P.PPr.DefRPr.I = opts.Font.Italic
		if idx := inStrSlice(supportedDrawingUnderlineTypes, opts.Font.Underline, true); idx != -1 {
1238 1239
			cTxPr.P.PPr.DefRPr.U = supportedDrawingUnderlineTypes[idx]
		}
1240
		if opts.Font.Color != "" {
1241
			cTxPr.P.PPr.DefRPr.SolidFill.SchemeClr = nil
1242
			cTxPr.P.PPr.DefRPr.SolidFill.SrgbClr = &attrValString{Val: stringPtr(strings.ReplaceAll(strings.ToUpper(opts.Font.Color), "#", ""))}
1243 1244 1245
		}
	}
	return cTxPr
xurime's avatar
xurime 已提交
1246 1247
}

xurime's avatar
xurime 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
// 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 已提交
1278 1279 1280 1281
// 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.
1282
func (f *File) drawingParser(path string) (*xlsxWsDr, int, error) {
xurime's avatar
xurime 已提交
1283 1284 1285 1286
	var (
		err error
		ok  bool
	)
1287 1288
	_, ok = f.Drawings.Load(path)
	if !ok {
1289 1290 1291 1292 1293
		content := xlsxWsDr{
			NS:  NameSpaceDrawingMLSpreadSheet.Value,
			Xdr: NameSpaceDrawingMLSpreadSheet.Value,
			A:   NameSpaceDrawingML.Value,
		}
1294
		if _, ok = f.Pkg.Load(path); ok { // Append Model
xurime's avatar
xurime 已提交
1295 1296 1297
			decodeWsDr := decodeWsDr{}
			if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(path)))).
				Decode(&decodeWsDr); err != nil && err != io.EOF {
1298
				return nil, 0, err
xurime's avatar
xurime 已提交
1299 1300
			}
			content.R = decodeWsDr.R
1301 1302 1303 1304 1305 1306
			for _, v := range decodeWsDr.AlternateContent {
				content.AlternateContent = append(content.AlternateContent, &xlsxAlternateContent{
					Content: v.Content,
					XMLNSMC: SourceRelationshipCompatibility.Value,
				})
			}
xurime's avatar
xurime 已提交
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
			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,
				})
			}
		}
1320
		f.Drawings.Store(path, &content)
xurime's avatar
xurime 已提交
1321
	}
xurime's avatar
xurime 已提交
1322 1323 1324 1325
	var wsDr *xlsxWsDr
	if drawing, ok := f.Drawings.Load(path); ok && drawing != nil {
		wsDr = drawing.(*xlsxWsDr)
	}
1326 1327
	wsDr.mu.Lock()
	defer wsDr.mu.Unlock()
1328
	return wsDr, len(wsDr.OneCellAnchor) + len(wsDr.TwoCellAnchor) + 2, nil
xurime's avatar
xurime 已提交
1329 1330 1331 1332
}

// 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 已提交
1333
func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rID int, opts *GraphicOptions) error {
xurime's avatar
xurime 已提交
1334 1335 1336 1337
	col, row, err := CellNameToCoordinates(cell)
	if err != nil {
		return err
	}
xurime's avatar
xurime 已提交
1338 1339
	width = int(float64(width) * opts.ScaleX)
	height = int(float64(height) * opts.ScaleY)
1340
	colStart, rowStart, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, col, row, opts.OffsetX, opts.OffsetY, width, height)
1341 1342 1343 1344
	content, cNvPrID, err := f.drawingParser(drawingXML)
	if err != nil {
		return err
	}
xurime's avatar
xurime 已提交
1345
	twoCellAnchor := xdrCellAnchor{}
1346
	twoCellAnchor.EditAs = opts.Positioning
xurime's avatar
xurime 已提交
1347 1348
	from := xlsxFrom{}
	from.Col = colStart
1349
	from.ColOff = opts.OffsetX * EMU
xurime's avatar
xurime 已提交
1350
	from.Row = rowStart
1351
	from.RowOff = opts.OffsetY * EMU
xurime's avatar
xurime 已提交
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	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{
1369
				URI: NameSpaceDrawingMLChart.Value,
xurime's avatar
xurime 已提交
1370
				Chart: &xlsxChart{
1371
					C:   NameSpaceDrawingMLChart.Value,
1372
					R:   SourceRelationship.Value,
xurime's avatar
xurime 已提交
1373 1374 1375 1376 1377 1378 1379 1380
					RID: "rId" + strconv.Itoa(rID),
				},
			},
		},
	}
	graphic, _ := xml.Marshal(graphicFrame)
	twoCellAnchor.GraphicFrame = string(graphic)
	twoCellAnchor.ClientData = &xdrClientData{
1381 1382
		FLocksWithSheet:  *opts.Locked,
		FPrintsWithSheet: *opts.PrintObject,
xurime's avatar
xurime 已提交
1383 1384
	}
	content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
1385
	f.Drawings.Store(drawingXML, content)
xurime's avatar
xurime 已提交
1386 1387
	return err
}
1388

1389 1390 1391
// 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 已提交
1392
func (f *File) addSheetDrawingChart(drawingXML string, rID int, opts *GraphicOptions) error {
1393 1394 1395 1396
	content, cNvPrID, err := f.drawingParser(drawingXML)
	if err != nil {
		return err
	}
1397
	absoluteAnchor := xdrCellAnchor{
1398
		EditAs: opts.Positioning,
1399
		Pos:    &xlsxPoint2D{},
1400
		Ext:    &aExt{},
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
	}

	graphicFrame := xlsxGraphicFrame{
		NvGraphicFramePr: xlsxNvGraphicFramePr{
			CNvPr: &xlsxCNvPr{
				ID:   cNvPrID,
				Name: "Chart " + strconv.Itoa(cNvPrID),
			},
		},
		Graphic: &xlsxGraphic{
			GraphicData: &xlsxGraphicData{
1412
				URI: NameSpaceDrawingMLChart.Value,
1413
				Chart: &xlsxChart{
1414
					C:   NameSpaceDrawingMLChart.Value,
1415
					R:   SourceRelationship.Value,
1416 1417 1418 1419 1420 1421 1422 1423
					RID: "rId" + strconv.Itoa(rID),
				},
			},
		},
	}
	graphic, _ := xml.Marshal(graphicFrame)
	absoluteAnchor.GraphicFrame = string(graphic)
	absoluteAnchor.ClientData = &xdrClientData{
1424 1425
		FLocksWithSheet:  *opts.Locked,
		FPrintsWithSheet: *opts.PrintObject,
1426 1427
	}
	content.AbsoluteAnchor = append(content.AbsoluteAnchor, &absoluteAnchor)
1428
	f.Drawings.Store(drawingXML, content)
1429
	return err
1430 1431
}

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

// 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)}}
}