chart.go 34 KB

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