chart.go 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. // Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.8 or later.
  9. package excelize
  10. import (
  11. "encoding/json"
  12. "encoding/xml"
  13. "strconv"
  14. "strings"
  15. )
  16. // This section defines the currently supported chart types.
  17. const (
  18. Area = "area"
  19. AreaStacked = "areaStacked"
  20. AreaPercentStacked = "areaPercentStacked"
  21. Area3D = "area3D"
  22. Area3DStacked = "area3DStacked"
  23. Area3DPercentStacked = "area3DPercentStacked"
  24. Bar = "bar"
  25. BarStacked = "barStacked"
  26. BarPercentStacked = "barPercentStacked"
  27. Bar3DClustered = "bar3DClustered"
  28. Bar3DStacked = "bar3DStacked"
  29. Bar3DPercentStacked = "bar3DPercentStacked"
  30. Col = "col"
  31. ColStacked = "colStacked"
  32. ColPercentStacked = "colPercentStacked"
  33. Col3DClustered = "col3DClustered"
  34. Col3D = "col3D"
  35. Col3DStacked = "col3DStacked"
  36. Col3DPercentStacked = "col3DPercentStacked"
  37. Doughnut = "doughnut"
  38. Line = "line"
  39. Pie = "pie"
  40. Pie3D = "pie3D"
  41. Radar = "radar"
  42. Scatter = "scatter"
  43. )
  44. // This section defines the default value of chart properties.
  45. var (
  46. chartView3DRotX = map[string]int{
  47. Area: 0,
  48. AreaStacked: 0,
  49. AreaPercentStacked: 0,
  50. Area3D: 15,
  51. Area3DStacked: 15,
  52. Area3DPercentStacked: 15,
  53. Bar: 0,
  54. BarStacked: 0,
  55. BarPercentStacked: 0,
  56. Bar3DClustered: 15,
  57. Bar3DStacked: 15,
  58. Bar3DPercentStacked: 15,
  59. Col: 0,
  60. ColStacked: 0,
  61. ColPercentStacked: 0,
  62. Col3DClustered: 15,
  63. Col3D: 15,
  64. Col3DStacked: 15,
  65. Col3DPercentStacked: 15,
  66. Doughnut: 0,
  67. Line: 0,
  68. Pie: 0,
  69. Pie3D: 30,
  70. Radar: 0,
  71. Scatter: 0,
  72. }
  73. chartView3DRotY = map[string]int{
  74. Area: 0,
  75. AreaStacked: 0,
  76. AreaPercentStacked: 0,
  77. Area3D: 20,
  78. Area3DStacked: 20,
  79. Area3DPercentStacked: 20,
  80. Bar: 0,
  81. BarStacked: 0,
  82. BarPercentStacked: 0,
  83. Bar3DClustered: 20,
  84. Bar3DStacked: 20,
  85. Bar3DPercentStacked: 20,
  86. Col: 0,
  87. ColStacked: 0,
  88. ColPercentStacked: 0,
  89. Col3DClustered: 20,
  90. Col3D: 20,
  91. Col3DStacked: 20,
  92. Col3DPercentStacked: 20,
  93. Doughnut: 0,
  94. Line: 0,
  95. Pie: 0,
  96. Pie3D: 0,
  97. Radar: 0,
  98. Scatter: 0,
  99. }
  100. chartView3DDepthPercent = map[string]int{
  101. Area: 100,
  102. AreaStacked: 100,
  103. AreaPercentStacked: 100,
  104. Area3D: 100,
  105. Area3DStacked: 100,
  106. Area3DPercentStacked: 100,
  107. Bar: 100,
  108. BarStacked: 100,
  109. BarPercentStacked: 100,
  110. Bar3DClustered: 100,
  111. Bar3DStacked: 100,
  112. Bar3DPercentStacked: 100,
  113. Col: 100,
  114. ColStacked: 100,
  115. ColPercentStacked: 100,
  116. Col3DClustered: 100,
  117. Col3D: 100,
  118. Col3DStacked: 100,
  119. Col3DPercentStacked: 100,
  120. Doughnut: 100,
  121. Line: 100,
  122. Pie: 100,
  123. Pie3D: 100,
  124. Radar: 100,
  125. Scatter: 100,
  126. }
  127. chartView3DRAngAx = map[string]int{
  128. Area: 0,
  129. AreaStacked: 0,
  130. AreaPercentStacked: 0,
  131. Area3D: 1,
  132. Area3DStacked: 1,
  133. Area3DPercentStacked: 1,
  134. Bar: 0,
  135. BarStacked: 0,
  136. BarPercentStacked: 0,
  137. Bar3DClustered: 1,
  138. Bar3DStacked: 1,
  139. Bar3DPercentStacked: 1,
  140. Col: 0,
  141. ColStacked: 0,
  142. ColPercentStacked: 0,
  143. Col3DClustered: 1,
  144. Col3D: 1,
  145. Col3DStacked: 1,
  146. Col3DPercentStacked: 1,
  147. Doughnut: 0,
  148. Line: 0,
  149. Pie: 0,
  150. Pie3D: 0,
  151. Radar: 0,
  152. Scatter: 0,
  153. }
  154. chartLegendPosition = map[string]string{
  155. "bottom": "b",
  156. "left": "l",
  157. "right": "r",
  158. "top": "t",
  159. "top_right": "tr",
  160. }
  161. chartValAxNumFmtFormatCode = map[string]string{
  162. Area: "General",
  163. AreaStacked: "General",
  164. AreaPercentStacked: "0%",
  165. Area3D: "General",
  166. Area3DStacked: "General",
  167. Area3DPercentStacked: "0%",
  168. Bar: "General",
  169. BarStacked: "General",
  170. BarPercentStacked: "0%",
  171. Bar3DClustered: "General",
  172. Bar3DStacked: "General",
  173. Bar3DPercentStacked: "0%",
  174. Col: "General",
  175. ColStacked: "General",
  176. ColPercentStacked: "0%",
  177. Col3DClustered: "General",
  178. Col3D: "General",
  179. Col3DStacked: "General",
  180. Col3DPercentStacked: "0%",
  181. Doughnut: "General",
  182. Line: "General",
  183. Pie: "General",
  184. Pie3D: "General",
  185. Radar: "General",
  186. Scatter: "General",
  187. }
  188. chartValAxCrossBetween = map[string]string{
  189. Area: "midCat",
  190. AreaStacked: "midCat",
  191. AreaPercentStacked: "midCat",
  192. Area3D: "midCat",
  193. Area3DStacked: "midCat",
  194. Area3DPercentStacked: "midCat",
  195. Bar: "between",
  196. BarStacked: "between",
  197. BarPercentStacked: "between",
  198. Bar3DClustered: "between",
  199. Bar3DStacked: "between",
  200. Bar3DPercentStacked: "between",
  201. Col: "between",
  202. ColStacked: "between",
  203. ColPercentStacked: "between",
  204. Col3DClustered: "between",
  205. Col3D: "between",
  206. Col3DStacked: "between",
  207. Col3DPercentStacked: "between",
  208. Doughnut: "between",
  209. Line: "between",
  210. Pie: "between",
  211. Pie3D: "between",
  212. Radar: "between",
  213. Scatter: "between",
  214. }
  215. plotAreaChartGrouping = map[string]string{
  216. Area: "standard",
  217. AreaStacked: "stacked",
  218. AreaPercentStacked: "percentStacked",
  219. Area3D: "standard",
  220. Area3DStacked: "stacked",
  221. Area3DPercentStacked: "percentStacked",
  222. Bar: "clustered",
  223. BarStacked: "stacked",
  224. BarPercentStacked: "percentStacked",
  225. Bar3DClustered: "clustered",
  226. Bar3DStacked: "stacked",
  227. Bar3DPercentStacked: "percentStacked",
  228. Col: "clustered",
  229. ColStacked: "stacked",
  230. ColPercentStacked: "percentStacked",
  231. Col3DClustered: "clustered",
  232. Col3D: "standard",
  233. Col3DStacked: "stacked",
  234. Col3DPercentStacked: "percentStacked",
  235. Line: "standard",
  236. }
  237. plotAreaChartBarDir = map[string]string{
  238. Bar: "bar",
  239. BarStacked: "bar",
  240. BarPercentStacked: "bar",
  241. Bar3DClustered: "bar",
  242. Bar3DStacked: "bar",
  243. Bar3DPercentStacked: "bar",
  244. Col: "col",
  245. ColStacked: "col",
  246. ColPercentStacked: "col",
  247. Col3DClustered: "col",
  248. Col3D: "col",
  249. Col3DStacked: "col",
  250. Col3DPercentStacked: "col",
  251. Line: "standard",
  252. }
  253. orientation = map[bool]string{
  254. true: "maxMin",
  255. false: "minMax",
  256. }
  257. catAxPos = map[bool]string{
  258. true: "t",
  259. false: "b",
  260. }
  261. valAxPos = map[bool]string{
  262. true: "r",
  263. false: "l",
  264. }
  265. )
  266. // parseFormatChartSet provides a function to parse the format settings of the
  267. // chart with default value.
  268. func parseFormatChartSet(formatSet string) (*formatChart, error) {
  269. format := formatChart{
  270. Dimension: formatChartDimension{
  271. Width: 480,
  272. Height: 290,
  273. },
  274. Format: formatPicture{
  275. FPrintsWithSheet: true,
  276. FLocksWithSheet: false,
  277. NoChangeAspect: false,
  278. OffsetX: 0,
  279. OffsetY: 0,
  280. XScale: 1.0,
  281. YScale: 1.0,
  282. },
  283. Legend: formatChartLegend{
  284. Position: "bottom",
  285. ShowLegendKey: false,
  286. },
  287. Title: formatChartTitle{
  288. Name: " ",
  289. },
  290. ShowBlanksAs: "gap",
  291. }
  292. err := json.Unmarshal([]byte(formatSet), &format)
  293. return &format, err
  294. }
  295. // AddChart provides the method to add chart in a sheet by given chart format
  296. // set (such as offset, scale, aspect ratio setting and print settings) and
  297. // properties set. For example, create 3D clustered column chart with data
  298. // Sheet1!$A$29:$D$32:
  299. //
  300. // package main
  301. //
  302. // import (
  303. // "fmt"
  304. //
  305. // "github.com/360EntSecGroup-Skylar/excelize"
  306. // )
  307. //
  308. // func main() {
  309. // categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  310. // values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  311. // f := excelize.NewFile()
  312. // for k, v := range categories {
  313. // f.SetCellValue("Sheet1", k, v)
  314. // }
  315. // for k, v := range values {
  316. // f.SetCellValue("Sheet1", k, v)
  317. // }
  318. // err := f.AddChart("Sheet1", "E1", `{"type":"col3DClustered","dimension":{"width":640,"height":480},"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"}],"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":"bottom","show_legend_key":false},"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}}`)
  319. // if err != nil {
  320. // fmt.Println(err)
  321. // return
  322. // }
  323. // // Save xlsx file by the given path.
  324. // err = xlsx.SaveAs("./Book1.xlsx")
  325. // if err != nil {
  326. // fmt.Println(err)
  327. // }
  328. // }
  329. //
  330. // The following shows the type of chart supported by excelize:
  331. //
  332. // Type | Chart
  333. // ----------------------+------------------------------
  334. // area | 2D area chart
  335. // areaStacked | 2D stacked area chart
  336. // areaPercentStacked | 2D 100% stacked area chart
  337. // area3D | 3D area chart
  338. // area3DStacked | 3D stacked area chart
  339. // area3DPercentStacked | 3D 100% stacked area chart
  340. // bar | 2D clustered bar chart
  341. // barStacked | 2D stacked bar chart
  342. // barPercentStacked | 2D 100% stacked bar chart
  343. // bar3DClustered | 3D clustered bar chart
  344. // bar3DStacked | 3D stacked bar chart
  345. // bar3DPercentStacked | 3D 100% stacked bar chart
  346. // col | 2D clustered column chart
  347. // colStacked | 2D stacked column chart
  348. // colPercentStacked | 2D 100% stacked column chart
  349. // col3DClustered | 3D clustered column chart
  350. // col3D | 3D column chart
  351. // col3DStacked | 3D stacked column chart
  352. // col3DPercentStacked | 3D 100% stacked column chart
  353. // doughnut | doughnut chart
  354. // line | line chart
  355. // pie | pie chart
  356. // pie3D | 3D pie chart
  357. // radar | radar chart
  358. // scatter | scatter chart
  359. //
  360. // In Excel a chart series is a collection of information that defines which data is plotted such as values, axis labels and formatting.
  361. //
  362. // The series options that can be set are:
  363. //
  364. // name
  365. // categories
  366. // values
  367. //
  368. // 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
  369. //
  370. // 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.
  371. //
  372. // 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.
  373. //
  374. // Set properties of the chart legend. The options that can be set are:
  375. //
  376. // position
  377. // show_legend_key
  378. //
  379. // position: Set the position of the chart legend. The default legend position is right. The available positions are:
  380. //
  381. // top
  382. // bottom
  383. // left
  384. // right
  385. // top_right
  386. //
  387. // show_legend_key: Set the legend keys shall be shown in data labels. The default value is false.
  388. //
  389. // Set properties of the chart title. The properties that can be set are:
  390. //
  391. // title
  392. //
  393. // 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.
  394. //
  395. // 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:
  396. //
  397. // gap
  398. // span
  399. // zero
  400. //
  401. // gap: Specifies that blank values shall be left as a gap.
  402. //
  403. // sapn: Specifies that blank values shall be spanned with a line.
  404. //
  405. // zero: Specifies that blank values shall be treated as zero.
  406. //
  407. // Set chart offset, scale, aspect ratio setting and print settings by format, same as function AddPicture.
  408. //
  409. // Set the position of the chart plot area by plotarea. The properties that can be set are:
  410. //
  411. // show_bubble_size
  412. // show_cat_name
  413. // show_leader_lines
  414. // show_percent
  415. // show_series_name
  416. // show_val
  417. //
  418. // 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.
  419. //
  420. // 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.
  421. //
  422. // show_leader_lines: Specifies leader lines shall be shown for data labels. The show_leader_lines property is optional. The default value is false.
  423. //
  424. // show_percent: Specifies that the percentage shall be shown in a data label. The show_percent property is optional. The default value is false.
  425. //
  426. // 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.
  427. //
  428. // show_val: Specifies that the value shall be shown in a data label. The show_val property is optional. The default value is false.
  429. //
  430. // Set the primary horizontal and vertical axis options by x_axis and y_axis. The properties that can be set are:
  431. //
  432. // reverse_order
  433. // maximum
  434. // minimum
  435. //
  436. // 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.
  437. //
  438. // maximum: Specifies that the fixed maximum, 0 is auto. The maximum property is optional. The default value is auto.
  439. //
  440. // minimum: Specifies that the fixed minimum, 0 is auto. The minimum property is optional. The default value is auto.
  441. //
  442. // Set chart size by dimension property. The dimension property is optional. The default width is 480, and height is 290.
  443. //
  444. func (f *File) AddChart(sheet, cell, format string) error {
  445. formatSet, err := parseFormatChartSet(format)
  446. if err != nil {
  447. return err
  448. }
  449. // Read sheet data.
  450. xlsx, err := f.workSheetReader(sheet)
  451. if err != nil {
  452. return err
  453. }
  454. // Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
  455. drawingID := f.countDrawings() + 1
  456. chartID := f.countCharts() + 1
  457. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  458. drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
  459. drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
  460. err = f.addDrawingChart(sheet, drawingXML, cell, formatSet.Dimension.Width, formatSet.Dimension.Height, drawingRID, &formatSet.Format)
  461. if err != nil {
  462. return err
  463. }
  464. f.addChart(formatSet)
  465. f.addContentTypePart(chartID, "chart")
  466. f.addContentTypePart(drawingID, "drawings")
  467. return err
  468. }
  469. // countCharts provides a function to get chart files count storage in the
  470. // folder xl/charts.
  471. func (f *File) countCharts() int {
  472. count := 0
  473. for k := range f.XLSX {
  474. if strings.Contains(k, "xl/charts/chart") {
  475. count++
  476. }
  477. }
  478. return count
  479. }
  480. // prepareDrawing provides a function to prepare drawing ID and XML by given
  481. // drawingID, worksheet name and default drawingXML.
  482. func (f *File) prepareDrawing(xlsx *xlsxWorksheet, drawingID int, sheet, drawingXML string) (int, string) {
  483. sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  484. if xlsx.Drawing != nil {
  485. // The worksheet already has a picture or chart relationships, use the relationships drawing ../drawings/drawing%d.xml.
  486. sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID)
  487. drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml"))
  488. drawingXML = strings.Replace(sheetRelationshipsDrawingXML, "..", "xl", -1)
  489. } else {
  490. // Add first picture for given sheet.
  491. rID := f.addSheetRelationships(sheet, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
  492. f.addSheetDrawing(sheet, rID)
  493. }
  494. return drawingID, drawingXML
  495. }
  496. // addChart provides a function to create chart as xl/charts/chart%d.xml by
  497. // given format sets.
  498. func (f *File) addChart(formatSet *formatChart) {
  499. count := f.countCharts()
  500. xlsxChartSpace := xlsxChartSpace{
  501. XMLNSc: NameSpaceDrawingMLChart,
  502. XMLNSa: NameSpaceDrawingML,
  503. XMLNSr: SourceRelationship,
  504. XMLNSc16r2: SourceRelationshipChart201506,
  505. Date1904: &attrValBool{Val: false},
  506. Lang: &attrValString{Val: "en-US"},
  507. RoundedCorners: &attrValBool{Val: false},
  508. Chart: cChart{
  509. Title: &cTitle{
  510. Tx: cTx{
  511. Rich: &cRich{
  512. P: aP{
  513. PPr: &aPPr{
  514. DefRPr: aRPr{
  515. Kern: 1200,
  516. Strike: "noStrike",
  517. U: "none",
  518. Sz: 1400,
  519. SolidFill: &aSolidFill{
  520. SchemeClr: &aSchemeClr{
  521. Val: "tx1",
  522. LumMod: &attrValInt{
  523. Val: 65000,
  524. },
  525. LumOff: &attrValInt{
  526. Val: 35000,
  527. },
  528. },
  529. },
  530. Ea: &aEa{
  531. Typeface: "+mn-ea",
  532. },
  533. Cs: &aCs{
  534. Typeface: "+mn-cs",
  535. },
  536. Latin: &aLatin{
  537. Typeface: "+mn-lt",
  538. },
  539. },
  540. },
  541. R: &aR{
  542. RPr: aRPr{
  543. Lang: "en-US",
  544. AltLang: "en-US",
  545. },
  546. T: formatSet.Title.Name,
  547. },
  548. },
  549. },
  550. },
  551. TxPr: cTxPr{
  552. P: aP{
  553. PPr: &aPPr{
  554. DefRPr: aRPr{
  555. Kern: 1200,
  556. U: "none",
  557. Sz: 14000,
  558. Strike: "noStrike",
  559. },
  560. },
  561. EndParaRPr: &aEndParaRPr{
  562. Lang: "en-US",
  563. },
  564. },
  565. },
  566. },
  567. View3D: &cView3D{
  568. RotX: &attrValInt{Val: chartView3DRotX[formatSet.Type]},
  569. RotY: &attrValInt{Val: chartView3DRotY[formatSet.Type]},
  570. DepthPercent: &attrValInt{Val: chartView3DDepthPercent[formatSet.Type]},
  571. RAngAx: &attrValInt{Val: chartView3DRAngAx[formatSet.Type]},
  572. },
  573. Floor: &cThicknessSpPr{
  574. Thickness: &attrValInt{Val: 0},
  575. },
  576. SideWall: &cThicknessSpPr{
  577. Thickness: &attrValInt{Val: 0},
  578. },
  579. BackWall: &cThicknessSpPr{
  580. Thickness: &attrValInt{Val: 0},
  581. },
  582. PlotArea: &cPlotArea{},
  583. Legend: &cLegend{
  584. LegendPos: &attrValString{Val: chartLegendPosition[formatSet.Legend.Position]},
  585. Overlay: &attrValBool{Val: false},
  586. },
  587. PlotVisOnly: &attrValBool{Val: false},
  588. DispBlanksAs: &attrValString{Val: formatSet.ShowBlanksAs},
  589. ShowDLblsOverMax: &attrValBool{Val: false},
  590. },
  591. SpPr: &cSpPr{
  592. SolidFill: &aSolidFill{
  593. SchemeClr: &aSchemeClr{Val: "bg1"},
  594. },
  595. Ln: &aLn{
  596. W: 9525,
  597. Cap: "flat",
  598. Cmpd: "sng",
  599. Algn: "ctr",
  600. SolidFill: &aSolidFill{
  601. SchemeClr: &aSchemeClr{Val: "tx1",
  602. LumMod: &attrValInt{
  603. Val: 15000,
  604. },
  605. LumOff: &attrValInt{
  606. Val: 85000,
  607. },
  608. },
  609. },
  610. },
  611. },
  612. PrintSettings: &cPrintSettings{
  613. PageMargins: &cPageMargins{
  614. B: 0.75,
  615. L: 0.7,
  616. R: 0.7,
  617. T: 0.7,
  618. Header: 0.3,
  619. Footer: 0.3,
  620. },
  621. },
  622. }
  623. plotAreaFunc := map[string]func(*formatChart) *cPlotArea{
  624. Area: f.drawBaseChart,
  625. AreaStacked: f.drawBaseChart,
  626. AreaPercentStacked: f.drawBaseChart,
  627. Area3D: f.drawBaseChart,
  628. Area3DStacked: f.drawBaseChart,
  629. Area3DPercentStacked: f.drawBaseChart,
  630. Bar: f.drawBaseChart,
  631. BarStacked: f.drawBaseChart,
  632. BarPercentStacked: f.drawBaseChart,
  633. Bar3DClustered: f.drawBaseChart,
  634. Bar3DStacked: f.drawBaseChart,
  635. Bar3DPercentStacked: f.drawBaseChart,
  636. Col: f.drawBaseChart,
  637. ColStacked: f.drawBaseChart,
  638. ColPercentStacked: f.drawBaseChart,
  639. Col3DClustered: f.drawBaseChart,
  640. Col3D: f.drawBaseChart,
  641. Col3DStacked: f.drawBaseChart,
  642. Col3DPercentStacked: f.drawBaseChart,
  643. Doughnut: f.drawDoughnutChart,
  644. Line: f.drawLineChart,
  645. Pie3D: f.drawPie3DChart,
  646. Pie: f.drawPieChart,
  647. Radar: f.drawRadarChart,
  648. Scatter: f.drawScatterChart,
  649. }
  650. xlsxChartSpace.Chart.PlotArea = plotAreaFunc[formatSet.Type](formatSet)
  651. chart, _ := xml.Marshal(xlsxChartSpace)
  652. media := "xl/charts/chart" + strconv.Itoa(count+1) + ".xml"
  653. f.saveFileList(media, chart)
  654. }
  655. // drawBaseChart provides a function to draw the c:plotArea element for bar,
  656. // and column series charts by given format sets.
  657. func (f *File) drawBaseChart(formatSet *formatChart) *cPlotArea {
  658. c := cCharts{
  659. BarDir: &attrValString{
  660. Val: "col",
  661. },
  662. Grouping: &attrValString{
  663. Val: "clustered",
  664. },
  665. VaryColors: &attrValBool{
  666. Val: true,
  667. },
  668. Ser: f.drawChartSeries(formatSet),
  669. DLbls: f.drawChartDLbls(formatSet),
  670. AxID: []*attrValInt{
  671. {Val: 754001152},
  672. {Val: 753999904},
  673. },
  674. }
  675. var ok bool
  676. c.BarDir.Val, ok = plotAreaChartBarDir[formatSet.Type]
  677. if !ok {
  678. c.BarDir = nil
  679. }
  680. c.Grouping.Val = plotAreaChartGrouping[formatSet.Type]
  681. if formatSet.Type == "colStacked" || formatSet.Type == "barStacked" || formatSet.Type == "barPercentStacked" || formatSet.Type == "colPercentStacked" || formatSet.Type == "areaPercentStacked" {
  682. c.Overlap = &attrValInt{Val: 100}
  683. }
  684. catAx := f.drawPlotAreaCatAx(formatSet)
  685. valAx := f.drawPlotAreaValAx(formatSet)
  686. charts := map[string]*cPlotArea{
  687. "area": {
  688. AreaChart: &c,
  689. CatAx: catAx,
  690. ValAx: valAx,
  691. },
  692. "areaStacked": {
  693. AreaChart: &c,
  694. CatAx: catAx,
  695. ValAx: valAx,
  696. },
  697. "areaPercentStacked": {
  698. AreaChart: &c,
  699. CatAx: catAx,
  700. ValAx: valAx,
  701. },
  702. "area3D": {
  703. Area3DChart: &c,
  704. CatAx: catAx,
  705. ValAx: valAx,
  706. },
  707. "area3DStacked": {
  708. Area3DChart: &c,
  709. CatAx: catAx,
  710. ValAx: valAx,
  711. },
  712. "area3DPercentStacked": {
  713. Area3DChart: &c,
  714. CatAx: catAx,
  715. ValAx: valAx,
  716. },
  717. "bar": {
  718. BarChart: &c,
  719. CatAx: catAx,
  720. ValAx: valAx,
  721. },
  722. "barStacked": {
  723. BarChart: &c,
  724. CatAx: catAx,
  725. ValAx: valAx,
  726. },
  727. "barPercentStacked": {
  728. BarChart: &c,
  729. CatAx: catAx,
  730. ValAx: valAx,
  731. },
  732. "bar3DClustered": {
  733. Bar3DChart: &c,
  734. CatAx: catAx,
  735. ValAx: valAx,
  736. },
  737. "bar3DStacked": {
  738. Bar3DChart: &c,
  739. CatAx: catAx,
  740. ValAx: valAx,
  741. },
  742. "bar3DPercentStacked": {
  743. Bar3DChart: &c,
  744. CatAx: catAx,
  745. ValAx: valAx,
  746. },
  747. "col": {
  748. BarChart: &c,
  749. CatAx: catAx,
  750. ValAx: valAx,
  751. },
  752. "colStacked": {
  753. BarChart: &c,
  754. CatAx: catAx,
  755. ValAx: valAx,
  756. },
  757. "colPercentStacked": {
  758. BarChart: &c,
  759. CatAx: catAx,
  760. ValAx: valAx,
  761. },
  762. "col3DClustered": {
  763. Bar3DChart: &c,
  764. CatAx: catAx,
  765. ValAx: valAx,
  766. },
  767. "col3D": {
  768. Bar3DChart: &c,
  769. CatAx: catAx,
  770. ValAx: valAx,
  771. },
  772. "col3DStacked": {
  773. Bar3DChart: &c,
  774. CatAx: catAx,
  775. ValAx: valAx,
  776. },
  777. "col3DPercentStacked": {
  778. Bar3DChart: &c,
  779. CatAx: catAx,
  780. ValAx: valAx,
  781. },
  782. }
  783. return charts[formatSet.Type]
  784. }
  785. // drawDoughnutChart provides a function to draw the c:plotArea element for
  786. // doughnut chart by given format sets.
  787. func (f *File) drawDoughnutChart(formatSet *formatChart) *cPlotArea {
  788. return &cPlotArea{
  789. DoughnutChart: &cCharts{
  790. VaryColors: &attrValBool{
  791. Val: true,
  792. },
  793. Ser: f.drawChartSeries(formatSet),
  794. HoleSize: &attrValInt{Val: 75},
  795. },
  796. }
  797. }
  798. // drawLineChart provides a function to draw the c:plotArea element for line
  799. // chart by given format sets.
  800. func (f *File) drawLineChart(formatSet *formatChart) *cPlotArea {
  801. return &cPlotArea{
  802. LineChart: &cCharts{
  803. Grouping: &attrValString{
  804. Val: plotAreaChartGrouping[formatSet.Type],
  805. },
  806. VaryColors: &attrValBool{
  807. Val: false,
  808. },
  809. Ser: f.drawChartSeries(formatSet),
  810. DLbls: f.drawChartDLbls(formatSet),
  811. Smooth: &attrValBool{
  812. Val: false,
  813. },
  814. AxID: []*attrValInt{
  815. {Val: 754001152},
  816. {Val: 753999904},
  817. },
  818. },
  819. CatAx: f.drawPlotAreaCatAx(formatSet),
  820. ValAx: f.drawPlotAreaValAx(formatSet),
  821. }
  822. }
  823. // drawPieChart provides a function to draw the c:plotArea element for pie
  824. // chart by given format sets.
  825. func (f *File) drawPieChart(formatSet *formatChart) *cPlotArea {
  826. return &cPlotArea{
  827. PieChart: &cCharts{
  828. VaryColors: &attrValBool{
  829. Val: true,
  830. },
  831. Ser: f.drawChartSeries(formatSet),
  832. },
  833. }
  834. }
  835. // drawPie3DChart provides a function to draw the c:plotArea element for 3D
  836. // pie chart by given format sets.
  837. func (f *File) drawPie3DChart(formatSet *formatChart) *cPlotArea {
  838. return &cPlotArea{
  839. Pie3DChart: &cCharts{
  840. VaryColors: &attrValBool{
  841. Val: true,
  842. },
  843. Ser: f.drawChartSeries(formatSet),
  844. },
  845. }
  846. }
  847. // drawRadarChart provides a function to draw the c:plotArea element for radar
  848. // chart by given format sets.
  849. func (f *File) drawRadarChart(formatSet *formatChart) *cPlotArea {
  850. return &cPlotArea{
  851. RadarChart: &cCharts{
  852. RadarStyle: &attrValString{
  853. Val: "marker",
  854. },
  855. VaryColors: &attrValBool{
  856. Val: false,
  857. },
  858. Ser: f.drawChartSeries(formatSet),
  859. DLbls: f.drawChartDLbls(formatSet),
  860. AxID: []*attrValInt{
  861. {Val: 754001152},
  862. {Val: 753999904},
  863. },
  864. },
  865. CatAx: f.drawPlotAreaCatAx(formatSet),
  866. ValAx: f.drawPlotAreaValAx(formatSet),
  867. }
  868. }
  869. // drawScatterChart provides a function to draw the c:plotArea element for
  870. // scatter chart by given format sets.
  871. func (f *File) drawScatterChart(formatSet *formatChart) *cPlotArea {
  872. return &cPlotArea{
  873. ScatterChart: &cCharts{
  874. ScatterStyle: &attrValString{
  875. Val: "smoothMarker", // line,lineMarker,marker,none,smooth,smoothMarker
  876. },
  877. VaryColors: &attrValBool{
  878. Val: false,
  879. },
  880. Ser: f.drawChartSeries(formatSet),
  881. DLbls: f.drawChartDLbls(formatSet),
  882. AxID: []*attrValInt{
  883. {Val: 754001152},
  884. {Val: 753999904},
  885. },
  886. },
  887. CatAx: f.drawPlotAreaCatAx(formatSet),
  888. ValAx: f.drawPlotAreaValAx(formatSet),
  889. }
  890. }
  891. // drawChartSeries provides a function to draw the c:ser element by given
  892. // format sets.
  893. func (f *File) drawChartSeries(formatSet *formatChart) *[]cSer {
  894. ser := []cSer{}
  895. for k := range formatSet.Series {
  896. ser = append(ser, cSer{
  897. IDx: &attrValInt{Val: k},
  898. Order: &attrValInt{Val: k},
  899. Tx: &cTx{
  900. StrRef: &cStrRef{
  901. F: formatSet.Series[k].Name,
  902. },
  903. },
  904. SpPr: f.drawChartSeriesSpPr(k, formatSet),
  905. Marker: f.drawChartSeriesMarker(k, formatSet),
  906. DPt: f.drawChartSeriesDPt(k, formatSet),
  907. DLbls: f.drawChartSeriesDLbls(formatSet),
  908. Cat: f.drawChartSeriesCat(formatSet.Series[k], formatSet),
  909. Val: f.drawChartSeriesVal(formatSet.Series[k], formatSet),
  910. XVal: f.drawChartSeriesXVal(formatSet.Series[k], formatSet),
  911. YVal: f.drawChartSeriesYVal(formatSet.Series[k], formatSet),
  912. })
  913. }
  914. return &ser
  915. }
  916. // drawChartSeriesSpPr provides a function to draw the c:spPr element by given
  917. // format sets.
  918. func (f *File) drawChartSeriesSpPr(i int, formatSet *formatChart) *cSpPr {
  919. spPrScatter := &cSpPr{
  920. Ln: &aLn{
  921. W: 25400,
  922. NoFill: " ",
  923. },
  924. }
  925. spPrLine := &cSpPr{
  926. Ln: &aLn{
  927. W: 25400,
  928. Cap: "rnd", // rnd, sq, flat
  929. SolidFill: &aSolidFill{
  930. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
  931. },
  932. },
  933. }
  934. chartSeriesSpPr := map[string]*cSpPr{Area: nil, AreaStacked: nil, AreaPercentStacked: nil, Area3D: nil, Area3DStacked: nil, Area3DPercentStacked: nil, Bar: nil, BarStacked: nil, BarPercentStacked: nil, Bar3DClustered: nil, Bar3DStacked: nil, Bar3DPercentStacked: nil, Col: nil, ColStacked: nil, ColPercentStacked: nil, Col3DClustered: nil, Col3D: nil, Col3DStacked: nil, Col3DPercentStacked: nil, Doughnut: nil, Line: spPrLine, Pie: nil, Pie3D: nil, Radar: nil, Scatter: spPrScatter}
  935. return chartSeriesSpPr[formatSet.Type]
  936. }
  937. // drawChartSeriesDPt provides a function to draw the c:dPt element by given
  938. // data index and format sets.
  939. func (f *File) drawChartSeriesDPt(i int, formatSet *formatChart) []*cDPt {
  940. dpt := []*cDPt{{
  941. IDx: &attrValInt{Val: i},
  942. Bubble3D: &attrValBool{Val: false},
  943. SpPr: &cSpPr{
  944. SolidFill: &aSolidFill{
  945. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
  946. },
  947. Ln: &aLn{
  948. W: 25400,
  949. Cap: "rnd",
  950. SolidFill: &aSolidFill{
  951. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  952. },
  953. },
  954. Sp3D: &aSp3D{
  955. ContourW: 25400,
  956. ContourClr: &aContourClr{
  957. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  958. },
  959. },
  960. },
  961. }}
  962. chartSeriesDPt := map[string][]*cDPt{Area: nil, AreaStacked: nil, AreaPercentStacked: nil, Area3D: nil, Area3DStacked: nil, Area3DPercentStacked: nil, Bar: nil, BarStacked: nil, BarPercentStacked: nil, Bar3DClustered: nil, Bar3DStacked: nil, Bar3DPercentStacked: nil, Col: nil, ColStacked: nil, ColPercentStacked: nil, Col3DClustered: nil, Col3D: nil, Col3DStacked: nil, Col3DPercentStacked: nil, Doughnut: nil, Line: nil, Pie: dpt, Pie3D: dpt, Radar: nil, Scatter: nil}
  963. return chartSeriesDPt[formatSet.Type]
  964. }
  965. // drawChartSeriesCat provides a function to draw the c:cat element by given
  966. // chart series and format sets.
  967. func (f *File) drawChartSeriesCat(v formatChartSeries, formatSet *formatChart) *cCat {
  968. cat := &cCat{
  969. StrRef: &cStrRef{
  970. F: v.Categories,
  971. },
  972. }
  973. chartSeriesCat := map[string]*cCat{Area: cat, AreaStacked: cat, AreaPercentStacked: cat, Area3D: cat, Area3DStacked: cat, Area3DPercentStacked: cat, Bar: cat, BarStacked: cat, BarPercentStacked: cat, Bar3DClustered: cat, Bar3DStacked: cat, Bar3DPercentStacked: cat, Col: cat, ColStacked: cat, ColPercentStacked: cat, Col3DClustered: cat, Col3D: cat, Col3DStacked: cat, Col3DPercentStacked: cat, Doughnut: cat, Line: cat, Pie: cat, Pie3D: cat, Radar: cat, Scatter: nil}
  974. return chartSeriesCat[formatSet.Type]
  975. }
  976. // drawChartSeriesVal provides a function to draw the c:val element by given
  977. // chart series and format sets.
  978. func (f *File) drawChartSeriesVal(v formatChartSeries, formatSet *formatChart) *cVal {
  979. val := &cVal{
  980. NumRef: &cNumRef{
  981. F: v.Values,
  982. },
  983. }
  984. chartSeriesVal := map[string]*cVal{Area: val, AreaStacked: val, AreaPercentStacked: val, Area3D: val, Area3DStacked: val, Area3DPercentStacked: val, Bar: val, BarStacked: val, BarPercentStacked: val, Bar3DClustered: val, Bar3DStacked: val, Bar3DPercentStacked: val, Col: val, ColStacked: val, ColPercentStacked: val, Col3DClustered: val, Col3D: val, Col3DStacked: val, Col3DPercentStacked: val, Doughnut: val, Line: val, Pie: val, Pie3D: val, Radar: val, Scatter: nil}
  985. return chartSeriesVal[formatSet.Type]
  986. }
  987. // drawChartSeriesMarker provides a function to draw the c:marker element by
  988. // given data index and format sets.
  989. func (f *File) drawChartSeriesMarker(i int, formatSet *formatChart) *cMarker {
  990. marker := &cMarker{
  991. Symbol: &attrValString{Val: "circle"},
  992. Size: &attrValInt{Val: 5},
  993. SpPr: &cSpPr{
  994. SolidFill: &aSolidFill{
  995. SchemeClr: &aSchemeClr{
  996. Val: "accent" + strconv.Itoa(i+1),
  997. },
  998. },
  999. Ln: &aLn{
  1000. W: 9252,
  1001. SolidFill: &aSolidFill{
  1002. SchemeClr: &aSchemeClr{
  1003. Val: "accent" + strconv.Itoa(i+1),
  1004. },
  1005. },
  1006. },
  1007. },
  1008. }
  1009. chartSeriesMarker := map[string]*cMarker{Area: nil, AreaStacked: nil, AreaPercentStacked: nil, Area3D: nil, Area3DStacked: nil, Area3DPercentStacked: nil, Bar: nil, BarStacked: nil, BarPercentStacked: nil, Bar3DClustered: nil, Bar3DStacked: nil, Bar3DPercentStacked: nil, Col: nil, ColStacked: nil, ColPercentStacked: nil, Col3DClustered: nil, Col3D: nil, Col3DStacked: nil, Col3DPercentStacked: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: marker}
  1010. return chartSeriesMarker[formatSet.Type]
  1011. }
  1012. // drawChartSeriesXVal provides a function to draw the c:xVal element by given
  1013. // chart series and format sets.
  1014. func (f *File) drawChartSeriesXVal(v formatChartSeries, formatSet *formatChart) *cCat {
  1015. cat := &cCat{
  1016. StrRef: &cStrRef{
  1017. F: v.Categories,
  1018. },
  1019. }
  1020. chartSeriesXVal := map[string]*cCat{Area: nil, AreaStacked: nil, AreaPercentStacked: nil, Area3D: nil, Area3DStacked: nil, Area3DPercentStacked: nil, Bar: nil, BarStacked: nil, BarPercentStacked: nil, Bar3DClustered: nil, Bar3DStacked: nil, Bar3DPercentStacked: nil, Col: nil, ColStacked: nil, ColPercentStacked: nil, Col3DClustered: nil, Col3D: nil, Col3DStacked: nil, Col3DPercentStacked: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: cat}
  1021. return chartSeriesXVal[formatSet.Type]
  1022. }
  1023. // drawChartSeriesYVal provides a function to draw the c:yVal element by given
  1024. // chart series and format sets.
  1025. func (f *File) drawChartSeriesYVal(v formatChartSeries, formatSet *formatChart) *cVal {
  1026. val := &cVal{
  1027. NumRef: &cNumRef{
  1028. F: v.Values,
  1029. },
  1030. }
  1031. chartSeriesYVal := map[string]*cVal{Area: nil, AreaStacked: nil, AreaPercentStacked: nil, Area3D: nil, Area3DStacked: nil, Area3DPercentStacked: nil, Bar: nil, BarStacked: nil, BarPercentStacked: nil, Bar3DClustered: nil, Bar3DStacked: nil, Bar3DPercentStacked: nil, Col: nil, ColStacked: nil, ColPercentStacked: nil, Col3DClustered: nil, Col3D: nil, Col3DStacked: nil, Col3DPercentStacked: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: val}
  1032. return chartSeriesYVal[formatSet.Type]
  1033. }
  1034. // drawChartDLbls provides a function to draw the c:dLbls element by given
  1035. // format sets.
  1036. func (f *File) drawChartDLbls(formatSet *formatChart) *cDLbls {
  1037. return &cDLbls{
  1038. ShowLegendKey: &attrValBool{Val: formatSet.Legend.ShowLegendKey},
  1039. ShowVal: &attrValBool{Val: formatSet.Plotarea.ShowVal},
  1040. ShowCatName: &attrValBool{Val: formatSet.Plotarea.ShowCatName},
  1041. ShowSerName: &attrValBool{Val: formatSet.Plotarea.ShowSerName},
  1042. ShowBubbleSize: &attrValBool{Val: formatSet.Plotarea.ShowBubbleSize},
  1043. ShowPercent: &attrValBool{Val: formatSet.Plotarea.ShowPercent},
  1044. ShowLeaderLines: &attrValBool{Val: formatSet.Plotarea.ShowLeaderLines},
  1045. }
  1046. }
  1047. // drawChartSeriesDLbls provides a function to draw the c:dLbls element by
  1048. // given format sets.
  1049. func (f *File) drawChartSeriesDLbls(formatSet *formatChart) *cDLbls {
  1050. dLbls := f.drawChartDLbls(formatSet)
  1051. chartSeriesDLbls := map[string]*cDLbls{Area: dLbls, AreaStacked: dLbls, AreaPercentStacked: dLbls, Area3D: dLbls, Area3DStacked: dLbls, Area3DPercentStacked: dLbls, Bar: dLbls, BarStacked: dLbls, BarPercentStacked: dLbls, Bar3DClustered: dLbls, Bar3DStacked: dLbls, Bar3DPercentStacked: dLbls, Col: dLbls, ColStacked: dLbls, ColPercentStacked: dLbls, Col3DClustered: dLbls, Col3D: dLbls, Col3DStacked: dLbls, Col3DPercentStacked: dLbls, Doughnut: dLbls, Line: dLbls, Pie: dLbls, Pie3D: dLbls, Radar: dLbls, Scatter: nil}
  1052. return chartSeriesDLbls[formatSet.Type]
  1053. }
  1054. // drawPlotAreaCatAx provides a function to draw the c:catAx element.
  1055. func (f *File) drawPlotAreaCatAx(formatSet *formatChart) []*cAxs {
  1056. min := &attrValFloat{Val: formatSet.XAxis.Minimum}
  1057. max := &attrValFloat{Val: formatSet.XAxis.Maximum}
  1058. if formatSet.XAxis.Minimum == 0 {
  1059. min = nil
  1060. }
  1061. if formatSet.XAxis.Maximum == 0 {
  1062. max = nil
  1063. }
  1064. return []*cAxs{
  1065. {
  1066. AxID: &attrValInt{Val: 754001152},
  1067. Scaling: &cScaling{
  1068. Orientation: &attrValString{Val: orientation[formatSet.XAxis.ReverseOrder]},
  1069. Max: max,
  1070. Min: min,
  1071. },
  1072. Delete: &attrValBool{Val: false},
  1073. AxPos: &attrValString{Val: catAxPos[formatSet.XAxis.ReverseOrder]},
  1074. NumFmt: &cNumFmt{
  1075. FormatCode: "General",
  1076. SourceLinked: true,
  1077. },
  1078. MajorTickMark: &attrValString{Val: "none"},
  1079. MinorTickMark: &attrValString{Val: "none"},
  1080. TickLblPos: &attrValString{Val: "nextTo"},
  1081. SpPr: f.drawPlotAreaSpPr(),
  1082. TxPr: f.drawPlotAreaTxPr(),
  1083. CrossAx: &attrValInt{Val: 753999904},
  1084. Crosses: &attrValString{Val: "autoZero"},
  1085. Auto: &attrValBool{Val: true},
  1086. LblAlgn: &attrValString{Val: "ctr"},
  1087. LblOffset: &attrValInt{Val: 100},
  1088. NoMultiLvlLbl: &attrValBool{Val: false},
  1089. },
  1090. }
  1091. }
  1092. // drawPlotAreaValAx provides a function to draw the c:valAx element.
  1093. func (f *File) drawPlotAreaValAx(formatSet *formatChart) []*cAxs {
  1094. min := &attrValFloat{Val: formatSet.YAxis.Minimum}
  1095. max := &attrValFloat{Val: formatSet.YAxis.Maximum}
  1096. if formatSet.YAxis.Minimum == 0 {
  1097. min = nil
  1098. }
  1099. if formatSet.YAxis.Maximum == 0 {
  1100. max = nil
  1101. }
  1102. return []*cAxs{
  1103. {
  1104. AxID: &attrValInt{Val: 753999904},
  1105. Scaling: &cScaling{
  1106. Orientation: &attrValString{Val: orientation[formatSet.YAxis.ReverseOrder]},
  1107. Max: max,
  1108. Min: min,
  1109. },
  1110. Delete: &attrValBool{Val: false},
  1111. AxPos: &attrValString{Val: valAxPos[formatSet.YAxis.ReverseOrder]},
  1112. NumFmt: &cNumFmt{
  1113. FormatCode: chartValAxNumFmtFormatCode[formatSet.Type],
  1114. SourceLinked: true,
  1115. },
  1116. MajorTickMark: &attrValString{Val: "none"},
  1117. MinorTickMark: &attrValString{Val: "none"},
  1118. TickLblPos: &attrValString{Val: "nextTo"},
  1119. SpPr: f.drawPlotAreaSpPr(),
  1120. TxPr: f.drawPlotAreaTxPr(),
  1121. CrossAx: &attrValInt{Val: 754001152},
  1122. Crosses: &attrValString{Val: "autoZero"},
  1123. CrossBetween: &attrValString{Val: chartValAxCrossBetween[formatSet.Type]},
  1124. },
  1125. }
  1126. }
  1127. // drawPlotAreaSpPr provides a function to draw the c:spPr element.
  1128. func (f *File) drawPlotAreaSpPr() *cSpPr {
  1129. return &cSpPr{
  1130. Ln: &aLn{
  1131. W: 9525,
  1132. Cap: "flat",
  1133. Cmpd: "sng",
  1134. Algn: "ctr",
  1135. SolidFill: &aSolidFill{
  1136. SchemeClr: &aSchemeClr{
  1137. Val: "tx1",
  1138. LumMod: &attrValInt{Val: 15000},
  1139. LumOff: &attrValInt{Val: 85000},
  1140. },
  1141. },
  1142. },
  1143. }
  1144. }
  1145. // drawPlotAreaTxPr provides a function to draw the c:txPr element.
  1146. func (f *File) drawPlotAreaTxPr() *cTxPr {
  1147. return &cTxPr{
  1148. BodyPr: aBodyPr{
  1149. Rot: -60000000,
  1150. SpcFirstLastPara: true,
  1151. VertOverflow: "ellipsis",
  1152. Vert: "horz",
  1153. Wrap: "square",
  1154. Anchor: "ctr",
  1155. AnchorCtr: true,
  1156. },
  1157. P: aP{
  1158. PPr: &aPPr{
  1159. DefRPr: aRPr{
  1160. Sz: 900,
  1161. B: false,
  1162. I: false,
  1163. U: "none",
  1164. Strike: "noStrike",
  1165. Kern: 1200,
  1166. Baseline: 0,
  1167. SolidFill: &aSolidFill{
  1168. SchemeClr: &aSchemeClr{
  1169. Val: "tx1",
  1170. LumMod: &attrValInt{Val: 15000},
  1171. LumOff: &attrValInt{Val: 85000},
  1172. },
  1173. },
  1174. Latin: &aLatin{Typeface: "+mn-lt"},
  1175. Ea: &aEa{Typeface: "+mn-ea"},
  1176. Cs: &aCs{Typeface: "+mn-cs"},
  1177. },
  1178. },
  1179. EndParaRPr: &aEndParaRPr{Lang: "en-US"},
  1180. },
  1181. }
  1182. }
  1183. // drawingParser provides a function to parse drawingXML. In order to solve
  1184. // the problem that the label structure is changed after serialization and
  1185. // deserialization, two different structures: decodeWsDr and encodeWsDr are
  1186. // defined.
  1187. func (f *File) drawingParser(path string) (*xlsxWsDr, int) {
  1188. cNvPrID := 1
  1189. if f.Drawings[path] == nil {
  1190. content := xlsxWsDr{}
  1191. content.A = NameSpaceDrawingML
  1192. content.Xdr = NameSpaceDrawingMLSpreadSheet
  1193. _, ok := f.XLSX[path]
  1194. if ok { // Append Model
  1195. decodeWsDr := decodeWsDr{}
  1196. _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(path)), &decodeWsDr)
  1197. content.R = decodeWsDr.R
  1198. cNvPrID = len(decodeWsDr.OneCellAnchor) + len(decodeWsDr.TwoCellAnchor) + 1
  1199. for _, v := range decodeWsDr.OneCellAnchor {
  1200. content.OneCellAnchor = append(content.OneCellAnchor, &xdrCellAnchor{
  1201. EditAs: v.EditAs,
  1202. GraphicFrame: v.Content,
  1203. })
  1204. }
  1205. for _, v := range decodeWsDr.TwoCellAnchor {
  1206. content.TwoCellAnchor = append(content.TwoCellAnchor, &xdrCellAnchor{
  1207. EditAs: v.EditAs,
  1208. GraphicFrame: v.Content,
  1209. })
  1210. }
  1211. }
  1212. f.Drawings[path] = &content
  1213. }
  1214. return f.Drawings[path], cNvPrID
  1215. }
  1216. // addDrawingChart provides a function to add chart graphic frame by given
  1217. // sheet, drawingXML, cell, width, height, relationship index and format sets.
  1218. func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rID int, formatSet *formatPicture) error {
  1219. col, row, err := CellNameToCoordinates(cell)
  1220. if err != nil {
  1221. return err
  1222. }
  1223. colIdx := col - 1
  1224. rowIdx := row - 1
  1225. width = int(float64(width) * formatSet.XScale)
  1226. height = int(float64(height) * formatSet.YScale)
  1227. colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
  1228. f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.OffsetX, formatSet.OffsetY, width, height)
  1229. content, cNvPrID := f.drawingParser(drawingXML)
  1230. twoCellAnchor := xdrCellAnchor{}
  1231. twoCellAnchor.EditAs = formatSet.Positioning
  1232. from := xlsxFrom{}
  1233. from.Col = colStart
  1234. from.ColOff = formatSet.OffsetX * EMU
  1235. from.Row = rowStart
  1236. from.RowOff = formatSet.OffsetY * EMU
  1237. to := xlsxTo{}
  1238. to.Col = colEnd
  1239. to.ColOff = x2 * EMU
  1240. to.Row = rowEnd
  1241. to.RowOff = y2 * EMU
  1242. twoCellAnchor.From = &from
  1243. twoCellAnchor.To = &to
  1244. graphicFrame := xlsxGraphicFrame{
  1245. NvGraphicFramePr: xlsxNvGraphicFramePr{
  1246. CNvPr: &xlsxCNvPr{
  1247. ID: f.countCharts() + f.countMedia() + 1,
  1248. Name: "Chart " + strconv.Itoa(cNvPrID),
  1249. },
  1250. },
  1251. Graphic: &xlsxGraphic{
  1252. GraphicData: &xlsxGraphicData{
  1253. URI: NameSpaceDrawingMLChart,
  1254. Chart: &xlsxChart{
  1255. C: NameSpaceDrawingMLChart,
  1256. R: SourceRelationship,
  1257. RID: "rId" + strconv.Itoa(rID),
  1258. },
  1259. },
  1260. },
  1261. }
  1262. graphic, _ := xml.Marshal(graphicFrame)
  1263. twoCellAnchor.GraphicFrame = string(graphic)
  1264. twoCellAnchor.ClientData = &xdrClientData{
  1265. FLocksWithSheet: formatSet.FLocksWithSheet,
  1266. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  1267. }
  1268. content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
  1269. f.Drawings[drawingXML] = content
  1270. return err
  1271. }