drawing.go 38 KB

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