chart.go 59 KB

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