chart.go 37 KB

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