chart.go 38 KB

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