drawing.go 38 KB

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