chart.go 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. // Copyright 2016 - 2020 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 "github.com/360EntSecGroup-Skylar/excelize"
  493. //
  494. // func main() {
  495. // categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  496. // values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  497. // f := excelize.NewFile()
  498. // for k, v := range categories {
  499. // f.SetCellValue("Sheet1", k, v)
  500. // }
  501. // for k, v := range values {
  502. // f.SetCellValue("Sheet1", k, v)
  503. // }
  504. // if 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}}`); err != nil {
  505. // println(err.Error())
  506. // return
  507. // }
  508. // // Save xlsx file by the given path.
  509. // if err := xlsx.SaveAs("Book1.xlsx"); err != nil {
  510. // println(err.Error())
  511. // }
  512. // }
  513. //
  514. // The following shows the type of chart supported by excelize:
  515. //
  516. // Type | Chart
  517. // -----------------------------+------------------------------
  518. // area | 2D area chart
  519. // areaStacked | 2D stacked area chart
  520. // areaPercentStacked | 2D 100% stacked area chart
  521. // area3D | 3D area chart
  522. // area3DStacked | 3D stacked area chart
  523. // area3DPercentStacked | 3D 100% stacked area chart
  524. // bar | 2D clustered bar chart
  525. // barStacked | 2D stacked bar chart
  526. // barPercentStacked | 2D 100% stacked bar chart
  527. // bar3DClustered | 3D clustered bar chart
  528. // bar3DStacked | 3D stacked bar chart
  529. // bar3DPercentStacked | 3D 100% stacked bar chart
  530. // bar3DConeClustered | 3D cone clustered bar chart
  531. // bar3DConeStacked | 3D cone stacked bar chart
  532. // bar3DConePercentStacked | 3D cone percent bar chart
  533. // bar3DPyramidClustered | 3D pyramid clustered bar chart
  534. // bar3DPyramidStacked | 3D pyramid stacked bar chart
  535. // bar3DPyramidPercentStacked | 3D pyramid percent stacked bar chart
  536. // bar3DCylinderClustered | 3D cylinder clustered bar chart
  537. // bar3DCylinderStacked | 3D cylinder stacked bar chart
  538. // bar3DCylinderPercentStacked | 3D cylinder percent stacked bar chart
  539. // col | 2D clustered column chart
  540. // colStacked | 2D stacked column chart
  541. // colPercentStacked | 2D 100% stacked column chart
  542. // col3DClustered | 3D clustered column chart
  543. // col3D | 3D column chart
  544. // col3DStacked | 3D stacked column chart
  545. // col3DPercentStacked | 3D 100% stacked column chart
  546. // col3DCone | 3D cone column chart
  547. // col3DConeClustered | 3D cone clustered column chart
  548. // col3DConeStacked | 3D cone stacked column chart
  549. // col3DConePercentStacked | 3D cone percent stacked column chart
  550. // col3DPyramid | 3D pyramid column chart
  551. // col3DPyramidClustered | 3D pyramid clustered column chart
  552. // col3DPyramidStacked | 3D pyramid stacked column chart
  553. // col3DPyramidPercentStacked | 3D pyramid percent stacked column chart
  554. // col3DCylinder | 3D cylinder column chart
  555. // col3DCylinderClustered | 3D cylinder clustered column chart
  556. // col3DCylinderStacked | 3D cylinder stacked column chart
  557. // col3DCylinderPercentStacked | 3D cylinder percent stacked column chart
  558. // doughnut | doughnut chart
  559. // line | line chart
  560. // pie | pie chart
  561. // pie3D | 3D pie chart
  562. // radar | radar chart
  563. // scatter | scatter chart
  564. // surface3D | 3D surface chart
  565. // wireframeSurface3D | 3D wireframe surface chart
  566. // contour | contour chart
  567. // wireframeContour | wireframe contour chart
  568. // bubble | bubble chart
  569. // bubble3D | 3D bubble chart
  570. //
  571. // In Excel a chart series is a collection of information that defines which data is plotted such as values, axis labels and formatting.
  572. //
  573. // The series options that can be set are:
  574. //
  575. // name
  576. // categories
  577. // values
  578. // line
  579. //
  580. // 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
  581. //
  582. // 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.
  583. //
  584. // 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.
  585. //
  586. // 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.
  587. //
  588. // Set properties of the chart legend. The options that can be set are:
  589. //
  590. // position
  591. // show_legend_key
  592. //
  593. // position: Set the position of the chart legend. The default legend position is right. The available positions are:
  594. //
  595. // top
  596. // bottom
  597. // left
  598. // right
  599. // top_right
  600. //
  601. // show_legend_key: Set the legend keys shall be shown in data labels. The default value is false.
  602. //
  603. // Set properties of the chart title. The properties that can be set are:
  604. //
  605. // title
  606. //
  607. // 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.
  608. //
  609. // 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:
  610. //
  611. // gap
  612. // span
  613. // zero
  614. //
  615. // gap: Specifies that blank values shall be left as a gap.
  616. //
  617. // sapn: Specifies that blank values shall be spanned with a line.
  618. //
  619. // zero: Specifies that blank values shall be treated as zero.
  620. //
  621. // Set chart offset, scale, aspect ratio setting and print settings by format, same as function AddPicture.
  622. //
  623. // Set the position of the chart plot area by plotarea. The properties that can be set are:
  624. //
  625. // show_bubble_size
  626. // show_cat_name
  627. // show_leader_lines
  628. // show_percent
  629. // show_series_name
  630. // show_val
  631. //
  632. // 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.
  633. //
  634. // 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.
  635. //
  636. // show_leader_lines: Specifies leader lines shall be shown for data labels. The show_leader_lines property is optional. The default value is false.
  637. //
  638. // show_percent: Specifies that the percentage shall be shown in a data label. The show_percent property is optional. The default value is false.
  639. //
  640. // 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.
  641. //
  642. // show_val: Specifies that the value shall be shown in a data label. The show_val property is optional. The default value is false.
  643. //
  644. // Set the primary horizontal and vertical axis options by x_axis and y_axis. The properties of x_axis that can be set are:
  645. //
  646. // major_grid_lines
  647. // minor_grid_lines
  648. // tick_label_skip
  649. // reverse_order
  650. // maximum
  651. // minimum
  652. //
  653. // The properties of y_axis that can be set are:
  654. //
  655. // major_grid_lines
  656. // minor_grid_lines
  657. // major_unit
  658. // reverse_order
  659. // maximum
  660. // minimum
  661. //
  662. // major_grid_lines: Specifies major gridlines.
  663. //
  664. // minor_grid_lines: Specifies minor gridlines.
  665. //
  666. // major_unit: Specifies the distance between major ticks. Shall contain a positive floating-point number. The major_unit property is optional. The default value is auto.
  667. //
  668. // tick_label_skip: Specifies how many tick labels to skip between label that is drawn. The tick_label_skip property is optional. The default value is auto.
  669. //
  670. // 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.
  671. //
  672. // maximum: Specifies that the fixed maximum, 0 is auto. The maximum property is optional. The default value is auto.
  673. //
  674. // minimum: Specifies that the fixed minimum, 0 is auto. The minimum property is optional. The default value is auto.
  675. //
  676. // Set chart size by dimension property. The dimension property is optional. The default width is 480, and height is 290.
  677. //
  678. func (f *File) AddChart(sheet, cell, format string) error {
  679. formatSet, err := parseFormatChartSet(format)
  680. if err != nil {
  681. return err
  682. }
  683. // Read sheet data.
  684. xlsx, err := f.workSheetReader(sheet)
  685. if err != nil {
  686. return err
  687. }
  688. if _, ok := chartValAxNumFmtFormatCode[formatSet.Type]; !ok {
  689. return errors.New("unsupported chart type " + formatSet.Type)
  690. }
  691. // Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
  692. drawingID := f.countDrawings() + 1
  693. chartID := f.countCharts() + 1
  694. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  695. drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
  696. drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
  697. drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
  698. err = f.addDrawingChart(sheet, drawingXML, cell, formatSet.Dimension.Width, formatSet.Dimension.Height, drawingRID, &formatSet.Format)
  699. if err != nil {
  700. return err
  701. }
  702. f.addChart(formatSet)
  703. f.addContentTypePart(chartID, "chart")
  704. f.addContentTypePart(drawingID, "drawings")
  705. return err
  706. }
  707. // countCharts provides a function to get chart files count storage in the
  708. // folder xl/charts.
  709. func (f *File) countCharts() int {
  710. count := 0
  711. for k := range f.XLSX {
  712. if strings.Contains(k, "xl/charts/chart") {
  713. count++
  714. }
  715. }
  716. return count
  717. }
  718. // prepareDrawing provides a function to prepare drawing ID and XML by given
  719. // drawingID, worksheet name and default drawingXML.
  720. func (f *File) prepareDrawing(xlsx *xlsxWorksheet, drawingID int, sheet, drawingXML string) (int, string) {
  721. sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  722. if xlsx.Drawing != nil {
  723. // The worksheet already has a picture or chart relationships, use the relationships drawing ../drawings/drawing%d.xml.
  724. sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID)
  725. drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml"))
  726. drawingXML = strings.Replace(sheetRelationshipsDrawingXML, "..", "xl", -1)
  727. } else {
  728. // Add first picture for given sheet.
  729. sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels"
  730. rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
  731. f.addSheetDrawing(sheet, rID)
  732. }
  733. return drawingID, drawingXML
  734. }
  735. // addChart provides a function to create chart as xl/charts/chart%d.xml by
  736. // given format sets.
  737. func (f *File) addChart(formatSet *formatChart) {
  738. count := f.countCharts()
  739. xlsxChartSpace := xlsxChartSpace{
  740. XMLNSc: NameSpaceDrawingMLChart,
  741. XMLNSa: NameSpaceDrawingML,
  742. XMLNSr: SourceRelationship,
  743. XMLNSc16r2: SourceRelationshipChart201506,
  744. Date1904: &attrValBool{Val: boolPtr(false)},
  745. Lang: &attrValString{Val: stringPtr("en-US")},
  746. RoundedCorners: &attrValBool{Val: boolPtr(false)},
  747. Chart: cChart{
  748. Title: &cTitle{
  749. Tx: cTx{
  750. Rich: &cRich{
  751. P: aP{
  752. PPr: &aPPr{
  753. DefRPr: aRPr{
  754. Kern: 1200,
  755. Strike: "noStrike",
  756. U: "none",
  757. Sz: 1400,
  758. SolidFill: &aSolidFill{
  759. SchemeClr: &aSchemeClr{
  760. Val: "tx1",
  761. LumMod: &attrValInt{
  762. Val: intPtr(65000),
  763. },
  764. LumOff: &attrValInt{
  765. Val: intPtr(35000),
  766. },
  767. },
  768. },
  769. Ea: &aEa{
  770. Typeface: "+mn-ea",
  771. },
  772. Cs: &aCs{
  773. Typeface: "+mn-cs",
  774. },
  775. Latin: &aLatin{
  776. Typeface: "+mn-lt",
  777. },
  778. },
  779. },
  780. R: &aR{
  781. RPr: aRPr{
  782. Lang: "en-US",
  783. AltLang: "en-US",
  784. },
  785. T: formatSet.Title.Name,
  786. },
  787. },
  788. },
  789. },
  790. TxPr: cTxPr{
  791. P: aP{
  792. PPr: &aPPr{
  793. DefRPr: aRPr{
  794. Kern: 1200,
  795. U: "none",
  796. Sz: 14000,
  797. Strike: "noStrike",
  798. },
  799. },
  800. EndParaRPr: &aEndParaRPr{
  801. Lang: "en-US",
  802. },
  803. },
  804. },
  805. Overlay: &attrValBool{Val: boolPtr(false)},
  806. },
  807. View3D: &cView3D{
  808. RotX: &attrValInt{Val: intPtr(chartView3DRotX[formatSet.Type])},
  809. RotY: &attrValInt{Val: intPtr(chartView3DRotY[formatSet.Type])},
  810. Perspective: &attrValInt{Val: intPtr(chartView3DPerspective[formatSet.Type])},
  811. RAngAx: &attrValInt{Val: intPtr(chartView3DRAngAx[formatSet.Type])},
  812. },
  813. Floor: &cThicknessSpPr{
  814. Thickness: &attrValInt{Val: intPtr(0)},
  815. },
  816. SideWall: &cThicknessSpPr{
  817. Thickness: &attrValInt{Val: intPtr(0)},
  818. },
  819. BackWall: &cThicknessSpPr{
  820. Thickness: &attrValInt{Val: intPtr(0)},
  821. },
  822. PlotArea: &cPlotArea{},
  823. Legend: &cLegend{
  824. LegendPos: &attrValString{Val: stringPtr(chartLegendPosition[formatSet.Legend.Position])},
  825. Overlay: &attrValBool{Val: boolPtr(false)},
  826. },
  827. PlotVisOnly: &attrValBool{Val: boolPtr(false)},
  828. DispBlanksAs: &attrValString{Val: stringPtr(formatSet.ShowBlanksAs)},
  829. ShowDLblsOverMax: &attrValBool{Val: boolPtr(false)},
  830. },
  831. SpPr: &cSpPr{
  832. SolidFill: &aSolidFill{
  833. SchemeClr: &aSchemeClr{Val: "bg1"},
  834. },
  835. Ln: &aLn{
  836. W: 9525,
  837. Cap: "flat",
  838. Cmpd: "sng",
  839. Algn: "ctr",
  840. SolidFill: &aSolidFill{
  841. SchemeClr: &aSchemeClr{Val: "tx1",
  842. LumMod: &attrValInt{
  843. Val: intPtr(15000),
  844. },
  845. LumOff: &attrValInt{
  846. Val: intPtr(85000),
  847. },
  848. },
  849. },
  850. },
  851. },
  852. PrintSettings: &cPrintSettings{
  853. PageMargins: &cPageMargins{
  854. B: 0.75,
  855. L: 0.7,
  856. R: 0.7,
  857. T: 0.7,
  858. Header: 0.3,
  859. Footer: 0.3,
  860. },
  861. },
  862. }
  863. plotAreaFunc := map[string]func(*formatChart) *cPlotArea{
  864. Area: f.drawBaseChart,
  865. AreaStacked: f.drawBaseChart,
  866. AreaPercentStacked: f.drawBaseChart,
  867. Area3D: f.drawBaseChart,
  868. Area3DStacked: f.drawBaseChart,
  869. Area3DPercentStacked: f.drawBaseChart,
  870. Bar: f.drawBaseChart,
  871. BarStacked: f.drawBaseChart,
  872. BarPercentStacked: f.drawBaseChart,
  873. Bar3DClustered: f.drawBaseChart,
  874. Bar3DStacked: f.drawBaseChart,
  875. Bar3DPercentStacked: f.drawBaseChart,
  876. Bar3DConeClustered: f.drawBaseChart,
  877. Bar3DConeStacked: f.drawBaseChart,
  878. Bar3DConePercentStacked: f.drawBaseChart,
  879. Bar3DPyramidClustered: f.drawBaseChart,
  880. Bar3DPyramidStacked: f.drawBaseChart,
  881. Bar3DPyramidPercentStacked: f.drawBaseChart,
  882. Bar3DCylinderClustered: f.drawBaseChart,
  883. Bar3DCylinderStacked: f.drawBaseChart,
  884. Bar3DCylinderPercentStacked: f.drawBaseChart,
  885. Col: f.drawBaseChart,
  886. ColStacked: f.drawBaseChart,
  887. ColPercentStacked: f.drawBaseChart,
  888. Col3D: f.drawBaseChart,
  889. Col3DClustered: f.drawBaseChart,
  890. Col3DStacked: f.drawBaseChart,
  891. Col3DPercentStacked: f.drawBaseChart,
  892. Col3DCone: f.drawBaseChart,
  893. Col3DConeClustered: f.drawBaseChart,
  894. Col3DConeStacked: f.drawBaseChart,
  895. Col3DConePercentStacked: f.drawBaseChart,
  896. Col3DPyramid: f.drawBaseChart,
  897. Col3DPyramidClustered: f.drawBaseChart,
  898. Col3DPyramidStacked: f.drawBaseChart,
  899. Col3DPyramidPercentStacked: f.drawBaseChart,
  900. Col3DCylinder: f.drawBaseChart,
  901. Col3DCylinderClustered: f.drawBaseChart,
  902. Col3DCylinderStacked: f.drawBaseChart,
  903. Col3DCylinderPercentStacked: f.drawBaseChart,
  904. Doughnut: f.drawDoughnutChart,
  905. Line: f.drawLineChart,
  906. Pie3D: f.drawPie3DChart,
  907. Pie: f.drawPieChart,
  908. Radar: f.drawRadarChart,
  909. Scatter: f.drawScatterChart,
  910. Surface3D: f.drawSurface3DChart,
  911. WireframeSurface3D: f.drawSurface3DChart,
  912. Contour: f.drawSurfaceChart,
  913. WireframeContour: f.drawSurfaceChart,
  914. Bubble: f.drawBaseChart,
  915. Bubble3D: f.drawBaseChart,
  916. }
  917. xlsxChartSpace.Chart.PlotArea = plotAreaFunc[formatSet.Type](formatSet)
  918. chart, _ := xml.Marshal(xlsxChartSpace)
  919. media := "xl/charts/chart" + strconv.Itoa(count+1) + ".xml"
  920. f.saveFileList(media, chart)
  921. }
  922. // drawBaseChart provides a function to draw the c:plotArea element for bar,
  923. // and column series charts by given format sets.
  924. func (f *File) drawBaseChart(formatSet *formatChart) *cPlotArea {
  925. c := cCharts{
  926. BarDir: &attrValString{
  927. Val: stringPtr("col"),
  928. },
  929. Grouping: &attrValString{
  930. Val: stringPtr("clustered"),
  931. },
  932. VaryColors: &attrValBool{
  933. Val: boolPtr(true),
  934. },
  935. Ser: f.drawChartSeries(formatSet),
  936. Shape: f.drawChartShape(formatSet),
  937. DLbls: f.drawChartDLbls(formatSet),
  938. AxID: []*attrValInt{
  939. {Val: intPtr(754001152)},
  940. {Val: intPtr(753999904)},
  941. },
  942. Overlap: &attrValInt{Val: intPtr(100)},
  943. }
  944. var ok bool
  945. if *c.BarDir.Val, ok = plotAreaChartBarDir[formatSet.Type]; !ok {
  946. c.BarDir = nil
  947. }
  948. if *c.Grouping.Val, ok = plotAreaChartGrouping[formatSet.Type]; !ok {
  949. c.Grouping = nil
  950. }
  951. if *c.Overlap.Val, ok = plotAreaChartOverlap[formatSet.Type]; !ok {
  952. c.Overlap = nil
  953. }
  954. catAx := f.drawPlotAreaCatAx(formatSet)
  955. valAx := f.drawPlotAreaValAx(formatSet)
  956. charts := map[string]*cPlotArea{
  957. "area": {
  958. AreaChart: &c,
  959. CatAx: catAx,
  960. ValAx: valAx,
  961. },
  962. "areaStacked": {
  963. AreaChart: &c,
  964. CatAx: catAx,
  965. ValAx: valAx,
  966. },
  967. "areaPercentStacked": {
  968. AreaChart: &c,
  969. CatAx: catAx,
  970. ValAx: valAx,
  971. },
  972. "area3D": {
  973. Area3DChart: &c,
  974. CatAx: catAx,
  975. ValAx: valAx,
  976. },
  977. "area3DStacked": {
  978. Area3DChart: &c,
  979. CatAx: catAx,
  980. ValAx: valAx,
  981. },
  982. "area3DPercentStacked": {
  983. Area3DChart: &c,
  984. CatAx: catAx,
  985. ValAx: valAx,
  986. },
  987. "bar": {
  988. BarChart: &c,
  989. CatAx: catAx,
  990. ValAx: valAx,
  991. },
  992. "barStacked": {
  993. BarChart: &c,
  994. CatAx: catAx,
  995. ValAx: valAx,
  996. },
  997. "barPercentStacked": {
  998. BarChart: &c,
  999. CatAx: catAx,
  1000. ValAx: valAx,
  1001. },
  1002. "bar3DClustered": {
  1003. Bar3DChart: &c,
  1004. CatAx: catAx,
  1005. ValAx: valAx,
  1006. },
  1007. "bar3DStacked": {
  1008. Bar3DChart: &c,
  1009. CatAx: catAx,
  1010. ValAx: valAx,
  1011. },
  1012. "bar3DPercentStacked": {
  1013. Bar3DChart: &c,
  1014. CatAx: catAx,
  1015. ValAx: valAx,
  1016. },
  1017. "bar3DConeClustered": {
  1018. Bar3DChart: &c,
  1019. CatAx: catAx,
  1020. ValAx: valAx,
  1021. },
  1022. "bar3DConeStacked": {
  1023. Bar3DChart: &c,
  1024. CatAx: catAx,
  1025. ValAx: valAx,
  1026. },
  1027. "bar3DConePercentStacked": {
  1028. Bar3DChart: &c,
  1029. CatAx: catAx,
  1030. ValAx: valAx,
  1031. },
  1032. "bar3DPyramidClustered": {
  1033. Bar3DChart: &c,
  1034. CatAx: catAx,
  1035. ValAx: valAx,
  1036. },
  1037. "bar3DPyramidStacked": {
  1038. Bar3DChart: &c,
  1039. CatAx: catAx,
  1040. ValAx: valAx,
  1041. },
  1042. "bar3DPyramidPercentStacked": {
  1043. Bar3DChart: &c,
  1044. CatAx: catAx,
  1045. ValAx: valAx,
  1046. },
  1047. "bar3DCylinderClustered": {
  1048. Bar3DChart: &c,
  1049. CatAx: catAx,
  1050. ValAx: valAx,
  1051. },
  1052. "bar3DCylinderStacked": {
  1053. Bar3DChart: &c,
  1054. CatAx: catAx,
  1055. ValAx: valAx,
  1056. },
  1057. "bar3DCylinderPercentStacked": {
  1058. Bar3DChart: &c,
  1059. CatAx: catAx,
  1060. ValAx: valAx,
  1061. },
  1062. "col": {
  1063. BarChart: &c,
  1064. CatAx: catAx,
  1065. ValAx: valAx,
  1066. },
  1067. "colStacked": {
  1068. BarChart: &c,
  1069. CatAx: catAx,
  1070. ValAx: valAx,
  1071. },
  1072. "colPercentStacked": {
  1073. BarChart: &c,
  1074. CatAx: catAx,
  1075. ValAx: valAx,
  1076. },
  1077. "col3D": {
  1078. Bar3DChart: &c,
  1079. CatAx: catAx,
  1080. ValAx: valAx,
  1081. },
  1082. "col3DClustered": {
  1083. Bar3DChart: &c,
  1084. CatAx: catAx,
  1085. ValAx: valAx,
  1086. },
  1087. "col3DStacked": {
  1088. Bar3DChart: &c,
  1089. CatAx: catAx,
  1090. ValAx: valAx,
  1091. },
  1092. "col3DPercentStacked": {
  1093. Bar3DChart: &c,
  1094. CatAx: catAx,
  1095. ValAx: valAx,
  1096. },
  1097. "col3DCone": {
  1098. Bar3DChart: &c,
  1099. CatAx: catAx,
  1100. ValAx: valAx,
  1101. },
  1102. "col3DConeClustered": {
  1103. Bar3DChart: &c,
  1104. CatAx: catAx,
  1105. ValAx: valAx,
  1106. },
  1107. "col3DConeStacked": {
  1108. Bar3DChart: &c,
  1109. CatAx: catAx,
  1110. ValAx: valAx,
  1111. },
  1112. "col3DConePercentStacked": {
  1113. Bar3DChart: &c,
  1114. CatAx: catAx,
  1115. ValAx: valAx,
  1116. },
  1117. "col3DPyramid": {
  1118. Bar3DChart: &c,
  1119. CatAx: catAx,
  1120. ValAx: valAx,
  1121. },
  1122. "col3DPyramidClustered": {
  1123. Bar3DChart: &c,
  1124. CatAx: catAx,
  1125. ValAx: valAx,
  1126. },
  1127. "col3DPyramidStacked": {
  1128. Bar3DChart: &c,
  1129. CatAx: catAx,
  1130. ValAx: valAx,
  1131. },
  1132. "col3DPyramidPercentStacked": {
  1133. Bar3DChart: &c,
  1134. CatAx: catAx,
  1135. ValAx: valAx,
  1136. },
  1137. "col3DCylinder": {
  1138. Bar3DChart: &c,
  1139. CatAx: catAx,
  1140. ValAx: valAx,
  1141. },
  1142. "col3DCylinderClustered": {
  1143. Bar3DChart: &c,
  1144. CatAx: catAx,
  1145. ValAx: valAx,
  1146. },
  1147. "col3DCylinderStacked": {
  1148. Bar3DChart: &c,
  1149. CatAx: catAx,
  1150. ValAx: valAx,
  1151. },
  1152. "col3DCylinderPercentStacked": {
  1153. Bar3DChart: &c,
  1154. CatAx: catAx,
  1155. ValAx: valAx,
  1156. },
  1157. "bubble": {
  1158. BubbleChart: &c,
  1159. CatAx: catAx,
  1160. ValAx: valAx,
  1161. },
  1162. "bubble3D": {
  1163. BubbleChart: &c,
  1164. CatAx: catAx,
  1165. ValAx: valAx,
  1166. },
  1167. }
  1168. return charts[formatSet.Type]
  1169. }
  1170. // drawDoughnutChart provides a function to draw the c:plotArea element for
  1171. // doughnut chart by given format sets.
  1172. func (f *File) drawDoughnutChart(formatSet *formatChart) *cPlotArea {
  1173. return &cPlotArea{
  1174. DoughnutChart: &cCharts{
  1175. VaryColors: &attrValBool{
  1176. Val: boolPtr(true),
  1177. },
  1178. Ser: f.drawChartSeries(formatSet),
  1179. HoleSize: &attrValInt{Val: intPtr(75)},
  1180. },
  1181. }
  1182. }
  1183. // drawLineChart provides a function to draw the c:plotArea element for line
  1184. // chart by given format sets.
  1185. func (f *File) drawLineChart(formatSet *formatChart) *cPlotArea {
  1186. return &cPlotArea{
  1187. LineChart: &cCharts{
  1188. Grouping: &attrValString{
  1189. Val: stringPtr(plotAreaChartGrouping[formatSet.Type]),
  1190. },
  1191. VaryColors: &attrValBool{
  1192. Val: boolPtr(false),
  1193. },
  1194. Ser: f.drawChartSeries(formatSet),
  1195. DLbls: f.drawChartDLbls(formatSet),
  1196. Smooth: &attrValBool{
  1197. Val: boolPtr(false),
  1198. },
  1199. AxID: []*attrValInt{
  1200. {Val: intPtr(754001152)},
  1201. {Val: intPtr(753999904)},
  1202. },
  1203. },
  1204. CatAx: f.drawPlotAreaCatAx(formatSet),
  1205. ValAx: f.drawPlotAreaValAx(formatSet),
  1206. }
  1207. }
  1208. // drawPieChart provides a function to draw the c:plotArea element for pie
  1209. // chart by given format sets.
  1210. func (f *File) drawPieChart(formatSet *formatChart) *cPlotArea {
  1211. return &cPlotArea{
  1212. PieChart: &cCharts{
  1213. VaryColors: &attrValBool{
  1214. Val: boolPtr(true),
  1215. },
  1216. Ser: f.drawChartSeries(formatSet),
  1217. },
  1218. }
  1219. }
  1220. // drawPie3DChart provides a function to draw the c:plotArea element for 3D
  1221. // pie chart by given format sets.
  1222. func (f *File) drawPie3DChart(formatSet *formatChart) *cPlotArea {
  1223. return &cPlotArea{
  1224. Pie3DChart: &cCharts{
  1225. VaryColors: &attrValBool{
  1226. Val: boolPtr(true),
  1227. },
  1228. Ser: f.drawChartSeries(formatSet),
  1229. },
  1230. }
  1231. }
  1232. // drawRadarChart provides a function to draw the c:plotArea element for radar
  1233. // chart by given format sets.
  1234. func (f *File) drawRadarChart(formatSet *formatChart) *cPlotArea {
  1235. return &cPlotArea{
  1236. RadarChart: &cCharts{
  1237. RadarStyle: &attrValString{
  1238. Val: stringPtr("marker"),
  1239. },
  1240. VaryColors: &attrValBool{
  1241. Val: boolPtr(false),
  1242. },
  1243. Ser: f.drawChartSeries(formatSet),
  1244. DLbls: f.drawChartDLbls(formatSet),
  1245. AxID: []*attrValInt{
  1246. {Val: intPtr(754001152)},
  1247. {Val: intPtr(753999904)},
  1248. },
  1249. },
  1250. CatAx: f.drawPlotAreaCatAx(formatSet),
  1251. ValAx: f.drawPlotAreaValAx(formatSet),
  1252. }
  1253. }
  1254. // drawScatterChart provides a function to draw the c:plotArea element for
  1255. // scatter chart by given format sets.
  1256. func (f *File) drawScatterChart(formatSet *formatChart) *cPlotArea {
  1257. return &cPlotArea{
  1258. ScatterChart: &cCharts{
  1259. ScatterStyle: &attrValString{
  1260. Val: stringPtr("smoothMarker"), // line,lineMarker,marker,none,smooth,smoothMarker
  1261. },
  1262. VaryColors: &attrValBool{
  1263. Val: boolPtr(false),
  1264. },
  1265. Ser: f.drawChartSeries(formatSet),
  1266. DLbls: f.drawChartDLbls(formatSet),
  1267. AxID: []*attrValInt{
  1268. {Val: intPtr(754001152)},
  1269. {Val: intPtr(753999904)},
  1270. },
  1271. },
  1272. CatAx: f.drawPlotAreaCatAx(formatSet),
  1273. ValAx: f.drawPlotAreaValAx(formatSet),
  1274. }
  1275. }
  1276. // drawSurface3DChart provides a function to draw the c:surface3DChart element by
  1277. // given format sets.
  1278. func (f *File) drawSurface3DChart(formatSet *formatChart) *cPlotArea {
  1279. plotArea := &cPlotArea{
  1280. Surface3DChart: &cCharts{
  1281. Ser: f.drawChartSeries(formatSet),
  1282. AxID: []*attrValInt{
  1283. {Val: intPtr(754001152)},
  1284. {Val: intPtr(753999904)},
  1285. {Val: intPtr(832256642)},
  1286. },
  1287. },
  1288. CatAx: f.drawPlotAreaCatAx(formatSet),
  1289. ValAx: f.drawPlotAreaValAx(formatSet),
  1290. SerAx: f.drawPlotAreaSerAx(formatSet),
  1291. }
  1292. if formatSet.Type == WireframeSurface3D {
  1293. plotArea.Surface3DChart.Wireframe = &attrValBool{Val: boolPtr(true)}
  1294. }
  1295. return plotArea
  1296. }
  1297. // drawSurfaceChart provides a function to draw the c:surfaceChart element by
  1298. // given format sets.
  1299. func (f *File) drawSurfaceChart(formatSet *formatChart) *cPlotArea {
  1300. plotArea := &cPlotArea{
  1301. SurfaceChart: &cCharts{
  1302. Ser: f.drawChartSeries(formatSet),
  1303. AxID: []*attrValInt{
  1304. {Val: intPtr(754001152)},
  1305. {Val: intPtr(753999904)},
  1306. {Val: intPtr(832256642)},
  1307. },
  1308. },
  1309. CatAx: f.drawPlotAreaCatAx(formatSet),
  1310. ValAx: f.drawPlotAreaValAx(formatSet),
  1311. SerAx: f.drawPlotAreaSerAx(formatSet),
  1312. }
  1313. if formatSet.Type == WireframeContour {
  1314. plotArea.SurfaceChart.Wireframe = &attrValBool{Val: boolPtr(true)}
  1315. }
  1316. return plotArea
  1317. }
  1318. // drawChartShape provides a function to draw the c:shape element by given
  1319. // format sets.
  1320. func (f *File) drawChartShape(formatSet *formatChart) *attrValString {
  1321. shapes := map[string]string{
  1322. Bar3DConeClustered: "cone",
  1323. Bar3DConeStacked: "cone",
  1324. Bar3DConePercentStacked: "cone",
  1325. Bar3DPyramidClustered: "pyramid",
  1326. Bar3DPyramidStacked: "pyramid",
  1327. Bar3DPyramidPercentStacked: "pyramid",
  1328. Bar3DCylinderClustered: "cylinder",
  1329. Bar3DCylinderStacked: "cylinder",
  1330. Bar3DCylinderPercentStacked: "cylinder",
  1331. Col3DCone: "cone",
  1332. Col3DConeClustered: "cone",
  1333. Col3DConeStacked: "cone",
  1334. Col3DConePercentStacked: "cone",
  1335. Col3DPyramid: "pyramid",
  1336. Col3DPyramidClustered: "pyramid",
  1337. Col3DPyramidStacked: "pyramid",
  1338. Col3DPyramidPercentStacked: "pyramid",
  1339. Col3DCylinder: "cylinder",
  1340. Col3DCylinderClustered: "cylinder",
  1341. Col3DCylinderStacked: "cylinder",
  1342. Col3DCylinderPercentStacked: "cylinder",
  1343. }
  1344. if shape, ok := shapes[formatSet.Type]; ok {
  1345. return &attrValString{Val: stringPtr(shape)}
  1346. }
  1347. return nil
  1348. }
  1349. // drawChartSeries provides a function to draw the c:ser element by given
  1350. // format sets.
  1351. func (f *File) drawChartSeries(formatSet *formatChart) *[]cSer {
  1352. ser := []cSer{}
  1353. for k := range formatSet.Series {
  1354. ser = append(ser, cSer{
  1355. IDx: &attrValInt{Val: intPtr(k)},
  1356. Order: &attrValInt{Val: intPtr(k)},
  1357. Tx: &cTx{
  1358. StrRef: &cStrRef{
  1359. F: formatSet.Series[k].Name,
  1360. },
  1361. },
  1362. SpPr: f.drawChartSeriesSpPr(k, formatSet),
  1363. Marker: f.drawChartSeriesMarker(k, formatSet),
  1364. DPt: f.drawChartSeriesDPt(k, formatSet),
  1365. DLbls: f.drawChartSeriesDLbls(formatSet),
  1366. Cat: f.drawChartSeriesCat(formatSet.Series[k], formatSet),
  1367. Val: f.drawChartSeriesVal(formatSet.Series[k], formatSet),
  1368. XVal: f.drawChartSeriesXVal(formatSet.Series[k], formatSet),
  1369. YVal: f.drawChartSeriesYVal(formatSet.Series[k], formatSet),
  1370. BubbleSize: f.drawCharSeriesBubbleSize(formatSet.Series[k], formatSet),
  1371. Bubble3D: f.drawCharSeriesBubble3D(formatSet),
  1372. })
  1373. }
  1374. return &ser
  1375. }
  1376. // drawChartSeriesSpPr provides a function to draw the c:spPr element by given
  1377. // format sets.
  1378. func (f *File) drawChartSeriesSpPr(i int, formatSet *formatChart) *cSpPr {
  1379. spPrScatter := &cSpPr{
  1380. Ln: &aLn{
  1381. W: 25400,
  1382. NoFill: " ",
  1383. },
  1384. }
  1385. spPrLine := &cSpPr{
  1386. Ln: &aLn{
  1387. W: f.ptToEMUs(formatSet.Series[i].Line.Width),
  1388. Cap: "rnd", // rnd, sq, flat
  1389. },
  1390. }
  1391. if i < 6 {
  1392. spPrLine.Ln.SolidFill = &aSolidFill{
  1393. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
  1394. }
  1395. }
  1396. chartSeriesSpPr := map[string]*cSpPr{Line: spPrLine, Scatter: spPrScatter}
  1397. return chartSeriesSpPr[formatSet.Type]
  1398. }
  1399. // drawChartSeriesDPt provides a function to draw the c:dPt element by given
  1400. // data index and format sets.
  1401. func (f *File) drawChartSeriesDPt(i int, formatSet *formatChart) []*cDPt {
  1402. dpt := []*cDPt{{
  1403. IDx: &attrValInt{Val: intPtr(i)},
  1404. Bubble3D: &attrValBool{Val: boolPtr(false)},
  1405. SpPr: &cSpPr{
  1406. SolidFill: &aSolidFill{
  1407. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
  1408. },
  1409. Ln: &aLn{
  1410. W: 25400,
  1411. Cap: "rnd",
  1412. SolidFill: &aSolidFill{
  1413. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  1414. },
  1415. },
  1416. Sp3D: &aSp3D{
  1417. ContourW: 25400,
  1418. ContourClr: &aContourClr{
  1419. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  1420. },
  1421. },
  1422. },
  1423. }}
  1424. chartSeriesDPt := map[string][]*cDPt{Pie: dpt, Pie3D: dpt}
  1425. return chartSeriesDPt[formatSet.Type]
  1426. }
  1427. // drawChartSeriesCat provides a function to draw the c:cat element by given
  1428. // chart series and format sets.
  1429. func (f *File) drawChartSeriesCat(v formatChartSeries, formatSet *formatChart) *cCat {
  1430. cat := &cCat{
  1431. StrRef: &cStrRef{
  1432. F: v.Categories,
  1433. },
  1434. }
  1435. chartSeriesCat := map[string]*cCat{Scatter: nil, Bubble: nil, Bubble3D: nil}
  1436. if _, ok := chartSeriesCat[formatSet.Type]; ok || v.Categories == "" {
  1437. return nil
  1438. }
  1439. return cat
  1440. }
  1441. // drawChartSeriesVal provides a function to draw the c:val element by given
  1442. // chart series and format sets.
  1443. func (f *File) drawChartSeriesVal(v formatChartSeries, formatSet *formatChart) *cVal {
  1444. val := &cVal{
  1445. NumRef: &cNumRef{
  1446. F: v.Values,
  1447. },
  1448. }
  1449. chartSeriesVal := map[string]*cVal{Scatter: nil, Bubble: nil, Bubble3D: nil}
  1450. if _, ok := chartSeriesVal[formatSet.Type]; ok {
  1451. return nil
  1452. }
  1453. return val
  1454. }
  1455. // drawChartSeriesMarker provides a function to draw the c:marker element by
  1456. // given data index and format sets.
  1457. func (f *File) drawChartSeriesMarker(i int, formatSet *formatChart) *cMarker {
  1458. marker := &cMarker{
  1459. Symbol: &attrValString{Val: stringPtr("circle")},
  1460. Size: &attrValInt{Val: intPtr(5)},
  1461. }
  1462. if i < 6 {
  1463. marker.SpPr = &cSpPr{
  1464. SolidFill: &aSolidFill{
  1465. SchemeClr: &aSchemeClr{
  1466. Val: "accent" + strconv.Itoa(i+1),
  1467. },
  1468. },
  1469. Ln: &aLn{
  1470. W: 9252,
  1471. SolidFill: &aSolidFill{
  1472. SchemeClr: &aSchemeClr{
  1473. Val: "accent" + strconv.Itoa(i+1),
  1474. },
  1475. },
  1476. },
  1477. }
  1478. }
  1479. chartSeriesMarker := map[string]*cMarker{Scatter: marker}
  1480. return chartSeriesMarker[formatSet.Type]
  1481. }
  1482. // drawChartSeriesXVal provides a function to draw the c:xVal element by given
  1483. // chart series and format sets.
  1484. func (f *File) drawChartSeriesXVal(v formatChartSeries, formatSet *formatChart) *cCat {
  1485. cat := &cCat{
  1486. StrRef: &cStrRef{
  1487. F: v.Categories,
  1488. },
  1489. }
  1490. chartSeriesXVal := map[string]*cCat{Scatter: cat}
  1491. return chartSeriesXVal[formatSet.Type]
  1492. }
  1493. // drawChartSeriesYVal provides a function to draw the c:yVal element by given
  1494. // chart series and format sets.
  1495. func (f *File) drawChartSeriesYVal(v formatChartSeries, formatSet *formatChart) *cVal {
  1496. val := &cVal{
  1497. NumRef: &cNumRef{
  1498. F: v.Values,
  1499. },
  1500. }
  1501. chartSeriesYVal := map[string]*cVal{Scatter: val, Bubble: val, Bubble3D: val}
  1502. return chartSeriesYVal[formatSet.Type]
  1503. }
  1504. // drawCharSeriesBubbleSize provides a function to draw the c:bubbleSize
  1505. // element by given chart series and format sets.
  1506. func (f *File) drawCharSeriesBubbleSize(v formatChartSeries, formatSet *formatChart) *cVal {
  1507. if _, ok := map[string]bool{Bubble: true, Bubble3D: true}[formatSet.Type]; !ok {
  1508. return nil
  1509. }
  1510. return &cVal{
  1511. NumRef: &cNumRef{
  1512. F: v.Values,
  1513. },
  1514. }
  1515. }
  1516. // drawCharSeriesBubble3D provides a function to draw the c:bubble3D element
  1517. // by given format sets.
  1518. func (f *File) drawCharSeriesBubble3D(formatSet *formatChart) *attrValBool {
  1519. if _, ok := map[string]bool{Bubble3D: true}[formatSet.Type]; !ok {
  1520. return nil
  1521. }
  1522. return &attrValBool{Val: boolPtr(true)}
  1523. }
  1524. // drawChartDLbls provides a function to draw the c:dLbls element by given
  1525. // format sets.
  1526. func (f *File) drawChartDLbls(formatSet *formatChart) *cDLbls {
  1527. return &cDLbls{
  1528. ShowLegendKey: &attrValBool{Val: boolPtr(formatSet.Legend.ShowLegendKey)},
  1529. ShowVal: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowVal)},
  1530. ShowCatName: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowCatName)},
  1531. ShowSerName: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowSerName)},
  1532. ShowBubbleSize: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowBubbleSize)},
  1533. ShowPercent: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowPercent)},
  1534. ShowLeaderLines: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowLeaderLines)},
  1535. }
  1536. }
  1537. // drawChartSeriesDLbls provides a function to draw the c:dLbls element by
  1538. // given format sets.
  1539. func (f *File) drawChartSeriesDLbls(formatSet *formatChart) *cDLbls {
  1540. dLbls := f.drawChartDLbls(formatSet)
  1541. chartSeriesDLbls := map[string]*cDLbls{Scatter: nil, Surface3D: nil, WireframeSurface3D: nil, Contour: nil, WireframeContour: nil, Bubble: nil, Bubble3D: nil}
  1542. if _, ok := chartSeriesDLbls[formatSet.Type]; ok {
  1543. return nil
  1544. }
  1545. return dLbls
  1546. }
  1547. // drawPlotAreaCatAx provides a function to draw the c:catAx element.
  1548. func (f *File) drawPlotAreaCatAx(formatSet *formatChart) []*cAxs {
  1549. min := &attrValFloat{Val: float64Ptr(formatSet.XAxis.Minimum)}
  1550. max := &attrValFloat{Val: float64Ptr(formatSet.XAxis.Maximum)}
  1551. if formatSet.XAxis.Minimum == 0 {
  1552. min = nil
  1553. }
  1554. if formatSet.XAxis.Maximum == 0 {
  1555. max = nil
  1556. }
  1557. axs := []*cAxs{
  1558. {
  1559. AxID: &attrValInt{Val: intPtr(754001152)},
  1560. Scaling: &cScaling{
  1561. Orientation: &attrValString{Val: stringPtr(orientation[formatSet.XAxis.ReverseOrder])},
  1562. Max: max,
  1563. Min: min,
  1564. },
  1565. Delete: &attrValBool{Val: boolPtr(false)},
  1566. AxPos: &attrValString{Val: stringPtr(catAxPos[formatSet.XAxis.ReverseOrder])},
  1567. NumFmt: &cNumFmt{
  1568. FormatCode: "General",
  1569. SourceLinked: true,
  1570. },
  1571. MajorTickMark: &attrValString{Val: stringPtr("none")},
  1572. MinorTickMark: &attrValString{Val: stringPtr("none")},
  1573. TickLblPos: &attrValString{Val: stringPtr("nextTo")},
  1574. SpPr: f.drawPlotAreaSpPr(),
  1575. TxPr: f.drawPlotAreaTxPr(),
  1576. CrossAx: &attrValInt{Val: intPtr(753999904)},
  1577. Crosses: &attrValString{Val: stringPtr("autoZero")},
  1578. Auto: &attrValBool{Val: boolPtr(true)},
  1579. LblAlgn: &attrValString{Val: stringPtr("ctr")},
  1580. LblOffset: &attrValInt{Val: intPtr(100)},
  1581. NoMultiLvlLbl: &attrValBool{Val: boolPtr(false)},
  1582. },
  1583. }
  1584. if formatSet.XAxis.MajorGridlines {
  1585. axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  1586. }
  1587. if formatSet.XAxis.MinorGridlines {
  1588. axs[0].MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  1589. }
  1590. if formatSet.XAxis.TickLabelSkip != 0 {
  1591. axs[0].TickLblSkip = &attrValInt{Val: intPtr(formatSet.XAxis.TickLabelSkip)}
  1592. }
  1593. return axs
  1594. }
  1595. // drawPlotAreaValAx provides a function to draw the c:valAx element.
  1596. func (f *File) drawPlotAreaValAx(formatSet *formatChart) []*cAxs {
  1597. min := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Minimum)}
  1598. max := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Maximum)}
  1599. if formatSet.YAxis.Minimum == 0 {
  1600. min = nil
  1601. }
  1602. if formatSet.YAxis.Maximum == 0 {
  1603. max = nil
  1604. }
  1605. axs := []*cAxs{
  1606. {
  1607. AxID: &attrValInt{Val: intPtr(753999904)},
  1608. Scaling: &cScaling{
  1609. Orientation: &attrValString{Val: stringPtr(orientation[formatSet.YAxis.ReverseOrder])},
  1610. Max: max,
  1611. Min: min,
  1612. },
  1613. Delete: &attrValBool{Val: boolPtr(false)},
  1614. AxPos: &attrValString{Val: stringPtr(valAxPos[formatSet.YAxis.ReverseOrder])},
  1615. NumFmt: &cNumFmt{
  1616. FormatCode: chartValAxNumFmtFormatCode[formatSet.Type],
  1617. SourceLinked: true,
  1618. },
  1619. MajorTickMark: &attrValString{Val: stringPtr("none")},
  1620. MinorTickMark: &attrValString{Val: stringPtr("none")},
  1621. TickLblPos: &attrValString{Val: stringPtr("nextTo")},
  1622. SpPr: f.drawPlotAreaSpPr(),
  1623. TxPr: f.drawPlotAreaTxPr(),
  1624. CrossAx: &attrValInt{Val: intPtr(754001152)},
  1625. Crosses: &attrValString{Val: stringPtr("autoZero")},
  1626. CrossBetween: &attrValString{Val: stringPtr(chartValAxCrossBetween[formatSet.Type])},
  1627. },
  1628. }
  1629. if formatSet.YAxis.MajorGridlines {
  1630. axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  1631. }
  1632. if formatSet.YAxis.MinorGridlines {
  1633. axs[0].MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  1634. }
  1635. if pos, ok := valTickLblPos[formatSet.Type]; ok {
  1636. axs[0].TickLblPos.Val = stringPtr(pos)
  1637. }
  1638. if formatSet.YAxis.MajorUnit != 0 {
  1639. axs[0].MajorUnit = &attrValFloat{Val: float64Ptr(formatSet.YAxis.MajorUnit)}
  1640. }
  1641. return axs
  1642. }
  1643. // drawPlotAreaSerAx provides a function to draw the c:serAx element.
  1644. func (f *File) drawPlotAreaSerAx(formatSet *formatChart) []*cAxs {
  1645. min := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Minimum)}
  1646. max := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Maximum)}
  1647. if formatSet.YAxis.Minimum == 0 {
  1648. min = nil
  1649. }
  1650. if formatSet.YAxis.Maximum == 0 {
  1651. max = nil
  1652. }
  1653. return []*cAxs{
  1654. {
  1655. AxID: &attrValInt{Val: intPtr(832256642)},
  1656. Scaling: &cScaling{
  1657. Orientation: &attrValString{Val: stringPtr(orientation[formatSet.YAxis.ReverseOrder])},
  1658. Max: max,
  1659. Min: min,
  1660. },
  1661. Delete: &attrValBool{Val: boolPtr(false)},
  1662. AxPos: &attrValString{Val: stringPtr(catAxPos[formatSet.XAxis.ReverseOrder])},
  1663. TickLblPos: &attrValString{Val: stringPtr("nextTo")},
  1664. SpPr: f.drawPlotAreaSpPr(),
  1665. TxPr: f.drawPlotAreaTxPr(),
  1666. CrossAx: &attrValInt{Val: intPtr(753999904)},
  1667. },
  1668. }
  1669. }
  1670. // drawPlotAreaSpPr provides a function to draw the c:spPr element.
  1671. func (f *File) drawPlotAreaSpPr() *cSpPr {
  1672. return &cSpPr{
  1673. Ln: &aLn{
  1674. W: 9525,
  1675. Cap: "flat",
  1676. Cmpd: "sng",
  1677. Algn: "ctr",
  1678. SolidFill: &aSolidFill{
  1679. SchemeClr: &aSchemeClr{
  1680. Val: "tx1",
  1681. LumMod: &attrValInt{Val: intPtr(15000)},
  1682. LumOff: &attrValInt{Val: intPtr(85000)},
  1683. },
  1684. },
  1685. },
  1686. }
  1687. }
  1688. // drawPlotAreaTxPr provides a function to draw the c:txPr element.
  1689. func (f *File) drawPlotAreaTxPr() *cTxPr {
  1690. return &cTxPr{
  1691. BodyPr: aBodyPr{
  1692. Rot: -60000000,
  1693. SpcFirstLastPara: true,
  1694. VertOverflow: "ellipsis",
  1695. Vert: "horz",
  1696. Wrap: "square",
  1697. Anchor: "ctr",
  1698. AnchorCtr: true,
  1699. },
  1700. P: aP{
  1701. PPr: &aPPr{
  1702. DefRPr: aRPr{
  1703. Sz: 900,
  1704. B: false,
  1705. I: false,
  1706. U: "none",
  1707. Strike: "noStrike",
  1708. Kern: 1200,
  1709. Baseline: 0,
  1710. SolidFill: &aSolidFill{
  1711. SchemeClr: &aSchemeClr{
  1712. Val: "tx1",
  1713. LumMod: &attrValInt{Val: intPtr(15000)},
  1714. LumOff: &attrValInt{Val: intPtr(85000)},
  1715. },
  1716. },
  1717. Latin: &aLatin{Typeface: "+mn-lt"},
  1718. Ea: &aEa{Typeface: "+mn-ea"},
  1719. Cs: &aCs{Typeface: "+mn-cs"},
  1720. },
  1721. },
  1722. EndParaRPr: &aEndParaRPr{Lang: "en-US"},
  1723. },
  1724. }
  1725. }
  1726. // drawingParser provides a function to parse drawingXML. In order to solve
  1727. // the problem that the label structure is changed after serialization and
  1728. // deserialization, two different structures: decodeWsDr and encodeWsDr are
  1729. // defined.
  1730. func (f *File) drawingParser(path string) (*xlsxWsDr, int) {
  1731. var (
  1732. err error
  1733. ok bool
  1734. )
  1735. if f.Drawings[path] == nil {
  1736. content := xlsxWsDr{}
  1737. content.A = NameSpaceDrawingML
  1738. content.Xdr = NameSpaceDrawingMLSpreadSheet
  1739. if _, ok = f.XLSX[path]; ok { // Append Model
  1740. decodeWsDr := decodeWsDr{}
  1741. if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(path)))).
  1742. Decode(&decodeWsDr); err != nil && err != io.EOF {
  1743. log.Printf("xml decode error: %s", err)
  1744. }
  1745. content.R = decodeWsDr.R
  1746. for _, v := range decodeWsDr.OneCellAnchor {
  1747. content.OneCellAnchor = append(content.OneCellAnchor, &xdrCellAnchor{
  1748. EditAs: v.EditAs,
  1749. GraphicFrame: v.Content,
  1750. })
  1751. }
  1752. for _, v := range decodeWsDr.TwoCellAnchor {
  1753. content.TwoCellAnchor = append(content.TwoCellAnchor, &xdrCellAnchor{
  1754. EditAs: v.EditAs,
  1755. GraphicFrame: v.Content,
  1756. })
  1757. }
  1758. }
  1759. f.Drawings[path] = &content
  1760. }
  1761. wsDr := f.Drawings[path]
  1762. return wsDr, len(wsDr.OneCellAnchor) + len(wsDr.TwoCellAnchor) + 2
  1763. }
  1764. // addDrawingChart provides a function to add chart graphic frame by given
  1765. // sheet, drawingXML, cell, width, height, relationship index and format sets.
  1766. func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rID int, formatSet *formatPicture) error {
  1767. col, row, err := CellNameToCoordinates(cell)
  1768. if err != nil {
  1769. return err
  1770. }
  1771. colIdx := col - 1
  1772. rowIdx := row - 1
  1773. width = int(float64(width) * formatSet.XScale)
  1774. height = int(float64(height) * formatSet.YScale)
  1775. colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
  1776. f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.OffsetX, formatSet.OffsetY, width, height)
  1777. content, cNvPrID := f.drawingParser(drawingXML)
  1778. twoCellAnchor := xdrCellAnchor{}
  1779. twoCellAnchor.EditAs = formatSet.Positioning
  1780. from := xlsxFrom{}
  1781. from.Col = colStart
  1782. from.ColOff = formatSet.OffsetX * EMU
  1783. from.Row = rowStart
  1784. from.RowOff = formatSet.OffsetY * EMU
  1785. to := xlsxTo{}
  1786. to.Col = colEnd
  1787. to.ColOff = x2 * EMU
  1788. to.Row = rowEnd
  1789. to.RowOff = y2 * EMU
  1790. twoCellAnchor.From = &from
  1791. twoCellAnchor.To = &to
  1792. graphicFrame := xlsxGraphicFrame{
  1793. NvGraphicFramePr: xlsxNvGraphicFramePr{
  1794. CNvPr: &xlsxCNvPr{
  1795. ID: cNvPrID,
  1796. Name: "Chart " + strconv.Itoa(cNvPrID),
  1797. },
  1798. },
  1799. Graphic: &xlsxGraphic{
  1800. GraphicData: &xlsxGraphicData{
  1801. URI: NameSpaceDrawingMLChart,
  1802. Chart: &xlsxChart{
  1803. C: NameSpaceDrawingMLChart,
  1804. R: SourceRelationship,
  1805. RID: "rId" + strconv.Itoa(rID),
  1806. },
  1807. },
  1808. },
  1809. }
  1810. graphic, _ := xml.Marshal(graphicFrame)
  1811. twoCellAnchor.GraphicFrame = string(graphic)
  1812. twoCellAnchor.ClientData = &xdrClientData{
  1813. FLocksWithSheet: formatSet.FLocksWithSheet,
  1814. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  1815. }
  1816. content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
  1817. f.Drawings[drawingXML] = content
  1818. return err
  1819. }
  1820. // ptToEMUs provides a function to convert pt to EMUs, 1 pt = 12700 EMUs. The
  1821. // range of pt is 0.25pt - 999pt. If the value of pt is outside the range, the
  1822. // default EMUs will be returned.
  1823. func (f *File) ptToEMUs(pt float64) int {
  1824. if 0.25 > pt || pt > 999 {
  1825. return 25400
  1826. }
  1827. return int(12700 * pt)
  1828. }