chart.go 61 KB

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