drawing.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. // Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX / XLSM / XLTM files. Supports reading and writing
  7. // spreadsheet documents generated by Microsoft Exce™ 2007 and later. Supports
  8. // complex components by high compatibility, and provided streaming API for
  9. // generating or reading data from a worksheet with huge amounts of data. This
  10. // library needs Go version 1.10 or later.
  11. package excelize
  12. import (
  13. "bytes"
  14. "encoding/xml"
  15. "fmt"
  16. "io"
  17. "log"
  18. "reflect"
  19. "strconv"
  20. "strings"
  21. )
  22. // prepareDrawing provides a function to prepare drawing ID and XML by given
  23. // drawingID, worksheet name and default drawingXML.
  24. func (f *File) prepareDrawing(ws *xlsxWorksheet, drawingID int, sheet, drawingXML string) (int, string) {
  25. sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  26. if ws.Drawing != nil {
  27. // The worksheet already has a picture or chart relationships, use the relationships drawing ../drawings/drawing%d.xml.
  28. sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID)
  29. drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml"))
  30. drawingXML = strings.Replace(sheetRelationshipsDrawingXML, "..", "xl", -1)
  31. } else {
  32. // Add first picture for given sheet.
  33. sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels"
  34. rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
  35. f.addSheetDrawing(sheet, rID)
  36. }
  37. return drawingID, drawingXML
  38. }
  39. // prepareChartSheetDrawing provides a function to prepare drawing ID and XML
  40. // by given drawingID, worksheet name and default drawingXML.
  41. func (f *File) prepareChartSheetDrawing(cs *xlsxChartsheet, drawingID int, sheet string) {
  42. sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  43. // Only allow one chart in a chartsheet.
  44. sheetRels := "xl/chartsheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/chartsheets/") + ".rels"
  45. rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
  46. f.addSheetNameSpace(sheet, SourceRelationship)
  47. cs.Drawing = &xlsxDrawing{
  48. RID: "rId" + strconv.Itoa(rID),
  49. }
  50. return
  51. }
  52. // addChart provides a function to create chart as xl/charts/chart%d.xml by
  53. // given format sets.
  54. func (f *File) addChart(formatSet *formatChart, comboCharts []*formatChart) {
  55. count := f.countCharts()
  56. xlsxChartSpace := xlsxChartSpace{
  57. XMLNSa: NameSpaceDrawingML.Value,
  58. Date1904: &attrValBool{Val: boolPtr(false)},
  59. Lang: &attrValString{Val: stringPtr("en-US")},
  60. RoundedCorners: &attrValBool{Val: boolPtr(false)},
  61. Chart: cChart{
  62. Title: &cTitle{
  63. Tx: cTx{
  64. Rich: &cRich{
  65. P: aP{
  66. PPr: &aPPr{
  67. DefRPr: aRPr{
  68. Kern: 1200,
  69. Strike: "noStrike",
  70. U: "none",
  71. Sz: 1400,
  72. SolidFill: &aSolidFill{
  73. SchemeClr: &aSchemeClr{
  74. Val: "tx1",
  75. LumMod: &attrValInt{
  76. Val: intPtr(65000),
  77. },
  78. LumOff: &attrValInt{
  79. Val: intPtr(35000),
  80. },
  81. },
  82. },
  83. Ea: &aEa{
  84. Typeface: "+mn-ea",
  85. },
  86. Cs: &aCs{
  87. Typeface: "+mn-cs",
  88. },
  89. Latin: &aLatin{
  90. Typeface: "+mn-lt",
  91. },
  92. },
  93. },
  94. R: &aR{
  95. RPr: aRPr{
  96. Lang: "en-US",
  97. AltLang: "en-US",
  98. },
  99. T: formatSet.Title.Name,
  100. },
  101. },
  102. },
  103. },
  104. TxPr: cTxPr{
  105. P: aP{
  106. PPr: &aPPr{
  107. DefRPr: aRPr{
  108. Kern: 1200,
  109. U: "none",
  110. Sz: 14000,
  111. Strike: "noStrike",
  112. },
  113. },
  114. EndParaRPr: &aEndParaRPr{
  115. Lang: "en-US",
  116. },
  117. },
  118. },
  119. Overlay: &attrValBool{Val: boolPtr(false)},
  120. },
  121. View3D: &cView3D{
  122. RotX: &attrValInt{Val: intPtr(chartView3DRotX[formatSet.Type])},
  123. RotY: &attrValInt{Val: intPtr(chartView3DRotY[formatSet.Type])},
  124. Perspective: &attrValInt{Val: intPtr(chartView3DPerspective[formatSet.Type])},
  125. RAngAx: &attrValInt{Val: intPtr(chartView3DRAngAx[formatSet.Type])},
  126. },
  127. Floor: &cThicknessSpPr{
  128. Thickness: &attrValInt{Val: intPtr(0)},
  129. },
  130. SideWall: &cThicknessSpPr{
  131. Thickness: &attrValInt{Val: intPtr(0)},
  132. },
  133. BackWall: &cThicknessSpPr{
  134. Thickness: &attrValInt{Val: intPtr(0)},
  135. },
  136. PlotArea: &cPlotArea{},
  137. Legend: &cLegend{
  138. LegendPos: &attrValString{Val: stringPtr(chartLegendPosition[formatSet.Legend.Position])},
  139. Overlay: &attrValBool{Val: boolPtr(false)},
  140. },
  141. PlotVisOnly: &attrValBool{Val: boolPtr(false)},
  142. DispBlanksAs: &attrValString{Val: stringPtr(formatSet.ShowBlanksAs)},
  143. ShowDLblsOverMax: &attrValBool{Val: boolPtr(false)},
  144. },
  145. SpPr: &cSpPr{
  146. SolidFill: &aSolidFill{
  147. SchemeClr: &aSchemeClr{Val: "bg1"},
  148. },
  149. Ln: &aLn{
  150. W: 9525,
  151. Cap: "flat",
  152. Cmpd: "sng",
  153. Algn: "ctr",
  154. SolidFill: &aSolidFill{
  155. SchemeClr: &aSchemeClr{Val: "tx1",
  156. LumMod: &attrValInt{
  157. Val: intPtr(15000),
  158. },
  159. LumOff: &attrValInt{
  160. Val: intPtr(85000),
  161. },
  162. },
  163. },
  164. },
  165. },
  166. PrintSettings: &cPrintSettings{
  167. PageMargins: &cPageMargins{
  168. B: 0.75,
  169. L: 0.7,
  170. R: 0.7,
  171. T: 0.7,
  172. Header: 0.3,
  173. Footer: 0.3,
  174. },
  175. },
  176. }
  177. plotAreaFunc := map[string]func(*formatChart) *cPlotArea{
  178. Area: f.drawBaseChart,
  179. AreaStacked: f.drawBaseChart,
  180. AreaPercentStacked: f.drawBaseChart,
  181. Area3D: f.drawBaseChart,
  182. Area3DStacked: f.drawBaseChart,
  183. Area3DPercentStacked: f.drawBaseChart,
  184. Bar: f.drawBaseChart,
  185. BarStacked: f.drawBaseChart,
  186. BarPercentStacked: f.drawBaseChart,
  187. Bar3DClustered: f.drawBaseChart,
  188. Bar3DStacked: f.drawBaseChart,
  189. Bar3DPercentStacked: f.drawBaseChart,
  190. Bar3DConeClustered: f.drawBaseChart,
  191. Bar3DConeStacked: f.drawBaseChart,
  192. Bar3DConePercentStacked: f.drawBaseChart,
  193. Bar3DPyramidClustered: f.drawBaseChart,
  194. Bar3DPyramidStacked: f.drawBaseChart,
  195. Bar3DPyramidPercentStacked: f.drawBaseChart,
  196. Bar3DCylinderClustered: f.drawBaseChart,
  197. Bar3DCylinderStacked: f.drawBaseChart,
  198. Bar3DCylinderPercentStacked: f.drawBaseChart,
  199. Col: f.drawBaseChart,
  200. ColStacked: f.drawBaseChart,
  201. ColPercentStacked: f.drawBaseChart,
  202. Col3D: f.drawBaseChart,
  203. Col3DClustered: f.drawBaseChart,
  204. Col3DStacked: f.drawBaseChart,
  205. Col3DPercentStacked: f.drawBaseChart,
  206. Col3DCone: f.drawBaseChart,
  207. Col3DConeClustered: f.drawBaseChart,
  208. Col3DConeStacked: f.drawBaseChart,
  209. Col3DConePercentStacked: f.drawBaseChart,
  210. Col3DPyramid: f.drawBaseChart,
  211. Col3DPyramidClustered: f.drawBaseChart,
  212. Col3DPyramidStacked: f.drawBaseChart,
  213. Col3DPyramidPercentStacked: f.drawBaseChart,
  214. Col3DCylinder: f.drawBaseChart,
  215. Col3DCylinderClustered: f.drawBaseChart,
  216. Col3DCylinderStacked: f.drawBaseChart,
  217. Col3DCylinderPercentStacked: f.drawBaseChart,
  218. Doughnut: f.drawDoughnutChart,
  219. Line: f.drawLineChart,
  220. Pie3D: f.drawPie3DChart,
  221. Pie: f.drawPieChart,
  222. PieOfPieChart: f.drawPieOfPieChart,
  223. BarOfPieChart: f.drawBarOfPieChart,
  224. Radar: f.drawRadarChart,
  225. Scatter: f.drawScatterChart,
  226. Surface3D: f.drawSurface3DChart,
  227. WireframeSurface3D: f.drawSurface3DChart,
  228. Contour: f.drawSurfaceChart,
  229. WireframeContour: f.drawSurfaceChart,
  230. Bubble: f.drawBaseChart,
  231. Bubble3D: f.drawBaseChart,
  232. }
  233. addChart := func(c, p *cPlotArea) {
  234. immutable, mutable := reflect.ValueOf(c).Elem(), reflect.ValueOf(p).Elem()
  235. for i := 0; i < mutable.NumField(); i++ {
  236. field := mutable.Field(i)
  237. if field.IsNil() {
  238. continue
  239. }
  240. immutable.FieldByName(mutable.Type().Field(i).Name).Set(field)
  241. }
  242. }
  243. addChart(xlsxChartSpace.Chart.PlotArea, plotAreaFunc[formatSet.Type](formatSet))
  244. order := len(formatSet.Series)
  245. for idx := range comboCharts {
  246. comboCharts[idx].order = order
  247. addChart(xlsxChartSpace.Chart.PlotArea, plotAreaFunc[comboCharts[idx].Type](comboCharts[idx]))
  248. order += len(comboCharts[idx].Series)
  249. }
  250. chart, _ := xml.Marshal(xlsxChartSpace)
  251. media := "xl/charts/chart" + strconv.Itoa(count+1) + ".xml"
  252. f.saveFileList(media, chart)
  253. }
  254. // drawBaseChart provides a function to draw the c:plotArea element for bar,
  255. // and column series charts by given format sets.
  256. func (f *File) drawBaseChart(formatSet *formatChart) *cPlotArea {
  257. c := cCharts{
  258. BarDir: &attrValString{
  259. Val: stringPtr("col"),
  260. },
  261. Grouping: &attrValString{
  262. Val: stringPtr("clustered"),
  263. },
  264. VaryColors: &attrValBool{
  265. Val: boolPtr(true),
  266. },
  267. Ser: f.drawChartSeries(formatSet),
  268. Shape: f.drawChartShape(formatSet),
  269. DLbls: f.drawChartDLbls(formatSet),
  270. AxID: []*attrValInt{
  271. {Val: intPtr(754001152)},
  272. {Val: intPtr(753999904)},
  273. },
  274. Overlap: &attrValInt{Val: intPtr(100)},
  275. }
  276. var ok bool
  277. if *c.BarDir.Val, ok = plotAreaChartBarDir[formatSet.Type]; !ok {
  278. c.BarDir = nil
  279. }
  280. if *c.Grouping.Val, ok = plotAreaChartGrouping[formatSet.Type]; !ok {
  281. c.Grouping = nil
  282. }
  283. if *c.Overlap.Val, ok = plotAreaChartOverlap[formatSet.Type]; !ok {
  284. c.Overlap = nil
  285. }
  286. catAx := f.drawPlotAreaCatAx(formatSet)
  287. valAx := f.drawPlotAreaValAx(formatSet)
  288. charts := map[string]*cPlotArea{
  289. "area": {
  290. AreaChart: &c,
  291. CatAx: catAx,
  292. ValAx: valAx,
  293. },
  294. "areaStacked": {
  295. AreaChart: &c,
  296. CatAx: catAx,
  297. ValAx: valAx,
  298. },
  299. "areaPercentStacked": {
  300. AreaChart: &c,
  301. CatAx: catAx,
  302. ValAx: valAx,
  303. },
  304. "area3D": {
  305. Area3DChart: &c,
  306. CatAx: catAx,
  307. ValAx: valAx,
  308. },
  309. "area3DStacked": {
  310. Area3DChart: &c,
  311. CatAx: catAx,
  312. ValAx: valAx,
  313. },
  314. "area3DPercentStacked": {
  315. Area3DChart: &c,
  316. CatAx: catAx,
  317. ValAx: valAx,
  318. },
  319. "bar": {
  320. BarChart: &c,
  321. CatAx: catAx,
  322. ValAx: valAx,
  323. },
  324. "barStacked": {
  325. BarChart: &c,
  326. CatAx: catAx,
  327. ValAx: valAx,
  328. },
  329. "barPercentStacked": {
  330. BarChart: &c,
  331. CatAx: catAx,
  332. ValAx: valAx,
  333. },
  334. "bar3DClustered": {
  335. Bar3DChart: &c,
  336. CatAx: catAx,
  337. ValAx: valAx,
  338. },
  339. "bar3DStacked": {
  340. Bar3DChart: &c,
  341. CatAx: catAx,
  342. ValAx: valAx,
  343. },
  344. "bar3DPercentStacked": {
  345. Bar3DChart: &c,
  346. CatAx: catAx,
  347. ValAx: valAx,
  348. },
  349. "bar3DConeClustered": {
  350. Bar3DChart: &c,
  351. CatAx: catAx,
  352. ValAx: valAx,
  353. },
  354. "bar3DConeStacked": {
  355. Bar3DChart: &c,
  356. CatAx: catAx,
  357. ValAx: valAx,
  358. },
  359. "bar3DConePercentStacked": {
  360. Bar3DChart: &c,
  361. CatAx: catAx,
  362. ValAx: valAx,
  363. },
  364. "bar3DPyramidClustered": {
  365. Bar3DChart: &c,
  366. CatAx: catAx,
  367. ValAx: valAx,
  368. },
  369. "bar3DPyramidStacked": {
  370. Bar3DChart: &c,
  371. CatAx: catAx,
  372. ValAx: valAx,
  373. },
  374. "bar3DPyramidPercentStacked": {
  375. Bar3DChart: &c,
  376. CatAx: catAx,
  377. ValAx: valAx,
  378. },
  379. "bar3DCylinderClustered": {
  380. Bar3DChart: &c,
  381. CatAx: catAx,
  382. ValAx: valAx,
  383. },
  384. "bar3DCylinderStacked": {
  385. Bar3DChart: &c,
  386. CatAx: catAx,
  387. ValAx: valAx,
  388. },
  389. "bar3DCylinderPercentStacked": {
  390. Bar3DChart: &c,
  391. CatAx: catAx,
  392. ValAx: valAx,
  393. },
  394. "col": {
  395. BarChart: &c,
  396. CatAx: catAx,
  397. ValAx: valAx,
  398. },
  399. "colStacked": {
  400. BarChart: &c,
  401. CatAx: catAx,
  402. ValAx: valAx,
  403. },
  404. "colPercentStacked": {
  405. BarChart: &c,
  406. CatAx: catAx,
  407. ValAx: valAx,
  408. },
  409. "col3D": {
  410. Bar3DChart: &c,
  411. CatAx: catAx,
  412. ValAx: valAx,
  413. },
  414. "col3DClustered": {
  415. Bar3DChart: &c,
  416. CatAx: catAx,
  417. ValAx: valAx,
  418. },
  419. "col3DStacked": {
  420. Bar3DChart: &c,
  421. CatAx: catAx,
  422. ValAx: valAx,
  423. },
  424. "col3DPercentStacked": {
  425. Bar3DChart: &c,
  426. CatAx: catAx,
  427. ValAx: valAx,
  428. },
  429. "col3DCone": {
  430. Bar3DChart: &c,
  431. CatAx: catAx,
  432. ValAx: valAx,
  433. },
  434. "col3DConeClustered": {
  435. Bar3DChart: &c,
  436. CatAx: catAx,
  437. ValAx: valAx,
  438. },
  439. "col3DConeStacked": {
  440. Bar3DChart: &c,
  441. CatAx: catAx,
  442. ValAx: valAx,
  443. },
  444. "col3DConePercentStacked": {
  445. Bar3DChart: &c,
  446. CatAx: catAx,
  447. ValAx: valAx,
  448. },
  449. "col3DPyramid": {
  450. Bar3DChart: &c,
  451. CatAx: catAx,
  452. ValAx: valAx,
  453. },
  454. "col3DPyramidClustered": {
  455. Bar3DChart: &c,
  456. CatAx: catAx,
  457. ValAx: valAx,
  458. },
  459. "col3DPyramidStacked": {
  460. Bar3DChart: &c,
  461. CatAx: catAx,
  462. ValAx: valAx,
  463. },
  464. "col3DPyramidPercentStacked": {
  465. Bar3DChart: &c,
  466. CatAx: catAx,
  467. ValAx: valAx,
  468. },
  469. "col3DCylinder": {
  470. Bar3DChart: &c,
  471. CatAx: catAx,
  472. ValAx: valAx,
  473. },
  474. "col3DCylinderClustered": {
  475. Bar3DChart: &c,
  476. CatAx: catAx,
  477. ValAx: valAx,
  478. },
  479. "col3DCylinderStacked": {
  480. Bar3DChart: &c,
  481. CatAx: catAx,
  482. ValAx: valAx,
  483. },
  484. "col3DCylinderPercentStacked": {
  485. Bar3DChart: &c,
  486. CatAx: catAx,
  487. ValAx: valAx,
  488. },
  489. "bubble": {
  490. BubbleChart: &c,
  491. CatAx: catAx,
  492. ValAx: valAx,
  493. },
  494. "bubble3D": {
  495. BubbleChart: &c,
  496. CatAx: catAx,
  497. ValAx: valAx,
  498. },
  499. }
  500. return charts[formatSet.Type]
  501. }
  502. // drawDoughnutChart provides a function to draw the c:plotArea element for
  503. // doughnut chart by given format sets.
  504. func (f *File) drawDoughnutChart(formatSet *formatChart) *cPlotArea {
  505. return &cPlotArea{
  506. DoughnutChart: &cCharts{
  507. VaryColors: &attrValBool{
  508. Val: boolPtr(true),
  509. },
  510. Ser: f.drawChartSeries(formatSet),
  511. HoleSize: &attrValInt{Val: intPtr(75)},
  512. },
  513. }
  514. }
  515. // drawLineChart provides a function to draw the c:plotArea element for line
  516. // chart by given format sets.
  517. func (f *File) drawLineChart(formatSet *formatChart) *cPlotArea {
  518. return &cPlotArea{
  519. LineChart: &cCharts{
  520. Grouping: &attrValString{
  521. Val: stringPtr(plotAreaChartGrouping[formatSet.Type]),
  522. },
  523. VaryColors: &attrValBool{
  524. Val: boolPtr(false),
  525. },
  526. Ser: f.drawChartSeries(formatSet),
  527. DLbls: f.drawChartDLbls(formatSet),
  528. Smooth: &attrValBool{
  529. Val: boolPtr(false),
  530. },
  531. AxID: []*attrValInt{
  532. {Val: intPtr(754001152)},
  533. {Val: intPtr(753999904)},
  534. },
  535. },
  536. CatAx: f.drawPlotAreaCatAx(formatSet),
  537. ValAx: f.drawPlotAreaValAx(formatSet),
  538. }
  539. }
  540. // drawPieChart provides a function to draw the c:plotArea element for pie
  541. // chart by given format sets.
  542. func (f *File) drawPieChart(formatSet *formatChart) *cPlotArea {
  543. return &cPlotArea{
  544. PieChart: &cCharts{
  545. VaryColors: &attrValBool{
  546. Val: boolPtr(true),
  547. },
  548. Ser: f.drawChartSeries(formatSet),
  549. },
  550. }
  551. }
  552. // drawPie3DChart provides a function to draw the c:plotArea element for 3D
  553. // pie chart by given format sets.
  554. func (f *File) drawPie3DChart(formatSet *formatChart) *cPlotArea {
  555. return &cPlotArea{
  556. Pie3DChart: &cCharts{
  557. VaryColors: &attrValBool{
  558. Val: boolPtr(true),
  559. },
  560. Ser: f.drawChartSeries(formatSet),
  561. },
  562. }
  563. }
  564. // drawPieOfPieChart provides a function to draw the c:plotArea element for
  565. // pie chart by given format sets.
  566. func (f *File) drawPieOfPieChart(formatSet *formatChart) *cPlotArea {
  567. return &cPlotArea{
  568. OfPieChart: &cCharts{
  569. OfPieType: &attrValString{
  570. Val: stringPtr("pie"),
  571. },
  572. VaryColors: &attrValBool{
  573. Val: boolPtr(true),
  574. },
  575. Ser: f.drawChartSeries(formatSet),
  576. SerLines: &attrValString{},
  577. },
  578. }
  579. }
  580. // drawBarOfPieChart provides a function to draw the c:plotArea element for
  581. // pie chart by given format sets.
  582. func (f *File) drawBarOfPieChart(formatSet *formatChart) *cPlotArea {
  583. return &cPlotArea{
  584. OfPieChart: &cCharts{
  585. OfPieType: &attrValString{
  586. Val: stringPtr("bar"),
  587. },
  588. VaryColors: &attrValBool{
  589. Val: boolPtr(true),
  590. },
  591. Ser: f.drawChartSeries(formatSet),
  592. SerLines: &attrValString{},
  593. },
  594. }
  595. }
  596. // drawRadarChart provides a function to draw the c:plotArea element for radar
  597. // chart by given format sets.
  598. func (f *File) drawRadarChart(formatSet *formatChart) *cPlotArea {
  599. return &cPlotArea{
  600. RadarChart: &cCharts{
  601. RadarStyle: &attrValString{
  602. Val: stringPtr("marker"),
  603. },
  604. VaryColors: &attrValBool{
  605. Val: boolPtr(false),
  606. },
  607. Ser: f.drawChartSeries(formatSet),
  608. DLbls: f.drawChartDLbls(formatSet),
  609. AxID: []*attrValInt{
  610. {Val: intPtr(754001152)},
  611. {Val: intPtr(753999904)},
  612. },
  613. },
  614. CatAx: f.drawPlotAreaCatAx(formatSet),
  615. ValAx: f.drawPlotAreaValAx(formatSet),
  616. }
  617. }
  618. // drawScatterChart provides a function to draw the c:plotArea element for
  619. // scatter chart by given format sets.
  620. func (f *File) drawScatterChart(formatSet *formatChart) *cPlotArea {
  621. return &cPlotArea{
  622. ScatterChart: &cCharts{
  623. ScatterStyle: &attrValString{
  624. Val: stringPtr("smoothMarker"), // line,lineMarker,marker,none,smooth,smoothMarker
  625. },
  626. VaryColors: &attrValBool{
  627. Val: boolPtr(false),
  628. },
  629. Ser: f.drawChartSeries(formatSet),
  630. DLbls: f.drawChartDLbls(formatSet),
  631. AxID: []*attrValInt{
  632. {Val: intPtr(754001152)},
  633. {Val: intPtr(753999904)},
  634. },
  635. },
  636. CatAx: f.drawPlotAreaCatAx(formatSet),
  637. ValAx: f.drawPlotAreaValAx(formatSet),
  638. }
  639. }
  640. // drawSurface3DChart provides a function to draw the c:surface3DChart element by
  641. // given format sets.
  642. func (f *File) drawSurface3DChart(formatSet *formatChart) *cPlotArea {
  643. plotArea := &cPlotArea{
  644. Surface3DChart: &cCharts{
  645. Ser: f.drawChartSeries(formatSet),
  646. AxID: []*attrValInt{
  647. {Val: intPtr(754001152)},
  648. {Val: intPtr(753999904)},
  649. {Val: intPtr(832256642)},
  650. },
  651. },
  652. CatAx: f.drawPlotAreaCatAx(formatSet),
  653. ValAx: f.drawPlotAreaValAx(formatSet),
  654. SerAx: f.drawPlotAreaSerAx(formatSet),
  655. }
  656. if formatSet.Type == WireframeSurface3D {
  657. plotArea.Surface3DChart.Wireframe = &attrValBool{Val: boolPtr(true)}
  658. }
  659. return plotArea
  660. }
  661. // drawSurfaceChart provides a function to draw the c:surfaceChart element by
  662. // given format sets.
  663. func (f *File) drawSurfaceChart(formatSet *formatChart) *cPlotArea {
  664. plotArea := &cPlotArea{
  665. SurfaceChart: &cCharts{
  666. Ser: f.drawChartSeries(formatSet),
  667. AxID: []*attrValInt{
  668. {Val: intPtr(754001152)},
  669. {Val: intPtr(753999904)},
  670. {Val: intPtr(832256642)},
  671. },
  672. },
  673. CatAx: f.drawPlotAreaCatAx(formatSet),
  674. ValAx: f.drawPlotAreaValAx(formatSet),
  675. SerAx: f.drawPlotAreaSerAx(formatSet),
  676. }
  677. if formatSet.Type == WireframeContour {
  678. plotArea.SurfaceChart.Wireframe = &attrValBool{Val: boolPtr(true)}
  679. }
  680. return plotArea
  681. }
  682. // drawChartShape provides a function to draw the c:shape element by given
  683. // format sets.
  684. func (f *File) drawChartShape(formatSet *formatChart) *attrValString {
  685. shapes := map[string]string{
  686. Bar3DConeClustered: "cone",
  687. Bar3DConeStacked: "cone",
  688. Bar3DConePercentStacked: "cone",
  689. Bar3DPyramidClustered: "pyramid",
  690. Bar3DPyramidStacked: "pyramid",
  691. Bar3DPyramidPercentStacked: "pyramid",
  692. Bar3DCylinderClustered: "cylinder",
  693. Bar3DCylinderStacked: "cylinder",
  694. Bar3DCylinderPercentStacked: "cylinder",
  695. Col3DCone: "cone",
  696. Col3DConeClustered: "cone",
  697. Col3DConeStacked: "cone",
  698. Col3DConePercentStacked: "cone",
  699. Col3DPyramid: "pyramid",
  700. Col3DPyramidClustered: "pyramid",
  701. Col3DPyramidStacked: "pyramid",
  702. Col3DPyramidPercentStacked: "pyramid",
  703. Col3DCylinder: "cylinder",
  704. Col3DCylinderClustered: "cylinder",
  705. Col3DCylinderStacked: "cylinder",
  706. Col3DCylinderPercentStacked: "cylinder",
  707. }
  708. if shape, ok := shapes[formatSet.Type]; ok {
  709. return &attrValString{Val: stringPtr(shape)}
  710. }
  711. return nil
  712. }
  713. // drawChartSeries provides a function to draw the c:ser element by given
  714. // format sets.
  715. func (f *File) drawChartSeries(formatSet *formatChart) *[]cSer {
  716. ser := []cSer{}
  717. for k := range formatSet.Series {
  718. ser = append(ser, cSer{
  719. IDx: &attrValInt{Val: intPtr(k + formatSet.order)},
  720. Order: &attrValInt{Val: intPtr(k + formatSet.order)},
  721. Tx: &cTx{
  722. StrRef: &cStrRef{
  723. F: formatSet.Series[k].Name,
  724. },
  725. },
  726. SpPr: f.drawChartSeriesSpPr(k, formatSet),
  727. Marker: f.drawChartSeriesMarker(k, formatSet),
  728. DPt: f.drawChartSeriesDPt(k, formatSet),
  729. DLbls: f.drawChartSeriesDLbls(formatSet),
  730. Cat: f.drawChartSeriesCat(formatSet.Series[k], formatSet),
  731. Val: f.drawChartSeriesVal(formatSet.Series[k], formatSet),
  732. XVal: f.drawChartSeriesXVal(formatSet.Series[k], formatSet),
  733. YVal: f.drawChartSeriesYVal(formatSet.Series[k], formatSet),
  734. BubbleSize: f.drawCharSeriesBubbleSize(formatSet.Series[k], formatSet),
  735. Bubble3D: f.drawCharSeriesBubble3D(formatSet),
  736. })
  737. }
  738. return &ser
  739. }
  740. // drawChartSeriesSpPr provides a function to draw the c:spPr element by given
  741. // format sets.
  742. func (f *File) drawChartSeriesSpPr(i int, formatSet *formatChart) *cSpPr {
  743. spPrScatter := &cSpPr{
  744. Ln: &aLn{
  745. W: 25400,
  746. NoFill: " ",
  747. },
  748. }
  749. spPrLine := &cSpPr{
  750. Ln: &aLn{
  751. W: f.ptToEMUs(formatSet.Series[i].Line.Width),
  752. Cap: "rnd", // rnd, sq, flat
  753. SolidFill: &aSolidFill{
  754. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa((formatSet.order+i)%6+1)},
  755. },
  756. },
  757. }
  758. chartSeriesSpPr := map[string]*cSpPr{Line: spPrLine, Scatter: spPrScatter}
  759. return chartSeriesSpPr[formatSet.Type]
  760. }
  761. // drawChartSeriesDPt provides a function to draw the c:dPt element by given
  762. // data index and format sets.
  763. func (f *File) drawChartSeriesDPt(i int, formatSet *formatChart) []*cDPt {
  764. dpt := []*cDPt{{
  765. IDx: &attrValInt{Val: intPtr(i)},
  766. Bubble3D: &attrValBool{Val: boolPtr(false)},
  767. SpPr: &cSpPr{
  768. SolidFill: &aSolidFill{
  769. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
  770. },
  771. Ln: &aLn{
  772. W: 25400,
  773. Cap: "rnd",
  774. SolidFill: &aSolidFill{
  775. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  776. },
  777. },
  778. Sp3D: &aSp3D{
  779. ContourW: 25400,
  780. ContourClr: &aContourClr{
  781. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  782. },
  783. },
  784. },
  785. }}
  786. chartSeriesDPt := map[string][]*cDPt{Pie: dpt, Pie3D: dpt}
  787. return chartSeriesDPt[formatSet.Type]
  788. }
  789. // drawChartSeriesCat provides a function to draw the c:cat element by given
  790. // chart series and format sets.
  791. func (f *File) drawChartSeriesCat(v formatChartSeries, formatSet *formatChart) *cCat {
  792. cat := &cCat{
  793. StrRef: &cStrRef{
  794. F: v.Categories,
  795. },
  796. }
  797. chartSeriesCat := map[string]*cCat{Scatter: nil, Bubble: nil, Bubble3D: nil}
  798. if _, ok := chartSeriesCat[formatSet.Type]; ok || v.Categories == "" {
  799. return nil
  800. }
  801. return cat
  802. }
  803. // drawChartSeriesVal provides a function to draw the c:val element by given
  804. // chart series and format sets.
  805. func (f *File) drawChartSeriesVal(v formatChartSeries, formatSet *formatChart) *cVal {
  806. val := &cVal{
  807. NumRef: &cNumRef{
  808. F: v.Values,
  809. },
  810. }
  811. chartSeriesVal := map[string]*cVal{Scatter: nil, Bubble: nil, Bubble3D: nil}
  812. if _, ok := chartSeriesVal[formatSet.Type]; ok {
  813. return nil
  814. }
  815. return val
  816. }
  817. // drawChartSeriesMarker provides a function to draw the c:marker element by
  818. // given data index and format sets.
  819. func (f *File) drawChartSeriesMarker(i int, formatSet *formatChart) *cMarker {
  820. defaultSymbol := map[string]*attrValString{Scatter: &attrValString{Val: stringPtr("circle")}}
  821. marker := &cMarker{
  822. Symbol: defaultSymbol[formatSet.Type],
  823. Size: &attrValInt{Val: intPtr(5)},
  824. }
  825. if symbol := stringPtr(formatSet.Series[i].Marker.Symbol); *symbol != "" {
  826. marker.Symbol = &attrValString{Val: symbol}
  827. }
  828. if size := intPtr(formatSet.Series[i].Marker.Size); *size != 0 {
  829. marker.Size = &attrValInt{Val: size}
  830. }
  831. if i < 6 {
  832. marker.SpPr = &cSpPr{
  833. SolidFill: &aSolidFill{
  834. SchemeClr: &aSchemeClr{
  835. Val: "accent" + strconv.Itoa(i+1),
  836. },
  837. },
  838. Ln: &aLn{
  839. W: 9252,
  840. SolidFill: &aSolidFill{
  841. SchemeClr: &aSchemeClr{
  842. Val: "accent" + strconv.Itoa(i+1),
  843. },
  844. },
  845. },
  846. }
  847. }
  848. chartSeriesMarker := map[string]*cMarker{Scatter: marker, Line: marker}
  849. return chartSeriesMarker[formatSet.Type]
  850. }
  851. // drawChartSeriesXVal provides a function to draw the c:xVal element by given
  852. // chart series and format sets.
  853. func (f *File) drawChartSeriesXVal(v formatChartSeries, formatSet *formatChart) *cCat {
  854. cat := &cCat{
  855. StrRef: &cStrRef{
  856. F: v.Categories,
  857. },
  858. }
  859. chartSeriesXVal := map[string]*cCat{Scatter: cat}
  860. return chartSeriesXVal[formatSet.Type]
  861. }
  862. // drawChartSeriesYVal provides a function to draw the c:yVal element by given
  863. // chart series and format sets.
  864. func (f *File) drawChartSeriesYVal(v formatChartSeries, formatSet *formatChart) *cVal {
  865. val := &cVal{
  866. NumRef: &cNumRef{
  867. F: v.Values,
  868. },
  869. }
  870. chartSeriesYVal := map[string]*cVal{Scatter: val, Bubble: val, Bubble3D: val}
  871. return chartSeriesYVal[formatSet.Type]
  872. }
  873. // drawCharSeriesBubbleSize provides a function to draw the c:bubbleSize
  874. // element by given chart series and format sets.
  875. func (f *File) drawCharSeriesBubbleSize(v formatChartSeries, formatSet *formatChart) *cVal {
  876. if _, ok := map[string]bool{Bubble: true, Bubble3D: true}[formatSet.Type]; !ok {
  877. return nil
  878. }
  879. return &cVal{
  880. NumRef: &cNumRef{
  881. F: v.Values,
  882. },
  883. }
  884. }
  885. // drawCharSeriesBubble3D provides a function to draw the c:bubble3D element
  886. // by given format sets.
  887. func (f *File) drawCharSeriesBubble3D(formatSet *formatChart) *attrValBool {
  888. if _, ok := map[string]bool{Bubble3D: true}[formatSet.Type]; !ok {
  889. return nil
  890. }
  891. return &attrValBool{Val: boolPtr(true)}
  892. }
  893. // drawChartDLbls provides a function to draw the c:dLbls element by given
  894. // format sets.
  895. func (f *File) drawChartDLbls(formatSet *formatChart) *cDLbls {
  896. return &cDLbls{
  897. ShowLegendKey: &attrValBool{Val: boolPtr(formatSet.Legend.ShowLegendKey)},
  898. ShowVal: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowVal)},
  899. ShowCatName: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowCatName)},
  900. ShowSerName: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowSerName)},
  901. ShowBubbleSize: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowBubbleSize)},
  902. ShowPercent: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowPercent)},
  903. ShowLeaderLines: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowLeaderLines)},
  904. }
  905. }
  906. // drawChartSeriesDLbls provides a function to draw the c:dLbls element by
  907. // given format sets.
  908. func (f *File) drawChartSeriesDLbls(formatSet *formatChart) *cDLbls {
  909. dLbls := f.drawChartDLbls(formatSet)
  910. chartSeriesDLbls := map[string]*cDLbls{Scatter: nil, Surface3D: nil, WireframeSurface3D: nil, Contour: nil, WireframeContour: nil, Bubble: nil, Bubble3D: nil}
  911. if _, ok := chartSeriesDLbls[formatSet.Type]; ok {
  912. return nil
  913. }
  914. return dLbls
  915. }
  916. // drawPlotAreaCatAx provides a function to draw the c:catAx element.
  917. func (f *File) drawPlotAreaCatAx(formatSet *formatChart) []*cAxs {
  918. min := &attrValFloat{Val: float64Ptr(formatSet.XAxis.Minimum)}
  919. max := &attrValFloat{Val: float64Ptr(formatSet.XAxis.Maximum)}
  920. if formatSet.XAxis.Minimum == 0 {
  921. min = nil
  922. }
  923. if formatSet.XAxis.Maximum == 0 {
  924. max = nil
  925. }
  926. axs := []*cAxs{
  927. {
  928. AxID: &attrValInt{Val: intPtr(754001152)},
  929. Scaling: &cScaling{
  930. Orientation: &attrValString{Val: stringPtr(orientation[formatSet.XAxis.ReverseOrder])},
  931. Max: max,
  932. Min: min,
  933. },
  934. Delete: &attrValBool{Val: boolPtr(false)},
  935. AxPos: &attrValString{Val: stringPtr(catAxPos[formatSet.XAxis.ReverseOrder])},
  936. NumFmt: &cNumFmt{
  937. FormatCode: "General",
  938. SourceLinked: true,
  939. },
  940. MajorTickMark: &attrValString{Val: stringPtr("none")},
  941. MinorTickMark: &attrValString{Val: stringPtr("none")},
  942. TickLblPos: &attrValString{Val: stringPtr("nextTo")},
  943. SpPr: f.drawPlotAreaSpPr(),
  944. TxPr: f.drawPlotAreaTxPr(),
  945. CrossAx: &attrValInt{Val: intPtr(753999904)},
  946. Crosses: &attrValString{Val: stringPtr("autoZero")},
  947. Auto: &attrValBool{Val: boolPtr(true)},
  948. LblAlgn: &attrValString{Val: stringPtr("ctr")},
  949. LblOffset: &attrValInt{Val: intPtr(100)},
  950. NoMultiLvlLbl: &attrValBool{Val: boolPtr(false)},
  951. },
  952. }
  953. if formatSet.XAxis.MajorGridlines {
  954. axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  955. }
  956. if formatSet.XAxis.MinorGridlines {
  957. axs[0].MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  958. }
  959. if formatSet.XAxis.TickLabelSkip != 0 {
  960. axs[0].TickLblSkip = &attrValInt{Val: intPtr(formatSet.XAxis.TickLabelSkip)}
  961. }
  962. return axs
  963. }
  964. // drawPlotAreaValAx provides a function to draw the c:valAx element.
  965. func (f *File) drawPlotAreaValAx(formatSet *formatChart) []*cAxs {
  966. min := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Minimum)}
  967. max := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Maximum)}
  968. if formatSet.YAxis.Minimum == 0 {
  969. min = nil
  970. }
  971. if formatSet.YAxis.Maximum == 0 {
  972. max = nil
  973. }
  974. var logBase *attrValFloat
  975. if formatSet.YAxis.LogBase >= 2 && formatSet.YAxis.LogBase <= 1000 {
  976. logBase = &attrValFloat{Val: float64Ptr(formatSet.YAxis.LogBase)}
  977. }
  978. axs := []*cAxs{
  979. {
  980. AxID: &attrValInt{Val: intPtr(753999904)},
  981. Scaling: &cScaling{
  982. LogBase: logBase,
  983. Orientation: &attrValString{Val: stringPtr(orientation[formatSet.YAxis.ReverseOrder])},
  984. Max: max,
  985. Min: min,
  986. },
  987. Delete: &attrValBool{Val: boolPtr(false)},
  988. AxPos: &attrValString{Val: stringPtr(valAxPos[formatSet.YAxis.ReverseOrder])},
  989. NumFmt: &cNumFmt{
  990. FormatCode: chartValAxNumFmtFormatCode[formatSet.Type],
  991. SourceLinked: true,
  992. },
  993. MajorTickMark: &attrValString{Val: stringPtr("none")},
  994. MinorTickMark: &attrValString{Val: stringPtr("none")},
  995. TickLblPos: &attrValString{Val: stringPtr("nextTo")},
  996. SpPr: f.drawPlotAreaSpPr(),
  997. TxPr: f.drawPlotAreaTxPr(),
  998. CrossAx: &attrValInt{Val: intPtr(754001152)},
  999. Crosses: &attrValString{Val: stringPtr("autoZero")},
  1000. CrossBetween: &attrValString{Val: stringPtr(chartValAxCrossBetween[formatSet.Type])},
  1001. },
  1002. }
  1003. if formatSet.YAxis.MajorGridlines {
  1004. axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  1005. }
  1006. if formatSet.YAxis.MinorGridlines {
  1007. axs[0].MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  1008. }
  1009. if pos, ok := valTickLblPos[formatSet.Type]; ok {
  1010. axs[0].TickLblPos.Val = stringPtr(pos)
  1011. }
  1012. if formatSet.YAxis.MajorUnit != 0 {
  1013. axs[0].MajorUnit = &attrValFloat{Val: float64Ptr(formatSet.YAxis.MajorUnit)}
  1014. }
  1015. return axs
  1016. }
  1017. // drawPlotAreaSerAx provides a function to draw the c:serAx element.
  1018. func (f *File) drawPlotAreaSerAx(formatSet *formatChart) []*cAxs {
  1019. min := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Minimum)}
  1020. max := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Maximum)}
  1021. if formatSet.YAxis.Minimum == 0 {
  1022. min = nil
  1023. }
  1024. if formatSet.YAxis.Maximum == 0 {
  1025. max = nil
  1026. }
  1027. return []*cAxs{
  1028. {
  1029. AxID: &attrValInt{Val: intPtr(832256642)},
  1030. Scaling: &cScaling{
  1031. Orientation: &attrValString{Val: stringPtr(orientation[formatSet.YAxis.ReverseOrder])},
  1032. Max: max,
  1033. Min: min,
  1034. },
  1035. Delete: &attrValBool{Val: boolPtr(false)},
  1036. AxPos: &attrValString{Val: stringPtr(catAxPos[formatSet.XAxis.ReverseOrder])},
  1037. TickLblPos: &attrValString{Val: stringPtr("nextTo")},
  1038. SpPr: f.drawPlotAreaSpPr(),
  1039. TxPr: f.drawPlotAreaTxPr(),
  1040. CrossAx: &attrValInt{Val: intPtr(753999904)},
  1041. },
  1042. }
  1043. }
  1044. // drawPlotAreaSpPr provides a function to draw the c:spPr element.
  1045. func (f *File) drawPlotAreaSpPr() *cSpPr {
  1046. return &cSpPr{
  1047. Ln: &aLn{
  1048. W: 9525,
  1049. Cap: "flat",
  1050. Cmpd: "sng",
  1051. Algn: "ctr",
  1052. SolidFill: &aSolidFill{
  1053. SchemeClr: &aSchemeClr{
  1054. Val: "tx1",
  1055. LumMod: &attrValInt{Val: intPtr(15000)},
  1056. LumOff: &attrValInt{Val: intPtr(85000)},
  1057. },
  1058. },
  1059. },
  1060. }
  1061. }
  1062. // drawPlotAreaTxPr provides a function to draw the c:txPr element.
  1063. func (f *File) drawPlotAreaTxPr() *cTxPr {
  1064. return &cTxPr{
  1065. BodyPr: aBodyPr{
  1066. Rot: -60000000,
  1067. SpcFirstLastPara: true,
  1068. VertOverflow: "ellipsis",
  1069. Vert: "horz",
  1070. Wrap: "square",
  1071. Anchor: "ctr",
  1072. AnchorCtr: true,
  1073. },
  1074. P: aP{
  1075. PPr: &aPPr{
  1076. DefRPr: aRPr{
  1077. Sz: 900,
  1078. B: false,
  1079. I: false,
  1080. U: "none",
  1081. Strike: "noStrike",
  1082. Kern: 1200,
  1083. Baseline: 0,
  1084. SolidFill: &aSolidFill{
  1085. SchemeClr: &aSchemeClr{
  1086. Val: "tx1",
  1087. LumMod: &attrValInt{Val: intPtr(15000)},
  1088. LumOff: &attrValInt{Val: intPtr(85000)},
  1089. },
  1090. },
  1091. Latin: &aLatin{Typeface: "+mn-lt"},
  1092. Ea: &aEa{Typeface: "+mn-ea"},
  1093. Cs: &aCs{Typeface: "+mn-cs"},
  1094. },
  1095. },
  1096. EndParaRPr: &aEndParaRPr{Lang: "en-US"},
  1097. },
  1098. }
  1099. }
  1100. // drawingParser provides a function to parse drawingXML. In order to solve
  1101. // the problem that the label structure is changed after serialization and
  1102. // deserialization, two different structures: decodeWsDr and encodeWsDr are
  1103. // defined.
  1104. func (f *File) drawingParser(path string) (*xlsxWsDr, int) {
  1105. var (
  1106. err error
  1107. ok bool
  1108. )
  1109. if f.Drawings[path] == nil {
  1110. content := xlsxWsDr{}
  1111. content.A = NameSpaceDrawingML.Value
  1112. content.Xdr = NameSpaceDrawingMLSpreadSheet.Value
  1113. if _, ok = f.XLSX[path]; ok { // Append Model
  1114. decodeWsDr := decodeWsDr{}
  1115. if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(path)))).
  1116. Decode(&decodeWsDr); err != nil && err != io.EOF {
  1117. log.Printf("xml decode error: %s", err)
  1118. }
  1119. content.R = decodeWsDr.R
  1120. for _, v := range decodeWsDr.OneCellAnchor {
  1121. content.OneCellAnchor = append(content.OneCellAnchor, &xdrCellAnchor{
  1122. EditAs: v.EditAs,
  1123. GraphicFrame: v.Content,
  1124. })
  1125. }
  1126. for _, v := range decodeWsDr.TwoCellAnchor {
  1127. content.TwoCellAnchor = append(content.TwoCellAnchor, &xdrCellAnchor{
  1128. EditAs: v.EditAs,
  1129. GraphicFrame: v.Content,
  1130. })
  1131. }
  1132. }
  1133. f.Drawings[path] = &content
  1134. }
  1135. wsDr := f.Drawings[path]
  1136. return wsDr, len(wsDr.OneCellAnchor) + len(wsDr.TwoCellAnchor) + 2
  1137. }
  1138. // addDrawingChart provides a function to add chart graphic frame by given
  1139. // sheet, drawingXML, cell, width, height, relationship index and format sets.
  1140. func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rID int, formatSet *formatPicture) error {
  1141. col, row, err := CellNameToCoordinates(cell)
  1142. if err != nil {
  1143. return err
  1144. }
  1145. colIdx := col - 1
  1146. rowIdx := row - 1
  1147. width = int(float64(width) * formatSet.XScale)
  1148. height = int(float64(height) * formatSet.YScale)
  1149. colStart, rowStart, colEnd, rowEnd, x2, y2 :=
  1150. f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.OffsetX, formatSet.OffsetY, width, height)
  1151. content, cNvPrID := f.drawingParser(drawingXML)
  1152. twoCellAnchor := xdrCellAnchor{}
  1153. twoCellAnchor.EditAs = formatSet.Positioning
  1154. from := xlsxFrom{}
  1155. from.Col = colStart
  1156. from.ColOff = formatSet.OffsetX * EMU
  1157. from.Row = rowStart
  1158. from.RowOff = formatSet.OffsetY * EMU
  1159. to := xlsxTo{}
  1160. to.Col = colEnd
  1161. to.ColOff = x2 * EMU
  1162. to.Row = rowEnd
  1163. to.RowOff = y2 * EMU
  1164. twoCellAnchor.From = &from
  1165. twoCellAnchor.To = &to
  1166. graphicFrame := xlsxGraphicFrame{
  1167. NvGraphicFramePr: xlsxNvGraphicFramePr{
  1168. CNvPr: &xlsxCNvPr{
  1169. ID: cNvPrID,
  1170. Name: "Chart " + strconv.Itoa(cNvPrID),
  1171. },
  1172. },
  1173. Graphic: &xlsxGraphic{
  1174. GraphicData: &xlsxGraphicData{
  1175. URI: NameSpaceDrawingMLChart.Value,
  1176. Chart: &xlsxChart{
  1177. C: NameSpaceDrawingMLChart.Value,
  1178. R: SourceRelationship.Value,
  1179. RID: "rId" + strconv.Itoa(rID),
  1180. },
  1181. },
  1182. },
  1183. }
  1184. graphic, _ := xml.Marshal(graphicFrame)
  1185. twoCellAnchor.GraphicFrame = string(graphic)
  1186. twoCellAnchor.ClientData = &xdrClientData{
  1187. FLocksWithSheet: formatSet.FLocksWithSheet,
  1188. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  1189. }
  1190. content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
  1191. f.Drawings[drawingXML] = content
  1192. return err
  1193. }
  1194. // addSheetDrawingChart provides a function to add chart graphic frame for
  1195. // chartsheet by given sheet, drawingXML, width, height, relationship index
  1196. // and format sets.
  1197. func (f *File) addSheetDrawingChart(drawingXML string, rID int, formatSet *formatPicture) {
  1198. content, cNvPrID := f.drawingParser(drawingXML)
  1199. absoluteAnchor := xdrCellAnchor{
  1200. EditAs: formatSet.Positioning,
  1201. Pos: &xlsxPoint2D{},
  1202. Ext: &xlsxExt{},
  1203. }
  1204. graphicFrame := xlsxGraphicFrame{
  1205. NvGraphicFramePr: xlsxNvGraphicFramePr{
  1206. CNvPr: &xlsxCNvPr{
  1207. ID: cNvPrID,
  1208. Name: "Chart " + strconv.Itoa(cNvPrID),
  1209. },
  1210. },
  1211. Graphic: &xlsxGraphic{
  1212. GraphicData: &xlsxGraphicData{
  1213. URI: NameSpaceDrawingMLChart.Value,
  1214. Chart: &xlsxChart{
  1215. C: NameSpaceDrawingMLChart.Value,
  1216. R: SourceRelationship.Value,
  1217. RID: "rId" + strconv.Itoa(rID),
  1218. },
  1219. },
  1220. },
  1221. }
  1222. graphic, _ := xml.Marshal(graphicFrame)
  1223. absoluteAnchor.GraphicFrame = string(graphic)
  1224. absoluteAnchor.ClientData = &xdrClientData{
  1225. FLocksWithSheet: formatSet.FLocksWithSheet,
  1226. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  1227. }
  1228. content.AbsoluteAnchor = append(content.AbsoluteAnchor, &absoluteAnchor)
  1229. f.Drawings[drawingXML] = content
  1230. return
  1231. }
  1232. // deleteDrawing provides a function to delete chart graphic frame by given by
  1233. // given coordinates and graphic type.
  1234. func (f *File) deleteDrawing(col, row int, drawingXML, drawingType string) (err error) {
  1235. var (
  1236. wsDr *xlsxWsDr
  1237. deTwoCellAnchor *decodeTwoCellAnchor
  1238. )
  1239. xdrCellAnchorFuncs := map[string]func(anchor *xdrCellAnchor) bool{
  1240. "Chart": func(anchor *xdrCellAnchor) bool { return anchor.Pic == nil },
  1241. "Pic": func(anchor *xdrCellAnchor) bool { return anchor.Pic != nil },
  1242. }
  1243. decodeTwoCellAnchorFuncs := map[string]func(anchor *decodeTwoCellAnchor) bool{
  1244. "Chart": func(anchor *decodeTwoCellAnchor) bool { return anchor.Pic == nil },
  1245. "Pic": func(anchor *decodeTwoCellAnchor) bool { return anchor.Pic != nil },
  1246. }
  1247. wsDr, _ = f.drawingParser(drawingXML)
  1248. for idx := 0; idx < len(wsDr.TwoCellAnchor); idx++ {
  1249. if err = nil; wsDr.TwoCellAnchor[idx].From != nil && xdrCellAnchorFuncs[drawingType](wsDr.TwoCellAnchor[idx]) {
  1250. if wsDr.TwoCellAnchor[idx].From.Col == col && wsDr.TwoCellAnchor[idx].From.Row == row {
  1251. wsDr.TwoCellAnchor = append(wsDr.TwoCellAnchor[:idx], wsDr.TwoCellAnchor[idx+1:]...)
  1252. idx--
  1253. }
  1254. }
  1255. }
  1256. for idx := 0; idx < len(wsDr.TwoCellAnchor); idx++ {
  1257. deTwoCellAnchor = new(decodeTwoCellAnchor)
  1258. if err = f.xmlNewDecoder(strings.NewReader("<decodeTwoCellAnchor>" + wsDr.TwoCellAnchor[idx].GraphicFrame + "</decodeTwoCellAnchor>")).
  1259. Decode(deTwoCellAnchor); err != nil && err != io.EOF {
  1260. err = fmt.Errorf("xml decode error: %s", err)
  1261. return
  1262. }
  1263. if err = nil; deTwoCellAnchor.From != nil && decodeTwoCellAnchorFuncs[drawingType](deTwoCellAnchor) {
  1264. if deTwoCellAnchor.From.Col == col && deTwoCellAnchor.From.Row == row {
  1265. wsDr.TwoCellAnchor = append(wsDr.TwoCellAnchor[:idx], wsDr.TwoCellAnchor[idx+1:]...)
  1266. idx--
  1267. }
  1268. }
  1269. }
  1270. f.Drawings[drawingXML] = wsDr
  1271. return err
  1272. }