chart.go 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. // Copyright 2016 - 2021 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 Excel™ 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.15 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. VaryColors: true,
  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 (
  506. // "fmt"
  507. //
  508. // "github.com/360EntSecGroup-Skylar/excelize/v2"
  509. // )
  510. //
  511. // func main() {
  512. // categories := map[string]string{
  513. // "A2": "Small", "A3": "Normal", "A4": "Large",
  514. // "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  515. // values := map[string]int{
  516. // "B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  517. // f := excelize.NewFile()
  518. // for k, v := range categories {
  519. // f.SetCellValue("Sheet1", k, v)
  520. // }
  521. // for k, v := range values {
  522. // f.SetCellValue("Sheet1", k, v)
  523. // }
  524. // if err := f.AddChart("Sheet1", "E1", `{
  525. // "type": "col3DClustered",
  526. // "series": [
  527. // {
  528. // "name": "Sheet1!$A$2",
  529. // "categories": "Sheet1!$B$1:$D$1",
  530. // "values": "Sheet1!$B$2:$D$2"
  531. // },
  532. // {
  533. // "name": "Sheet1!$A$3",
  534. // "categories": "Sheet1!$B$1:$D$1",
  535. // "values": "Sheet1!$B$3:$D$3"
  536. // },
  537. // {
  538. // "name": "Sheet1!$A$4",
  539. // "categories": "Sheet1!$B$1:$D$1",
  540. // "values": "Sheet1!$B$4:$D$4"
  541. // }],
  542. // "title":
  543. // {
  544. // "name": "Fruit 3D Clustered Column Chart"
  545. // },
  546. // "legend":
  547. // {
  548. // "none": false,
  549. // "position": "bottom",
  550. // "show_legend_key": false
  551. // },
  552. // "plotarea":
  553. // {
  554. // "show_bubble_size": true,
  555. // "show_cat_name": false,
  556. // "show_leader_lines": false,
  557. // "show_percent": true,
  558. // "show_series_name": true,
  559. // "show_val": true
  560. // },
  561. // "show_blanks_as": "zero",
  562. // "x_axis":
  563. // {
  564. // "reverse_order": true
  565. // },
  566. // "y_axis":
  567. // {
  568. // "maximum": 7.5,
  569. // "minimum": 0.5
  570. // }
  571. // }`); err != nil {
  572. // fmt.Println(err)
  573. // return
  574. // }
  575. // // Save spreadsheet by the given path.
  576. // if err := f.SaveAs("Book1.xlsx"); err != nil {
  577. // fmt.Println(err)
  578. // }
  579. // }
  580. //
  581. // The following shows the type of chart supported by excelize:
  582. //
  583. // Type | Chart
  584. // -----------------------------+------------------------------
  585. // area | 2D area chart
  586. // areaStacked | 2D stacked area chart
  587. // areaPercentStacked | 2D 100% stacked area chart
  588. // area3D | 3D area chart
  589. // area3DStacked | 3D stacked area chart
  590. // area3DPercentStacked | 3D 100% stacked area chart
  591. // bar | 2D clustered bar chart
  592. // barStacked | 2D stacked bar chart
  593. // barPercentStacked | 2D 100% stacked bar chart
  594. // bar3DClustered | 3D clustered bar chart
  595. // bar3DStacked | 3D stacked bar chart
  596. // bar3DPercentStacked | 3D 100% stacked bar chart
  597. // bar3DConeClustered | 3D cone clustered bar chart
  598. // bar3DConeStacked | 3D cone stacked bar chart
  599. // bar3DConePercentStacked | 3D cone percent bar chart
  600. // bar3DPyramidClustered | 3D pyramid clustered bar chart
  601. // bar3DPyramidStacked | 3D pyramid stacked bar chart
  602. // bar3DPyramidPercentStacked | 3D pyramid percent stacked bar chart
  603. // bar3DCylinderClustered | 3D cylinder clustered bar chart
  604. // bar3DCylinderStacked | 3D cylinder stacked bar chart
  605. // bar3DCylinderPercentStacked | 3D cylinder percent stacked bar chart
  606. // col | 2D clustered column chart
  607. // colStacked | 2D stacked column chart
  608. // colPercentStacked | 2D 100% stacked column chart
  609. // col3DClustered | 3D clustered column chart
  610. // col3D | 3D column chart
  611. // col3DStacked | 3D stacked column chart
  612. // col3DPercentStacked | 3D 100% stacked column chart
  613. // col3DCone | 3D cone column chart
  614. // col3DConeClustered | 3D cone clustered column chart
  615. // col3DConeStacked | 3D cone stacked column chart
  616. // col3DConePercentStacked | 3D cone percent stacked column chart
  617. // col3DPyramid | 3D pyramid column chart
  618. // col3DPyramidClustered | 3D pyramid clustered column chart
  619. // col3DPyramidStacked | 3D pyramid stacked column chart
  620. // col3DPyramidPercentStacked | 3D pyramid percent stacked column chart
  621. // col3DCylinder | 3D cylinder column chart
  622. // col3DCylinderClustered | 3D cylinder clustered column chart
  623. // col3DCylinderStacked | 3D cylinder stacked column chart
  624. // col3DCylinderPercentStacked | 3D cylinder percent stacked column chart
  625. // doughnut | doughnut chart
  626. // line | line chart
  627. // pie | pie chart
  628. // pie3D | 3D pie chart
  629. // pieOfPie | pie of pie chart
  630. // barOfPie | bar of pie chart
  631. // radar | radar chart
  632. // scatter | scatter chart
  633. // surface3D | 3D surface chart
  634. // wireframeSurface3D | 3D wireframe surface chart
  635. // contour | contour chart
  636. // wireframeContour | wireframe contour chart
  637. // bubble | bubble chart
  638. // bubble3D | 3D bubble chart
  639. //
  640. // In Excel a chart series is a collection of information that defines which data is plotted such as values, axis labels and formatting.
  641. //
  642. // The series options that can be set are:
  643. //
  644. // name
  645. // categories
  646. // values
  647. // line
  648. // marker
  649. //
  650. // 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
  651. //
  652. // 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.
  653. //
  654. // 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.
  655. //
  656. // 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.
  657. //
  658. // 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'):
  659. //
  660. // circle
  661. // dash
  662. // diamond
  663. // dot
  664. // none
  665. // picture
  666. // plus
  667. // square
  668. // star
  669. // triangle
  670. // x
  671. // auto
  672. //
  673. // Set properties of the chart legend. The options that can be set are:
  674. //
  675. // none
  676. // position
  677. // show_legend_key
  678. //
  679. // none: Specified if show the legend without overlapping the chart. The default value is 'false'.
  680. //
  681. // 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:
  682. //
  683. // top
  684. // bottom
  685. // left
  686. // right
  687. // top_right
  688. //
  689. // show_legend_key: Set the legend keys shall be shown in data labels. The default value is false.
  690. //
  691. // Set properties of the chart title. The properties that can be set are:
  692. //
  693. // title
  694. //
  695. // 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.
  696. //
  697. // 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:
  698. //
  699. // gap
  700. // span
  701. // zero
  702. //
  703. // gap: Specifies that blank values shall be left as a gap.
  704. //
  705. // span: Specifies that blank values shall be spanned with a line.
  706. //
  707. // zero: Specifies that blank values shall be treated as zero.
  708. //
  709. // Specifies that each data marker in the series has a different color by vary_colors. The default value is true.
  710. //
  711. // Set chart offset, scale, aspect ratio setting and print settings by format, same as function AddPicture.
  712. //
  713. // Set the position of the chart plot area by plotarea. The properties that can be set are:
  714. //
  715. // show_bubble_size
  716. // show_cat_name
  717. // show_leader_lines
  718. // show_percent
  719. // show_series_name
  720. // show_val
  721. //
  722. // 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.
  723. //
  724. // 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.
  725. //
  726. // show_leader_lines: Specifies leader lines shall be shown for data labels. The show_leader_lines property is optional. The default value is false.
  727. //
  728. // show_percent: Specifies that the percentage shall be shown in a data label. The show_percent property is optional. The default value is false.
  729. //
  730. // 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.
  731. //
  732. // show_val: Specifies that the value shall be shown in a data label. The show_val property is optional. The default value is false.
  733. //
  734. // Set the primary horizontal and vertical axis options by x_axis and y_axis. The properties of x_axis that can be set are:
  735. //
  736. // major_grid_lines
  737. // minor_grid_lines
  738. // tick_label_skip
  739. // reverse_order
  740. // maximum
  741. // minimum
  742. //
  743. // The properties of y_axis that can be set are:
  744. //
  745. // major_grid_lines
  746. // minor_grid_lines
  747. // major_unit
  748. // reverse_order
  749. // maximum
  750. // minimum
  751. //
  752. // major_grid_lines: Specifies major gridlines.
  753. //
  754. // minor_grid_lines: Specifies minor gridlines.
  755. //
  756. // 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.
  757. //
  758. // 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.
  759. //
  760. // 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.
  761. //
  762. // maximum: Specifies that the fixed maximum, 0 is auto. The maximum property is optional. The default value is auto.
  763. //
  764. // minimum: Specifies that the fixed minimum, 0 is auto. The minimum property is optional. The default value is auto.
  765. //
  766. // Set chart size by dimension property. The dimension property is optional. The default width is 480, and height is 290.
  767. //
  768. // combo: Specifies the create a chart that combines two or more chart types
  769. // in a single chart. For example, create a clustered column - line chart with
  770. // data Sheet1!$E$1:$L$15:
  771. //
  772. // package main
  773. //
  774. // import (
  775. // "fmt"
  776. //
  777. // "github.com/360EntSecGroup-Skylar/excelize/v2"
  778. // )
  779. //
  780. // func main() {
  781. // categories := map[string]string{
  782. // "A2": "Small", "A3": "Normal", "A4": "Large",
  783. // "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  784. // values := map[string]int{
  785. // "B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  786. // f := excelize.NewFile()
  787. // for k, v := range categories {
  788. // f.SetCellValue("Sheet1", k, v)
  789. // }
  790. // for k, v := range values {
  791. // f.SetCellValue("Sheet1", k, v)
  792. // }
  793. // if err := f.AddChart("Sheet1", "E1", `{
  794. // "type": "col",
  795. // "series": [
  796. // {
  797. // "name": "Sheet1!$A$2",
  798. // "categories": "",
  799. // "values": "Sheet1!$B$2:$D$2"
  800. // },
  801. // {
  802. // "name": "Sheet1!$A$3",
  803. // "categories": "Sheet1!$B$1:$D$1",
  804. // "values": "Sheet1!$B$3:$D$3"
  805. // }],
  806. // "format":
  807. // {
  808. // "x_scale": 1.0,
  809. // "y_scale": 1.0,
  810. // "x_offset": 15,
  811. // "y_offset": 10,
  812. // "print_obj": true,
  813. // "lock_aspect_ratio": false,
  814. // "locked": false
  815. // },
  816. // "title":
  817. // {
  818. // "name": "Clustered Column - Line Chart"
  819. // },
  820. // "legend":
  821. // {
  822. // "position": "left",
  823. // "show_legend_key": false
  824. // },
  825. // "plotarea":
  826. // {
  827. // "show_bubble_size": true,
  828. // "show_cat_name": false,
  829. // "show_leader_lines": false,
  830. // "show_percent": true,
  831. // "show_series_name": true,
  832. // "show_val": true
  833. // }
  834. // }`, `{
  835. // "type": "line",
  836. // "series": [
  837. // {
  838. // "name": "Sheet1!$A$4",
  839. // "categories": "Sheet1!$B$1:$D$1",
  840. // "values": "Sheet1!$B$4:$D$4",
  841. // "marker":
  842. // {
  843. // "symbol": "none",
  844. // "size": 10
  845. // }
  846. // }],
  847. // "format":
  848. // {
  849. // "x_scale": 1,
  850. // "y_scale": 1,
  851. // "x_offset": 15,
  852. // "y_offset": 10,
  853. // "print_obj": true,
  854. // "lock_aspect_ratio": false,
  855. // "locked": false
  856. // },
  857. // "legend":
  858. // {
  859. // "position": "right",
  860. // "show_legend_key": false
  861. // },
  862. // "plotarea":
  863. // {
  864. // "show_bubble_size": true,
  865. // "show_cat_name": false,
  866. // "show_leader_lines": false,
  867. // "show_percent": true,
  868. // "show_series_name": true,
  869. // "show_val": true
  870. // }
  871. // }`); err != nil {
  872. // fmt.Println(err)
  873. // return
  874. // }
  875. // // Save spreadsheet file by the given path.
  876. // if err := f.SaveAs("Book1.xlsx"); err != nil {
  877. // fmt.Println(err)
  878. // }
  879. // }
  880. //
  881. func (f *File) AddChart(sheet, cell, format string, combo ...string) error {
  882. // Read sheet data.
  883. ws, err := f.workSheetReader(sheet)
  884. if err != nil {
  885. return err
  886. }
  887. formatSet, comboCharts, err := f.getFormatChart(format, combo)
  888. if err != nil {
  889. return err
  890. }
  891. // Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
  892. drawingID := f.countDrawings() + 1
  893. chartID := f.countCharts() + 1
  894. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  895. drawingID, drawingXML = f.prepareDrawing(ws, drawingID, sheet, drawingXML)
  896. drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
  897. drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
  898. err = f.addDrawingChart(sheet, drawingXML, cell, formatSet.Dimension.Width, formatSet.Dimension.Height, drawingRID, &formatSet.Format)
  899. if err != nil {
  900. return err
  901. }
  902. f.addChart(formatSet, comboCharts)
  903. f.addContentTypePart(chartID, "chart")
  904. f.addContentTypePart(drawingID, "drawings")
  905. f.addSheetNameSpace(sheet, SourceRelationship)
  906. return err
  907. }
  908. // AddChartSheet provides the method to create a chartsheet by given chart
  909. // format set (such as offset, scale, aspect ratio setting and print settings)
  910. // and properties set. In Excel a chartsheet is a worksheet that only contains
  911. // a chart.
  912. func (f *File) AddChartSheet(sheet, format string, combo ...string) error {
  913. // Check if the worksheet already exists
  914. if f.GetSheetIndex(sheet) != -1 {
  915. return ErrExistsWorksheet
  916. }
  917. formatSet, comboCharts, err := f.getFormatChart(format, combo)
  918. if err != nil {
  919. return err
  920. }
  921. cs := xlsxChartsheet{
  922. SheetViews: &xlsxChartsheetViews{
  923. SheetView: []*xlsxChartsheetView{{ZoomScaleAttr: 100, ZoomToFitAttr: true}},
  924. },
  925. }
  926. f.SheetCount++
  927. wb := f.workbookReader()
  928. sheetID := 0
  929. for _, v := range wb.Sheets.Sheet {
  930. if v.SheetID > sheetID {
  931. sheetID = v.SheetID
  932. }
  933. }
  934. sheetID++
  935. path := "xl/chartsheets/sheet" + strconv.Itoa(sheetID) + ".xml"
  936. f.sheetMap[trimSheetName(sheet)] = path
  937. f.Sheet[path] = nil
  938. drawingID := f.countDrawings() + 1
  939. chartID := f.countCharts() + 1
  940. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  941. f.prepareChartSheetDrawing(&cs, drawingID, sheet)
  942. drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
  943. drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
  944. f.addSheetDrawingChart(drawingXML, drawingRID, &formatSet.Format)
  945. f.addChart(formatSet, comboCharts)
  946. f.addContentTypePart(chartID, "chart")
  947. f.addContentTypePart(sheetID, "chartsheet")
  948. f.addContentTypePart(drawingID, "drawings")
  949. // Update workbook.xml.rels
  950. rID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipChartsheet, fmt.Sprintf("/xl/chartsheets/sheet%d.xml", sheetID), "")
  951. // Update workbook.xml
  952. f.setWorkbook(sheet, sheetID, rID)
  953. chartsheet, _ := xml.Marshal(cs)
  954. f.addSheetNameSpace(sheet, NameSpaceSpreadSheet)
  955. f.saveFileList(path, replaceRelationshipsBytes(f.replaceNameSpaceBytes(path, chartsheet)))
  956. return err
  957. }
  958. // getFormatChart provides a function to check format set of the chart and
  959. // create chart format.
  960. func (f *File) getFormatChart(format string, combo []string) (*formatChart, []*formatChart, error) {
  961. comboCharts := []*formatChart{}
  962. formatSet, err := parseFormatChartSet(format)
  963. if err != nil {
  964. return formatSet, comboCharts, err
  965. }
  966. for _, comboFormat := range combo {
  967. comboChart, err := parseFormatChartSet(comboFormat)
  968. if err != nil {
  969. return formatSet, comboCharts, err
  970. }
  971. if _, ok := chartValAxNumFmtFormatCode[comboChart.Type]; !ok {
  972. return formatSet, comboCharts, errors.New("unsupported chart type " + comboChart.Type)
  973. }
  974. comboCharts = append(comboCharts, comboChart)
  975. }
  976. if _, ok := chartValAxNumFmtFormatCode[formatSet.Type]; !ok {
  977. return formatSet, comboCharts, errors.New("unsupported chart type " + formatSet.Type)
  978. }
  979. return formatSet, comboCharts, err
  980. }
  981. // DeleteChart provides a function to delete chart in XLSX by given worksheet
  982. // and cell name.
  983. func (f *File) DeleteChart(sheet, cell string) (err error) {
  984. col, row, err := CellNameToCoordinates(cell)
  985. if err != nil {
  986. return
  987. }
  988. col--
  989. row--
  990. ws, err := f.workSheetReader(sheet)
  991. if err != nil {
  992. return
  993. }
  994. if ws.Drawing == nil {
  995. return
  996. }
  997. drawingXML := strings.Replace(f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID), "..", "xl", -1)
  998. return f.deleteDrawing(col, row, drawingXML, "Chart")
  999. }
  1000. // countCharts provides a function to get chart files count storage in the
  1001. // folder xl/charts.
  1002. func (f *File) countCharts() int {
  1003. count := 0
  1004. for k := range f.XLSX {
  1005. if strings.Contains(k, "xl/charts/chart") {
  1006. count++
  1007. }
  1008. }
  1009. return count
  1010. }
  1011. // ptToEMUs provides a function to convert pt to EMUs, 1 pt = 12700 EMUs. The
  1012. // range of pt is 0.25pt - 999pt. If the value of pt is outside the range, the
  1013. // default EMUs will be returned.
  1014. func (f *File) ptToEMUs(pt float64) int {
  1015. if 0.25 > pt || pt > 999 {
  1016. return 25400
  1017. }
  1018. return int(12700 * pt)
  1019. }