chart.go 35 KB

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