chart.go 65 KB

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