chart.go 59 KB

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