chart.go 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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 / XLSM / XLTM files. Supports reading and writing
  7. // spreadsheet documents generated by Microsoft Exce™ 2007 and later. Supports
  8. // complex components by high compatibility, and provided streaming API for
  9. // generating or reading data from a worksheet with huge amounts of data. This
  10. // library needs Go version 1.10 or later.
  11. package excelize
  12. import (
  13. "encoding/json"
  14. "encoding/xml"
  15. "errors"
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. )
  20. // This section defines the currently supported chart types.
  21. const (
  22. Area = "area"
  23. AreaStacked = "areaStacked"
  24. AreaPercentStacked = "areaPercentStacked"
  25. Area3D = "area3D"
  26. Area3DStacked = "area3DStacked"
  27. Area3DPercentStacked = "area3DPercentStacked"
  28. Bar = "bar"
  29. BarStacked = "barStacked"
  30. BarPercentStacked = "barPercentStacked"
  31. Bar3DClustered = "bar3DClustered"
  32. Bar3DStacked = "bar3DStacked"
  33. Bar3DPercentStacked = "bar3DPercentStacked"
  34. Bar3DConeClustered = "bar3DConeClustered"
  35. Bar3DConeStacked = "bar3DConeStacked"
  36. Bar3DConePercentStacked = "bar3DConePercentStacked"
  37. Bar3DPyramidClustered = "bar3DPyramidClustered"
  38. Bar3DPyramidStacked = "bar3DPyramidStacked"
  39. Bar3DPyramidPercentStacked = "bar3DPyramidPercentStacked"
  40. Bar3DCylinderClustered = "bar3DCylinderClustered"
  41. Bar3DCylinderStacked = "bar3DCylinderStacked"
  42. Bar3DCylinderPercentStacked = "bar3DCylinderPercentStacked"
  43. Col = "col"
  44. ColStacked = "colStacked"
  45. ColPercentStacked = "colPercentStacked"
  46. Col3D = "col3D"
  47. Col3DClustered = "col3DClustered"
  48. Col3DStacked = "col3DStacked"
  49. Col3DPercentStacked = "col3DPercentStacked"
  50. Col3DCone = "col3DCone"
  51. Col3DConeClustered = "col3DConeClustered"
  52. Col3DConeStacked = "col3DConeStacked"
  53. Col3DConePercentStacked = "col3DConePercentStacked"
  54. Col3DPyramid = "col3DPyramid"
  55. Col3DPyramidClustered = "col3DPyramidClustered"
  56. Col3DPyramidStacked = "col3DPyramidStacked"
  57. Col3DPyramidPercentStacked = "col3DPyramidPercentStacked"
  58. Col3DCylinder = "col3DCylinder"
  59. Col3DCylinderClustered = "col3DCylinderClustered"
  60. Col3DCylinderStacked = "col3DCylinderStacked"
  61. Col3DCylinderPercentStacked = "col3DCylinderPercentStacked"
  62. Doughnut = "doughnut"
  63. Line = "line"
  64. Pie = "pie"
  65. Pie3D = "pie3D"
  66. PieOfPieChart = "pieOfPie"
  67. BarOfPieChart = "barOfPie"
  68. Radar = "radar"
  69. Scatter = "scatter"
  70. Surface3D = "surface3D"
  71. WireframeSurface3D = "wireframeSurface3D"
  72. Contour = "contour"
  73. WireframeContour = "wireframeContour"
  74. Bubble = "bubble"
  75. Bubble3D = "bubble3D"
  76. )
  77. // This section defines the default value of chart properties.
  78. var (
  79. chartView3DRotX = map[string]int{
  80. Area: 0,
  81. AreaStacked: 0,
  82. AreaPercentStacked: 0,
  83. Area3D: 15,
  84. Area3DStacked: 15,
  85. Area3DPercentStacked: 15,
  86. Bar: 0,
  87. BarStacked: 0,
  88. BarPercentStacked: 0,
  89. Bar3DClustered: 15,
  90. Bar3DStacked: 15,
  91. Bar3DPercentStacked: 15,
  92. Bar3DConeClustered: 15,
  93. Bar3DConeStacked: 15,
  94. Bar3DConePercentStacked: 15,
  95. Bar3DPyramidClustered: 15,
  96. Bar3DPyramidStacked: 15,
  97. Bar3DPyramidPercentStacked: 15,
  98. Bar3DCylinderClustered: 15,
  99. Bar3DCylinderStacked: 15,
  100. Bar3DCylinderPercentStacked: 15,
  101. Col: 0,
  102. ColStacked: 0,
  103. ColPercentStacked: 0,
  104. Col3D: 15,
  105. Col3DClustered: 15,
  106. Col3DStacked: 15,
  107. Col3DPercentStacked: 15,
  108. Col3DCone: 15,
  109. Col3DConeClustered: 15,
  110. Col3DConeStacked: 15,
  111. Col3DConePercentStacked: 15,
  112. Col3DPyramid: 15,
  113. Col3DPyramidClustered: 15,
  114. Col3DPyramidStacked: 15,
  115. Col3DPyramidPercentStacked: 15,
  116. Col3DCylinder: 15,
  117. Col3DCylinderClustered: 15,
  118. Col3DCylinderStacked: 15,
  119. Col3DCylinderPercentStacked: 15,
  120. Doughnut: 0,
  121. Line: 0,
  122. Pie: 0,
  123. Pie3D: 30,
  124. PieOfPieChart: 0,
  125. BarOfPieChart: 0,
  126. Radar: 0,
  127. Scatter: 0,
  128. Surface3D: 15,
  129. WireframeSurface3D: 15,
  130. Contour: 90,
  131. WireframeContour: 90,
  132. }
  133. chartView3DRotY = map[string]int{
  134. Area: 0,
  135. AreaStacked: 0,
  136. AreaPercentStacked: 0,
  137. Area3D: 20,
  138. Area3DStacked: 20,
  139. Area3DPercentStacked: 20,
  140. Bar: 0,
  141. BarStacked: 0,
  142. BarPercentStacked: 0,
  143. Bar3DClustered: 20,
  144. Bar3DStacked: 20,
  145. Bar3DPercentStacked: 20,
  146. Bar3DConeClustered: 20,
  147. Bar3DConeStacked: 20,
  148. Bar3DConePercentStacked: 20,
  149. Bar3DPyramidClustered: 20,
  150. Bar3DPyramidStacked: 20,
  151. Bar3DPyramidPercentStacked: 20,
  152. Bar3DCylinderClustered: 20,
  153. Bar3DCylinderStacked: 20,
  154. Bar3DCylinderPercentStacked: 20,
  155. Col: 0,
  156. ColStacked: 0,
  157. ColPercentStacked: 0,
  158. Col3D: 20,
  159. Col3DClustered: 20,
  160. Col3DStacked: 20,
  161. Col3DPercentStacked: 20,
  162. Col3DCone: 20,
  163. Col3DConeClustered: 20,
  164. Col3DConeStacked: 20,
  165. Col3DConePercentStacked: 20,
  166. Col3DPyramid: 20,
  167. Col3DPyramidClustered: 20,
  168. Col3DPyramidStacked: 20,
  169. Col3DPyramidPercentStacked: 20,
  170. Col3DCylinder: 20,
  171. Col3DCylinderClustered: 20,
  172. Col3DCylinderStacked: 20,
  173. Col3DCylinderPercentStacked: 20,
  174. Doughnut: 0,
  175. Line: 0,
  176. Pie: 0,
  177. Pie3D: 0,
  178. PieOfPieChart: 0,
  179. BarOfPieChart: 0,
  180. Radar: 0,
  181. Scatter: 0,
  182. Surface3D: 20,
  183. WireframeSurface3D: 20,
  184. Contour: 0,
  185. WireframeContour: 0,
  186. }
  187. plotAreaChartOverlap = map[string]int{
  188. BarStacked: 100,
  189. BarPercentStacked: 100,
  190. ColStacked: 100,
  191. ColPercentStacked: 100,
  192. }
  193. chartView3DPerspective = map[string]int{
  194. Contour: 0,
  195. WireframeContour: 0,
  196. }
  197. chartView3DRAngAx = map[string]int{
  198. Area: 0,
  199. AreaStacked: 0,
  200. AreaPercentStacked: 0,
  201. Area3D: 1,
  202. Area3DStacked: 1,
  203. Area3DPercentStacked: 1,
  204. Bar: 0,
  205. BarStacked: 0,
  206. BarPercentStacked: 0,
  207. Bar3DClustered: 1,
  208. Bar3DStacked: 1,
  209. Bar3DPercentStacked: 1,
  210. Bar3DConeClustered: 1,
  211. Bar3DConeStacked: 1,
  212. Bar3DConePercentStacked: 1,
  213. Bar3DPyramidClustered: 1,
  214. Bar3DPyramidStacked: 1,
  215. Bar3DPyramidPercentStacked: 1,
  216. Bar3DCylinderClustered: 1,
  217. Bar3DCylinderStacked: 1,
  218. Bar3DCylinderPercentStacked: 1,
  219. Col: 0,
  220. ColStacked: 0,
  221. ColPercentStacked: 0,
  222. Col3D: 1,
  223. Col3DClustered: 1,
  224. Col3DStacked: 1,
  225. Col3DPercentStacked: 1,
  226. Col3DCone: 1,
  227. Col3DConeClustered: 1,
  228. Col3DConeStacked: 1,
  229. Col3DConePercentStacked: 1,
  230. Col3DPyramid: 1,
  231. Col3DPyramidClustered: 1,
  232. Col3DPyramidStacked: 1,
  233. Col3DPyramidPercentStacked: 1,
  234. Col3DCylinder: 1,
  235. Col3DCylinderClustered: 1,
  236. Col3DCylinderStacked: 1,
  237. Col3DCylinderPercentStacked: 1,
  238. Doughnut: 0,
  239. Line: 0,
  240. Pie: 0,
  241. Pie3D: 0,
  242. PieOfPieChart: 0,
  243. BarOfPieChart: 0,
  244. Radar: 0,
  245. Scatter: 0,
  246. Surface3D: 0,
  247. WireframeSurface3D: 0,
  248. Contour: 0,
  249. Bubble: 0,
  250. Bubble3D: 0,
  251. }
  252. chartLegendPosition = map[string]string{
  253. "bottom": "b",
  254. "left": "l",
  255. "right": "r",
  256. "top": "t",
  257. "top_right": "tr",
  258. }
  259. chartValAxNumFmtFormatCode = map[string]string{
  260. Area: "General",
  261. AreaStacked: "General",
  262. AreaPercentStacked: "0%",
  263. Area3D: "General",
  264. Area3DStacked: "General",
  265. Area3DPercentStacked: "0%",
  266. Bar: "General",
  267. BarStacked: "General",
  268. BarPercentStacked: "0%",
  269. Bar3DClustered: "General",
  270. Bar3DStacked: "General",
  271. Bar3DPercentStacked: "0%",
  272. Bar3DConeClustered: "General",
  273. Bar3DConeStacked: "General",
  274. Bar3DConePercentStacked: "0%",
  275. Bar3DPyramidClustered: "General",
  276. Bar3DPyramidStacked: "General",
  277. Bar3DPyramidPercentStacked: "0%",
  278. Bar3DCylinderClustered: "General",
  279. Bar3DCylinderStacked: "General",
  280. Bar3DCylinderPercentStacked: "0%",
  281. Col: "General",
  282. ColStacked: "General",
  283. ColPercentStacked: "0%",
  284. Col3D: "General",
  285. Col3DClustered: "General",
  286. Col3DStacked: "General",
  287. Col3DPercentStacked: "0%",
  288. Col3DCone: "General",
  289. Col3DConeClustered: "General",
  290. Col3DConeStacked: "General",
  291. Col3DConePercentStacked: "0%",
  292. Col3DPyramid: "General",
  293. Col3DPyramidClustered: "General",
  294. Col3DPyramidStacked: "General",
  295. Col3DPyramidPercentStacked: "0%",
  296. Col3DCylinder: "General",
  297. Col3DCylinderClustered: "General",
  298. Col3DCylinderStacked: "General",
  299. Col3DCylinderPercentStacked: "0%",
  300. Doughnut: "General",
  301. Line: "General",
  302. Pie: "General",
  303. Pie3D: "General",
  304. PieOfPieChart: "General",
  305. BarOfPieChart: "General",
  306. Radar: "General",
  307. Scatter: "General",
  308. Surface3D: "General",
  309. WireframeSurface3D: "General",
  310. Contour: "General",
  311. WireframeContour: "General",
  312. Bubble: "General",
  313. Bubble3D: "General",
  314. }
  315. chartValAxCrossBetween = map[string]string{
  316. Area: "midCat",
  317. AreaStacked: "midCat",
  318. AreaPercentStacked: "midCat",
  319. Area3D: "midCat",
  320. Area3DStacked: "midCat",
  321. Area3DPercentStacked: "midCat",
  322. Bar: "between",
  323. BarStacked: "between",
  324. BarPercentStacked: "between",
  325. Bar3DClustered: "between",
  326. Bar3DStacked: "between",
  327. Bar3DPercentStacked: "between",
  328. Bar3DConeClustered: "between",
  329. Bar3DConeStacked: "between",
  330. Bar3DConePercentStacked: "between",
  331. Bar3DPyramidClustered: "between",
  332. Bar3DPyramidStacked: "between",
  333. Bar3DPyramidPercentStacked: "between",
  334. Bar3DCylinderClustered: "between",
  335. Bar3DCylinderStacked: "between",
  336. Bar3DCylinderPercentStacked: "between",
  337. Col: "between",
  338. ColStacked: "between",
  339. ColPercentStacked: "between",
  340. Col3D: "between",
  341. Col3DClustered: "between",
  342. Col3DStacked: "between",
  343. Col3DPercentStacked: "between",
  344. Col3DCone: "between",
  345. Col3DConeClustered: "between",
  346. Col3DConeStacked: "between",
  347. Col3DConePercentStacked: "between",
  348. Col3DPyramid: "between",
  349. Col3DPyramidClustered: "between",
  350. Col3DPyramidStacked: "between",
  351. Col3DPyramidPercentStacked: "between",
  352. Col3DCylinder: "between",
  353. Col3DCylinderClustered: "between",
  354. Col3DCylinderStacked: "between",
  355. Col3DCylinderPercentStacked: "between",
  356. Doughnut: "between",
  357. Line: "between",
  358. Pie: "between",
  359. Pie3D: "between",
  360. PieOfPieChart: "between",
  361. BarOfPieChart: "between",
  362. Radar: "between",
  363. Scatter: "between",
  364. Surface3D: "midCat",
  365. WireframeSurface3D: "midCat",
  366. Contour: "midCat",
  367. WireframeContour: "midCat",
  368. Bubble: "midCat",
  369. Bubble3D: "midCat",
  370. }
  371. plotAreaChartGrouping = map[string]string{
  372. Area: "standard",
  373. AreaStacked: "stacked",
  374. AreaPercentStacked: "percentStacked",
  375. Area3D: "standard",
  376. Area3DStacked: "stacked",
  377. Area3DPercentStacked: "percentStacked",
  378. Bar: "clustered",
  379. BarStacked: "stacked",
  380. BarPercentStacked: "percentStacked",
  381. Bar3DClustered: "clustered",
  382. Bar3DStacked: "stacked",
  383. Bar3DPercentStacked: "percentStacked",
  384. Bar3DConeClustered: "clustered",
  385. Bar3DConeStacked: "stacked",
  386. Bar3DConePercentStacked: "percentStacked",
  387. Bar3DPyramidClustered: "clustered",
  388. Bar3DPyramidStacked: "stacked",
  389. Bar3DPyramidPercentStacked: "percentStacked",
  390. Bar3DCylinderClustered: "clustered",
  391. Bar3DCylinderStacked: "stacked",
  392. Bar3DCylinderPercentStacked: "percentStacked",
  393. Col: "clustered",
  394. ColStacked: "stacked",
  395. ColPercentStacked: "percentStacked",
  396. Col3D: "standard",
  397. Col3DClustered: "clustered",
  398. Col3DStacked: "stacked",
  399. Col3DPercentStacked: "percentStacked",
  400. Col3DCone: "standard",
  401. Col3DConeClustered: "clustered",
  402. Col3DConeStacked: "stacked",
  403. Col3DConePercentStacked: "percentStacked",
  404. Col3DPyramid: "standard",
  405. Col3DPyramidClustered: "clustered",
  406. Col3DPyramidStacked: "stacked",
  407. Col3DPyramidPercentStacked: "percentStacked",
  408. Col3DCylinder: "standard",
  409. Col3DCylinderClustered: "clustered",
  410. Col3DCylinderStacked: "stacked",
  411. Col3DCylinderPercentStacked: "percentStacked",
  412. Line: "standard",
  413. }
  414. plotAreaChartBarDir = map[string]string{
  415. Bar: "bar",
  416. BarStacked: "bar",
  417. BarPercentStacked: "bar",
  418. Bar3DClustered: "bar",
  419. Bar3DStacked: "bar",
  420. Bar3DPercentStacked: "bar",
  421. Bar3DConeClustered: "bar",
  422. Bar3DConeStacked: "bar",
  423. Bar3DConePercentStacked: "bar",
  424. Bar3DPyramidClustered: "bar",
  425. Bar3DPyramidStacked: "bar",
  426. Bar3DPyramidPercentStacked: "bar",
  427. Bar3DCylinderClustered: "bar",
  428. Bar3DCylinderStacked: "bar",
  429. Bar3DCylinderPercentStacked: "bar",
  430. Col: "col",
  431. ColStacked: "col",
  432. ColPercentStacked: "col",
  433. Col3D: "col",
  434. Col3DClustered: "col",
  435. Col3DStacked: "col",
  436. Col3DPercentStacked: "col",
  437. Col3DCone: "col",
  438. Col3DConeStacked: "col",
  439. Col3DConeClustered: "col",
  440. Col3DConePercentStacked: "col",
  441. Col3DPyramid: "col",
  442. Col3DPyramidClustered: "col",
  443. Col3DPyramidStacked: "col",
  444. Col3DPyramidPercentStacked: "col",
  445. Col3DCylinder: "col",
  446. Col3DCylinderClustered: "col",
  447. Col3DCylinderStacked: "col",
  448. Col3DCylinderPercentStacked: "col",
  449. Line: "standard",
  450. }
  451. orientation = map[bool]string{
  452. true: "maxMin",
  453. false: "minMax",
  454. }
  455. catAxPos = map[bool]string{
  456. true: "t",
  457. false: "b",
  458. }
  459. valAxPos = map[bool]string{
  460. true: "r",
  461. false: "l",
  462. }
  463. valTickLblPos = map[string]string{
  464. Contour: "none",
  465. WireframeContour: "none",
  466. }
  467. )
  468. // parseFormatChartSet provides a function to parse the format settings of the
  469. // chart with default value.
  470. func parseFormatChartSet(formatSet string) (*formatChart, error) {
  471. format := formatChart{
  472. Dimension: formatChartDimension{
  473. Width: 480,
  474. Height: 290,
  475. },
  476. Format: formatPicture{
  477. FPrintsWithSheet: true,
  478. FLocksWithSheet: false,
  479. NoChangeAspect: false,
  480. OffsetX: 0,
  481. OffsetY: 0,
  482. XScale: 1.0,
  483. YScale: 1.0,
  484. },
  485. Legend: formatChartLegend{
  486. Position: "bottom",
  487. ShowLegendKey: false,
  488. },
  489. Title: formatChartTitle{
  490. Name: " ",
  491. },
  492. ShowBlanksAs: "gap",
  493. }
  494. err := json.Unmarshal([]byte(formatSet), &format)
  495. return &format, err
  496. }
  497. // AddChart provides the method to add chart in a sheet by given chart format
  498. // set (such as offset, scale, aspect ratio setting and print settings) and
  499. // properties set. For example, create 3D clustered column chart with data
  500. // Sheet1!$E$1:$L$15:
  501. //
  502. // package main
  503. //
  504. // import (
  505. // "fmt"
  506. //
  507. // "github.com/360EntSecGroup-Skylar/excelize/v2"
  508. // )
  509. //
  510. // func main() {
  511. // categories := map[string]string{
  512. // "A2": "Small", "A3": "Normal", "A4": "Large",
  513. // "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  514. // values := map[string]int{
  515. // "B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  516. // f := excelize.NewFile()
  517. // for k, v := range categories {
  518. // f.SetCellValue("Sheet1", k, v)
  519. // }
  520. // for k, v := range values {
  521. // f.SetCellValue("Sheet1", k, v)
  522. // }
  523. // if err := f.AddChart("Sheet1", "E1", `{
  524. // "type": "col3DClustered",
  525. // "series": [
  526. // {
  527. // "name": "Sheet1!$A$2",
  528. // "categories": "Sheet1!$B$1:$D$1",
  529. // "values": "Sheet1!$B$2:$D$2"
  530. // },
  531. // {
  532. // "name": "Sheet1!$A$3",
  533. // "categories": "Sheet1!$B$1:$D$1",
  534. // "values": "Sheet1!$B$3:$D$3"
  535. // },
  536. // {
  537. // "name": "Sheet1!$A$4",
  538. // "categories": "Sheet1!$B$1:$D$1",
  539. // "values": "Sheet1!$B$4:$D$4"
  540. // }],
  541. // "title":
  542. // {
  543. // "name": "Fruit 3D Clustered Column Chart"
  544. // },
  545. // "legend":
  546. // {
  547. // "none": false,
  548. // "position": "bottom",
  549. // "show_legend_key": false
  550. // },
  551. // "plotarea":
  552. // {
  553. // "show_bubble_size": true,
  554. // "show_cat_name": false,
  555. // "show_leader_lines": false,
  556. // "show_percent": true,
  557. // "show_series_name": true,
  558. // "show_val": true
  559. // },
  560. // "show_blanks_as": "zero",
  561. // "x_axis":
  562. // {
  563. // "reverse_order": true
  564. // },
  565. // "y_axis":
  566. // {
  567. // "maximum": 7.5,
  568. // "minimum": 0.5
  569. // }
  570. // }`); err != nil {
  571. // fmt.Println(err)
  572. // return
  573. // }
  574. // // Save spreadsheet by the given path.
  575. // if err := f.SaveAs("Book1.xlsx"); err != nil {
  576. // fmt.Println(err)
  577. // }
  578. // }
  579. //
  580. // The following shows the type of chart supported by excelize:
  581. //
  582. // Type | Chart
  583. // -----------------------------+------------------------------
  584. // area | 2D area chart
  585. // areaStacked | 2D stacked area chart
  586. // areaPercentStacked | 2D 100% stacked area chart
  587. // area3D | 3D area chart
  588. // area3DStacked | 3D stacked area chart
  589. // area3DPercentStacked | 3D 100% stacked area chart
  590. // bar | 2D clustered bar chart
  591. // barStacked | 2D stacked bar chart
  592. // barPercentStacked | 2D 100% stacked bar chart
  593. // bar3DClustered | 3D clustered bar chart
  594. // bar3DStacked | 3D stacked bar chart
  595. // bar3DPercentStacked | 3D 100% stacked bar chart
  596. // bar3DConeClustered | 3D cone clustered bar chart
  597. // bar3DConeStacked | 3D cone stacked bar chart
  598. // bar3DConePercentStacked | 3D cone percent bar chart
  599. // bar3DPyramidClustered | 3D pyramid clustered bar chart
  600. // bar3DPyramidStacked | 3D pyramid stacked bar chart
  601. // bar3DPyramidPercentStacked | 3D pyramid percent stacked bar chart
  602. // bar3DCylinderClustered | 3D cylinder clustered bar chart
  603. // bar3DCylinderStacked | 3D cylinder stacked bar chart
  604. // bar3DCylinderPercentStacked | 3D cylinder percent stacked bar chart
  605. // col | 2D clustered column chart
  606. // colStacked | 2D stacked column chart
  607. // colPercentStacked | 2D 100% stacked column chart
  608. // col3DClustered | 3D clustered column chart
  609. // col3D | 3D column chart
  610. // col3DStacked | 3D stacked column chart
  611. // col3DPercentStacked | 3D 100% stacked column chart
  612. // col3DCone | 3D cone column chart
  613. // col3DConeClustered | 3D cone clustered column chart
  614. // col3DConeStacked | 3D cone stacked column chart
  615. // col3DConePercentStacked | 3D cone percent stacked column chart
  616. // col3DPyramid | 3D pyramid column chart
  617. // col3DPyramidClustered | 3D pyramid clustered column chart
  618. // col3DPyramidStacked | 3D pyramid stacked column chart
  619. // col3DPyramidPercentStacked | 3D pyramid percent stacked column chart
  620. // col3DCylinder | 3D cylinder column chart
  621. // col3DCylinderClustered | 3D cylinder clustered column chart
  622. // col3DCylinderStacked | 3D cylinder stacked column chart
  623. // col3DCylinderPercentStacked | 3D cylinder percent stacked column chart
  624. // doughnut | doughnut chart
  625. // line | line chart
  626. // pie | pie chart
  627. // pie3D | 3D pie chart
  628. // pieOfPie | pie of pie chart
  629. // barOfPie | bar of pie chart
  630. // radar | radar chart
  631. // scatter | scatter chart
  632. // surface3D | 3D surface chart
  633. // wireframeSurface3D | 3D wireframe surface chart
  634. // contour | contour chart
  635. // wireframeContour | wireframe contour chart
  636. // bubble | bubble chart
  637. // bubble3D | 3D bubble chart
  638. //
  639. // In Excel a chart series is a collection of information that defines which data is plotted such as values, axis labels and formatting.
  640. //
  641. // The series options that can be set are:
  642. //
  643. // name
  644. // categories
  645. // values
  646. // line
  647. // marker
  648. //
  649. // 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
  650. //
  651. // 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.
  652. //
  653. // 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.
  654. //
  655. // 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.
  656. //
  657. // marker: This sets the marker of the line chart and scatter chart. The range of optional field 'size' is 2-72 (default value is 5). The enumeration value of optional field 'symbol' are (default value is 'auto'):
  658. //
  659. // circle
  660. // dash
  661. // diamond
  662. // dot
  663. // none
  664. // picture
  665. // plus
  666. // square
  667. // star
  668. // triangle
  669. // x
  670. // auto
  671. //
  672. // Set properties of the chart legend. The options that can be set are:
  673. //
  674. // none
  675. // position
  676. // show_legend_key
  677. //
  678. // none: Specified if show the legend without overlapping the chart. The default value is 'false'.
  679. //
  680. // position: Set the position of the chart legend. The default legend position is right. This parameter only takes effect when 'none' is false. The available positions are:
  681. //
  682. // top
  683. // bottom
  684. // left
  685. // right
  686. // top_right
  687. //
  688. // show_legend_key: Set the legend keys shall be shown in data labels. The default value is false.
  689. //
  690. // Set properties of the chart title. The properties that can be set are:
  691. //
  692. // title
  693. //
  694. // 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.
  695. //
  696. // 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:
  697. //
  698. // gap
  699. // span
  700. // zero
  701. //
  702. // gap: Specifies that blank values shall be left as a gap.
  703. //
  704. // span: Specifies that blank values shall be spanned with a line.
  705. //
  706. // zero: Specifies that blank values shall be treated as zero.
  707. //
  708. // Set chart offset, scale, aspect ratio setting and print settings by format, same as function AddPicture.
  709. //
  710. // Set the position of the chart plot area by plotarea. The properties that can be set are:
  711. //
  712. // show_bubble_size
  713. // show_cat_name
  714. // show_leader_lines
  715. // show_percent
  716. // show_series_name
  717. // show_val
  718. //
  719. // 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.
  720. //
  721. // 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.
  722. //
  723. // show_leader_lines: Specifies leader lines shall be shown for data labels. The show_leader_lines property is optional. The default value is false.
  724. //
  725. // show_percent: Specifies that the percentage shall be shown in a data label. The show_percent property is optional. The default value is false.
  726. //
  727. // 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.
  728. //
  729. // show_val: Specifies that the value shall be shown in a data label. The show_val property is optional. The default value is false.
  730. //
  731. // Set the primary horizontal and vertical axis options by x_axis and y_axis. The properties of x_axis that can be set are:
  732. //
  733. // major_grid_lines
  734. // minor_grid_lines
  735. // tick_label_skip
  736. // reverse_order
  737. // maximum
  738. // minimum
  739. //
  740. // The properties of y_axis that can be set are:
  741. //
  742. // major_grid_lines
  743. // minor_grid_lines
  744. // major_unit
  745. // reverse_order
  746. // maximum
  747. // minimum
  748. //
  749. // major_grid_lines: Specifies major gridlines.
  750. //
  751. // minor_grid_lines: Specifies minor gridlines.
  752. //
  753. // 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.
  754. //
  755. // 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.
  756. //
  757. // 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.
  758. //
  759. // maximum: Specifies that the fixed maximum, 0 is auto. The maximum property is optional. The default value is auto.
  760. //
  761. // minimum: Specifies that the fixed minimum, 0 is auto. The minimum property is optional. The default value is auto.
  762. //
  763. // Set chart size by dimension property. The dimension property is optional. The default width is 480, and height is 290.
  764. //
  765. // combo: Specifies the create a chart that combines two or more chart types
  766. // in a single chart. For example, create a clustered column - line chart with
  767. // data Sheet1!$E$1:$L$15:
  768. //
  769. // package main
  770. //
  771. // import (
  772. // "fmt"
  773. //
  774. // "github.com/360EntSecGroup-Skylar/excelize/v2"
  775. // )
  776. //
  777. // func main() {
  778. // categories := map[string]string{
  779. // "A2": "Small", "A3": "Normal", "A4": "Large",
  780. // "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  781. // values := map[string]int{
  782. // "B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  783. // f := excelize.NewFile()
  784. // for k, v := range categories {
  785. // f.SetCellValue("Sheet1", k, v)
  786. // }
  787. // for k, v := range values {
  788. // f.SetCellValue("Sheet1", k, v)
  789. // }
  790. // if err := f.AddChart("Sheet1", "E1", `{
  791. // "type": "col",
  792. // "series": [
  793. // {
  794. // "name": "Sheet1!$A$2",
  795. // "categories": "",
  796. // "values": "Sheet1!$B$2:$D$2"
  797. // },
  798. // {
  799. // "name": "Sheet1!$A$3",
  800. // "categories": "Sheet1!$B$1:$D$1",
  801. // "values": "Sheet1!$B$3:$D$3"
  802. // }],
  803. // "format":
  804. // {
  805. // "x_scale": 1.0,
  806. // "y_scale": 1.0,
  807. // "x_offset": 15,
  808. // "y_offset": 10,
  809. // "print_obj": true,
  810. // "lock_aspect_ratio": false,
  811. // "locked": false
  812. // },
  813. // "title":
  814. // {
  815. // "name": "Clustered Column - Line Chart"
  816. // },
  817. // "legend":
  818. // {
  819. // "position": "left",
  820. // "show_legend_key": false
  821. // },
  822. // "plotarea":
  823. // {
  824. // "show_bubble_size": true,
  825. // "show_cat_name": false,
  826. // "show_leader_lines": false,
  827. // "show_percent": true,
  828. // "show_series_name": true,
  829. // "show_val": true
  830. // }
  831. // }`, `{
  832. // "type": "line",
  833. // "series": [
  834. // {
  835. // "name": "Sheet1!$A$4",
  836. // "categories": "Sheet1!$B$1:$D$1",
  837. // "values": "Sheet1!$B$4:$D$4",
  838. // "marker":
  839. // {
  840. // "symbol": "none",
  841. // "size": 10
  842. // }
  843. // }],
  844. // "format":
  845. // {
  846. // "x_scale": 1,
  847. // "y_scale": 1,
  848. // "x_offset": 15,
  849. // "y_offset": 10,
  850. // "print_obj": true,
  851. // "lock_aspect_ratio": false,
  852. // "locked": false
  853. // },
  854. // "legend":
  855. // {
  856. // "position": "right",
  857. // "show_legend_key": false
  858. // },
  859. // "plotarea":
  860. // {
  861. // "show_bubble_size": true,
  862. // "show_cat_name": false,
  863. // "show_leader_lines": false,
  864. // "show_percent": true,
  865. // "show_series_name": true,
  866. // "show_val": true
  867. // }
  868. // }`); err != nil {
  869. // fmt.Println(err)
  870. // return
  871. // }
  872. // // Save spreadsheet file by the given path.
  873. // if err := f.SaveAs("Book1.xlsx"); err != nil {
  874. // fmt.Println(err)
  875. // }
  876. // }
  877. //
  878. func (f *File) AddChart(sheet, cell, format string, combo ...string) error {
  879. // Read sheet data.
  880. ws, err := f.workSheetReader(sheet)
  881. if err != nil {
  882. return err
  883. }
  884. formatSet, comboCharts, err := f.getFormatChart(format, combo)
  885. if err != nil {
  886. return err
  887. }
  888. // Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
  889. drawingID := f.countDrawings() + 1
  890. chartID := f.countCharts() + 1
  891. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  892. drawingID, drawingXML = f.prepareDrawing(ws, drawingID, sheet, drawingXML)
  893. drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
  894. drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
  895. err = f.addDrawingChart(sheet, drawingXML, cell, formatSet.Dimension.Width, formatSet.Dimension.Height, drawingRID, &formatSet.Format)
  896. if err != nil {
  897. return err
  898. }
  899. f.addChart(formatSet, comboCharts)
  900. f.addContentTypePart(chartID, "chart")
  901. f.addContentTypePart(drawingID, "drawings")
  902. f.addSheetNameSpace(sheet, SourceRelationship)
  903. return err
  904. }
  905. // AddChartSheet provides the method to create a chartsheet by given chart
  906. // format set (such as offset, scale, aspect ratio setting and print settings)
  907. // and properties set. In Excel a chartsheet is a worksheet that only contains
  908. // a chart.
  909. func (f *File) AddChartSheet(sheet, format string, combo ...string) error {
  910. // Check if the worksheet already exists
  911. if f.GetSheetIndex(sheet) != -1 {
  912. return errors.New("the same name worksheet already exists")
  913. }
  914. formatSet, comboCharts, err := f.getFormatChart(format, combo)
  915. if err != nil {
  916. return err
  917. }
  918. cs := xlsxChartsheet{
  919. SheetViews: []*xlsxChartsheetViews{{
  920. SheetView: []*xlsxChartsheetView{{ZoomScaleAttr: 100, ZoomToFitAttr: true}}},
  921. },
  922. }
  923. f.SheetCount++
  924. wb := f.workbookReader()
  925. sheetID := 0
  926. for _, v := range wb.Sheets.Sheet {
  927. if v.SheetID > sheetID {
  928. sheetID = v.SheetID
  929. }
  930. }
  931. sheetID++
  932. path := "xl/chartsheets/sheet" + strconv.Itoa(sheetID) + ".xml"
  933. f.sheetMap[trimSheetName(sheet)] = path
  934. f.Sheet[path] = nil
  935. drawingID := f.countDrawings() + 1
  936. chartID := f.countCharts() + 1
  937. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  938. f.prepareChartSheetDrawing(&cs, drawingID, sheet)
  939. drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
  940. drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
  941. f.addSheetDrawingChart(drawingXML, drawingRID, &formatSet.Format)
  942. f.addChart(formatSet, comboCharts)
  943. f.addContentTypePart(chartID, "chart")
  944. f.addContentTypePart(sheetID, "chartsheet")
  945. f.addContentTypePart(drawingID, "drawings")
  946. // Update workbook.xml.rels
  947. rID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipChartsheet, fmt.Sprintf("/xl/chartsheets/sheet%d.xml", sheetID), "")
  948. // Update workbook.xml
  949. f.setWorkbook(sheet, sheetID, rID)
  950. chartsheet, _ := xml.Marshal(cs)
  951. f.addSheetNameSpace(sheet, NameSpaceSpreadSheet)
  952. f.saveFileList(path, replaceRelationshipsBytes(f.replaceNameSpaceBytes(path, chartsheet)))
  953. return err
  954. }
  955. // getFormatChart provides a function to check format set of the chart and
  956. // create chart format.
  957. func (f *File) getFormatChart(format string, combo []string) (*formatChart, []*formatChart, error) {
  958. comboCharts := []*formatChart{}
  959. formatSet, err := parseFormatChartSet(format)
  960. if err != nil {
  961. return formatSet, comboCharts, err
  962. }
  963. for _, comboFormat := range combo {
  964. comboChart, err := parseFormatChartSet(comboFormat)
  965. if err != nil {
  966. return formatSet, comboCharts, err
  967. }
  968. if _, ok := chartValAxNumFmtFormatCode[comboChart.Type]; !ok {
  969. return formatSet, comboCharts, errors.New("unsupported chart type " + comboChart.Type)
  970. }
  971. comboCharts = append(comboCharts, comboChart)
  972. }
  973. if _, ok := chartValAxNumFmtFormatCode[formatSet.Type]; !ok {
  974. return formatSet, comboCharts, errors.New("unsupported chart type " + formatSet.Type)
  975. }
  976. return formatSet, comboCharts, err
  977. }
  978. // DeleteChart provides a function to delete chart in XLSX by given worksheet
  979. // and cell name.
  980. func (f *File) DeleteChart(sheet, cell string) (err error) {
  981. col, row, err := CellNameToCoordinates(cell)
  982. if err != nil {
  983. return
  984. }
  985. col--
  986. row--
  987. ws, err := f.workSheetReader(sheet)
  988. if err != nil {
  989. return
  990. }
  991. if ws.Drawing == nil {
  992. return
  993. }
  994. drawingXML := strings.Replace(f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID), "..", "xl", -1)
  995. return f.deleteDrawing(col, row, drawingXML, "Chart")
  996. }
  997. // countCharts provides a function to get chart files count storage in the
  998. // folder xl/charts.
  999. func (f *File) countCharts() int {
  1000. count := 0
  1001. for k := range f.XLSX {
  1002. if strings.Contains(k, "xl/charts/chart") {
  1003. count++
  1004. }
  1005. }
  1006. return count
  1007. }
  1008. // ptToEMUs provides a function to convert pt to EMUs, 1 pt = 12700 EMUs. The
  1009. // range of pt is 0.25pt - 999pt. If the value of pt is outside the range, the
  1010. // default EMUs will be returned.
  1011. func (f *File) ptToEMUs(pt float64) int {
  1012. if 0.25 > pt || pt > 999 {
  1013. return 25400
  1014. }
  1015. return int(12700 * pt)
  1016. }