chart.go 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. Package excelize providing a set of functions that allow you to write to
  3. and read from XLSX files. Support reads and writes XLSX file generated by
  4. Microsoft Excel™ 2007 and later. Support save file without losing original
  5. charts of XLSX. This library needs Go version 1.8 or later.
  6. Copyright 2016 - 2018 The excelize Authors. All rights reserved. Use of
  7. this source code is governed by a BSD-style license that can be found in
  8. the LICENSE file.
  9. */
  10. package excelize
  11. import (
  12. "encoding/json"
  13. "encoding/xml"
  14. "strconv"
  15. "strings"
  16. )
  17. // This section defines the currently supported chart types.
  18. const (
  19. Bar = "bar"
  20. BarStacked = "barStacked"
  21. BarPercentStacked = "barPercentStacked"
  22. Bar3DClustered = "bar3DClustered"
  23. Bar3DStacked = "bar3DStacked"
  24. Bar3DPercentStacked = "bar3DPercentStacked"
  25. Col = "col"
  26. ColStacked = "colStacked"
  27. ColPercentStacked = "colPercentStacked"
  28. Col3DClustered = "col3DClustered"
  29. Col3D = "col3D"
  30. Col3DStacked = "col3DStacked"
  31. Col3DPercentStacked = "col3DPercentStacked"
  32. Doughnut = "doughnut"
  33. Line = "line"
  34. Pie = "pie"
  35. Pie3D = "pie3D"
  36. Radar = "radar"
  37. Scatter = "scatter"
  38. )
  39. // This section defines the default value of chart properties.
  40. var (
  41. chartView3DRotX = map[string]int{
  42. Bar: 0,
  43. BarStacked: 0,
  44. BarPercentStacked: 0,
  45. Bar3DClustered: 15,
  46. Bar3DStacked: 15,
  47. Bar3DPercentStacked: 15,
  48. Col: 0,
  49. ColStacked: 0,
  50. ColPercentStacked: 0,
  51. Col3DClustered: 15,
  52. Col3D: 15,
  53. Col3DStacked: 15,
  54. Col3DPercentStacked: 15,
  55. Doughnut: 0,
  56. Line: 0,
  57. Pie: 0,
  58. Pie3D: 30,
  59. Radar: 0,
  60. Scatter: 0,
  61. }
  62. chartView3DRotY = map[string]int{
  63. Bar: 0,
  64. BarStacked: 0,
  65. BarPercentStacked: 0,
  66. Bar3DClustered: 20,
  67. Bar3DStacked: 20,
  68. Bar3DPercentStacked: 20,
  69. Col: 0,
  70. ColStacked: 0,
  71. ColPercentStacked: 0,
  72. Col3DClustered: 20,
  73. Col3D: 20,
  74. Col3DStacked: 20,
  75. Col3DPercentStacked: 20,
  76. Doughnut: 0,
  77. Line: 0,
  78. Pie: 0,
  79. Pie3D: 0,
  80. Radar: 0,
  81. Scatter: 0,
  82. }
  83. chartView3DDepthPercent = map[string]int{
  84. Bar: 100,
  85. BarStacked: 100,
  86. BarPercentStacked: 100,
  87. Bar3DClustered: 100,
  88. Bar3DStacked: 100,
  89. Bar3DPercentStacked: 100,
  90. Col: 100,
  91. ColStacked: 100,
  92. ColPercentStacked: 100,
  93. Col3DClustered: 100,
  94. Col3D: 100,
  95. Col3DStacked: 100,
  96. Col3DPercentStacked: 100,
  97. Doughnut: 100,
  98. Line: 100,
  99. Pie: 100,
  100. Pie3D: 100,
  101. Radar: 100,
  102. Scatter: 100,
  103. }
  104. chartView3DRAngAx = map[string]int{
  105. Bar: 0,
  106. BarStacked: 0,
  107. BarPercentStacked: 0,
  108. Bar3DClustered: 1,
  109. Bar3DStacked: 1,
  110. Bar3DPercentStacked: 1,
  111. Col: 0,
  112. ColStacked: 0,
  113. ColPercentStacked: 0,
  114. Col3DClustered: 1,
  115. Col3D: 1,
  116. Col3DStacked: 1,
  117. Col3DPercentStacked: 1,
  118. Doughnut: 0,
  119. Line: 0,
  120. Pie: 0,
  121. Pie3D: 0,
  122. Radar: 0,
  123. Scatter: 0,
  124. }
  125. chartLegendPosition = map[string]string{
  126. "bottom": "b",
  127. "left": "l",
  128. "right": "r",
  129. "top": "t",
  130. "top_right": "tr",
  131. }
  132. chartValAxNumFmtFormatCode = map[string]string{
  133. Bar: "General",
  134. BarStacked: "General",
  135. BarPercentStacked: "0%",
  136. Bar3DClustered: "General",
  137. Bar3DStacked: "General",
  138. Bar3DPercentStacked: "0%",
  139. Col: "General",
  140. ColStacked: "General",
  141. ColPercentStacked: "0%",
  142. Col3DClustered: "General",
  143. Col3D: "General",
  144. Col3DStacked: "General",
  145. Col3DPercentStacked: "0%",
  146. Doughnut: "General",
  147. Line: "General",
  148. Pie: "General",
  149. Pie3D: "General",
  150. Radar: "General",
  151. Scatter: "General",
  152. }
  153. plotAreaChartGrouping = map[string]string{
  154. Bar: "clustered",
  155. BarStacked: "stacked",
  156. BarPercentStacked: "percentStacked",
  157. Bar3DClustered: "clustered",
  158. Bar3DStacked: "stacked",
  159. Bar3DPercentStacked: "percentStacked",
  160. Col: "clustered",
  161. ColStacked: "stacked",
  162. ColPercentStacked: "percentStacked",
  163. Col3DClustered: "clustered",
  164. Col3D: "standard",
  165. Col3DStacked: "stacked",
  166. Col3DPercentStacked: "percentStacked",
  167. Line: "standard",
  168. }
  169. plotAreaChartBarDir = map[string]string{
  170. Bar: "bar",
  171. BarStacked: "bar",
  172. BarPercentStacked: "bar",
  173. Bar3DClustered: "bar",
  174. Bar3DStacked: "bar",
  175. Bar3DPercentStacked: "bar",
  176. Col: "col",
  177. ColStacked: "col",
  178. ColPercentStacked: "col",
  179. Col3DClustered: "col",
  180. Col3D: "col",
  181. Col3DStacked: "col",
  182. Col3DPercentStacked: "col",
  183. Line: "standard",
  184. }
  185. orientation = map[bool]string{
  186. true: "maxMin",
  187. false: "minMax",
  188. }
  189. catAxPos = map[bool]string{
  190. true: "t",
  191. false: "b",
  192. }
  193. valAxPos = map[bool]string{
  194. true: "r",
  195. false: "l",
  196. }
  197. )
  198. // parseFormatChartSet provides a function to parse the format settings of the
  199. // chart with default value.
  200. func parseFormatChartSet(formatSet string) (*formatChart, error) {
  201. format := formatChart{
  202. Dimension: formatChartDimension{
  203. Width: 480,
  204. Height: 290,
  205. },
  206. Format: formatPicture{
  207. FPrintsWithSheet: true,
  208. FLocksWithSheet: false,
  209. NoChangeAspect: false,
  210. OffsetX: 0,
  211. OffsetY: 0,
  212. XScale: 1.0,
  213. YScale: 1.0,
  214. },
  215. Legend: formatChartLegend{
  216. Position: "bottom",
  217. ShowLegendKey: false,
  218. },
  219. Title: formatChartTitle{
  220. Name: " ",
  221. },
  222. ShowBlanksAs: "gap",
  223. }
  224. err := json.Unmarshal([]byte(formatSet), &format)
  225. return &format, err
  226. }
  227. // AddChart provides the method to add chart in a sheet by given chart format
  228. // set (such as offset, scale, aspect ratio setting and print settings) and
  229. // properties set. For example, create 3D clustered column chart with data
  230. // Sheet1!$A$29:$D$32:
  231. //
  232. // package main
  233. //
  234. // import (
  235. // "fmt"
  236. //
  237. // "github.com/360EntSecGroup-Skylar/excelize"
  238. // )
  239. //
  240. // func main() {
  241. // categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  242. // values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  243. // xlsx := excelize.NewFile()
  244. // for k, v := range categories {
  245. // xlsx.SetCellValue("Sheet1", k, v)
  246. // }
  247. // for k, v := range values {
  248. // xlsx.SetCellValue("Sheet1", k, v)
  249. // }
  250. // xlsx.AddChart("Sheet1", "E1", `{"type":"col3DClustered","dimension":{"width":640,"height":480},"series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit 3D Clustered Column Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero","x_axis":{"reverse_order":true},"y_axis":{"maximum":7.5,"minimum":0.5}}`)
  251. // // Save xlsx file by the given path.
  252. // err := xlsx.SaveAs("./Book1.xlsx")
  253. // if err != nil {
  254. // fmt.Println(err)
  255. // }
  256. // }
  257. //
  258. // The following shows the type of chart supported by excelize:
  259. //
  260. // Type | Chart
  261. // ---------------------+------------------------------
  262. // bar | 2D clustered bar chart
  263. // barStacked | 2D stacked bar chart
  264. // barPercentStacked | 2D 100% stacked bar chart
  265. // bar3DClustered | 3D clustered bar chart
  266. // bar3DStacked | 3D stacked bar chart
  267. // bar3DPercentStacked | 3D 100% stacked bar chart
  268. // col | 2D clustered column chart
  269. // colStacked | 2D stacked column chart
  270. // colPercentStacked | 2D 100% stacked column chart
  271. // col3DClustered | 3D clustered column chart
  272. // col3D | 3D column chart
  273. // col3DStacked | 3D stacked column chart
  274. // col3DPercentStacked | 3D 100% stacked column chart
  275. // doughnut | doughnut chart
  276. // line | line chart
  277. // pie | pie chart
  278. // pie3D | 3D pie chart
  279. // radar | radar chart
  280. // scatter | scatter chart
  281. //
  282. // In Excel a chart series is a collection of information that defines which data is plotted such as values, axis labels and formatting.
  283. //
  284. // The series options that can be set are:
  285. //
  286. // name
  287. // categories
  288. // values
  289. //
  290. // name: Set the name for the series. The name is displayed in the chart legend and in the formula bar. The name property is optional and if it isn't supplied it will default to Series 1..n. The name can also be a formula such as Sheet1!$A$1
  291. //
  292. // categories: This sets the chart category labels. The category is more or less the same as the X axis. In most chart types the categories property is optional and the chart will just assume a sequential series from 1..n.
  293. //
  294. // values: This is the most important property of a series and is the only mandatory option for every chart object. This option links the chart with the worksheet data that it displays.
  295. //
  296. // Set properties of the chart legend. The options that can be set are:
  297. //
  298. // position
  299. // show_legend_key
  300. //
  301. // position: Set the position of the chart legend. The default legend position is right. The available positions are:
  302. //
  303. // top
  304. // bottom
  305. // left
  306. // right
  307. // top_right
  308. //
  309. // show_legend_key: Set the legend keys shall be shown in data labels. The default value is false.
  310. //
  311. // Set properties of the chart title. The properties that can be set are:
  312. //
  313. // title
  314. //
  315. // name: Set the name (title) for the chart. The name is displayed above the chart. The name can also be a formula such as Sheet1!$A$1 or a list with a sheetname. The name property is optional. The default is to have no chart title.
  316. //
  317. // Specifies how blank cells are plotted on the chart by show_blanks_as. The default value is gap. The options that can be set are:
  318. //
  319. // gap
  320. // span
  321. // zero
  322. //
  323. // gap: Specifies that blank values shall be left as a gap.
  324. //
  325. // sapn: Specifies that blank values shall be spanned with a line.
  326. //
  327. // zero: Specifies that blank values shall be treated as zero.
  328. //
  329. // Set chart offset, scale, aspect ratio setting and print settings by format, same as function AddPicture.
  330. //
  331. // Set the position of the chart plot area by plotarea. The properties that can be set are:
  332. //
  333. // show_bubble_size
  334. // show_cat_name
  335. // show_leader_lines
  336. // show_percent
  337. // show_series_name
  338. // show_val
  339. //
  340. // show_bubble_size: Specifies the bubble size shall be shown in a data label. The show_bubble_size property is optional. The default value is false.
  341. //
  342. // show_cat_name: Specifies that the category name shall be shown in the data label. The show_cat_name property is optional. The default value is true.
  343. //
  344. // show_leader_lines: Specifies leader lines shall be shown for data labels. The show_leader_lines property is optional. The default value is false.
  345. //
  346. // show_percent: Specifies that the percentage shall be shown in a data label. The show_percent property is optional. The default value is false.
  347. //
  348. // show_series_name: Specifies that the series name shall be shown in a data label. The show_series_name property is optional. The default value is false.
  349. //
  350. // show_val: Specifies that the value shall be shown in a data label. The show_val property is optional. The default value is false.
  351. //
  352. // Set the primary horizontal and vertical axis options by x_axis and y_axis. The properties that can be set are:
  353. //
  354. // reverse_order
  355. // maximum
  356. // minimum
  357. //
  358. // reverse_order: Specifies that the categories or values on reverse order (orientation of the chart). The reverse_order property is optional. The default value is false.
  359. //
  360. // maximum: Specifies that the fixed maximum, 0 is auto. The maximum property is optional. The default value is auto.
  361. //
  362. // minimum: Specifies that the fixed minimum, 0 is auto. The minimum property is optional. The default value is auto.
  363. //
  364. // Set chart size by dimension property. The dimension property is optional. The default width is 480, and height is 290.
  365. //
  366. func (f *File) AddChart(sheet, cell, format string) error {
  367. formatSet, err := parseFormatChartSet(format)
  368. if err != nil {
  369. return err
  370. }
  371. // Read sheet data.
  372. xlsx := f.workSheetReader(sheet)
  373. // Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
  374. drawingID := f.countDrawings() + 1
  375. chartID := f.countCharts() + 1
  376. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  377. drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
  378. drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
  379. f.addDrawingChart(sheet, drawingXML, cell, formatSet.Dimension.Width, formatSet.Dimension.Height, drawingRID, &formatSet.Format)
  380. f.addChart(formatSet)
  381. f.addContentTypePart(chartID, "chart")
  382. f.addContentTypePart(drawingID, "drawings")
  383. return err
  384. }
  385. // countCharts provides a function to get chart files count storage in the
  386. // folder xl/charts.
  387. func (f *File) countCharts() int {
  388. count := 0
  389. for k := range f.XLSX {
  390. if strings.Contains(k, "xl/charts/chart") {
  391. count++
  392. }
  393. }
  394. return count
  395. }
  396. // prepareDrawing provides a function to prepare drawing ID and XML by given
  397. // drawingID, worksheet name and default drawingXML.
  398. func (f *File) prepareDrawing(xlsx *xlsxWorksheet, drawingID int, sheet, drawingXML string) (int, string) {
  399. sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  400. if xlsx.Drawing != nil {
  401. // The worksheet already has a picture or chart relationships, use the relationships drawing ../drawings/drawing%d.xml.
  402. sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID)
  403. drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml"))
  404. drawingXML = strings.Replace(sheetRelationshipsDrawingXML, "..", "xl", -1)
  405. } else {
  406. // Add first picture for given sheet.
  407. rID := f.addSheetRelationships(sheet, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
  408. f.addSheetDrawing(sheet, rID)
  409. }
  410. return drawingID, drawingXML
  411. }
  412. // addChart provides a function to create chart as xl/charts/chart%d.xml by
  413. // given format sets.
  414. func (f *File) addChart(formatSet *formatChart) {
  415. count := f.countCharts()
  416. xlsxChartSpace := xlsxChartSpace{
  417. XMLNSc: NameSpaceDrawingMLChart,
  418. XMLNSa: NameSpaceDrawingML,
  419. XMLNSr: SourceRelationship,
  420. XMLNSc16r2: SourceRelationshipChart201506,
  421. Date1904: &attrValBool{Val: false},
  422. Lang: &attrValString{Val: "en-US"},
  423. RoundedCorners: &attrValBool{Val: false},
  424. Chart: cChart{
  425. Title: &cTitle{
  426. Tx: cTx{
  427. Rich: &cRich{
  428. P: aP{
  429. PPr: &aPPr{
  430. DefRPr: aRPr{
  431. Kern: 1200,
  432. Strike: "noStrike",
  433. U: "none",
  434. Sz: 1400,
  435. SolidFill: &aSolidFill{
  436. SchemeClr: &aSchemeClr{
  437. Val: "tx1",
  438. LumMod: &attrValInt{
  439. Val: 65000,
  440. },
  441. LumOff: &attrValInt{
  442. Val: 35000,
  443. },
  444. },
  445. },
  446. Ea: &aEa{
  447. Typeface: "+mn-ea",
  448. },
  449. Cs: &aCs{
  450. Typeface: "+mn-cs",
  451. },
  452. Latin: &aLatin{
  453. Typeface: "+mn-lt",
  454. },
  455. },
  456. },
  457. R: &aR{
  458. RPr: aRPr{
  459. Lang: "en-US",
  460. AltLang: "en-US",
  461. },
  462. T: formatSet.Title.Name,
  463. },
  464. },
  465. },
  466. },
  467. TxPr: cTxPr{
  468. P: aP{
  469. PPr: &aPPr{
  470. DefRPr: aRPr{
  471. Kern: 1200,
  472. U: "none",
  473. Sz: 14000,
  474. Strike: "noStrike",
  475. },
  476. },
  477. EndParaRPr: &aEndParaRPr{
  478. Lang: "en-US",
  479. },
  480. },
  481. },
  482. },
  483. View3D: &cView3D{
  484. RotX: &attrValInt{Val: chartView3DRotX[formatSet.Type]},
  485. RotY: &attrValInt{Val: chartView3DRotY[formatSet.Type]},
  486. DepthPercent: &attrValInt{Val: chartView3DDepthPercent[formatSet.Type]},
  487. RAngAx: &attrValInt{Val: chartView3DRAngAx[formatSet.Type]},
  488. },
  489. Floor: &cThicknessSpPr{
  490. Thickness: &attrValInt{Val: 0},
  491. },
  492. SideWall: &cThicknessSpPr{
  493. Thickness: &attrValInt{Val: 0},
  494. },
  495. BackWall: &cThicknessSpPr{
  496. Thickness: &attrValInt{Val: 0},
  497. },
  498. PlotArea: &cPlotArea{},
  499. Legend: &cLegend{
  500. LegendPos: &attrValString{Val: chartLegendPosition[formatSet.Legend.Position]},
  501. Overlay: &attrValBool{Val: false},
  502. },
  503. PlotVisOnly: &attrValBool{Val: false},
  504. DispBlanksAs: &attrValString{Val: formatSet.ShowBlanksAs},
  505. ShowDLblsOverMax: &attrValBool{Val: false},
  506. },
  507. SpPr: &cSpPr{
  508. SolidFill: &aSolidFill{
  509. SchemeClr: &aSchemeClr{Val: "bg1"},
  510. },
  511. Ln: &aLn{
  512. W: 9525,
  513. Cap: "flat",
  514. Cmpd: "sng",
  515. Algn: "ctr",
  516. SolidFill: &aSolidFill{
  517. SchemeClr: &aSchemeClr{Val: "tx1",
  518. LumMod: &attrValInt{
  519. Val: 15000,
  520. },
  521. LumOff: &attrValInt{
  522. Val: 85000,
  523. },
  524. },
  525. },
  526. },
  527. },
  528. PrintSettings: &cPrintSettings{
  529. PageMargins: &cPageMargins{
  530. B: 0.75,
  531. L: 0.7,
  532. R: 0.7,
  533. T: 0.7,
  534. Header: 0.3,
  535. Footer: 0.3,
  536. },
  537. },
  538. }
  539. plotAreaFunc := map[string]func(*formatChart) *cPlotArea{
  540. Bar: f.drawBaseChart,
  541. BarStacked: f.drawBaseChart,
  542. BarPercentStacked: f.drawBaseChart,
  543. Bar3DClustered: f.drawBaseChart,
  544. Bar3DStacked: f.drawBaseChart,
  545. Bar3DPercentStacked: f.drawBaseChart,
  546. Col: f.drawBaseChart,
  547. ColStacked: f.drawBaseChart,
  548. ColPercentStacked: f.drawBaseChart,
  549. Col3DClustered: f.drawBaseChart,
  550. Col3D: f.drawBaseChart,
  551. Col3DStacked: f.drawBaseChart,
  552. Col3DPercentStacked: f.drawBaseChart,
  553. Doughnut: f.drawDoughnutChart,
  554. Line: f.drawLineChart,
  555. Pie3D: f.drawPie3DChart,
  556. Pie: f.drawPieChart,
  557. Radar: f.drawRadarChart,
  558. Scatter: f.drawScatterChart,
  559. }
  560. xlsxChartSpace.Chart.PlotArea = plotAreaFunc[formatSet.Type](formatSet)
  561. chart, _ := xml.Marshal(xlsxChartSpace)
  562. media := "xl/charts/chart" + strconv.Itoa(count+1) + ".xml"
  563. f.saveFileList(media, chart)
  564. }
  565. // drawBaseChart provides a function to draw the c:plotArea element for bar,
  566. // and column series charts by given format sets.
  567. func (f *File) drawBaseChart(formatSet *formatChart) *cPlotArea {
  568. c := cCharts{
  569. BarDir: &attrValString{
  570. Val: "col",
  571. },
  572. Grouping: &attrValString{
  573. Val: "clustered",
  574. },
  575. VaryColors: &attrValBool{
  576. Val: true,
  577. },
  578. Ser: f.drawChartSeries(formatSet),
  579. DLbls: f.drawChartDLbls(formatSet),
  580. AxID: []*attrValInt{
  581. {Val: 754001152},
  582. {Val: 753999904},
  583. },
  584. }
  585. c.BarDir.Val = plotAreaChartBarDir[formatSet.Type]
  586. c.Grouping.Val = plotAreaChartGrouping[formatSet.Type]
  587. if formatSet.Type == "colStacked" || formatSet.Type == "barStacked" || formatSet.Type == "barPercentStacked" || formatSet.Type == "colPercentStacked" {
  588. c.Overlap = &attrValInt{Val: 100}
  589. }
  590. catAx := f.drawPlotAreaCatAx(formatSet)
  591. valAx := f.drawPlotAreaValAx(formatSet)
  592. charts := map[string]*cPlotArea{
  593. "bar": {
  594. BarChart: &c,
  595. CatAx: catAx,
  596. ValAx: valAx,
  597. },
  598. "barStacked": {
  599. BarChart: &c,
  600. CatAx: catAx,
  601. ValAx: valAx,
  602. },
  603. "barPercentStacked": {
  604. BarChart: &c,
  605. CatAx: catAx,
  606. ValAx: valAx,
  607. },
  608. "bar3DClustered": {
  609. Bar3DChart: &c,
  610. CatAx: catAx,
  611. ValAx: valAx,
  612. },
  613. "bar3DStacked": {
  614. Bar3DChart: &c,
  615. CatAx: catAx,
  616. ValAx: valAx,
  617. },
  618. "bar3DPercentStacked": {
  619. Bar3DChart: &c,
  620. CatAx: catAx,
  621. ValAx: valAx,
  622. },
  623. "col": {
  624. BarChart: &c,
  625. CatAx: catAx,
  626. ValAx: valAx,
  627. },
  628. "colStacked": {
  629. BarChart: &c,
  630. CatAx: catAx,
  631. ValAx: valAx,
  632. },
  633. "colPercentStacked": {
  634. BarChart: &c,
  635. CatAx: catAx,
  636. ValAx: valAx,
  637. },
  638. "col3DClustered": {
  639. Bar3DChart: &c,
  640. CatAx: catAx,
  641. ValAx: valAx,
  642. },
  643. "col3D": {
  644. Bar3DChart: &c,
  645. CatAx: catAx,
  646. ValAx: valAx,
  647. },
  648. "col3DStacked": {
  649. Bar3DChart: &c,
  650. CatAx: catAx,
  651. ValAx: valAx,
  652. },
  653. "col3DPercentStacked": {
  654. Bar3DChart: &c,
  655. CatAx: catAx,
  656. ValAx: valAx,
  657. },
  658. }
  659. return charts[formatSet.Type]
  660. }
  661. // drawDoughnutChart provides a function to draw the c:plotArea element for
  662. // doughnut chart by given format sets.
  663. func (f *File) drawDoughnutChart(formatSet *formatChart) *cPlotArea {
  664. return &cPlotArea{
  665. DoughnutChart: &cCharts{
  666. VaryColors: &attrValBool{
  667. Val: true,
  668. },
  669. Ser: f.drawChartSeries(formatSet),
  670. HoleSize: &attrValInt{Val: 75},
  671. },
  672. }
  673. }
  674. // drawLineChart provides a function to draw the c:plotArea element for line
  675. // chart by given format sets.
  676. func (f *File) drawLineChart(formatSet *formatChart) *cPlotArea {
  677. return &cPlotArea{
  678. LineChart: &cCharts{
  679. Grouping: &attrValString{
  680. Val: plotAreaChartGrouping[formatSet.Type],
  681. },
  682. VaryColors: &attrValBool{
  683. Val: false,
  684. },
  685. Ser: f.drawChartSeries(formatSet),
  686. DLbls: f.drawChartDLbls(formatSet),
  687. Smooth: &attrValBool{
  688. Val: false,
  689. },
  690. AxID: []*attrValInt{
  691. {Val: 754001152},
  692. {Val: 753999904},
  693. },
  694. },
  695. CatAx: f.drawPlotAreaCatAx(formatSet),
  696. ValAx: f.drawPlotAreaValAx(formatSet),
  697. }
  698. }
  699. // drawPieChart provides a function to draw the c:plotArea element for pie
  700. // chart by given format sets.
  701. func (f *File) drawPieChart(formatSet *formatChart) *cPlotArea {
  702. return &cPlotArea{
  703. PieChart: &cCharts{
  704. VaryColors: &attrValBool{
  705. Val: true,
  706. },
  707. Ser: f.drawChartSeries(formatSet),
  708. },
  709. }
  710. }
  711. // drawPie3DChart provides a function to draw the c:plotArea element for 3D
  712. // pie chart by given format sets.
  713. func (f *File) drawPie3DChart(formatSet *formatChart) *cPlotArea {
  714. return &cPlotArea{
  715. Pie3DChart: &cCharts{
  716. VaryColors: &attrValBool{
  717. Val: true,
  718. },
  719. Ser: f.drawChartSeries(formatSet),
  720. },
  721. }
  722. }
  723. // drawRadarChart provides a function to draw the c:plotArea element for radar
  724. // chart by given format sets.
  725. func (f *File) drawRadarChart(formatSet *formatChart) *cPlotArea {
  726. return &cPlotArea{
  727. RadarChart: &cCharts{
  728. RadarStyle: &attrValString{
  729. Val: "marker",
  730. },
  731. VaryColors: &attrValBool{
  732. Val: false,
  733. },
  734. Ser: f.drawChartSeries(formatSet),
  735. DLbls: f.drawChartDLbls(formatSet),
  736. AxID: []*attrValInt{
  737. {Val: 754001152},
  738. {Val: 753999904},
  739. },
  740. },
  741. CatAx: f.drawPlotAreaCatAx(formatSet),
  742. ValAx: f.drawPlotAreaValAx(formatSet),
  743. }
  744. }
  745. // drawScatterChart provides a function to draw the c:plotArea element for
  746. // scatter chart by given format sets.
  747. func (f *File) drawScatterChart(formatSet *formatChart) *cPlotArea {
  748. return &cPlotArea{
  749. ScatterChart: &cCharts{
  750. ScatterStyle: &attrValString{
  751. Val: "smoothMarker", // line,lineMarker,marker,none,smooth,smoothMarker
  752. },
  753. VaryColors: &attrValBool{
  754. Val: false,
  755. },
  756. Ser: f.drawChartSeries(formatSet),
  757. DLbls: f.drawChartDLbls(formatSet),
  758. AxID: []*attrValInt{
  759. {Val: 754001152},
  760. {Val: 753999904},
  761. },
  762. },
  763. CatAx: f.drawPlotAreaCatAx(formatSet),
  764. ValAx: f.drawPlotAreaValAx(formatSet),
  765. }
  766. }
  767. // drawChartSeries provides a function to draw the c:ser element by given
  768. // format sets.
  769. func (f *File) drawChartSeries(formatSet *formatChart) *[]cSer {
  770. ser := []cSer{}
  771. for k := range formatSet.Series {
  772. ser = append(ser, cSer{
  773. IDx: &attrValInt{Val: k},
  774. Order: &attrValInt{Val: k},
  775. Tx: &cTx{
  776. StrRef: &cStrRef{
  777. F: formatSet.Series[k].Name,
  778. },
  779. },
  780. SpPr: f.drawChartSeriesSpPr(k, formatSet),
  781. Marker: f.drawChartSeriesMarker(k, formatSet),
  782. DPt: f.drawChartSeriesDPt(k, formatSet),
  783. DLbls: f.drawChartSeriesDLbls(formatSet),
  784. Cat: f.drawChartSeriesCat(formatSet.Series[k], formatSet),
  785. Val: f.drawChartSeriesVal(formatSet.Series[k], formatSet),
  786. XVal: f.drawChartSeriesXVal(formatSet.Series[k], formatSet),
  787. YVal: f.drawChartSeriesYVal(formatSet.Series[k], formatSet),
  788. })
  789. }
  790. return &ser
  791. }
  792. // drawChartSeriesSpPr provides a function to draw the c:spPr element by given
  793. // format sets.
  794. func (f *File) drawChartSeriesSpPr(i int, formatSet *formatChart) *cSpPr {
  795. spPrScatter := &cSpPr{
  796. Ln: &aLn{
  797. W: 25400,
  798. NoFill: " ",
  799. },
  800. }
  801. spPrLine := &cSpPr{
  802. Ln: &aLn{
  803. W: 25400,
  804. Cap: "rnd", // rnd, sq, flat
  805. SolidFill: &aSolidFill{
  806. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
  807. },
  808. },
  809. }
  810. chartSeriesSpPr := map[string]*cSpPr{Bar: nil, BarStacked: nil, BarPercentStacked: nil, Bar3DClustered: nil, Bar3DStacked: nil, Bar3DPercentStacked: nil, Col: nil, ColStacked: nil, ColPercentStacked: nil, Col3DClustered: nil, Col3D: nil, Col3DStacked: nil, Col3DPercentStacked: nil, Doughnut: nil, Line: spPrLine, Pie: nil, Pie3D: nil, Radar: nil, Scatter: spPrScatter}
  811. return chartSeriesSpPr[formatSet.Type]
  812. }
  813. // drawChartSeriesDPt provides a function to draw the c:dPt element by given
  814. // data index and format sets.
  815. func (f *File) drawChartSeriesDPt(i int, formatSet *formatChart) []*cDPt {
  816. dpt := []*cDPt{{
  817. IDx: &attrValInt{Val: i},
  818. Bubble3D: &attrValBool{Val: false},
  819. SpPr: &cSpPr{
  820. SolidFill: &aSolidFill{
  821. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
  822. },
  823. Ln: &aLn{
  824. W: 25400,
  825. Cap: "rnd",
  826. SolidFill: &aSolidFill{
  827. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  828. },
  829. },
  830. Sp3D: &aSp3D{
  831. ContourW: 25400,
  832. ContourClr: &aContourClr{
  833. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  834. },
  835. },
  836. },
  837. }}
  838. chartSeriesDPt := map[string][]*cDPt{Bar: nil, BarStacked: nil, BarPercentStacked: nil, Bar3DClustered: nil, Bar3DStacked: nil, Bar3DPercentStacked: nil, Col: nil, ColStacked: nil, ColPercentStacked: nil, Col3DClustered: nil, Col3D: nil, Col3DStacked: nil, Col3DPercentStacked: nil, Doughnut: nil, Line: nil, Pie: dpt, Pie3D: dpt, Radar: nil, Scatter: nil}
  839. return chartSeriesDPt[formatSet.Type]
  840. }
  841. // drawChartSeriesCat provides a function to draw the c:cat element by given
  842. // chart series and format sets.
  843. func (f *File) drawChartSeriesCat(v formatChartSeries, formatSet *formatChart) *cCat {
  844. cat := &cCat{
  845. StrRef: &cStrRef{
  846. F: v.Categories,
  847. },
  848. }
  849. chartSeriesCat := map[string]*cCat{Bar: cat, BarStacked: cat, BarPercentStacked: cat, Bar3DClustered: cat, Bar3DStacked: cat, Bar3DPercentStacked: cat, Col: cat, ColStacked: cat, ColPercentStacked: cat, Col3DClustered: cat, Col3D: cat, Col3DStacked: cat, Col3DPercentStacked: cat, Doughnut: cat, Line: cat, Pie: cat, Pie3D: cat, Radar: cat, Scatter: nil}
  850. return chartSeriesCat[formatSet.Type]
  851. }
  852. // drawChartSeriesVal provides a function to draw the c:val element by given
  853. // chart series and format sets.
  854. func (f *File) drawChartSeriesVal(v formatChartSeries, formatSet *formatChart) *cVal {
  855. val := &cVal{
  856. NumRef: &cNumRef{
  857. F: v.Values,
  858. },
  859. }
  860. chartSeriesVal := map[string]*cVal{Bar: val, BarStacked: val, BarPercentStacked: val, Bar3DClustered: val, Bar3DStacked: val, Bar3DPercentStacked: val, Col: val, ColStacked: val, ColPercentStacked: val, Col3DClustered: val, Col3D: val, Col3DStacked: val, Col3DPercentStacked: val, Doughnut: val, Line: val, Pie: val, Pie3D: val, Radar: val, Scatter: nil}
  861. return chartSeriesVal[formatSet.Type]
  862. }
  863. // drawChartSeriesMarker provides a function to draw the c:marker element by
  864. // given data index and format sets.
  865. func (f *File) drawChartSeriesMarker(i int, formatSet *formatChart) *cMarker {
  866. marker := &cMarker{
  867. Symbol: &attrValString{Val: "circle"},
  868. Size: &attrValInt{Val: 5},
  869. SpPr: &cSpPr{
  870. SolidFill: &aSolidFill{
  871. SchemeClr: &aSchemeClr{
  872. Val: "accent" + strconv.Itoa(i+1),
  873. },
  874. },
  875. Ln: &aLn{
  876. W: 9252,
  877. SolidFill: &aSolidFill{
  878. SchemeClr: &aSchemeClr{
  879. Val: "accent" + strconv.Itoa(i+1),
  880. },
  881. },
  882. },
  883. },
  884. }
  885. chartSeriesMarker := map[string]*cMarker{Bar: nil, BarStacked: nil, BarPercentStacked: nil, Bar3DClustered: nil, Bar3DStacked: nil, Bar3DPercentStacked: nil, Col: nil, ColStacked: nil, ColPercentStacked: nil, Col3DClustered: nil, Col3D: nil, Col3DStacked: nil, Col3DPercentStacked: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: marker}
  886. return chartSeriesMarker[formatSet.Type]
  887. }
  888. // drawChartSeriesXVal provides a function to draw the c:xVal element by given
  889. // chart series and format sets.
  890. func (f *File) drawChartSeriesXVal(v formatChartSeries, formatSet *formatChart) *cCat {
  891. cat := &cCat{
  892. StrRef: &cStrRef{
  893. F: v.Categories,
  894. },
  895. }
  896. chartSeriesXVal := map[string]*cCat{Bar: nil, BarStacked: nil, BarPercentStacked: nil, Bar3DClustered: nil, Bar3DStacked: nil, Bar3DPercentStacked: nil, Col: nil, ColStacked: nil, ColPercentStacked: nil, Col3DClustered: nil, Col3D: nil, Col3DStacked: nil, Col3DPercentStacked: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: cat}
  897. return chartSeriesXVal[formatSet.Type]
  898. }
  899. // drawChartSeriesYVal provides a function to draw the c:yVal element by given
  900. // chart series and format sets.
  901. func (f *File) drawChartSeriesYVal(v formatChartSeries, formatSet *formatChart) *cVal {
  902. val := &cVal{
  903. NumRef: &cNumRef{
  904. F: v.Values,
  905. },
  906. }
  907. chartSeriesYVal := map[string]*cVal{Bar: nil, BarStacked: nil, BarPercentStacked: nil, Bar3DClustered: nil, Bar3DStacked: nil, Bar3DPercentStacked: nil, Col: nil, ColStacked: nil, ColPercentStacked: nil, Col3DClustered: nil, Col3D: nil, Col3DStacked: nil, Col3DPercentStacked: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: val}
  908. return chartSeriesYVal[formatSet.Type]
  909. }
  910. // drawChartDLbls provides a function to draw the c:dLbls element by given
  911. // format sets.
  912. func (f *File) drawChartDLbls(formatSet *formatChart) *cDLbls {
  913. return &cDLbls{
  914. ShowLegendKey: &attrValBool{Val: formatSet.Legend.ShowLegendKey},
  915. ShowVal: &attrValBool{Val: formatSet.Plotarea.ShowVal},
  916. ShowCatName: &attrValBool{Val: formatSet.Plotarea.ShowCatName},
  917. ShowSerName: &attrValBool{Val: formatSet.Plotarea.ShowSerName},
  918. ShowBubbleSize: &attrValBool{Val: formatSet.Plotarea.ShowBubbleSize},
  919. ShowPercent: &attrValBool{Val: formatSet.Plotarea.ShowPercent},
  920. ShowLeaderLines: &attrValBool{Val: formatSet.Plotarea.ShowLeaderLines},
  921. }
  922. }
  923. // drawChartSeriesDLbls provides a function to draw the c:dLbls element by
  924. // given format sets.
  925. func (f *File) drawChartSeriesDLbls(formatSet *formatChart) *cDLbls {
  926. dLbls := f.drawChartDLbls(formatSet)
  927. chartSeriesDLbls := map[string]*cDLbls{Bar: dLbls, BarStacked: dLbls, BarPercentStacked: dLbls, Bar3DClustered: dLbls, Bar3DStacked: dLbls, Bar3DPercentStacked: dLbls, Col: dLbls, ColStacked: dLbls, ColPercentStacked: dLbls, Col3DClustered: dLbls, Col3D: dLbls, Col3DStacked: dLbls, Col3DPercentStacked: dLbls, Doughnut: dLbls, Line: dLbls, Pie: dLbls, Pie3D: dLbls, Radar: dLbls, Scatter: nil}
  928. return chartSeriesDLbls[formatSet.Type]
  929. }
  930. // drawPlotAreaCatAx provides a function to draw the c:catAx element.
  931. func (f *File) drawPlotAreaCatAx(formatSet *formatChart) []*cAxs {
  932. min := &attrValFloat{Val: formatSet.XAxis.Minimum}
  933. max := &attrValFloat{Val: formatSet.XAxis.Maximum}
  934. if formatSet.XAxis.Minimum == 0 {
  935. min = nil
  936. }
  937. if formatSet.XAxis.Maximum == 0 {
  938. max = nil
  939. }
  940. return []*cAxs{
  941. {
  942. AxID: &attrValInt{Val: 754001152},
  943. Scaling: &cScaling{
  944. Orientation: &attrValString{Val: orientation[formatSet.XAxis.ReverseOrder]},
  945. Max: max,
  946. Min: min,
  947. },
  948. Delete: &attrValBool{Val: false},
  949. AxPos: &attrValString{Val: catAxPos[formatSet.XAxis.ReverseOrder]},
  950. NumFmt: &cNumFmt{
  951. FormatCode: "General",
  952. SourceLinked: true,
  953. },
  954. MajorTickMark: &attrValString{Val: "none"},
  955. MinorTickMark: &attrValString{Val: "none"},
  956. TickLblPos: &attrValString{Val: "nextTo"},
  957. SpPr: f.drawPlotAreaSpPr(),
  958. TxPr: f.drawPlotAreaTxPr(),
  959. CrossAx: &attrValInt{Val: 753999904},
  960. Crosses: &attrValString{Val: "autoZero"},
  961. Auto: &attrValBool{Val: true},
  962. LblAlgn: &attrValString{Val: "ctr"},
  963. LblOffset: &attrValInt{Val: 100},
  964. NoMultiLvlLbl: &attrValBool{Val: false},
  965. },
  966. }
  967. }
  968. // drawPlotAreaValAx provides a function to draw the c:valAx element.
  969. func (f *File) drawPlotAreaValAx(formatSet *formatChart) []*cAxs {
  970. min := &attrValFloat{Val: formatSet.YAxis.Minimum}
  971. max := &attrValFloat{Val: formatSet.YAxis.Maximum}
  972. if formatSet.YAxis.Minimum == 0 {
  973. min = nil
  974. }
  975. if formatSet.YAxis.Maximum == 0 {
  976. max = nil
  977. }
  978. return []*cAxs{
  979. {
  980. AxID: &attrValInt{Val: 753999904},
  981. Scaling: &cScaling{
  982. Orientation: &attrValString{Val: orientation[formatSet.YAxis.ReverseOrder]},
  983. Max: max,
  984. Min: min,
  985. },
  986. Delete: &attrValBool{Val: false},
  987. AxPos: &attrValString{Val: valAxPos[formatSet.YAxis.ReverseOrder]},
  988. NumFmt: &cNumFmt{
  989. FormatCode: chartValAxNumFmtFormatCode[formatSet.Type],
  990. SourceLinked: true,
  991. },
  992. MajorTickMark: &attrValString{Val: "none"},
  993. MinorTickMark: &attrValString{Val: "none"},
  994. TickLblPos: &attrValString{Val: "nextTo"},
  995. SpPr: f.drawPlotAreaSpPr(),
  996. TxPr: f.drawPlotAreaTxPr(),
  997. CrossAx: &attrValInt{Val: 754001152},
  998. Crosses: &attrValString{Val: "autoZero"},
  999. CrossBetween: &attrValString{Val: "between"},
  1000. },
  1001. }
  1002. }
  1003. // drawPlotAreaSpPr provides a function to draw the c:spPr element.
  1004. func (f *File) drawPlotAreaSpPr() *cSpPr {
  1005. return &cSpPr{
  1006. Ln: &aLn{
  1007. W: 9525,
  1008. Cap: "flat",
  1009. Cmpd: "sng",
  1010. Algn: "ctr",
  1011. SolidFill: &aSolidFill{
  1012. SchemeClr: &aSchemeClr{
  1013. Val: "tx1",
  1014. LumMod: &attrValInt{Val: 15000},
  1015. LumOff: &attrValInt{Val: 85000},
  1016. },
  1017. },
  1018. },
  1019. }
  1020. }
  1021. // drawPlotAreaTxPr provides a function to draw the c:txPr element.
  1022. func (f *File) drawPlotAreaTxPr() *cTxPr {
  1023. return &cTxPr{
  1024. BodyPr: aBodyPr{
  1025. Rot: -60000000,
  1026. SpcFirstLastPara: true,
  1027. VertOverflow: "ellipsis",
  1028. Vert: "horz",
  1029. Wrap: "square",
  1030. Anchor: "ctr",
  1031. AnchorCtr: true,
  1032. },
  1033. P: aP{
  1034. PPr: &aPPr{
  1035. DefRPr: aRPr{
  1036. Sz: 900,
  1037. B: false,
  1038. I: false,
  1039. U: "none",
  1040. Strike: "noStrike",
  1041. Kern: 1200,
  1042. Baseline: 0,
  1043. SolidFill: &aSolidFill{
  1044. SchemeClr: &aSchemeClr{
  1045. Val: "tx1",
  1046. LumMod: &attrValInt{Val: 15000},
  1047. LumOff: &attrValInt{Val: 85000},
  1048. },
  1049. },
  1050. Latin: &aLatin{Typeface: "+mn-lt"},
  1051. Ea: &aEa{Typeface: "+mn-ea"},
  1052. Cs: &aCs{Typeface: "+mn-cs"},
  1053. },
  1054. },
  1055. EndParaRPr: &aEndParaRPr{Lang: "en-US"},
  1056. },
  1057. }
  1058. }
  1059. // drawingParser provides a function to parse drawingXML. In order to solve
  1060. // the problem that the label structure is changed after serialization and
  1061. // deserialization, two different structures: decodeWsDr and encodeWsDr are
  1062. // defined.
  1063. func (f *File) drawingParser(drawingXML string, content *xlsxWsDr) int {
  1064. cNvPrID := 1
  1065. _, ok := f.XLSX[drawingXML]
  1066. if ok { // Append Model
  1067. decodeWsDr := decodeWsDr{}
  1068. _ = xml.Unmarshal([]byte(f.readXML(drawingXML)), &decodeWsDr)
  1069. content.R = decodeWsDr.R
  1070. cNvPrID = len(decodeWsDr.OneCellAnchor) + len(decodeWsDr.TwoCellAnchor) + 1
  1071. for _, v := range decodeWsDr.OneCellAnchor {
  1072. content.OneCellAnchor = append(content.OneCellAnchor, &xdrCellAnchor{
  1073. EditAs: v.EditAs,
  1074. GraphicFrame: v.Content,
  1075. })
  1076. }
  1077. for _, v := range decodeWsDr.TwoCellAnchor {
  1078. content.TwoCellAnchor = append(content.TwoCellAnchor, &xdrCellAnchor{
  1079. EditAs: v.EditAs,
  1080. GraphicFrame: v.Content,
  1081. })
  1082. }
  1083. }
  1084. return cNvPrID
  1085. }
  1086. // addDrawingChart provides a function to add chart graphic frame by given
  1087. // sheet, drawingXML, cell, width, height, relationship index and format sets.
  1088. func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rID int, formatSet *formatPicture) {
  1089. cell = strings.ToUpper(cell)
  1090. fromCol := string(strings.Map(letterOnlyMapF, cell))
  1091. fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
  1092. row := fromRow - 1
  1093. col := TitleToNumber(fromCol)
  1094. width = int(float64(width) * formatSet.XScale)
  1095. height = int(float64(height) * formatSet.YScale)
  1096. colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, col, row, formatSet.OffsetX, formatSet.OffsetY, width, height)
  1097. content := xlsxWsDr{}
  1098. content.A = NameSpaceDrawingML
  1099. content.Xdr = NameSpaceDrawingMLSpreadSheet
  1100. cNvPrID := f.drawingParser(drawingXML, &content)
  1101. twoCellAnchor := xdrCellAnchor{}
  1102. twoCellAnchor.EditAs = formatSet.Positioning
  1103. from := xlsxFrom{}
  1104. from.Col = colStart
  1105. from.ColOff = formatSet.OffsetX * EMU
  1106. from.Row = rowStart
  1107. from.RowOff = formatSet.OffsetY * EMU
  1108. to := xlsxTo{}
  1109. to.Col = colEnd
  1110. to.ColOff = x2 * EMU
  1111. to.Row = rowEnd
  1112. to.RowOff = y2 * EMU
  1113. twoCellAnchor.From = &from
  1114. twoCellAnchor.To = &to
  1115. graphicFrame := xlsxGraphicFrame{
  1116. NvGraphicFramePr: xlsxNvGraphicFramePr{
  1117. CNvPr: &xlsxCNvPr{
  1118. ID: f.countCharts() + f.countMedia() + 1,
  1119. Name: "Chart " + strconv.Itoa(cNvPrID),
  1120. },
  1121. },
  1122. Graphic: &xlsxGraphic{
  1123. GraphicData: &xlsxGraphicData{
  1124. URI: NameSpaceDrawingMLChart,
  1125. Chart: &xlsxChart{
  1126. C: NameSpaceDrawingMLChart,
  1127. R: SourceRelationship,
  1128. RID: "rId" + strconv.Itoa(rID),
  1129. },
  1130. },
  1131. },
  1132. }
  1133. graphic, _ := xml.Marshal(graphicFrame)
  1134. twoCellAnchor.GraphicFrame = string(graphic)
  1135. twoCellAnchor.ClientData = &xdrClientData{
  1136. FLocksWithSheet: formatSet.FLocksWithSheet,
  1137. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  1138. }
  1139. content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
  1140. output, _ := xml.Marshal(content)
  1141. f.saveFileList(drawingXML, output)
  1142. }