chart.go 33 KB

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