chart.go 38 KB

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