chart.go 35 KB

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