chart.go 38 KB

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