You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1837 lines
59KB

  1. // Copyright 2016 - 2019 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.10 or later.
  9. package excelize
  10. import (
  11. "encoding/json"
  12. "encoding/xml"
  13. "errors"
  14. "strconv"
  15. "strings"
  16. )
  17. // This section defines the currently supported chart types.
  18. const (
  19. Area = "area"
  20. AreaStacked = "areaStacked"
  21. AreaPercentStacked = "areaPercentStacked"
  22. Area3D = "area3D"
  23. Area3DStacked = "area3DStacked"
  24. Area3DPercentStacked = "area3DPercentStacked"
  25. Bar = "bar"
  26. BarStacked = "barStacked"
  27. BarPercentStacked = "barPercentStacked"
  28. Bar3DClustered = "bar3DClustered"
  29. Bar3DStacked = "bar3DStacked"
  30. Bar3DPercentStacked = "bar3DPercentStacked"
  31. Bar3DConeClustered = "bar3DConeClustered"
  32. Bar3DConeStacked = "bar3DConeStacked"
  33. Bar3DConePercentStacked = "bar3DConePercentStacked"
  34. Bar3DPyramidClustered = "bar3DPyramidClustered"
  35. Bar3DPyramidStacked = "bar3DPyramidStacked"
  36. Bar3DPyramidPercentStacked = "bar3DPyramidPercentStacked"
  37. Bar3DCylinderClustered = "bar3DCylinderClustered"
  38. Bar3DCylinderStacked = "bar3DCylinderStacked"
  39. Bar3DCylinderPercentStacked = "bar3DCylinderPercentStacked"
  40. Col = "col"
  41. ColStacked = "colStacked"
  42. ColPercentStacked = "colPercentStacked"
  43. Col3D = "col3D"
  44. Col3DClustered = "col3DClustered"
  45. Col3DStacked = "col3DStacked"
  46. Col3DPercentStacked = "col3DPercentStacked"
  47. Col3DCone = "col3DCone"
  48. Col3DConeClustered = "col3DConeClustered"
  49. Col3DConeStacked = "col3DConeStacked"
  50. Col3DConePercentStacked = "col3DConePercentStacked"
  51. Col3DPyramid = "col3DPyramid"
  52. Col3DPyramidClustered = "col3DPyramidClustered"
  53. Col3DPyramidStacked = "col3DPyramidStacked"
  54. Col3DPyramidPercentStacked = "col3DPyramidPercentStacked"
  55. Col3DCylinder = "col3DCylinder"
  56. Col3DCylinderClustered = "col3DCylinderClustered"
  57. Col3DCylinderStacked = "col3DCylinderStacked"
  58. Col3DCylinderPercentStacked = "col3DCylinderPercentStacked"
  59. Doughnut = "doughnut"
  60. Line = "line"
  61. Pie = "pie"
  62. Pie3D = "pie3D"
  63. Radar = "radar"
  64. Scatter = "scatter"
  65. Surface3D = "surface3D"
  66. WireframeSurface3D = "wireframeSurface3D"
  67. Contour = "contour"
  68. WireframeContour = "wireframeContour"
  69. Bubble = "bubble"
  70. Bubble3D = "bubble3D"
  71. )
  72. // This section defines the default value of chart properties.
  73. var (
  74. chartView3DRotX = map[string]int{
  75. Area: 0,
  76. AreaStacked: 0,
  77. AreaPercentStacked: 0,
  78. Area3D: 15,
  79. Area3DStacked: 15,
  80. Area3DPercentStacked: 15,
  81. Bar: 0,
  82. BarStacked: 0,
  83. BarPercentStacked: 0,
  84. Bar3DClustered: 15,
  85. Bar3DStacked: 15,
  86. Bar3DPercentStacked: 15,
  87. Bar3DConeClustered: 15,
  88. Bar3DConeStacked: 15,
  89. Bar3DConePercentStacked: 15,
  90. Bar3DPyramidClustered: 15,
  91. Bar3DPyramidStacked: 15,
  92. Bar3DPyramidPercentStacked: 15,
  93. Bar3DCylinderClustered: 15,
  94. Bar3DCylinderStacked: 15,
  95. Bar3DCylinderPercentStacked: 15,
  96. Col: 0,
  97. ColStacked: 0,
  98. ColPercentStacked: 0,
  99. Col3D: 15,
  100. Col3DClustered: 15,
  101. Col3DStacked: 15,
  102. Col3DPercentStacked: 15,
  103. Col3DCone: 15,
  104. Col3DConeClustered: 15,
  105. Col3DConeStacked: 15,
  106. Col3DConePercentStacked: 15,
  107. Col3DPyramid: 15,
  108. Col3DPyramidClustered: 15,
  109. Col3DPyramidStacked: 15,
  110. Col3DPyramidPercentStacked: 15,
  111. Col3DCylinder: 15,
  112. Col3DCylinderClustered: 15,
  113. Col3DCylinderStacked: 15,
  114. Col3DCylinderPercentStacked: 15,
  115. Doughnut: 0,
  116. Line: 0,
  117. Pie: 0,
  118. Pie3D: 30,
  119. Radar: 0,
  120. Scatter: 0,
  121. Surface3D: 15,
  122. WireframeSurface3D: 15,
  123. Contour: 90,
  124. WireframeContour: 90,
  125. }
  126. chartView3DRotY = map[string]int{
  127. Area: 0,
  128. AreaStacked: 0,
  129. AreaPercentStacked: 0,
  130. Area3D: 20,
  131. Area3DStacked: 20,
  132. Area3DPercentStacked: 20,
  133. Bar: 0,
  134. BarStacked: 0,
  135. BarPercentStacked: 0,
  136. Bar3DClustered: 20,
  137. Bar3DStacked: 20,
  138. Bar3DPercentStacked: 20,
  139. Bar3DConeClustered: 20,
  140. Bar3DConeStacked: 20,
  141. Bar3DConePercentStacked: 20,
  142. Bar3DPyramidClustered: 20,
  143. Bar3DPyramidStacked: 20,
  144. Bar3DPyramidPercentStacked: 20,
  145. Bar3DCylinderClustered: 20,
  146. Bar3DCylinderStacked: 20,
  147. Bar3DCylinderPercentStacked: 20,
  148. Col: 0,
  149. ColStacked: 0,
  150. ColPercentStacked: 0,
  151. Col3D: 20,
  152. Col3DClustered: 20,
  153. Col3DStacked: 20,
  154. Col3DPercentStacked: 20,
  155. Col3DCone: 20,
  156. Col3DConeClustered: 20,
  157. Col3DConeStacked: 20,
  158. Col3DConePercentStacked: 20,
  159. Col3DPyramid: 20,
  160. Col3DPyramidClustered: 20,
  161. Col3DPyramidStacked: 20,
  162. Col3DPyramidPercentStacked: 20,
  163. Col3DCylinder: 20,
  164. Col3DCylinderClustered: 20,
  165. Col3DCylinderStacked: 20,
  166. Col3DCylinderPercentStacked: 20,
  167. Doughnut: 0,
  168. Line: 0,
  169. Pie: 0,
  170. Pie3D: 0,
  171. Radar: 0,
  172. Scatter: 0,
  173. Surface3D: 20,
  174. WireframeSurface3D: 20,
  175. Contour: 0,
  176. WireframeContour: 0,
  177. }
  178. plotAreaChartOverlap = map[string]int{
  179. BarStacked: 100,
  180. BarPercentStacked: 100,
  181. ColStacked: 100,
  182. ColPercentStacked: 100,
  183. }
  184. chartView3DPerspective = map[string]int{
  185. Contour: 0,
  186. WireframeContour: 0,
  187. }
  188. chartView3DRAngAx = map[string]int{
  189. Area: 0,
  190. AreaStacked: 0,
  191. AreaPercentStacked: 0,
  192. Area3D: 1,
  193. Area3DStacked: 1,
  194. Area3DPercentStacked: 1,
  195. Bar: 0,
  196. BarStacked: 0,
  197. BarPercentStacked: 0,
  198. Bar3DClustered: 1,
  199. Bar3DStacked: 1,
  200. Bar3DPercentStacked: 1,
  201. Bar3DConeClustered: 1,
  202. Bar3DConeStacked: 1,
  203. Bar3DConePercentStacked: 1,
  204. Bar3DPyramidClustered: 1,
  205. Bar3DPyramidStacked: 1,
  206. Bar3DPyramidPercentStacked: 1,
  207. Bar3DCylinderClustered: 1,
  208. Bar3DCylinderStacked: 1,
  209. Bar3DCylinderPercentStacked: 1,
  210. Col: 0,
  211. ColStacked: 0,
  212. ColPercentStacked: 0,
  213. Col3D: 1,
  214. Col3DClustered: 1,
  215. Col3DStacked: 1,
  216. Col3DPercentStacked: 1,
  217. Col3DCone: 1,
  218. Col3DConeClustered: 1,
  219. Col3DConeStacked: 1,
  220. Col3DConePercentStacked: 1,
  221. Col3DPyramid: 1,
  222. Col3DPyramidClustered: 1,
  223. Col3DPyramidStacked: 1,
  224. Col3DPyramidPercentStacked: 1,
  225. Col3DCylinder: 1,
  226. Col3DCylinderClustered: 1,
  227. Col3DCylinderStacked: 1,
  228. Col3DCylinderPercentStacked: 1,
  229. Doughnut: 0,
  230. Line: 0,
  231. Pie: 0,
  232. Pie3D: 0,
  233. Radar: 0,
  234. Scatter: 0,
  235. Surface3D: 0,
  236. WireframeSurface3D: 0,
  237. Contour: 0,
  238. Bubble: 0,
  239. Bubble3D: 0,
  240. }
  241. chartLegendPosition = map[string]string{
  242. "bottom": "b",
  243. "left": "l",
  244. "right": "r",
  245. "top": "t",
  246. "top_right": "tr",
  247. }
  248. chartValAxNumFmtFormatCode = map[string]string{
  249. Area: "General",
  250. AreaStacked: "General",
  251. AreaPercentStacked: "0%",
  252. Area3D: "General",
  253. Area3DStacked: "General",
  254. Area3DPercentStacked: "0%",
  255. Bar: "General",
  256. BarStacked: "General",
  257. BarPercentStacked: "0%",
  258. Bar3DClustered: "General",
  259. Bar3DStacked: "General",
  260. Bar3DPercentStacked: "0%",
  261. Bar3DConeClustered: "General",
  262. Bar3DConeStacked: "General",
  263. Bar3DConePercentStacked: "0%",
  264. Bar3DPyramidClustered: "General",
  265. Bar3DPyramidStacked: "General",
  266. Bar3DPyramidPercentStacked: "0%",
  267. Bar3DCylinderClustered: "General",
  268. Bar3DCylinderStacked: "General",
  269. Bar3DCylinderPercentStacked: "0%",
  270. Col: "General",
  271. ColStacked: "General",
  272. ColPercentStacked: "0%",
  273. Col3D: "General",
  274. Col3DClustered: "General",
  275. Col3DStacked: "General",
  276. Col3DPercentStacked: "0%",
  277. Col3DCone: "General",
  278. Col3DConeClustered: "General",
  279. Col3DConeStacked: "General",
  280. Col3DConePercentStacked: "0%",
  281. Col3DPyramid: "General",
  282. Col3DPyramidClustered: "General",
  283. Col3DPyramidStacked: "General",
  284. Col3DPyramidPercentStacked: "0%",
  285. Col3DCylinder: "General",
  286. Col3DCylinderClustered: "General",
  287. Col3DCylinderStacked: "General",
  288. Col3DCylinderPercentStacked: "0%",
  289. Doughnut: "General",
  290. Line: "General",
  291. Pie: "General",
  292. Pie3D: "General",
  293. Radar: "General",
  294. Scatter: "General",
  295. Surface3D: "General",
  296. WireframeSurface3D: "General",
  297. Contour: "General",
  298. WireframeContour: "General",
  299. Bubble: "General",
  300. Bubble3D: "General",
  301. }
  302. chartValAxCrossBetween = map[string]string{
  303. Area: "midCat",
  304. AreaStacked: "midCat",
  305. AreaPercentStacked: "midCat",
  306. Area3D: "midCat",
  307. Area3DStacked: "midCat",
  308. Area3DPercentStacked: "midCat",
  309. Bar: "between",
  310. BarStacked: "between",
  311. BarPercentStacked: "between",
  312. Bar3DClustered: "between",
  313. Bar3DStacked: "between",
  314. Bar3DPercentStacked: "between",
  315. Bar3DConeClustered: "between",
  316. Bar3DConeStacked: "between",
  317. Bar3DConePercentStacked: "between",
  318. Bar3DPyramidClustered: "between",
  319. Bar3DPyramidStacked: "between",
  320. Bar3DPyramidPercentStacked: "between",
  321. Bar3DCylinderClustered: "between",
  322. Bar3DCylinderStacked: "between",
  323. Bar3DCylinderPercentStacked: "between",
  324. Col: "between",
  325. ColStacked: "between",
  326. ColPercentStacked: "between",
  327. Col3D: "between",
  328. Col3DClustered: "between",
  329. Col3DStacked: "between",
  330. Col3DPercentStacked: "between",
  331. Col3DCone: "between",
  332. Col3DConeClustered: "between",
  333. Col3DConeStacked: "between",
  334. Col3DConePercentStacked: "between",
  335. Col3DPyramid: "between",
  336. Col3DPyramidClustered: "between",
  337. Col3DPyramidStacked: "between",
  338. Col3DPyramidPercentStacked: "between",
  339. Col3DCylinder: "between",
  340. Col3DCylinderClustered: "between",
  341. Col3DCylinderStacked: "between",
  342. Col3DCylinderPercentStacked: "between",
  343. Doughnut: "between",
  344. Line: "between",
  345. Pie: "between",
  346. Pie3D: "between",
  347. Radar: "between",
  348. Scatter: "between",
  349. Surface3D: "midCat",
  350. WireframeSurface3D: "midCat",
  351. Contour: "midCat",
  352. WireframeContour: "midCat",
  353. Bubble: "midCat",
  354. Bubble3D: "midCat",
  355. }
  356. plotAreaChartGrouping = map[string]string{
  357. Area: "standard",
  358. AreaStacked: "stacked",
  359. AreaPercentStacked: "percentStacked",
  360. Area3D: "standard",
  361. Area3DStacked: "stacked",
  362. Area3DPercentStacked: "percentStacked",
  363. Bar: "clustered",
  364. BarStacked: "stacked",
  365. BarPercentStacked: "percentStacked",
  366. Bar3DClustered: "clustered",
  367. Bar3DStacked: "stacked",
  368. Bar3DPercentStacked: "percentStacked",
  369. Bar3DConeClustered: "clustered",
  370. Bar3DConeStacked: "stacked",
  371. Bar3DConePercentStacked: "percentStacked",
  372. Bar3DPyramidClustered: "clustered",
  373. Bar3DPyramidStacked: "stacked",
  374. Bar3DPyramidPercentStacked: "percentStacked",
  375. Bar3DCylinderClustered: "clustered",
  376. Bar3DCylinderStacked: "stacked",
  377. Bar3DCylinderPercentStacked: "percentStacked",
  378. Col: "clustered",
  379. ColStacked: "stacked",
  380. ColPercentStacked: "percentStacked",
  381. Col3D: "standard",
  382. Col3DClustered: "clustered",
  383. Col3DStacked: "stacked",
  384. Col3DPercentStacked: "percentStacked",
  385. Col3DCone: "standard",
  386. Col3DConeClustered: "clustered",
  387. Col3DConeStacked: "stacked",
  388. Col3DConePercentStacked: "percentStacked",
  389. Col3DPyramid: "standard",
  390. Col3DPyramidClustered: "clustered",
  391. Col3DPyramidStacked: "stacked",
  392. Col3DPyramidPercentStacked: "percentStacked",
  393. Col3DCylinder: "standard",
  394. Col3DCylinderClustered: "clustered",
  395. Col3DCylinderStacked: "stacked",
  396. Col3DCylinderPercentStacked: "percentStacked",
  397. Line: "standard",
  398. }
  399. plotAreaChartBarDir = map[string]string{
  400. Bar: "bar",
  401. BarStacked: "bar",
  402. BarPercentStacked: "bar",
  403. Bar3DClustered: "bar",
  404. Bar3DStacked: "bar",
  405. Bar3DPercentStacked: "bar",
  406. Bar3DConeClustered: "bar",
  407. Bar3DConeStacked: "bar",
  408. Bar3DConePercentStacked: "bar",
  409. Bar3DPyramidClustered: "bar",
  410. Bar3DPyramidStacked: "bar",
  411. Bar3DPyramidPercentStacked: "bar",
  412. Bar3DCylinderClustered: "bar",
  413. Bar3DCylinderStacked: "bar",
  414. Bar3DCylinderPercentStacked: "bar",
  415. Col: "col",
  416. ColStacked: "col",
  417. ColPercentStacked: "col",
  418. Col3D: "col",
  419. Col3DClustered: "col",
  420. Col3DStacked: "col",
  421. Col3DPercentStacked: "col",
  422. Col3DCone: "col",
  423. Col3DConeStacked: "col",
  424. Col3DConeClustered: "col",
  425. Col3DConePercentStacked: "col",
  426. Col3DPyramid: "col",
  427. Col3DPyramidClustered: "col",
  428. Col3DPyramidStacked: "col",
  429. Col3DPyramidPercentStacked: "col",
  430. Col3DCylinder: "col",
  431. Col3DCylinderClustered: "col",
  432. Col3DCylinderStacked: "col",
  433. Col3DCylinderPercentStacked: "col",
  434. Line: "standard",
  435. }
  436. orientation = map[bool]string{
  437. true: "maxMin",
  438. false: "minMax",
  439. }
  440. catAxPos = map[bool]string{
  441. true: "t",
  442. false: "b",
  443. }
  444. valAxPos = map[bool]string{
  445. true: "r",
  446. false: "l",
  447. }
  448. valTickLblPos = map[string]string{
  449. Contour: "none",
  450. WireframeContour: "none",
  451. }
  452. )
  453. // parseFormatChartSet provides a function to parse the format settings of the
  454. // chart with default value.
  455. func parseFormatChartSet(formatSet string) (*formatChart, error) {
  456. format := formatChart{
  457. Dimension: formatChartDimension{
  458. Width: 480,
  459. Height: 290,
  460. },
  461. Format: formatPicture{
  462. FPrintsWithSheet: true,
  463. FLocksWithSheet: false,
  464. NoChangeAspect: false,
  465. OffsetX: 0,
  466. OffsetY: 0,
  467. XScale: 1.0,
  468. YScale: 1.0,
  469. },
  470. Legend: formatChartLegend{
  471. Position: "bottom",
  472. ShowLegendKey: false,
  473. },
  474. Title: formatChartTitle{
  475. Name: " ",
  476. },
  477. ShowBlanksAs: "gap",
  478. }
  479. err := json.Unmarshal([]byte(formatSet), &format)
  480. return &format, err
  481. }
  482. // AddChart provides the method to add chart in a sheet by given chart format
  483. // set (such as offset, scale, aspect ratio setting and print settings) and
  484. // properties set. For example, create 3D clustered column chart with data
  485. // Sheet1!$A$29:$D$32:
  486. //
  487. // package main
  488. //
  489. // import (
  490. // "fmt"
  491. //
  492. // "github.com/360EntSecGroup-Skylar/excelize"
  493. // )
  494. //
  495. // func main() {
  496. // categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  497. // values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  498. // f := excelize.NewFile()
  499. // for k, v := range categories {
  500. // f.SetCellValue("Sheet1", k, v)
  501. // }
  502. // for k, v := range values {
  503. // f.SetCellValue("Sheet1", k, v)
  504. // }
  505. // err := f.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}}`)
  506. // if err != nil {
  507. // fmt.Println(err)
  508. // return
  509. // }
  510. // // Save xlsx file by the given path.
  511. // err = xlsx.SaveAs("./Book1.xlsx")
  512. // if err != nil {
  513. // fmt.Println(err)
  514. // }
  515. // }
  516. //
  517. // The following shows the type of chart supported by excelize:
  518. //
  519. // Type | Chart
  520. // -----------------------------+------------------------------
  521. // area | 2D area chart
  522. // areaStacked | 2D stacked area chart
  523. // areaPercentStacked | 2D 100% stacked area chart
  524. // area3D | 3D area chart
  525. // area3DStacked | 3D stacked area chart
  526. // area3DPercentStacked | 3D 100% stacked area chart
  527. // bar | 2D clustered bar chart
  528. // barStacked | 2D stacked bar chart
  529. // barPercentStacked | 2D 100% stacked bar chart
  530. // bar3DClustered | 3D clustered bar chart
  531. // bar3DStacked | 3D stacked bar chart
  532. // bar3DPercentStacked | 3D 100% stacked bar chart
  533. // bar3DConeClustered | 3D cone clustered bar chart
  534. // bar3DConeStacked | 3D cone stacked bar chart
  535. // bar3DConePercentStacked | 3D cone percent bar chart
  536. // bar3DPyramidClustered | 3D pyramid clustered bar chart
  537. // bar3DPyramidStacked | 3D pyramid stacked bar chart
  538. // bar3DPyramidPercentStacked | 3D pyramid percent stacked bar chart
  539. // bar3DCylinderClustered | 3D cylinder clustered bar chart
  540. // bar3DCylinderStacked | 3D cylinder stacked bar chart
  541. // bar3DCylinderPercentStacked | 3D cylinder percent stacked bar chart
  542. // col | 2D clustered column chart
  543. // colStacked | 2D stacked column chart
  544. // colPercentStacked | 2D 100% stacked column chart
  545. // col3DClustered | 3D clustered column chart
  546. // col3D | 3D column chart
  547. // col3DStacked | 3D stacked column chart
  548. // col3DPercentStacked | 3D 100% stacked column chart
  549. // col3DCone | 3D cone column chart
  550. // col3DConeClustered | 3D cone clustered column chart
  551. // col3DConeStacked | 3D cone stacked column chart
  552. // col3DConePercentStacked | 3D cone percent stacked column chart
  553. // col3DPyramid | 3D pyramid column chart
  554. // col3DPyramidClustered | 3D pyramid clustered column chart
  555. // col3DPyramidStacked | 3D pyramid stacked column chart
  556. // col3DPyramidPercentStacked | 3D pyramid percent stacked column chart
  557. // col3DCylinder | 3D cylinder column chart
  558. // col3DCylinderClustered | 3D cylinder clustered column chart
  559. // col3DCylinderStacked | 3D cylinder stacked column chart
  560. // col3DCylinderPercentStacked | 3D cylinder percent stacked column chart
  561. // doughnut | doughnut chart
  562. // line | line chart
  563. // pie | pie chart
  564. // pie3D | 3D pie chart
  565. // radar | radar chart
  566. // scatter | scatter chart
  567. // surface3D | 3D surface chart
  568. // wireframeSurface3D | 3D wireframe surface chart
  569. // contour | contour chart
  570. // wireframeContour | wireframe contour chart
  571. // bubble | bubble chart
  572. // bubble3D | 3D bubble chart
  573. //
  574. // In Excel a chart series is a collection of information that defines which data is plotted such as values, axis labels and formatting.
  575. //
  576. // The series options that can be set are:
  577. //
  578. // name
  579. // categories
  580. // values
  581. // line
  582. //
  583. // 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
  584. //
  585. // 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.
  586. //
  587. // 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.
  588. //
  589. // line: This sets the line format of the line chart. The line property is optional and if it isn't supplied it will default style. The options that can be set is width. The range of width is 0.25pt - 999pt. If the value of width is outside the range, the default width of the line is 2pt.
  590. //
  591. // Set properties of the chart legend. The options that can be set are:
  592. //
  593. // position
  594. // show_legend_key
  595. //
  596. // position: Set the position of the chart legend. The default legend position is right. The available positions are:
  597. //
  598. // top
  599. // bottom
  600. // left
  601. // right
  602. // top_right
  603. //
  604. // show_legend_key: Set the legend keys shall be shown in data labels. The default value is false.
  605. //
  606. // Set properties of the chart title. The properties that can be set are:
  607. //
  608. // title
  609. //
  610. // 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.
  611. //
  612. // 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:
  613. //
  614. // gap
  615. // span
  616. // zero
  617. //
  618. // gap: Specifies that blank values shall be left as a gap.
  619. //
  620. // sapn: Specifies that blank values shall be spanned with a line.
  621. //
  622. // zero: Specifies that blank values shall be treated as zero.
  623. //
  624. // Set chart offset, scale, aspect ratio setting and print settings by format, same as function AddPicture.
  625. //
  626. // Set the position of the chart plot area by plotarea. The properties that can be set are:
  627. //
  628. // show_bubble_size
  629. // show_cat_name
  630. // show_leader_lines
  631. // show_percent
  632. // show_series_name
  633. // show_val
  634. //
  635. // 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.
  636. //
  637. // 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.
  638. //
  639. // show_leader_lines: Specifies leader lines shall be shown for data labels. The show_leader_lines property is optional. The default value is false.
  640. //
  641. // show_percent: Specifies that the percentage shall be shown in a data label. The show_percent property is optional. The default value is false.
  642. //
  643. // 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.
  644. //
  645. // show_val: Specifies that the value shall be shown in a data label. The show_val property is optional. The default value is false.
  646. //
  647. // Set the primary horizontal and vertical axis options by x_axis and y_axis. The properties that can be set are:
  648. //
  649. // reverse_order
  650. // maximum
  651. // minimum
  652. //
  653. // 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.
  654. //
  655. // maximum: Specifies that the fixed maximum, 0 is auto. The maximum property is optional. The default value is auto.
  656. //
  657. // minimum: Specifies that the fixed minimum, 0 is auto. The minimum property is optional. The default value is auto.
  658. //
  659. // Set chart size by dimension property. The dimension property is optional. The default width is 480, and height is 290.
  660. //
  661. func (f *File) AddChart(sheet, cell, format string) error {
  662. formatSet, err := parseFormatChartSet(format)
  663. if err != nil {
  664. return err
  665. }
  666. // Read sheet data.
  667. xlsx, err := f.workSheetReader(sheet)
  668. if err != nil {
  669. return err
  670. }
  671. if _, ok := chartValAxNumFmtFormatCode[formatSet.Type]; !ok {
  672. return errors.New("unsupported chart type " + formatSet.Type)
  673. }
  674. // Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
  675. drawingID := f.countDrawings() + 1
  676. chartID := f.countCharts() + 1
  677. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  678. drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
  679. drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
  680. drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
  681. err = f.addDrawingChart(sheet, drawingXML, cell, formatSet.Dimension.Width, formatSet.Dimension.Height, drawingRID, &formatSet.Format)
  682. if err != nil {
  683. return err
  684. }
  685. f.addChart(formatSet)
  686. f.addContentTypePart(chartID, "chart")
  687. f.addContentTypePart(drawingID, "drawings")
  688. return err
  689. }
  690. // countCharts provides a function to get chart files count storage in the
  691. // folder xl/charts.
  692. func (f *File) countCharts() int {
  693. count := 0
  694. for k := range f.XLSX {
  695. if strings.Contains(k, "xl/charts/chart") {
  696. count++
  697. }
  698. }
  699. return count
  700. }
  701. // prepareDrawing provides a function to prepare drawing ID and XML by given
  702. // drawingID, worksheet name and default drawingXML.
  703. func (f *File) prepareDrawing(xlsx *xlsxWorksheet, drawingID int, sheet, drawingXML string) (int, string) {
  704. sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  705. if xlsx.Drawing != nil {
  706. // The worksheet already has a picture or chart relationships, use the relationships drawing ../drawings/drawing%d.xml.
  707. sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID)
  708. drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml"))
  709. drawingXML = strings.Replace(sheetRelationshipsDrawingXML, "..", "xl", -1)
  710. } else {
  711. // Add first picture for given sheet.
  712. sheetPath, _ := f.sheetMap[trimSheetName(sheet)]
  713. sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(sheetPath, "xl/worksheets/") + ".rels"
  714. rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
  715. f.addSheetDrawing(sheet, rID)
  716. }
  717. return drawingID, drawingXML
  718. }
  719. // addChart provides a function to create chart as xl/charts/chart%d.xml by
  720. // given format sets.
  721. func (f *File) addChart(formatSet *formatChart) {
  722. count := f.countCharts()
  723. xlsxChartSpace := xlsxChartSpace{
  724. XMLNSc: NameSpaceDrawingMLChart,
  725. XMLNSa: NameSpaceDrawingML,
  726. XMLNSr: SourceRelationship,
  727. XMLNSc16r2: SourceRelationshipChart201506,
  728. Date1904: &attrValBool{Val: false},
  729. Lang: &attrValString{Val: "en-US"},
  730. RoundedCorners: &attrValBool{Val: false},
  731. Chart: cChart{
  732. Title: &cTitle{
  733. Tx: cTx{
  734. Rich: &cRich{
  735. P: aP{
  736. PPr: &aPPr{
  737. DefRPr: aRPr{
  738. Kern: 1200,
  739. Strike: "noStrike",
  740. U: "none",
  741. Sz: 1400,
  742. SolidFill: &aSolidFill{
  743. SchemeClr: &aSchemeClr{
  744. Val: "tx1",
  745. LumMod: &attrValInt{
  746. Val: 65000,
  747. },
  748. LumOff: &attrValInt{
  749. Val: 35000,
  750. },
  751. },
  752. },
  753. Ea: &aEa{
  754. Typeface: "+mn-ea",
  755. },
  756. Cs: &aCs{
  757. Typeface: "+mn-cs",
  758. },
  759. Latin: &aLatin{
  760. Typeface: "+mn-lt",
  761. },
  762. },
  763. },
  764. R: &aR{
  765. RPr: aRPr{
  766. Lang: "en-US",
  767. AltLang: "en-US",
  768. },
  769. T: formatSet.Title.Name,
  770. },
  771. },
  772. },
  773. },
  774. TxPr: cTxPr{
  775. P: aP{
  776. PPr: &aPPr{
  777. DefRPr: aRPr{
  778. Kern: 1200,
  779. U: "none",
  780. Sz: 14000,
  781. Strike: "noStrike",
  782. },
  783. },
  784. EndParaRPr: &aEndParaRPr{
  785. Lang: "en-US",
  786. },
  787. },
  788. },
  789. },
  790. View3D: &cView3D{
  791. RotX: &attrValInt{Val: chartView3DRotX[formatSet.Type]},
  792. RotY: &attrValInt{Val: chartView3DRotY[formatSet.Type]},
  793. Perspective: &attrValInt{Val: chartView3DPerspective[formatSet.Type]},
  794. RAngAx: &attrValInt{Val: chartView3DRAngAx[formatSet.Type]},
  795. },
  796. Floor: &cThicknessSpPr{
  797. Thickness: &attrValInt{Val: 0},
  798. },
  799. SideWall: &cThicknessSpPr{
  800. Thickness: &attrValInt{Val: 0},
  801. },
  802. BackWall: &cThicknessSpPr{
  803. Thickness: &attrValInt{Val: 0},
  804. },
  805. PlotArea: &cPlotArea{},
  806. Legend: &cLegend{
  807. LegendPos: &attrValString{Val: chartLegendPosition[formatSet.Legend.Position]},
  808. Overlay: &attrValBool{Val: false},
  809. },
  810. PlotVisOnly: &attrValBool{Val: false},
  811. DispBlanksAs: &attrValString{Val: formatSet.ShowBlanksAs},
  812. ShowDLblsOverMax: &attrValBool{Val: false},
  813. },
  814. SpPr: &cSpPr{
  815. SolidFill: &aSolidFill{
  816. SchemeClr: &aSchemeClr{Val: "bg1"},
  817. },
  818. Ln: &aLn{
  819. W: 9525,
  820. Cap: "flat",
  821. Cmpd: "sng",
  822. Algn: "ctr",
  823. SolidFill: &aSolidFill{
  824. SchemeClr: &aSchemeClr{Val: "tx1",
  825. LumMod: &attrValInt{
  826. Val: 15000,
  827. },
  828. LumOff: &attrValInt{
  829. Val: 85000,
  830. },
  831. },
  832. },
  833. },
  834. },
  835. PrintSettings: &cPrintSettings{
  836. PageMargins: &cPageMargins{
  837. B: 0.75,
  838. L: 0.7,
  839. R: 0.7,
  840. T: 0.7,
  841. Header: 0.3,
  842. Footer: 0.3,
  843. },
  844. },
  845. }
  846. plotAreaFunc := map[string]func(*formatChart) *cPlotArea{
  847. Area: f.drawBaseChart,
  848. AreaStacked: f.drawBaseChart,
  849. AreaPercentStacked: f.drawBaseChart,
  850. Area3D: f.drawBaseChart,
  851. Area3DStacked: f.drawBaseChart,
  852. Area3DPercentStacked: f.drawBaseChart,
  853. Bar: f.drawBaseChart,
  854. BarStacked: f.drawBaseChart,
  855. BarPercentStacked: f.drawBaseChart,
  856. Bar3DClustered: f.drawBaseChart,
  857. Bar3DStacked: f.drawBaseChart,
  858. Bar3DPercentStacked: f.drawBaseChart,
  859. Bar3DConeClustered: f.drawBaseChart,
  860. Bar3DConeStacked: f.drawBaseChart,
  861. Bar3DConePercentStacked: f.drawBaseChart,
  862. Bar3DPyramidClustered: f.drawBaseChart,
  863. Bar3DPyramidStacked: f.drawBaseChart,
  864. Bar3DPyramidPercentStacked: f.drawBaseChart,
  865. Bar3DCylinderClustered: f.drawBaseChart,
  866. Bar3DCylinderStacked: f.drawBaseChart,
  867. Bar3DCylinderPercentStacked: f.drawBaseChart,
  868. Col: f.drawBaseChart,
  869. ColStacked: f.drawBaseChart,
  870. ColPercentStacked: f.drawBaseChart,
  871. Col3D: f.drawBaseChart,
  872. Col3DClustered: f.drawBaseChart,
  873. Col3DStacked: f.drawBaseChart,
  874. Col3DPercentStacked: f.drawBaseChart,
  875. Col3DCone: f.drawBaseChart,
  876. Col3DConeClustered: f.drawBaseChart,
  877. Col3DConeStacked: f.drawBaseChart,
  878. Col3DConePercentStacked: f.drawBaseChart,
  879. Col3DPyramid: f.drawBaseChart,
  880. Col3DPyramidClustered: f.drawBaseChart,
  881. Col3DPyramidStacked: f.drawBaseChart,
  882. Col3DPyramidPercentStacked: f.drawBaseChart,
  883. Col3DCylinder: f.drawBaseChart,
  884. Col3DCylinderClustered: f.drawBaseChart,
  885. Col3DCylinderStacked: f.drawBaseChart,
  886. Col3DCylinderPercentStacked: f.drawBaseChart,
  887. Doughnut: f.drawDoughnutChart,
  888. Line: f.drawLineChart,
  889. Pie3D: f.drawPie3DChart,
  890. Pie: f.drawPieChart,
  891. Radar: f.drawRadarChart,
  892. Scatter: f.drawScatterChart,
  893. Surface3D: f.drawSurface3DChart,
  894. WireframeSurface3D: f.drawSurface3DChart,
  895. Contour: f.drawSurfaceChart,
  896. WireframeContour: f.drawSurfaceChart,
  897. Bubble: f.drawBaseChart,
  898. Bubble3D: f.drawBaseChart,
  899. }
  900. xlsxChartSpace.Chart.PlotArea = plotAreaFunc[formatSet.Type](formatSet)
  901. chart, _ := xml.Marshal(xlsxChartSpace)
  902. media := "xl/charts/chart" + strconv.Itoa(count+1) + ".xml"
  903. f.saveFileList(media, chart)
  904. }
  905. // drawBaseChart provides a function to draw the c:plotArea element for bar,
  906. // and column series charts by given format sets.
  907. func (f *File) drawBaseChart(formatSet *formatChart) *cPlotArea {
  908. c := cCharts{
  909. BarDir: &attrValString{
  910. Val: "col",
  911. },
  912. Grouping: &attrValString{
  913. Val: "clustered",
  914. },
  915. VaryColors: &attrValBool{
  916. Val: true,
  917. },
  918. Ser: f.drawChartSeries(formatSet),
  919. Shape: f.drawChartShape(formatSet),
  920. DLbls: f.drawChartDLbls(formatSet),
  921. AxID: []*attrValInt{
  922. {Val: 754001152},
  923. {Val: 753999904},
  924. },
  925. Overlap: &attrValInt{Val: 100},
  926. }
  927. var ok bool
  928. if c.BarDir.Val, ok = plotAreaChartBarDir[formatSet.Type]; !ok {
  929. c.BarDir = nil
  930. }
  931. if c.Grouping.Val, ok = plotAreaChartGrouping[formatSet.Type]; !ok {
  932. c.Grouping = nil
  933. }
  934. if c.Overlap.Val, ok = plotAreaChartOverlap[formatSet.Type]; !ok {
  935. c.Overlap = nil
  936. }
  937. catAx := f.drawPlotAreaCatAx(formatSet)
  938. valAx := f.drawPlotAreaValAx(formatSet)
  939. charts := map[string]*cPlotArea{
  940. "area": {
  941. AreaChart: &c,
  942. CatAx: catAx,
  943. ValAx: valAx,
  944. },
  945. "areaStacked": {
  946. AreaChart: &c,
  947. CatAx: catAx,
  948. ValAx: valAx,
  949. },
  950. "areaPercentStacked": {
  951. AreaChart: &c,
  952. CatAx: catAx,
  953. ValAx: valAx,
  954. },
  955. "area3D": {
  956. Area3DChart: &c,
  957. CatAx: catAx,
  958. ValAx: valAx,
  959. },
  960. "area3DStacked": {
  961. Area3DChart: &c,
  962. CatAx: catAx,
  963. ValAx: valAx,
  964. },
  965. "area3DPercentStacked": {
  966. Area3DChart: &c,
  967. CatAx: catAx,
  968. ValAx: valAx,
  969. },
  970. "bar": {
  971. BarChart: &c,
  972. CatAx: catAx,
  973. ValAx: valAx,
  974. },
  975. "barStacked": {
  976. BarChart: &c,
  977. CatAx: catAx,
  978. ValAx: valAx,
  979. },
  980. "barPercentStacked": {
  981. BarChart: &c,
  982. CatAx: catAx,
  983. ValAx: valAx,
  984. },
  985. "bar3DClustered": {
  986. Bar3DChart: &c,
  987. CatAx: catAx,
  988. ValAx: valAx,
  989. },
  990. "bar3DStacked": {
  991. Bar3DChart: &c,
  992. CatAx: catAx,
  993. ValAx: valAx,
  994. },
  995. "bar3DPercentStacked": {
  996. Bar3DChart: &c,
  997. CatAx: catAx,
  998. ValAx: valAx,
  999. },
  1000. "bar3DConeClustered": {
  1001. Bar3DChart: &c,
  1002. CatAx: catAx,
  1003. ValAx: valAx,
  1004. },
  1005. "bar3DConeStacked": {
  1006. Bar3DChart: &c,
  1007. CatAx: catAx,
  1008. ValAx: valAx,
  1009. },
  1010. "bar3DConePercentStacked": {
  1011. Bar3DChart: &c,
  1012. CatAx: catAx,
  1013. ValAx: valAx,
  1014. },
  1015. "bar3DPyramidClustered": {
  1016. Bar3DChart: &c,
  1017. CatAx: catAx,
  1018. ValAx: valAx,
  1019. },
  1020. "bar3DPyramidStacked": {
  1021. Bar3DChart: &c,
  1022. CatAx: catAx,
  1023. ValAx: valAx,
  1024. },
  1025. "bar3DPyramidPercentStacked": {
  1026. Bar3DChart: &c,
  1027. CatAx: catAx,
  1028. ValAx: valAx,
  1029. },
  1030. "bar3DCylinderClustered": {
  1031. Bar3DChart: &c,
  1032. CatAx: catAx,
  1033. ValAx: valAx,
  1034. },
  1035. "bar3DCylinderStacked": {
  1036. Bar3DChart: &c,
  1037. CatAx: catAx,
  1038. ValAx: valAx,
  1039. },
  1040. "bar3DCylinderPercentStacked": {
  1041. Bar3DChart: &c,
  1042. CatAx: catAx,
  1043. ValAx: valAx,
  1044. },
  1045. "col": {
  1046. BarChart: &c,
  1047. CatAx: catAx,
  1048. ValAx: valAx,
  1049. },
  1050. "colStacked": {
  1051. BarChart: &c,
  1052. CatAx: catAx,
  1053. ValAx: valAx,
  1054. },
  1055. "colPercentStacked": {
  1056. BarChart: &c,
  1057. CatAx: catAx,
  1058. ValAx: valAx,
  1059. },
  1060. "col3D": {
  1061. Bar3DChart: &c,
  1062. CatAx: catAx,
  1063. ValAx: valAx,
  1064. },
  1065. "col3DClustered": {
  1066. Bar3DChart: &c,
  1067. CatAx: catAx,
  1068. ValAx: valAx,
  1069. },
  1070. "col3DStacked": {
  1071. Bar3DChart: &c,
  1072. CatAx: catAx,
  1073. ValAx: valAx,
  1074. },
  1075. "col3DPercentStacked": {
  1076. Bar3DChart: &c,
  1077. CatAx: catAx,
  1078. ValAx: valAx,
  1079. },
  1080. "col3DCone": {
  1081. Bar3DChart: &c,
  1082. CatAx: catAx,
  1083. ValAx: valAx,
  1084. },
  1085. "col3DConeClustered": {
  1086. Bar3DChart: &c,
  1087. CatAx: catAx,
  1088. ValAx: valAx,
  1089. },
  1090. "col3DConeStacked": {
  1091. Bar3DChart: &c,
  1092. CatAx: catAx,
  1093. ValAx: valAx,
  1094. },
  1095. "col3DConePercentStacked": {
  1096. Bar3DChart: &c,
  1097. CatAx: catAx,
  1098. ValAx: valAx,
  1099. },
  1100. "col3DPyramid": {
  1101. Bar3DChart: &c,
  1102. CatAx: catAx,
  1103. ValAx: valAx,
  1104. },
  1105. "col3DPyramidClustered": {
  1106. Bar3DChart: &c,
  1107. CatAx: catAx,
  1108. ValAx: valAx,
  1109. },
  1110. "col3DPyramidStacked": {
  1111. Bar3DChart: &c,
  1112. CatAx: catAx,
  1113. ValAx: valAx,
  1114. },
  1115. "col3DPyramidPercentStacked": {
  1116. Bar3DChart: &c,
  1117. CatAx: catAx,
  1118. ValAx: valAx,
  1119. },
  1120. "col3DCylinder": {
  1121. Bar3DChart: &c,
  1122. CatAx: catAx,
  1123. ValAx: valAx,
  1124. },
  1125. "col3DCylinderClustered": {
  1126. Bar3DChart: &c,
  1127. CatAx: catAx,
  1128. ValAx: valAx,
  1129. },
  1130. "col3DCylinderStacked": {
  1131. Bar3DChart: &c,
  1132. CatAx: catAx,
  1133. ValAx: valAx,
  1134. },
  1135. "col3DCylinderPercentStacked": {
  1136. Bar3DChart: &c,
  1137. CatAx: catAx,
  1138. ValAx: valAx,
  1139. },
  1140. "bubble": {
  1141. BubbleChart: &c,
  1142. CatAx: catAx,
  1143. ValAx: valAx,
  1144. },
  1145. "bubble3D": {
  1146. BubbleChart: &c,
  1147. CatAx: catAx,
  1148. ValAx: valAx,
  1149. },
  1150. }
  1151. return charts[formatSet.Type]
  1152. }
  1153. // drawDoughnutChart provides a function to draw the c:plotArea element for
  1154. // doughnut chart by given format sets.
  1155. func (f *File) drawDoughnutChart(formatSet *formatChart) *cPlotArea {
  1156. return &cPlotArea{
  1157. DoughnutChart: &cCharts{
  1158. VaryColors: &attrValBool{
  1159. Val: true,
  1160. },
  1161. Ser: f.drawChartSeries(formatSet),
  1162. HoleSize: &attrValInt{Val: 75},
  1163. },
  1164. }
  1165. }
  1166. // drawLineChart provides a function to draw the c:plotArea element for line
  1167. // chart by given format sets.
  1168. func (f *File) drawLineChart(formatSet *formatChart) *cPlotArea {
  1169. return &cPlotArea{
  1170. LineChart: &cCharts{
  1171. Grouping: &attrValString{
  1172. Val: plotAreaChartGrouping[formatSet.Type],
  1173. },
  1174. VaryColors: &attrValBool{
  1175. Val: false,
  1176. },
  1177. Ser: f.drawChartSeries(formatSet),
  1178. DLbls: f.drawChartDLbls(formatSet),
  1179. Smooth: &attrValBool{
  1180. Val: false,
  1181. },
  1182. AxID: []*attrValInt{
  1183. {Val: 754001152},
  1184. {Val: 753999904},
  1185. },
  1186. },
  1187. CatAx: f.drawPlotAreaCatAx(formatSet),
  1188. ValAx: f.drawPlotAreaValAx(formatSet),
  1189. }
  1190. }
  1191. // drawPieChart provides a function to draw the c:plotArea element for pie
  1192. // chart by given format sets.
  1193. func (f *File) drawPieChart(formatSet *formatChart) *cPlotArea {
  1194. return &cPlotArea{
  1195. PieChart: &cCharts{
  1196. VaryColors: &attrValBool{
  1197. Val: true,
  1198. },
  1199. Ser: f.drawChartSeries(formatSet),
  1200. },
  1201. }
  1202. }
  1203. // drawPie3DChart provides a function to draw the c:plotArea element for 3D
  1204. // pie chart by given format sets.
  1205. func (f *File) drawPie3DChart(formatSet *formatChart) *cPlotArea {
  1206. return &cPlotArea{
  1207. Pie3DChart: &cCharts{
  1208. VaryColors: &attrValBool{
  1209. Val: true,
  1210. },
  1211. Ser: f.drawChartSeries(formatSet),
  1212. },
  1213. }
  1214. }
  1215. // drawRadarChart provides a function to draw the c:plotArea element for radar
  1216. // chart by given format sets.
  1217. func (f *File) drawRadarChart(formatSet *formatChart) *cPlotArea {
  1218. return &cPlotArea{
  1219. RadarChart: &cCharts{
  1220. RadarStyle: &attrValString{
  1221. Val: "marker",
  1222. },
  1223. VaryColors: &attrValBool{
  1224. Val: false,
  1225. },
  1226. Ser: f.drawChartSeries(formatSet),
  1227. DLbls: f.drawChartDLbls(formatSet),
  1228. AxID: []*attrValInt{
  1229. {Val: 754001152},
  1230. {Val: 753999904},
  1231. },
  1232. },
  1233. CatAx: f.drawPlotAreaCatAx(formatSet),
  1234. ValAx: f.drawPlotAreaValAx(formatSet),
  1235. }
  1236. }
  1237. // drawScatterChart provides a function to draw the c:plotArea element for
  1238. // scatter chart by given format sets.
  1239. func (f *File) drawScatterChart(formatSet *formatChart) *cPlotArea {
  1240. return &cPlotArea{
  1241. ScatterChart: &cCharts{
  1242. ScatterStyle: &attrValString{
  1243. Val: "smoothMarker", // line,lineMarker,marker,none,smooth,smoothMarker
  1244. },
  1245. VaryColors: &attrValBool{
  1246. Val: false,
  1247. },
  1248. Ser: f.drawChartSeries(formatSet),
  1249. DLbls: f.drawChartDLbls(formatSet),
  1250. AxID: []*attrValInt{
  1251. {Val: 754001152},
  1252. {Val: 753999904},
  1253. },
  1254. },
  1255. CatAx: f.drawPlotAreaCatAx(formatSet),
  1256. ValAx: f.drawPlotAreaValAx(formatSet),
  1257. }
  1258. }
  1259. // drawSurface3DChart provides a function to draw the c:surface3DChart element by
  1260. // given format sets.
  1261. func (f *File) drawSurface3DChart(formatSet *formatChart) *cPlotArea {
  1262. plotArea := &cPlotArea{
  1263. Surface3DChart: &cCharts{
  1264. Ser: f.drawChartSeries(formatSet),
  1265. AxID: []*attrValInt{
  1266. {Val: 754001152},
  1267. {Val: 753999904},
  1268. {Val: 832256642},
  1269. },
  1270. },
  1271. CatAx: f.drawPlotAreaCatAx(formatSet),
  1272. ValAx: f.drawPlotAreaValAx(formatSet),
  1273. SerAx: f.drawPlotAreaSerAx(formatSet),
  1274. }
  1275. if formatSet.Type == WireframeSurface3D {
  1276. plotArea.Surface3DChart.Wireframe = &attrValBool{Val: true}
  1277. }
  1278. return plotArea
  1279. }
  1280. // drawSurfaceChart provides a function to draw the c:surfaceChart element by
  1281. // given format sets.
  1282. func (f *File) drawSurfaceChart(formatSet *formatChart) *cPlotArea {
  1283. plotArea := &cPlotArea{
  1284. SurfaceChart: &cCharts{
  1285. Ser: f.drawChartSeries(formatSet),
  1286. AxID: []*attrValInt{
  1287. {Val: 754001152},
  1288. {Val: 753999904},
  1289. {Val: 832256642},
  1290. },
  1291. },
  1292. CatAx: f.drawPlotAreaCatAx(formatSet),
  1293. ValAx: f.drawPlotAreaValAx(formatSet),
  1294. SerAx: f.drawPlotAreaSerAx(formatSet),
  1295. }
  1296. if formatSet.Type == WireframeContour {
  1297. plotArea.SurfaceChart.Wireframe = &attrValBool{Val: true}
  1298. }
  1299. return plotArea
  1300. }
  1301. // drawChartShape provides a function to draw the c:shape element by given
  1302. // format sets.
  1303. func (f *File) drawChartShape(formatSet *formatChart) *attrValString {
  1304. shapes := map[string]string{
  1305. Bar3DConeClustered: "cone",
  1306. Bar3DConeStacked: "cone",
  1307. Bar3DConePercentStacked: "cone",
  1308. Bar3DPyramidClustered: "pyramid",
  1309. Bar3DPyramidStacked: "pyramid",
  1310. Bar3DPyramidPercentStacked: "pyramid",
  1311. Bar3DCylinderClustered: "cylinder",
  1312. Bar3DCylinderStacked: "cylinder",
  1313. Bar3DCylinderPercentStacked: "cylinder",
  1314. Col3DCone: "cone",
  1315. Col3DConeClustered: "cone",
  1316. Col3DConeStacked: "cone",
  1317. Col3DConePercentStacked: "cone",
  1318. Col3DPyramid: "pyramid",
  1319. Col3DPyramidClustered: "pyramid",
  1320. Col3DPyramidStacked: "pyramid",
  1321. Col3DPyramidPercentStacked: "pyramid",
  1322. Col3DCylinder: "cylinder",
  1323. Col3DCylinderClustered: "cylinder",
  1324. Col3DCylinderStacked: "cylinder",
  1325. Col3DCylinderPercentStacked: "cylinder",
  1326. }
  1327. if shape, ok := shapes[formatSet.Type]; ok {
  1328. return &attrValString{Val: shape}
  1329. }
  1330. return nil
  1331. }
  1332. // drawChartSeries provides a function to draw the c:ser element by given
  1333. // format sets.
  1334. func (f *File) drawChartSeries(formatSet *formatChart) *[]cSer {
  1335. ser := []cSer{}
  1336. for k := range formatSet.Series {
  1337. ser = append(ser, cSer{
  1338. IDx: &attrValInt{Val: k},
  1339. Order: &attrValInt{Val: k},
  1340. Tx: &cTx{
  1341. StrRef: &cStrRef{
  1342. F: formatSet.Series[k].Name,
  1343. },
  1344. },
  1345. SpPr: f.drawChartSeriesSpPr(k, formatSet),
  1346. Marker: f.drawChartSeriesMarker(k, formatSet),
  1347. DPt: f.drawChartSeriesDPt(k, formatSet),
  1348. DLbls: f.drawChartSeriesDLbls(formatSet),
  1349. Cat: f.drawChartSeriesCat(formatSet.Series[k], formatSet),
  1350. Val: f.drawChartSeriesVal(formatSet.Series[k], formatSet),
  1351. XVal: f.drawChartSeriesXVal(formatSet.Series[k], formatSet),
  1352. YVal: f.drawChartSeriesYVal(formatSet.Series[k], formatSet),
  1353. BubbleSize: f.drawCharSeriesBubbleSize(formatSet.Series[k], formatSet),
  1354. Bubble3D: f.drawCharSeriesBubble3D(formatSet),
  1355. })
  1356. }
  1357. return &ser
  1358. }
  1359. // drawChartSeriesSpPr provides a function to draw the c:spPr element by given
  1360. // format sets.
  1361. func (f *File) drawChartSeriesSpPr(i int, formatSet *formatChart) *cSpPr {
  1362. spPrScatter := &cSpPr{
  1363. Ln: &aLn{
  1364. W: 25400,
  1365. NoFill: " ",
  1366. },
  1367. }
  1368. spPrLine := &cSpPr{
  1369. Ln: &aLn{
  1370. W: f.ptToEMUs(formatSet.Series[i].Line.Width),
  1371. Cap: "rnd", // rnd, sq, flat
  1372. },
  1373. }
  1374. if i < 6 {
  1375. spPrLine.Ln.SolidFill = &aSolidFill{
  1376. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
  1377. }
  1378. }
  1379. chartSeriesSpPr := map[string]*cSpPr{Line: spPrLine, Scatter: spPrScatter}
  1380. return chartSeriesSpPr[formatSet.Type]
  1381. }
  1382. // drawChartSeriesDPt provides a function to draw the c:dPt element by given
  1383. // data index and format sets.
  1384. func (f *File) drawChartSeriesDPt(i int, formatSet *formatChart) []*cDPt {
  1385. dpt := []*cDPt{{
  1386. IDx: &attrValInt{Val: i},
  1387. Bubble3D: &attrValBool{Val: false},
  1388. SpPr: &cSpPr{
  1389. SolidFill: &aSolidFill{
  1390. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
  1391. },
  1392. Ln: &aLn{
  1393. W: 25400,
  1394. Cap: "rnd",
  1395. SolidFill: &aSolidFill{
  1396. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  1397. },
  1398. },
  1399. Sp3D: &aSp3D{
  1400. ContourW: 25400,
  1401. ContourClr: &aContourClr{
  1402. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  1403. },
  1404. },
  1405. },
  1406. }}
  1407. chartSeriesDPt := map[string][]*cDPt{Pie: dpt, Pie3D: dpt}
  1408. return chartSeriesDPt[formatSet.Type]
  1409. }
  1410. // drawChartSeriesCat provides a function to draw the c:cat element by given
  1411. // chart series and format sets.
  1412. func (f *File) drawChartSeriesCat(v formatChartSeries, formatSet *formatChart) *cCat {
  1413. cat := &cCat{
  1414. StrRef: &cStrRef{
  1415. F: v.Categories,
  1416. },
  1417. }
  1418. chartSeriesCat := map[string]*cCat{Scatter: nil, Bubble: nil, Bubble3D: nil}
  1419. if _, ok := chartSeriesCat[formatSet.Type]; ok || v.Categories == "" {
  1420. return nil
  1421. }
  1422. return cat
  1423. }
  1424. // drawChartSeriesVal provides a function to draw the c:val element by given
  1425. // chart series and format sets.
  1426. func (f *File) drawChartSeriesVal(v formatChartSeries, formatSet *formatChart) *cVal {
  1427. val := &cVal{
  1428. NumRef: &cNumRef{
  1429. F: v.Values,
  1430. },
  1431. }
  1432. chartSeriesVal := map[string]*cVal{Scatter: nil, Bubble: nil, Bubble3D: nil}
  1433. if _, ok := chartSeriesVal[formatSet.Type]; ok {
  1434. return nil
  1435. }
  1436. return val
  1437. }
  1438. // drawChartSeriesMarker provides a function to draw the c:marker element by
  1439. // given data index and format sets.
  1440. func (f *File) drawChartSeriesMarker(i int, formatSet *formatChart) *cMarker {
  1441. marker := &cMarker{
  1442. Symbol: &attrValString{Val: "circle"},
  1443. Size: &attrValInt{Val: 5},
  1444. }
  1445. if i < 6 {
  1446. marker.SpPr = &cSpPr{
  1447. SolidFill: &aSolidFill{
  1448. SchemeClr: &aSchemeClr{
  1449. Val: "accent" + strconv.Itoa(i+1),
  1450. },
  1451. },
  1452. Ln: &aLn{
  1453. W: 9252,
  1454. SolidFill: &aSolidFill{
  1455. SchemeClr: &aSchemeClr{
  1456. Val: "accent" + strconv.Itoa(i+1),
  1457. },
  1458. },
  1459. },
  1460. }
  1461. }
  1462. chartSeriesMarker := map[string]*cMarker{Scatter: marker}
  1463. return chartSeriesMarker[formatSet.Type]
  1464. }
  1465. // drawChartSeriesXVal provides a function to draw the c:xVal element by given
  1466. // chart series and format sets.
  1467. func (f *File) drawChartSeriesXVal(v formatChartSeries, formatSet *formatChart) *cCat {
  1468. cat := &cCat{
  1469. StrRef: &cStrRef{
  1470. F: v.Categories,
  1471. },
  1472. }
  1473. chartSeriesXVal := map[string]*cCat{Scatter: cat}
  1474. return chartSeriesXVal[formatSet.Type]
  1475. }
  1476. // drawChartSeriesYVal provides a function to draw the c:yVal element by given
  1477. // chart series and format sets.
  1478. func (f *File) drawChartSeriesYVal(v formatChartSeries, formatSet *formatChart) *cVal {
  1479. val := &cVal{
  1480. NumRef: &cNumRef{
  1481. F: v.Values,
  1482. },
  1483. }
  1484. chartSeriesYVal := map[string]*cVal{Scatter: val, Bubble: val, Bubble3D: val}
  1485. return chartSeriesYVal[formatSet.Type]
  1486. }
  1487. // drawCharSeriesBubbleSize provides a function to draw the c:bubbleSize
  1488. // element by given chart series and format sets.
  1489. func (f *File) drawCharSeriesBubbleSize(v formatChartSeries, formatSet *formatChart) *cVal {
  1490. if _, ok := map[string]bool{Bubble: true, Bubble3D: true}[formatSet.Type]; !ok {
  1491. return nil
  1492. }
  1493. return &cVal{
  1494. NumRef: &cNumRef{
  1495. F: v.Values,
  1496. },
  1497. }
  1498. }
  1499. // drawCharSeriesBubble3D provides a function to draw the c:bubble3D element
  1500. // by given format sets.
  1501. func (f *File) drawCharSeriesBubble3D(formatSet *formatChart) *attrValBool {
  1502. if _, ok := map[string]bool{Bubble3D: true}[formatSet.Type]; !ok {
  1503. return nil
  1504. }
  1505. return &attrValBool{Val: true}
  1506. }
  1507. // drawChartDLbls provides a function to draw the c:dLbls element by given
  1508. // format sets.
  1509. func (f *File) drawChartDLbls(formatSet *formatChart) *cDLbls {
  1510. return &cDLbls{
  1511. ShowLegendKey: &attrValBool{Val: formatSet.Legend.ShowLegendKey},
  1512. ShowVal: &attrValBool{Val: formatSet.Plotarea.ShowVal},
  1513. ShowCatName: &attrValBool{Val: formatSet.Plotarea.ShowCatName},
  1514. ShowSerName: &attrValBool{Val: formatSet.Plotarea.ShowSerName},
  1515. ShowBubbleSize: &attrValBool{Val: formatSet.Plotarea.ShowBubbleSize},
  1516. ShowPercent: &attrValBool{Val: formatSet.Plotarea.ShowPercent},
  1517. ShowLeaderLines: &attrValBool{Val: formatSet.Plotarea.ShowLeaderLines},
  1518. }
  1519. }
  1520. // drawChartSeriesDLbls provides a function to draw the c:dLbls element by
  1521. // given format sets.
  1522. func (f *File) drawChartSeriesDLbls(formatSet *formatChart) *cDLbls {
  1523. dLbls := f.drawChartDLbls(formatSet)
  1524. chartSeriesDLbls := map[string]*cDLbls{Scatter: nil, Surface3D: nil, WireframeSurface3D: nil, Contour: nil, WireframeContour: nil, Bubble: nil, Bubble3D: nil}
  1525. if _, ok := chartSeriesDLbls[formatSet.Type]; ok {
  1526. return nil
  1527. }
  1528. return dLbls
  1529. }
  1530. // drawPlotAreaCatAx provides a function to draw the c:catAx element.
  1531. func (f *File) drawPlotAreaCatAx(formatSet *formatChart) []*cAxs {
  1532. min := &attrValFloat{Val: formatSet.XAxis.Minimum}
  1533. max := &attrValFloat{Val: formatSet.XAxis.Maximum}
  1534. if formatSet.XAxis.Minimum == 0 {
  1535. min = nil
  1536. }
  1537. if formatSet.XAxis.Maximum == 0 {
  1538. max = nil
  1539. }
  1540. axs := []*cAxs{
  1541. {
  1542. AxID: &attrValInt{Val: 754001152},
  1543. Scaling: &cScaling{
  1544. Orientation: &attrValString{Val: orientation[formatSet.XAxis.ReverseOrder]},
  1545. Max: max,
  1546. Min: min,
  1547. },
  1548. Delete: &attrValBool{Val: false},
  1549. AxPos: &attrValString{Val: catAxPos[formatSet.XAxis.ReverseOrder]},
  1550. NumFmt: &cNumFmt{
  1551. FormatCode: "General",
  1552. SourceLinked: true,
  1553. },
  1554. MajorTickMark: &attrValString{Val: "none"},
  1555. MinorTickMark: &attrValString{Val: "none"},
  1556. TickLblPos: &attrValString{Val: "nextTo"},
  1557. SpPr: f.drawPlotAreaSpPr(),
  1558. TxPr: f.drawPlotAreaTxPr(),
  1559. CrossAx: &attrValInt{Val: 753999904},
  1560. Crosses: &attrValString{Val: "autoZero"},
  1561. Auto: &attrValBool{Val: true},
  1562. LblAlgn: &attrValString{Val: "ctr"},
  1563. LblOffset: &attrValInt{Val: 100},
  1564. NoMultiLvlLbl: &attrValBool{Val: false},
  1565. },
  1566. }
  1567. if formatSet.XAxis.MajorGridlines {
  1568. axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  1569. }
  1570. return axs
  1571. }
  1572. // drawPlotAreaValAx provides a function to draw the c:valAx element.
  1573. func (f *File) drawPlotAreaValAx(formatSet *formatChart) []*cAxs {
  1574. min := &attrValFloat{Val: formatSet.YAxis.Minimum}
  1575. max := &attrValFloat{Val: formatSet.YAxis.Maximum}
  1576. if formatSet.YAxis.Minimum == 0 {
  1577. min = nil
  1578. }
  1579. if formatSet.YAxis.Maximum == 0 {
  1580. max = nil
  1581. }
  1582. axs := []*cAxs{
  1583. {
  1584. AxID: &attrValInt{Val: 753999904},
  1585. Scaling: &cScaling{
  1586. Orientation: &attrValString{Val: orientation[formatSet.YAxis.ReverseOrder]},
  1587. Max: max,
  1588. Min: min,
  1589. },
  1590. Delete: &attrValBool{Val: false},
  1591. AxPos: &attrValString{Val: valAxPos[formatSet.YAxis.ReverseOrder]},
  1592. NumFmt: &cNumFmt{
  1593. FormatCode: chartValAxNumFmtFormatCode[formatSet.Type],
  1594. SourceLinked: true,
  1595. },
  1596. MajorTickMark: &attrValString{Val: "none"},
  1597. MinorTickMark: &attrValString{Val: "none"},
  1598. TickLblPos: &attrValString{Val: "nextTo"},
  1599. SpPr: f.drawPlotAreaSpPr(),
  1600. TxPr: f.drawPlotAreaTxPr(),
  1601. CrossAx: &attrValInt{Val: 754001152},
  1602. Crosses: &attrValString{Val: "autoZero"},
  1603. CrossBetween: &attrValString{Val: chartValAxCrossBetween[formatSet.Type]},
  1604. },
  1605. }
  1606. if formatSet.YAxis.MajorGridlines {
  1607. axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  1608. }
  1609. if pos, ok := valTickLblPos[formatSet.Type]; ok {
  1610. axs[0].TickLblPos.Val = pos
  1611. }
  1612. return axs
  1613. }
  1614. // drawPlotAreaSerAx provides a function to draw the c:serAx element.
  1615. func (f *File) drawPlotAreaSerAx(formatSet *formatChart) []*cAxs {
  1616. min := &attrValFloat{Val: formatSet.YAxis.Minimum}
  1617. max := &attrValFloat{Val: formatSet.YAxis.Maximum}
  1618. if formatSet.YAxis.Minimum == 0 {
  1619. min = nil
  1620. }
  1621. if formatSet.YAxis.Maximum == 0 {
  1622. max = nil
  1623. }
  1624. return []*cAxs{
  1625. {
  1626. AxID: &attrValInt{Val: 832256642},
  1627. Scaling: &cScaling{
  1628. Orientation: &attrValString{Val: orientation[formatSet.YAxis.ReverseOrder]},
  1629. Max: max,
  1630. Min: min,
  1631. },
  1632. Delete: &attrValBool{Val: false},
  1633. AxPos: &attrValString{Val: catAxPos[formatSet.XAxis.ReverseOrder]},
  1634. TickLblPos: &attrValString{Val: "nextTo"},
  1635. SpPr: f.drawPlotAreaSpPr(),
  1636. TxPr: f.drawPlotAreaTxPr(),
  1637. CrossAx: &attrValInt{Val: 753999904},
  1638. },
  1639. }
  1640. }
  1641. // drawPlotAreaSpPr provides a function to draw the c:spPr element.
  1642. func (f *File) drawPlotAreaSpPr() *cSpPr {
  1643. return &cSpPr{
  1644. Ln: &aLn{
  1645. W: 9525,
  1646. Cap: "flat",
  1647. Cmpd: "sng",
  1648. Algn: "ctr",
  1649. SolidFill: &aSolidFill{
  1650. SchemeClr: &aSchemeClr{
  1651. Val: "tx1",
  1652. LumMod: &attrValInt{Val: 15000},
  1653. LumOff: &attrValInt{Val: 85000},
  1654. },
  1655. },
  1656. },
  1657. }
  1658. }
  1659. // drawPlotAreaTxPr provides a function to draw the c:txPr element.
  1660. func (f *File) drawPlotAreaTxPr() *cTxPr {
  1661. return &cTxPr{
  1662. BodyPr: aBodyPr{
  1663. Rot: -60000000,
  1664. SpcFirstLastPara: true,
  1665. VertOverflow: "ellipsis",
  1666. Vert: "horz",
  1667. Wrap: "square",
  1668. Anchor: "ctr",
  1669. AnchorCtr: true,
  1670. },
  1671. P: aP{
  1672. PPr: &aPPr{
  1673. DefRPr: aRPr{
  1674. Sz: 900,
  1675. B: false,
  1676. I: false,
  1677. U: "none",
  1678. Strike: "noStrike",
  1679. Kern: 1200,
  1680. Baseline: 0,
  1681. SolidFill: &aSolidFill{
  1682. SchemeClr: &aSchemeClr{
  1683. Val: "tx1",
  1684. LumMod: &attrValInt{Val: 15000},
  1685. LumOff: &attrValInt{Val: 85000},
  1686. },
  1687. },
  1688. Latin: &aLatin{Typeface: "+mn-lt"},
  1689. Ea: &aEa{Typeface: "+mn-ea"},
  1690. Cs: &aCs{Typeface: "+mn-cs"},
  1691. },
  1692. },
  1693. EndParaRPr: &aEndParaRPr{Lang: "en-US"},
  1694. },
  1695. }
  1696. }
  1697. // drawingParser provides a function to parse drawingXML. In order to solve
  1698. // the problem that the label structure is changed after serialization and
  1699. // deserialization, two different structures: decodeWsDr and encodeWsDr are
  1700. // defined.
  1701. func (f *File) drawingParser(path string) (*xlsxWsDr, int) {
  1702. if f.Drawings[path] == nil {
  1703. content := xlsxWsDr{}
  1704. content.A = NameSpaceDrawingML
  1705. content.Xdr = NameSpaceDrawingMLSpreadSheet
  1706. _, ok := f.XLSX[path]
  1707. if ok { // Append Model
  1708. decodeWsDr := decodeWsDr{}
  1709. _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(path)), &decodeWsDr)
  1710. content.R = decodeWsDr.R
  1711. for _, v := range decodeWsDr.OneCellAnchor {
  1712. content.OneCellAnchor = append(content.OneCellAnchor, &xdrCellAnchor{
  1713. EditAs: v.EditAs,
  1714. GraphicFrame: v.Content,
  1715. })
  1716. }
  1717. for _, v := range decodeWsDr.TwoCellAnchor {
  1718. content.TwoCellAnchor = append(content.TwoCellAnchor, &xdrCellAnchor{
  1719. EditAs: v.EditAs,
  1720. GraphicFrame: v.Content,
  1721. })
  1722. }
  1723. }
  1724. f.Drawings[path] = &content
  1725. }
  1726. wsDr := f.Drawings[path]
  1727. return wsDr, len(wsDr.OneCellAnchor) + len(wsDr.TwoCellAnchor) + 2
  1728. }
  1729. // addDrawingChart provides a function to add chart graphic frame by given
  1730. // sheet, drawingXML, cell, width, height, relationship index and format sets.
  1731. func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rID int, formatSet *formatPicture) error {
  1732. col, row, err := CellNameToCoordinates(cell)
  1733. if err != nil {
  1734. return err
  1735. }
  1736. colIdx := col - 1
  1737. rowIdx := row - 1
  1738. width = int(float64(width) * formatSet.XScale)
  1739. height = int(float64(height) * formatSet.YScale)
  1740. colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
  1741. f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.OffsetX, formatSet.OffsetY, width, height)
  1742. content, cNvPrID := f.drawingParser(drawingXML)
  1743. twoCellAnchor := xdrCellAnchor{}
  1744. twoCellAnchor.EditAs = formatSet.Positioning
  1745. from := xlsxFrom{}
  1746. from.Col = colStart
  1747. from.ColOff = formatSet.OffsetX * EMU
  1748. from.Row = rowStart
  1749. from.RowOff = formatSet.OffsetY * EMU
  1750. to := xlsxTo{}
  1751. to.Col = colEnd
  1752. to.ColOff = x2 * EMU
  1753. to.Row = rowEnd
  1754. to.RowOff = y2 * EMU
  1755. twoCellAnchor.From = &from
  1756. twoCellAnchor.To = &to
  1757. graphicFrame := xlsxGraphicFrame{
  1758. NvGraphicFramePr: xlsxNvGraphicFramePr{
  1759. CNvPr: &xlsxCNvPr{
  1760. ID: cNvPrID,
  1761. Name: "Chart " + strconv.Itoa(cNvPrID),
  1762. },
  1763. },
  1764. Graphic: &xlsxGraphic{
  1765. GraphicData: &xlsxGraphicData{
  1766. URI: NameSpaceDrawingMLChart,
  1767. Chart: &xlsxChart{
  1768. C: NameSpaceDrawingMLChart,
  1769. R: SourceRelationship,
  1770. RID: "rId" + strconv.Itoa(rID),
  1771. },
  1772. },
  1773. },
  1774. }
  1775. graphic, _ := xml.Marshal(graphicFrame)
  1776. twoCellAnchor.GraphicFrame = string(graphic)
  1777. twoCellAnchor.ClientData = &xdrClientData{
  1778. FLocksWithSheet: formatSet.FLocksWithSheet,
  1779. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  1780. }
  1781. content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
  1782. f.Drawings[drawingXML] = content
  1783. return err
  1784. }
  1785. // ptToEMUs provides a function to convert pt to EMUs, 1 pt = 12700 EMUs. The
  1786. // range of pt is 0.25pt - 999pt. If the value of pt is outside the range, the
  1787. // default EMUs will be returned.
  1788. func (f *File) ptToEMUs(pt float64) int {
  1789. if 0.25 > pt || pt > 999 {
  1790. return 25400
  1791. }
  1792. return int(12700 * pt)
  1793. }