drawing.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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. if formatSet.Legend.None {
  234. xlsxChartSpace.Chart.Legend = nil
  235. }
  236. addChart := func(c, p *cPlotArea) {
  237. immutable, mutable := reflect.ValueOf(c).Elem(), reflect.ValueOf(p).Elem()
  238. for i := 0; i < mutable.NumField(); i++ {
  239. field := mutable.Field(i)
  240. if field.IsNil() {
  241. continue
  242. }
  243. immutable.FieldByName(mutable.Type().Field(i).Name).Set(field)
  244. }
  245. }
  246. addChart(xlsxChartSpace.Chart.PlotArea, plotAreaFunc[formatSet.Type](formatSet))
  247. order := len(formatSet.Series)
  248. for idx := range comboCharts {
  249. comboCharts[idx].order = order
  250. addChart(xlsxChartSpace.Chart.PlotArea, plotAreaFunc[comboCharts[idx].Type](comboCharts[idx]))
  251. order += len(comboCharts[idx].Series)
  252. }
  253. chart, _ := xml.Marshal(xlsxChartSpace)
  254. media := "xl/charts/chart" + strconv.Itoa(count+1) + ".xml"
  255. f.saveFileList(media, chart)
  256. }
  257. // drawBaseChart provides a function to draw the c:plotArea element for bar,
  258. // and column series charts by given format sets.
  259. func (f *File) drawBaseChart(formatSet *formatChart) *cPlotArea {
  260. c := cCharts{
  261. BarDir: &attrValString{
  262. Val: stringPtr("col"),
  263. },
  264. Grouping: &attrValString{
  265. Val: stringPtr("clustered"),
  266. },
  267. VaryColors: &attrValBool{
  268. Val: boolPtr(true),
  269. },
  270. Ser: f.drawChartSeries(formatSet),
  271. Shape: f.drawChartShape(formatSet),
  272. DLbls: f.drawChartDLbls(formatSet),
  273. AxID: []*attrValInt{
  274. {Val: intPtr(754001152)},
  275. {Val: intPtr(753999904)},
  276. },
  277. Overlap: &attrValInt{Val: intPtr(100)},
  278. }
  279. var ok bool
  280. if *c.BarDir.Val, ok = plotAreaChartBarDir[formatSet.Type]; !ok {
  281. c.BarDir = nil
  282. }
  283. if *c.Grouping.Val, ok = plotAreaChartGrouping[formatSet.Type]; !ok {
  284. c.Grouping = nil
  285. }
  286. if *c.Overlap.Val, ok = plotAreaChartOverlap[formatSet.Type]; !ok {
  287. c.Overlap = nil
  288. }
  289. catAx := f.drawPlotAreaCatAx(formatSet)
  290. valAx := f.drawPlotAreaValAx(formatSet)
  291. charts := map[string]*cPlotArea{
  292. "area": {
  293. AreaChart: &c,
  294. CatAx: catAx,
  295. ValAx: valAx,
  296. },
  297. "areaStacked": {
  298. AreaChart: &c,
  299. CatAx: catAx,
  300. ValAx: valAx,
  301. },
  302. "areaPercentStacked": {
  303. AreaChart: &c,
  304. CatAx: catAx,
  305. ValAx: valAx,
  306. },
  307. "area3D": {
  308. Area3DChart: &c,
  309. CatAx: catAx,
  310. ValAx: valAx,
  311. },
  312. "area3DStacked": {
  313. Area3DChart: &c,
  314. CatAx: catAx,
  315. ValAx: valAx,
  316. },
  317. "area3DPercentStacked": {
  318. Area3DChart: &c,
  319. CatAx: catAx,
  320. ValAx: valAx,
  321. },
  322. "bar": {
  323. BarChart: &c,
  324. CatAx: catAx,
  325. ValAx: valAx,
  326. },
  327. "barStacked": {
  328. BarChart: &c,
  329. CatAx: catAx,
  330. ValAx: valAx,
  331. },
  332. "barPercentStacked": {
  333. BarChart: &c,
  334. CatAx: catAx,
  335. ValAx: valAx,
  336. },
  337. "bar3DClustered": {
  338. Bar3DChart: &c,
  339. CatAx: catAx,
  340. ValAx: valAx,
  341. },
  342. "bar3DStacked": {
  343. Bar3DChart: &c,
  344. CatAx: catAx,
  345. ValAx: valAx,
  346. },
  347. "bar3DPercentStacked": {
  348. Bar3DChart: &c,
  349. CatAx: catAx,
  350. ValAx: valAx,
  351. },
  352. "bar3DConeClustered": {
  353. Bar3DChart: &c,
  354. CatAx: catAx,
  355. ValAx: valAx,
  356. },
  357. "bar3DConeStacked": {
  358. Bar3DChart: &c,
  359. CatAx: catAx,
  360. ValAx: valAx,
  361. },
  362. "bar3DConePercentStacked": {
  363. Bar3DChart: &c,
  364. CatAx: catAx,
  365. ValAx: valAx,
  366. },
  367. "bar3DPyramidClustered": {
  368. Bar3DChart: &c,
  369. CatAx: catAx,
  370. ValAx: valAx,
  371. },
  372. "bar3DPyramidStacked": {
  373. Bar3DChart: &c,
  374. CatAx: catAx,
  375. ValAx: valAx,
  376. },
  377. "bar3DPyramidPercentStacked": {
  378. Bar3DChart: &c,
  379. CatAx: catAx,
  380. ValAx: valAx,
  381. },
  382. "bar3DCylinderClustered": {
  383. Bar3DChart: &c,
  384. CatAx: catAx,
  385. ValAx: valAx,
  386. },
  387. "bar3DCylinderStacked": {
  388. Bar3DChart: &c,
  389. CatAx: catAx,
  390. ValAx: valAx,
  391. },
  392. "bar3DCylinderPercentStacked": {
  393. Bar3DChart: &c,
  394. CatAx: catAx,
  395. ValAx: valAx,
  396. },
  397. "col": {
  398. BarChart: &c,
  399. CatAx: catAx,
  400. ValAx: valAx,
  401. },
  402. "colStacked": {
  403. BarChart: &c,
  404. CatAx: catAx,
  405. ValAx: valAx,
  406. },
  407. "colPercentStacked": {
  408. BarChart: &c,
  409. CatAx: catAx,
  410. ValAx: valAx,
  411. },
  412. "col3D": {
  413. Bar3DChart: &c,
  414. CatAx: catAx,
  415. ValAx: valAx,
  416. },
  417. "col3DClustered": {
  418. Bar3DChart: &c,
  419. CatAx: catAx,
  420. ValAx: valAx,
  421. },
  422. "col3DStacked": {
  423. Bar3DChart: &c,
  424. CatAx: catAx,
  425. ValAx: valAx,
  426. },
  427. "col3DPercentStacked": {
  428. Bar3DChart: &c,
  429. CatAx: catAx,
  430. ValAx: valAx,
  431. },
  432. "col3DCone": {
  433. Bar3DChart: &c,
  434. CatAx: catAx,
  435. ValAx: valAx,
  436. },
  437. "col3DConeClustered": {
  438. Bar3DChart: &c,
  439. CatAx: catAx,
  440. ValAx: valAx,
  441. },
  442. "col3DConeStacked": {
  443. Bar3DChart: &c,
  444. CatAx: catAx,
  445. ValAx: valAx,
  446. },
  447. "col3DConePercentStacked": {
  448. Bar3DChart: &c,
  449. CatAx: catAx,
  450. ValAx: valAx,
  451. },
  452. "col3DPyramid": {
  453. Bar3DChart: &c,
  454. CatAx: catAx,
  455. ValAx: valAx,
  456. },
  457. "col3DPyramidClustered": {
  458. Bar3DChart: &c,
  459. CatAx: catAx,
  460. ValAx: valAx,
  461. },
  462. "col3DPyramidStacked": {
  463. Bar3DChart: &c,
  464. CatAx: catAx,
  465. ValAx: valAx,
  466. },
  467. "col3DPyramidPercentStacked": {
  468. Bar3DChart: &c,
  469. CatAx: catAx,
  470. ValAx: valAx,
  471. },
  472. "col3DCylinder": {
  473. Bar3DChart: &c,
  474. CatAx: catAx,
  475. ValAx: valAx,
  476. },
  477. "col3DCylinderClustered": {
  478. Bar3DChart: &c,
  479. CatAx: catAx,
  480. ValAx: valAx,
  481. },
  482. "col3DCylinderStacked": {
  483. Bar3DChart: &c,
  484. CatAx: catAx,
  485. ValAx: valAx,
  486. },
  487. "col3DCylinderPercentStacked": {
  488. Bar3DChart: &c,
  489. CatAx: catAx,
  490. ValAx: valAx,
  491. },
  492. "bubble": {
  493. BubbleChart: &c,
  494. CatAx: catAx,
  495. ValAx: valAx,
  496. },
  497. "bubble3D": {
  498. BubbleChart: &c,
  499. CatAx: catAx,
  500. ValAx: valAx,
  501. },
  502. }
  503. return charts[formatSet.Type]
  504. }
  505. // drawDoughnutChart provides a function to draw the c:plotArea element for
  506. // doughnut chart by given format sets.
  507. func (f *File) drawDoughnutChart(formatSet *formatChart) *cPlotArea {
  508. return &cPlotArea{
  509. DoughnutChart: &cCharts{
  510. VaryColors: &attrValBool{
  511. Val: boolPtr(true),
  512. },
  513. Ser: f.drawChartSeries(formatSet),
  514. HoleSize: &attrValInt{Val: intPtr(75)},
  515. },
  516. }
  517. }
  518. // drawLineChart provides a function to draw the c:plotArea element for line
  519. // chart by given format sets.
  520. func (f *File) drawLineChart(formatSet *formatChart) *cPlotArea {
  521. return &cPlotArea{
  522. LineChart: &cCharts{
  523. Grouping: &attrValString{
  524. Val: stringPtr(plotAreaChartGrouping[formatSet.Type]),
  525. },
  526. VaryColors: &attrValBool{
  527. Val: boolPtr(false),
  528. },
  529. Ser: f.drawChartSeries(formatSet),
  530. DLbls: f.drawChartDLbls(formatSet),
  531. Smooth: &attrValBool{
  532. Val: boolPtr(false),
  533. },
  534. AxID: []*attrValInt{
  535. {Val: intPtr(754001152)},
  536. {Val: intPtr(753999904)},
  537. },
  538. },
  539. CatAx: f.drawPlotAreaCatAx(formatSet),
  540. ValAx: f.drawPlotAreaValAx(formatSet),
  541. }
  542. }
  543. // drawPieChart provides a function to draw the c:plotArea element for pie
  544. // chart by given format sets.
  545. func (f *File) drawPieChart(formatSet *formatChart) *cPlotArea {
  546. return &cPlotArea{
  547. PieChart: &cCharts{
  548. VaryColors: &attrValBool{
  549. Val: boolPtr(true),
  550. },
  551. Ser: f.drawChartSeries(formatSet),
  552. },
  553. }
  554. }
  555. // drawPie3DChart provides a function to draw the c:plotArea element for 3D
  556. // pie chart by given format sets.
  557. func (f *File) drawPie3DChart(formatSet *formatChart) *cPlotArea {
  558. return &cPlotArea{
  559. Pie3DChart: &cCharts{
  560. VaryColors: &attrValBool{
  561. Val: boolPtr(true),
  562. },
  563. Ser: f.drawChartSeries(formatSet),
  564. },
  565. }
  566. }
  567. // drawPieOfPieChart provides a function to draw the c:plotArea element for
  568. // pie chart by given format sets.
  569. func (f *File) drawPieOfPieChart(formatSet *formatChart) *cPlotArea {
  570. return &cPlotArea{
  571. OfPieChart: &cCharts{
  572. OfPieType: &attrValString{
  573. Val: stringPtr("pie"),
  574. },
  575. VaryColors: &attrValBool{
  576. Val: boolPtr(true),
  577. },
  578. Ser: f.drawChartSeries(formatSet),
  579. SerLines: &attrValString{},
  580. },
  581. }
  582. }
  583. // drawBarOfPieChart provides a function to draw the c:plotArea element for
  584. // pie chart by given format sets.
  585. func (f *File) drawBarOfPieChart(formatSet *formatChart) *cPlotArea {
  586. return &cPlotArea{
  587. OfPieChart: &cCharts{
  588. OfPieType: &attrValString{
  589. Val: stringPtr("bar"),
  590. },
  591. VaryColors: &attrValBool{
  592. Val: boolPtr(true),
  593. },
  594. Ser: f.drawChartSeries(formatSet),
  595. SerLines: &attrValString{},
  596. },
  597. }
  598. }
  599. // drawRadarChart provides a function to draw the c:plotArea element for radar
  600. // chart by given format sets.
  601. func (f *File) drawRadarChart(formatSet *formatChart) *cPlotArea {
  602. return &cPlotArea{
  603. RadarChart: &cCharts{
  604. RadarStyle: &attrValString{
  605. Val: stringPtr("marker"),
  606. },
  607. VaryColors: &attrValBool{
  608. Val: boolPtr(false),
  609. },
  610. Ser: f.drawChartSeries(formatSet),
  611. DLbls: f.drawChartDLbls(formatSet),
  612. AxID: []*attrValInt{
  613. {Val: intPtr(754001152)},
  614. {Val: intPtr(753999904)},
  615. },
  616. },
  617. CatAx: f.drawPlotAreaCatAx(formatSet),
  618. ValAx: f.drawPlotAreaValAx(formatSet),
  619. }
  620. }
  621. // drawScatterChart provides a function to draw the c:plotArea element for
  622. // scatter chart by given format sets.
  623. func (f *File) drawScatterChart(formatSet *formatChart) *cPlotArea {
  624. return &cPlotArea{
  625. ScatterChart: &cCharts{
  626. ScatterStyle: &attrValString{
  627. Val: stringPtr("smoothMarker"), // line,lineMarker,marker,none,smooth,smoothMarker
  628. },
  629. VaryColors: &attrValBool{
  630. Val: boolPtr(false),
  631. },
  632. Ser: f.drawChartSeries(formatSet),
  633. DLbls: f.drawChartDLbls(formatSet),
  634. AxID: []*attrValInt{
  635. {Val: intPtr(754001152)},
  636. {Val: intPtr(753999904)},
  637. },
  638. },
  639. CatAx: f.drawPlotAreaCatAx(formatSet),
  640. ValAx: f.drawPlotAreaValAx(formatSet),
  641. }
  642. }
  643. // drawSurface3DChart provides a function to draw the c:surface3DChart element by
  644. // given format sets.
  645. func (f *File) drawSurface3DChart(formatSet *formatChart) *cPlotArea {
  646. plotArea := &cPlotArea{
  647. Surface3DChart: &cCharts{
  648. Ser: f.drawChartSeries(formatSet),
  649. AxID: []*attrValInt{
  650. {Val: intPtr(754001152)},
  651. {Val: intPtr(753999904)},
  652. {Val: intPtr(832256642)},
  653. },
  654. },
  655. CatAx: f.drawPlotAreaCatAx(formatSet),
  656. ValAx: f.drawPlotAreaValAx(formatSet),
  657. SerAx: f.drawPlotAreaSerAx(formatSet),
  658. }
  659. if formatSet.Type == WireframeSurface3D {
  660. plotArea.Surface3DChart.Wireframe = &attrValBool{Val: boolPtr(true)}
  661. }
  662. return plotArea
  663. }
  664. // drawSurfaceChart provides a function to draw the c:surfaceChart element by
  665. // given format sets.
  666. func (f *File) drawSurfaceChart(formatSet *formatChart) *cPlotArea {
  667. plotArea := &cPlotArea{
  668. SurfaceChart: &cCharts{
  669. Ser: f.drawChartSeries(formatSet),
  670. AxID: []*attrValInt{
  671. {Val: intPtr(754001152)},
  672. {Val: intPtr(753999904)},
  673. {Val: intPtr(832256642)},
  674. },
  675. },
  676. CatAx: f.drawPlotAreaCatAx(formatSet),
  677. ValAx: f.drawPlotAreaValAx(formatSet),
  678. SerAx: f.drawPlotAreaSerAx(formatSet),
  679. }
  680. if formatSet.Type == WireframeContour {
  681. plotArea.SurfaceChart.Wireframe = &attrValBool{Val: boolPtr(true)}
  682. }
  683. return plotArea
  684. }
  685. // drawChartShape provides a function to draw the c:shape element by given
  686. // format sets.
  687. func (f *File) drawChartShape(formatSet *formatChart) *attrValString {
  688. shapes := map[string]string{
  689. Bar3DConeClustered: "cone",
  690. Bar3DConeStacked: "cone",
  691. Bar3DConePercentStacked: "cone",
  692. Bar3DPyramidClustered: "pyramid",
  693. Bar3DPyramidStacked: "pyramid",
  694. Bar3DPyramidPercentStacked: "pyramid",
  695. Bar3DCylinderClustered: "cylinder",
  696. Bar3DCylinderStacked: "cylinder",
  697. Bar3DCylinderPercentStacked: "cylinder",
  698. Col3DCone: "cone",
  699. Col3DConeClustered: "cone",
  700. Col3DConeStacked: "cone",
  701. Col3DConePercentStacked: "cone",
  702. Col3DPyramid: "pyramid",
  703. Col3DPyramidClustered: "pyramid",
  704. Col3DPyramidStacked: "pyramid",
  705. Col3DPyramidPercentStacked: "pyramid",
  706. Col3DCylinder: "cylinder",
  707. Col3DCylinderClustered: "cylinder",
  708. Col3DCylinderStacked: "cylinder",
  709. Col3DCylinderPercentStacked: "cylinder",
  710. }
  711. if shape, ok := shapes[formatSet.Type]; ok {
  712. return &attrValString{Val: stringPtr(shape)}
  713. }
  714. return nil
  715. }
  716. // drawChartSeries provides a function to draw the c:ser element by given
  717. // format sets.
  718. func (f *File) drawChartSeries(formatSet *formatChart) *[]cSer {
  719. ser := []cSer{}
  720. for k := range formatSet.Series {
  721. ser = append(ser, cSer{
  722. IDx: &attrValInt{Val: intPtr(k + formatSet.order)},
  723. Order: &attrValInt{Val: intPtr(k + formatSet.order)},
  724. Tx: &cTx{
  725. StrRef: &cStrRef{
  726. F: formatSet.Series[k].Name,
  727. },
  728. },
  729. SpPr: f.drawChartSeriesSpPr(k, formatSet),
  730. Marker: f.drawChartSeriesMarker(k, formatSet),
  731. DPt: f.drawChartSeriesDPt(k, formatSet),
  732. DLbls: f.drawChartSeriesDLbls(formatSet),
  733. Cat: f.drawChartSeriesCat(formatSet.Series[k], formatSet),
  734. Val: f.drawChartSeriesVal(formatSet.Series[k], formatSet),
  735. XVal: f.drawChartSeriesXVal(formatSet.Series[k], formatSet),
  736. YVal: f.drawChartSeriesYVal(formatSet.Series[k], formatSet),
  737. BubbleSize: f.drawCharSeriesBubbleSize(formatSet.Series[k], formatSet),
  738. Bubble3D: f.drawCharSeriesBubble3D(formatSet),
  739. })
  740. }
  741. return &ser
  742. }
  743. // drawChartSeriesSpPr provides a function to draw the c:spPr element by given
  744. // format sets.
  745. func (f *File) drawChartSeriesSpPr(i int, formatSet *formatChart) *cSpPr {
  746. spPrScatter := &cSpPr{
  747. Ln: &aLn{
  748. W: 25400,
  749. NoFill: " ",
  750. },
  751. }
  752. spPrLine := &cSpPr{
  753. Ln: &aLn{
  754. W: f.ptToEMUs(formatSet.Series[i].Line.Width),
  755. Cap: "rnd", // rnd, sq, flat
  756. SolidFill: &aSolidFill{
  757. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa((formatSet.order+i)%6+1)},
  758. },
  759. },
  760. }
  761. chartSeriesSpPr := map[string]*cSpPr{Line: spPrLine, Scatter: spPrScatter}
  762. return chartSeriesSpPr[formatSet.Type]
  763. }
  764. // drawChartSeriesDPt provides a function to draw the c:dPt element by given
  765. // data index and format sets.
  766. func (f *File) drawChartSeriesDPt(i int, formatSet *formatChart) []*cDPt {
  767. dpt := []*cDPt{{
  768. IDx: &attrValInt{Val: intPtr(i)},
  769. Bubble3D: &attrValBool{Val: boolPtr(false)},
  770. SpPr: &cSpPr{
  771. SolidFill: &aSolidFill{
  772. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
  773. },
  774. Ln: &aLn{
  775. W: 25400,
  776. Cap: "rnd",
  777. SolidFill: &aSolidFill{
  778. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  779. },
  780. },
  781. Sp3D: &aSp3D{
  782. ContourW: 25400,
  783. ContourClr: &aContourClr{
  784. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  785. },
  786. },
  787. },
  788. }}
  789. chartSeriesDPt := map[string][]*cDPt{Pie: dpt, Pie3D: dpt}
  790. return chartSeriesDPt[formatSet.Type]
  791. }
  792. // drawChartSeriesCat provides a function to draw the c:cat element by given
  793. // chart series and format sets.
  794. func (f *File) drawChartSeriesCat(v formatChartSeries, formatSet *formatChart) *cCat {
  795. cat := &cCat{
  796. StrRef: &cStrRef{
  797. F: v.Categories,
  798. },
  799. }
  800. chartSeriesCat := map[string]*cCat{Scatter: nil, Bubble: nil, Bubble3D: nil}
  801. if _, ok := chartSeriesCat[formatSet.Type]; ok || v.Categories == "" {
  802. return nil
  803. }
  804. return cat
  805. }
  806. // drawChartSeriesVal provides a function to draw the c:val element by given
  807. // chart series and format sets.
  808. func (f *File) drawChartSeriesVal(v formatChartSeries, formatSet *formatChart) *cVal {
  809. val := &cVal{
  810. NumRef: &cNumRef{
  811. F: v.Values,
  812. },
  813. }
  814. chartSeriesVal := map[string]*cVal{Scatter: nil, Bubble: nil, Bubble3D: nil}
  815. if _, ok := chartSeriesVal[formatSet.Type]; ok {
  816. return nil
  817. }
  818. return val
  819. }
  820. // drawChartSeriesMarker provides a function to draw the c:marker element by
  821. // given data index and format sets.
  822. func (f *File) drawChartSeriesMarker(i int, formatSet *formatChart) *cMarker {
  823. defaultSymbol := map[string]*attrValString{Scatter: {Val: stringPtr("circle")}}
  824. marker := &cMarker{
  825. Symbol: defaultSymbol[formatSet.Type],
  826. Size: &attrValInt{Val: intPtr(5)},
  827. }
  828. if symbol := stringPtr(formatSet.Series[i].Marker.Symbol); *symbol != "" {
  829. marker.Symbol = &attrValString{Val: symbol}
  830. }
  831. if size := intPtr(formatSet.Series[i].Marker.Size); *size != 0 {
  832. marker.Size = &attrValInt{Val: size}
  833. }
  834. if i < 6 {
  835. marker.SpPr = &cSpPr{
  836. SolidFill: &aSolidFill{
  837. SchemeClr: &aSchemeClr{
  838. Val: "accent" + strconv.Itoa(i+1),
  839. },
  840. },
  841. Ln: &aLn{
  842. W: 9252,
  843. SolidFill: &aSolidFill{
  844. SchemeClr: &aSchemeClr{
  845. Val: "accent" + strconv.Itoa(i+1),
  846. },
  847. },
  848. },
  849. }
  850. }
  851. chartSeriesMarker := map[string]*cMarker{Scatter: marker, Line: marker}
  852. return chartSeriesMarker[formatSet.Type]
  853. }
  854. // drawChartSeriesXVal provides a function to draw the c:xVal element by given
  855. // chart series and format sets.
  856. func (f *File) drawChartSeriesXVal(v formatChartSeries, formatSet *formatChart) *cCat {
  857. cat := &cCat{
  858. StrRef: &cStrRef{
  859. F: v.Categories,
  860. },
  861. }
  862. chartSeriesXVal := map[string]*cCat{Scatter: cat}
  863. return chartSeriesXVal[formatSet.Type]
  864. }
  865. // drawChartSeriesYVal provides a function to draw the c:yVal element by given
  866. // chart series and format sets.
  867. func (f *File) drawChartSeriesYVal(v formatChartSeries, formatSet *formatChart) *cVal {
  868. val := &cVal{
  869. NumRef: &cNumRef{
  870. F: v.Values,
  871. },
  872. }
  873. chartSeriesYVal := map[string]*cVal{Scatter: val, Bubble: val, Bubble3D: val}
  874. return chartSeriesYVal[formatSet.Type]
  875. }
  876. // drawCharSeriesBubbleSize provides a function to draw the c:bubbleSize
  877. // element by given chart series and format sets.
  878. func (f *File) drawCharSeriesBubbleSize(v formatChartSeries, formatSet *formatChart) *cVal {
  879. if _, ok := map[string]bool{Bubble: true, Bubble3D: true}[formatSet.Type]; !ok {
  880. return nil
  881. }
  882. return &cVal{
  883. NumRef: &cNumRef{
  884. F: v.Values,
  885. },
  886. }
  887. }
  888. // drawCharSeriesBubble3D provides a function to draw the c:bubble3D element
  889. // by given format sets.
  890. func (f *File) drawCharSeriesBubble3D(formatSet *formatChart) *attrValBool {
  891. if _, ok := map[string]bool{Bubble3D: true}[formatSet.Type]; !ok {
  892. return nil
  893. }
  894. return &attrValBool{Val: boolPtr(true)}
  895. }
  896. // drawChartDLbls provides a function to draw the c:dLbls element by given
  897. // format sets.
  898. func (f *File) drawChartDLbls(formatSet *formatChart) *cDLbls {
  899. return &cDLbls{
  900. ShowLegendKey: &attrValBool{Val: boolPtr(formatSet.Legend.ShowLegendKey)},
  901. ShowVal: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowVal)},
  902. ShowCatName: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowCatName)},
  903. ShowSerName: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowSerName)},
  904. ShowBubbleSize: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowBubbleSize)},
  905. ShowPercent: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowPercent)},
  906. ShowLeaderLines: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowLeaderLines)},
  907. }
  908. }
  909. // drawChartSeriesDLbls provides a function to draw the c:dLbls element by
  910. // given format sets.
  911. func (f *File) drawChartSeriesDLbls(formatSet *formatChart) *cDLbls {
  912. dLbls := f.drawChartDLbls(formatSet)
  913. chartSeriesDLbls := map[string]*cDLbls{Scatter: nil, Surface3D: nil, WireframeSurface3D: nil, Contour: nil, WireframeContour: nil, Bubble: nil, Bubble3D: nil}
  914. if _, ok := chartSeriesDLbls[formatSet.Type]; ok {
  915. return nil
  916. }
  917. return dLbls
  918. }
  919. // drawPlotAreaCatAx provides a function to draw the c:catAx element.
  920. func (f *File) drawPlotAreaCatAx(formatSet *formatChart) []*cAxs {
  921. min := &attrValFloat{Val: float64Ptr(formatSet.XAxis.Minimum)}
  922. max := &attrValFloat{Val: float64Ptr(formatSet.XAxis.Maximum)}
  923. if formatSet.XAxis.Minimum == 0 {
  924. min = nil
  925. }
  926. if formatSet.XAxis.Maximum == 0 {
  927. max = nil
  928. }
  929. axs := []*cAxs{
  930. {
  931. AxID: &attrValInt{Val: intPtr(754001152)},
  932. Scaling: &cScaling{
  933. Orientation: &attrValString{Val: stringPtr(orientation[formatSet.XAxis.ReverseOrder])},
  934. Max: max,
  935. Min: min,
  936. },
  937. Delete: &attrValBool{Val: boolPtr(false)},
  938. AxPos: &attrValString{Val: stringPtr(catAxPos[formatSet.XAxis.ReverseOrder])},
  939. NumFmt: &cNumFmt{
  940. FormatCode: "General",
  941. SourceLinked: true,
  942. },
  943. MajorTickMark: &attrValString{Val: stringPtr("none")},
  944. MinorTickMark: &attrValString{Val: stringPtr("none")},
  945. TickLblPos: &attrValString{Val: stringPtr("nextTo")},
  946. SpPr: f.drawPlotAreaSpPr(),
  947. TxPr: f.drawPlotAreaTxPr(),
  948. CrossAx: &attrValInt{Val: intPtr(753999904)},
  949. Crosses: &attrValString{Val: stringPtr("autoZero")},
  950. Auto: &attrValBool{Val: boolPtr(true)},
  951. LblAlgn: &attrValString{Val: stringPtr("ctr")},
  952. LblOffset: &attrValInt{Val: intPtr(100)},
  953. NoMultiLvlLbl: &attrValBool{Val: boolPtr(false)},
  954. },
  955. }
  956. if formatSet.XAxis.MajorGridlines {
  957. axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  958. }
  959. if formatSet.XAxis.MinorGridlines {
  960. axs[0].MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  961. }
  962. if formatSet.XAxis.TickLabelSkip != 0 {
  963. axs[0].TickLblSkip = &attrValInt{Val: intPtr(formatSet.XAxis.TickLabelSkip)}
  964. }
  965. return axs
  966. }
  967. // drawPlotAreaValAx provides a function to draw the c:valAx element.
  968. func (f *File) drawPlotAreaValAx(formatSet *formatChart) []*cAxs {
  969. min := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Minimum)}
  970. max := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Maximum)}
  971. if formatSet.YAxis.Minimum == 0 {
  972. min = nil
  973. }
  974. if formatSet.YAxis.Maximum == 0 {
  975. max = nil
  976. }
  977. var logBase *attrValFloat
  978. if formatSet.YAxis.LogBase >= 2 && formatSet.YAxis.LogBase <= 1000 {
  979. logBase = &attrValFloat{Val: float64Ptr(formatSet.YAxis.LogBase)}
  980. }
  981. axs := []*cAxs{
  982. {
  983. AxID: &attrValInt{Val: intPtr(753999904)},
  984. Scaling: &cScaling{
  985. LogBase: logBase,
  986. Orientation: &attrValString{Val: stringPtr(orientation[formatSet.YAxis.ReverseOrder])},
  987. Max: max,
  988. Min: min,
  989. },
  990. Delete: &attrValBool{Val: boolPtr(false)},
  991. AxPos: &attrValString{Val: stringPtr(valAxPos[formatSet.YAxis.ReverseOrder])},
  992. NumFmt: &cNumFmt{
  993. FormatCode: chartValAxNumFmtFormatCode[formatSet.Type],
  994. SourceLinked: true,
  995. },
  996. MajorTickMark: &attrValString{Val: stringPtr("none")},
  997. MinorTickMark: &attrValString{Val: stringPtr("none")},
  998. TickLblPos: &attrValString{Val: stringPtr("nextTo")},
  999. SpPr: f.drawPlotAreaSpPr(),
  1000. TxPr: f.drawPlotAreaTxPr(),
  1001. CrossAx: &attrValInt{Val: intPtr(754001152)},
  1002. Crosses: &attrValString{Val: stringPtr("autoZero")},
  1003. CrossBetween: &attrValString{Val: stringPtr(chartValAxCrossBetween[formatSet.Type])},
  1004. },
  1005. }
  1006. if formatSet.YAxis.MajorGridlines {
  1007. axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  1008. }
  1009. if formatSet.YAxis.MinorGridlines {
  1010. axs[0].MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  1011. }
  1012. if pos, ok := valTickLblPos[formatSet.Type]; ok {
  1013. axs[0].TickLblPos.Val = stringPtr(pos)
  1014. }
  1015. if formatSet.YAxis.MajorUnit != 0 {
  1016. axs[0].MajorUnit = &attrValFloat{Val: float64Ptr(formatSet.YAxis.MajorUnit)}
  1017. }
  1018. return axs
  1019. }
  1020. // drawPlotAreaSerAx provides a function to draw the c:serAx element.
  1021. func (f *File) drawPlotAreaSerAx(formatSet *formatChart) []*cAxs {
  1022. min := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Minimum)}
  1023. max := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Maximum)}
  1024. if formatSet.YAxis.Minimum == 0 {
  1025. min = nil
  1026. }
  1027. if formatSet.YAxis.Maximum == 0 {
  1028. max = nil
  1029. }
  1030. return []*cAxs{
  1031. {
  1032. AxID: &attrValInt{Val: intPtr(832256642)},
  1033. Scaling: &cScaling{
  1034. Orientation: &attrValString{Val: stringPtr(orientation[formatSet.YAxis.ReverseOrder])},
  1035. Max: max,
  1036. Min: min,
  1037. },
  1038. Delete: &attrValBool{Val: boolPtr(false)},
  1039. AxPos: &attrValString{Val: stringPtr(catAxPos[formatSet.XAxis.ReverseOrder])},
  1040. TickLblPos: &attrValString{Val: stringPtr("nextTo")},
  1041. SpPr: f.drawPlotAreaSpPr(),
  1042. TxPr: f.drawPlotAreaTxPr(),
  1043. CrossAx: &attrValInt{Val: intPtr(753999904)},
  1044. },
  1045. }
  1046. }
  1047. // drawPlotAreaSpPr provides a function to draw the c:spPr element.
  1048. func (f *File) drawPlotAreaSpPr() *cSpPr {
  1049. return &cSpPr{
  1050. Ln: &aLn{
  1051. W: 9525,
  1052. Cap: "flat",
  1053. Cmpd: "sng",
  1054. Algn: "ctr",
  1055. SolidFill: &aSolidFill{
  1056. SchemeClr: &aSchemeClr{
  1057. Val: "tx1",
  1058. LumMod: &attrValInt{Val: intPtr(15000)},
  1059. LumOff: &attrValInt{Val: intPtr(85000)},
  1060. },
  1061. },
  1062. },
  1063. }
  1064. }
  1065. // drawPlotAreaTxPr provides a function to draw the c:txPr element.
  1066. func (f *File) drawPlotAreaTxPr() *cTxPr {
  1067. return &cTxPr{
  1068. BodyPr: aBodyPr{
  1069. Rot: -60000000,
  1070. SpcFirstLastPara: true,
  1071. VertOverflow: "ellipsis",
  1072. Vert: "horz",
  1073. Wrap: "square",
  1074. Anchor: "ctr",
  1075. AnchorCtr: true,
  1076. },
  1077. P: aP{
  1078. PPr: &aPPr{
  1079. DefRPr: aRPr{
  1080. Sz: 900,
  1081. B: false,
  1082. I: false,
  1083. U: "none",
  1084. Strike: "noStrike",
  1085. Kern: 1200,
  1086. Baseline: 0,
  1087. SolidFill: &aSolidFill{
  1088. SchemeClr: &aSchemeClr{
  1089. Val: "tx1",
  1090. LumMod: &attrValInt{Val: intPtr(15000)},
  1091. LumOff: &attrValInt{Val: intPtr(85000)},
  1092. },
  1093. },
  1094. Latin: &aLatin{Typeface: "+mn-lt"},
  1095. Ea: &aEa{Typeface: "+mn-ea"},
  1096. Cs: &aCs{Typeface: "+mn-cs"},
  1097. },
  1098. },
  1099. EndParaRPr: &aEndParaRPr{Lang: "en-US"},
  1100. },
  1101. }
  1102. }
  1103. // drawingParser provides a function to parse drawingXML. In order to solve
  1104. // the problem that the label structure is changed after serialization and
  1105. // deserialization, two different structures: decodeWsDr and encodeWsDr are
  1106. // defined.
  1107. func (f *File) drawingParser(path string) (*xlsxWsDr, int) {
  1108. var (
  1109. err error
  1110. ok bool
  1111. )
  1112. if f.Drawings[path] == nil {
  1113. content := xlsxWsDr{}
  1114. content.A = NameSpaceDrawingML.Value
  1115. content.Xdr = NameSpaceDrawingMLSpreadSheet.Value
  1116. if _, ok = f.XLSX[path]; ok { // Append Model
  1117. decodeWsDr := decodeWsDr{}
  1118. if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(path)))).
  1119. Decode(&decodeWsDr); err != nil && err != io.EOF {
  1120. log.Printf("xml decode error: %s", err)
  1121. }
  1122. content.R = decodeWsDr.R
  1123. for _, v := range decodeWsDr.OneCellAnchor {
  1124. content.OneCellAnchor = append(content.OneCellAnchor, &xdrCellAnchor{
  1125. EditAs: v.EditAs,
  1126. GraphicFrame: v.Content,
  1127. })
  1128. }
  1129. for _, v := range decodeWsDr.TwoCellAnchor {
  1130. content.TwoCellAnchor = append(content.TwoCellAnchor, &xdrCellAnchor{
  1131. EditAs: v.EditAs,
  1132. GraphicFrame: v.Content,
  1133. })
  1134. }
  1135. }
  1136. f.Drawings[path] = &content
  1137. }
  1138. wsDr := f.Drawings[path]
  1139. return wsDr, len(wsDr.OneCellAnchor) + len(wsDr.TwoCellAnchor) + 2
  1140. }
  1141. // addDrawingChart provides a function to add chart graphic frame by given
  1142. // sheet, drawingXML, cell, width, height, relationship index and format sets.
  1143. func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rID int, formatSet *formatPicture) error {
  1144. col, row, err := CellNameToCoordinates(cell)
  1145. if err != nil {
  1146. return err
  1147. }
  1148. colIdx := col - 1
  1149. rowIdx := row - 1
  1150. width = int(float64(width) * formatSet.XScale)
  1151. height = int(float64(height) * formatSet.YScale)
  1152. colStart, rowStart, colEnd, rowEnd, x2, y2 :=
  1153. f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.OffsetX, formatSet.OffsetY, width, height)
  1154. content, cNvPrID := f.drawingParser(drawingXML)
  1155. twoCellAnchor := xdrCellAnchor{}
  1156. twoCellAnchor.EditAs = formatSet.Positioning
  1157. from := xlsxFrom{}
  1158. from.Col = colStart
  1159. from.ColOff = formatSet.OffsetX * EMU
  1160. from.Row = rowStart
  1161. from.RowOff = formatSet.OffsetY * EMU
  1162. to := xlsxTo{}
  1163. to.Col = colEnd
  1164. to.ColOff = x2 * EMU
  1165. to.Row = rowEnd
  1166. to.RowOff = y2 * EMU
  1167. twoCellAnchor.From = &from
  1168. twoCellAnchor.To = &to
  1169. graphicFrame := xlsxGraphicFrame{
  1170. NvGraphicFramePr: xlsxNvGraphicFramePr{
  1171. CNvPr: &xlsxCNvPr{
  1172. ID: cNvPrID,
  1173. Name: "Chart " + strconv.Itoa(cNvPrID),
  1174. },
  1175. },
  1176. Graphic: &xlsxGraphic{
  1177. GraphicData: &xlsxGraphicData{
  1178. URI: NameSpaceDrawingMLChart.Value,
  1179. Chart: &xlsxChart{
  1180. C: NameSpaceDrawingMLChart.Value,
  1181. R: SourceRelationship.Value,
  1182. RID: "rId" + strconv.Itoa(rID),
  1183. },
  1184. },
  1185. },
  1186. }
  1187. graphic, _ := xml.Marshal(graphicFrame)
  1188. twoCellAnchor.GraphicFrame = string(graphic)
  1189. twoCellAnchor.ClientData = &xdrClientData{
  1190. FLocksWithSheet: formatSet.FLocksWithSheet,
  1191. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  1192. }
  1193. content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
  1194. f.Drawings[drawingXML] = content
  1195. return err
  1196. }
  1197. // addSheetDrawingChart provides a function to add chart graphic frame for
  1198. // chartsheet by given sheet, drawingXML, width, height, relationship index
  1199. // and format sets.
  1200. func (f *File) addSheetDrawingChart(drawingXML string, rID int, formatSet *formatPicture) {
  1201. content, cNvPrID := f.drawingParser(drawingXML)
  1202. absoluteAnchor := xdrCellAnchor{
  1203. EditAs: formatSet.Positioning,
  1204. Pos: &xlsxPoint2D{},
  1205. Ext: &xlsxExt{},
  1206. }
  1207. graphicFrame := xlsxGraphicFrame{
  1208. NvGraphicFramePr: xlsxNvGraphicFramePr{
  1209. CNvPr: &xlsxCNvPr{
  1210. ID: cNvPrID,
  1211. Name: "Chart " + strconv.Itoa(cNvPrID),
  1212. },
  1213. },
  1214. Graphic: &xlsxGraphic{
  1215. GraphicData: &xlsxGraphicData{
  1216. URI: NameSpaceDrawingMLChart.Value,
  1217. Chart: &xlsxChart{
  1218. C: NameSpaceDrawingMLChart.Value,
  1219. R: SourceRelationship.Value,
  1220. RID: "rId" + strconv.Itoa(rID),
  1221. },
  1222. },
  1223. },
  1224. }
  1225. graphic, _ := xml.Marshal(graphicFrame)
  1226. absoluteAnchor.GraphicFrame = string(graphic)
  1227. absoluteAnchor.ClientData = &xdrClientData{
  1228. FLocksWithSheet: formatSet.FLocksWithSheet,
  1229. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  1230. }
  1231. content.AbsoluteAnchor = append(content.AbsoluteAnchor, &absoluteAnchor)
  1232. f.Drawings[drawingXML] = content
  1233. return
  1234. }
  1235. // deleteDrawing provides a function to delete chart graphic frame by given by
  1236. // given coordinates and graphic type.
  1237. func (f *File) deleteDrawing(col, row int, drawingXML, drawingType string) (err error) {
  1238. var (
  1239. wsDr *xlsxWsDr
  1240. deTwoCellAnchor *decodeTwoCellAnchor
  1241. )
  1242. xdrCellAnchorFuncs := map[string]func(anchor *xdrCellAnchor) bool{
  1243. "Chart": func(anchor *xdrCellAnchor) bool { return anchor.Pic == nil },
  1244. "Pic": func(anchor *xdrCellAnchor) bool { return anchor.Pic != nil },
  1245. }
  1246. decodeTwoCellAnchorFuncs := map[string]func(anchor *decodeTwoCellAnchor) bool{
  1247. "Chart": func(anchor *decodeTwoCellAnchor) bool { return anchor.Pic == nil },
  1248. "Pic": func(anchor *decodeTwoCellAnchor) bool { return anchor.Pic != nil },
  1249. }
  1250. wsDr, _ = f.drawingParser(drawingXML)
  1251. for idx := 0; idx < len(wsDr.TwoCellAnchor); idx++ {
  1252. if err = nil; wsDr.TwoCellAnchor[idx].From != nil && xdrCellAnchorFuncs[drawingType](wsDr.TwoCellAnchor[idx]) {
  1253. if wsDr.TwoCellAnchor[idx].From.Col == col && wsDr.TwoCellAnchor[idx].From.Row == row {
  1254. wsDr.TwoCellAnchor = append(wsDr.TwoCellAnchor[:idx], wsDr.TwoCellAnchor[idx+1:]...)
  1255. idx--
  1256. }
  1257. }
  1258. }
  1259. for idx := 0; idx < len(wsDr.TwoCellAnchor); idx++ {
  1260. deTwoCellAnchor = new(decodeTwoCellAnchor)
  1261. if err = f.xmlNewDecoder(strings.NewReader("<decodeTwoCellAnchor>" + wsDr.TwoCellAnchor[idx].GraphicFrame + "</decodeTwoCellAnchor>")).
  1262. Decode(deTwoCellAnchor); err != nil && err != io.EOF {
  1263. err = fmt.Errorf("xml decode error: %s", err)
  1264. return
  1265. }
  1266. if err = nil; deTwoCellAnchor.From != nil && decodeTwoCellAnchorFuncs[drawingType](deTwoCellAnchor) {
  1267. if deTwoCellAnchor.From.Col == col && deTwoCellAnchor.From.Row == row {
  1268. wsDr.TwoCellAnchor = append(wsDr.TwoCellAnchor[:idx], wsDr.TwoCellAnchor[idx+1:]...)
  1269. idx--
  1270. }
  1271. }
  1272. }
  1273. f.Drawings[drawingXML] = wsDr
  1274. return err
  1275. }