chart.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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/v2"
  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 spreadsheet 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. // marker
  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. // marker: This sets the marker of the line chart and scatter chart. The range of optional field 'size' is 2-72 (default value is 5). The enumeration value of optional field 'symbol' are (default value is 'auto'):
  608. //
  609. // circle
  610. // dash
  611. // diamond
  612. // dot
  613. // none
  614. // picture
  615. // plus
  616. // square
  617. // star
  618. // triangle
  619. // x
  620. // auto
  621. //
  622. // Set properties of the chart legend. The options that can be set are:
  623. //
  624. // position
  625. // show_legend_key
  626. //
  627. // position: Set the position of the chart legend. The default legend position is right. The available positions are:
  628. //
  629. // top
  630. // bottom
  631. // left
  632. // right
  633. // top_right
  634. //
  635. // show_legend_key: Set the legend keys shall be shown in data labels. The default value is false.
  636. //
  637. // Set properties of the chart title. The properties that can be set are:
  638. //
  639. // title
  640. //
  641. // 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.
  642. //
  643. // 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:
  644. //
  645. // gap
  646. // span
  647. // zero
  648. //
  649. // gap: Specifies that blank values shall be left as a gap.
  650. //
  651. // span: Specifies that blank values shall be spanned with a line.
  652. //
  653. // zero: Specifies that blank values shall be treated as zero.
  654. //
  655. // Set chart offset, scale, aspect ratio setting and print settings by format, same as function AddPicture.
  656. //
  657. // Set the position of the chart plot area by plotarea. The properties that can be set are:
  658. //
  659. // show_bubble_size
  660. // show_cat_name
  661. // show_leader_lines
  662. // show_percent
  663. // show_series_name
  664. // show_val
  665. //
  666. // 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.
  667. //
  668. // 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.
  669. //
  670. // show_leader_lines: Specifies leader lines shall be shown for data labels. The show_leader_lines property is optional. The default value is false.
  671. //
  672. // show_percent: Specifies that the percentage shall be shown in a data label. The show_percent property is optional. The default value is false.
  673. //
  674. // 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.
  675. //
  676. // show_val: Specifies that the value shall be shown in a data label. The show_val property is optional. The default value is false.
  677. //
  678. // Set the primary horizontal and vertical axis options by x_axis and y_axis. The properties of x_axis that can be set are:
  679. //
  680. // major_grid_lines
  681. // minor_grid_lines
  682. // tick_label_skip
  683. // reverse_order
  684. // maximum
  685. // minimum
  686. //
  687. // The properties of y_axis that can be set are:
  688. //
  689. // major_grid_lines
  690. // minor_grid_lines
  691. // major_unit
  692. // reverse_order
  693. // maximum
  694. // minimum
  695. //
  696. // major_grid_lines: Specifies major gridlines.
  697. //
  698. // minor_grid_lines: Specifies minor gridlines.
  699. //
  700. // 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.
  701. //
  702. // 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.
  703. //
  704. // 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.
  705. //
  706. // maximum: Specifies that the fixed maximum, 0 is auto. The maximum property is optional. The default value is auto.
  707. //
  708. // minimum: Specifies that the fixed minimum, 0 is auto. The minimum property is optional. The default value is auto.
  709. //
  710. // Set chart size by dimension property. The dimension property is optional. The default width is 480, and height is 290.
  711. //
  712. // combo: Specifies the create a chart that combines two or more chart types
  713. // in a single chart. For example, create a clustered column - line chart with
  714. // data Sheet1!$E$1:$L$15:
  715. //
  716. // package main
  717. //
  718. // import (
  719. // "fmt"
  720. //
  721. // "github.com/360EntSecGroup-Skylar/excelize/v2"
  722. // )
  723. //
  724. // func main() {
  725. // categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  726. // values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  727. // f := excelize.NewFile()
  728. // for k, v := range categories {
  729. // f.SetCellValue("Sheet1", k, v)
  730. // }
  731. // for k, v := range values {
  732. // f.SetCellValue("Sheet1", k, v)
  733. // }
  734. // 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","marker":{"symbol":"none","size":10}}],"format":{"x_scale":1,"y_scale":1,"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 {
  735. // fmt.Println(err)
  736. // return
  737. // }
  738. // // Save spreadsheet file by the given path.
  739. // if err := f.SaveAs("Book1.xlsx"); err != nil {
  740. // fmt.Println(err)
  741. // }
  742. // }
  743. //
  744. func (f *File) AddChart(sheet, cell, format string, combo ...string) error {
  745. // Read sheet data.
  746. ws, err := f.workSheetReader(sheet)
  747. if err != nil {
  748. return err
  749. }
  750. formatSet, comboCharts, err := f.getFormatChart(format, combo)
  751. if err != nil {
  752. return err
  753. }
  754. // Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
  755. drawingID := f.countDrawings() + 1
  756. chartID := f.countCharts() + 1
  757. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  758. drawingID, drawingXML = f.prepareDrawing(ws, drawingID, sheet, drawingXML)
  759. drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
  760. drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
  761. err = f.addDrawingChart(sheet, drawingXML, cell, formatSet.Dimension.Width, formatSet.Dimension.Height, drawingRID, &formatSet.Format)
  762. if err != nil {
  763. return err
  764. }
  765. f.addChart(formatSet, comboCharts)
  766. f.addContentTypePart(chartID, "chart")
  767. f.addContentTypePart(drawingID, "drawings")
  768. f.addSheetNameSpace(sheet, SourceRelationship)
  769. return err
  770. }
  771. // AddChartSheet provides the method to create a chartsheet by given chart
  772. // format set (such as offset, scale, aspect ratio setting and print settings)
  773. // and properties set. In Excel a chartsheet is a worksheet that only contains
  774. // a chart.
  775. func (f *File) AddChartSheet(sheet, format string, combo ...string) error {
  776. // Check if the worksheet already exists
  777. if f.GetSheetIndex(sheet) != -1 {
  778. return errors.New("the same name worksheet already exists")
  779. }
  780. formatSet, comboCharts, err := f.getFormatChart(format, combo)
  781. if err != nil {
  782. return err
  783. }
  784. cs := xlsxChartsheet{
  785. SheetViews: []*xlsxChartsheetViews{{
  786. SheetView: []*xlsxChartsheetView{{ZoomScaleAttr: 100, ZoomToFitAttr: true}}},
  787. },
  788. }
  789. f.SheetCount++
  790. wb := f.workbookReader()
  791. sheetID := 0
  792. for _, v := range wb.Sheets.Sheet {
  793. if v.SheetID > sheetID {
  794. sheetID = v.SheetID
  795. }
  796. }
  797. sheetID++
  798. path := "xl/chartsheets/sheet" + strconv.Itoa(sheetID) + ".xml"
  799. f.sheetMap[trimSheetName(sheet)] = path
  800. f.Sheet[path] = nil
  801. drawingID := f.countDrawings() + 1
  802. chartID := f.countCharts() + 1
  803. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  804. f.prepareChartSheetDrawing(&cs, drawingID, sheet)
  805. drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
  806. drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
  807. f.addSheetDrawingChart(drawingXML, drawingRID, &formatSet.Format)
  808. f.addChart(formatSet, comboCharts)
  809. f.addContentTypePart(chartID, "chart")
  810. f.addContentTypePart(sheetID, "chartsheet")
  811. f.addContentTypePart(drawingID, "drawings")
  812. // Update workbook.xml.rels
  813. rID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipChartsheet, fmt.Sprintf("/xl/chartsheets/sheet%d.xml", sheetID), "")
  814. // Update workbook.xml
  815. f.setWorkbook(sheet, sheetID, rID)
  816. chartsheet, _ := xml.Marshal(cs)
  817. f.addSheetNameSpace(sheet, NameSpaceSpreadSheet)
  818. f.saveFileList(path, replaceRelationshipsBytes(f.replaceNameSpaceBytes(path, chartsheet)))
  819. return err
  820. }
  821. // getFormatChart provides a function to check format set of the chart and
  822. // create chart format.
  823. func (f *File) getFormatChart(format string, combo []string) (*formatChart, []*formatChart, error) {
  824. comboCharts := []*formatChart{}
  825. formatSet, err := parseFormatChartSet(format)
  826. if err != nil {
  827. return formatSet, comboCharts, err
  828. }
  829. for _, comboFormat := range combo {
  830. comboChart, err := parseFormatChartSet(comboFormat)
  831. if err != nil {
  832. return formatSet, comboCharts, err
  833. }
  834. if _, ok := chartValAxNumFmtFormatCode[comboChart.Type]; !ok {
  835. return formatSet, comboCharts, errors.New("unsupported chart type " + comboChart.Type)
  836. }
  837. comboCharts = append(comboCharts, comboChart)
  838. }
  839. if _, ok := chartValAxNumFmtFormatCode[formatSet.Type]; !ok {
  840. return formatSet, comboCharts, errors.New("unsupported chart type " + formatSet.Type)
  841. }
  842. return formatSet, comboCharts, err
  843. }
  844. // DeleteChart provides a function to delete chart in XLSX by given worksheet
  845. // and cell name.
  846. func (f *File) DeleteChart(sheet, cell string) (err error) {
  847. col, row, err := CellNameToCoordinates(cell)
  848. if err != nil {
  849. return
  850. }
  851. col--
  852. row--
  853. ws, err := f.workSheetReader(sheet)
  854. if err != nil {
  855. return
  856. }
  857. if ws.Drawing == nil {
  858. return
  859. }
  860. drawingXML := strings.Replace(f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID), "..", "xl", -1)
  861. return f.deleteDrawing(col, row, drawingXML, "Chart")
  862. }
  863. // countCharts provides a function to get chart files count storage in the
  864. // folder xl/charts.
  865. func (f *File) countCharts() int {
  866. count := 0
  867. for k := range f.XLSX {
  868. if strings.Contains(k, "xl/charts/chart") {
  869. count++
  870. }
  871. }
  872. return count
  873. }
  874. // ptToEMUs provides a function to convert pt to EMUs, 1 pt = 12700 EMUs. The
  875. // range of pt is 0.25pt - 999pt. If the value of pt is outside the range, the
  876. // default EMUs will be returned.
  877. func (f *File) ptToEMUs(pt float64) int {
  878. if 0.25 > pt || pt > 999 {
  879. return 25400
  880. }
  881. return int(12700 * pt)
  882. }