drawing.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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. f.addSheetNameSpace(sheet, SourceRelationship)
  47. xlsx.Drawing = &xlsxDrawing{
  48. RID: "rId" + strconv.Itoa(rID),
  49. }
  50. return
  51. }
  52. // addChart provides a function to create chart as xl/charts/chart%d.xml by
  53. // given format sets.
  54. func (f *File) addChart(formatSet *formatChart, comboCharts []*formatChart) {
  55. count := f.countCharts()
  56. xlsxChartSpace := xlsxChartSpace{
  57. XMLNSa: NameSpaceDrawingML.Value,
  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. SolidFill: &aSolidFill{
  754. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa((formatSet.order+i)%6+1)},
  755. },
  756. },
  757. }
  758. chartSeriesSpPr := map[string]*cSpPr{Line: spPrLine, Scatter: spPrScatter}
  759. return chartSeriesSpPr[formatSet.Type]
  760. }
  761. // drawChartSeriesDPt provides a function to draw the c:dPt element by given
  762. // data index and format sets.
  763. func (f *File) drawChartSeriesDPt(i int, formatSet *formatChart) []*cDPt {
  764. dpt := []*cDPt{{
  765. IDx: &attrValInt{Val: intPtr(i)},
  766. Bubble3D: &attrValBool{Val: boolPtr(false)},
  767. SpPr: &cSpPr{
  768. SolidFill: &aSolidFill{
  769. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
  770. },
  771. Ln: &aLn{
  772. W: 25400,
  773. Cap: "rnd",
  774. SolidFill: &aSolidFill{
  775. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  776. },
  777. },
  778. Sp3D: &aSp3D{
  779. ContourW: 25400,
  780. ContourClr: &aContourClr{
  781. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  782. },
  783. },
  784. },
  785. }}
  786. chartSeriesDPt := map[string][]*cDPt{Pie: dpt, Pie3D: dpt}
  787. return chartSeriesDPt[formatSet.Type]
  788. }
  789. // drawChartSeriesCat provides a function to draw the c:cat element by given
  790. // chart series and format sets.
  791. func (f *File) drawChartSeriesCat(v formatChartSeries, formatSet *formatChart) *cCat {
  792. cat := &cCat{
  793. StrRef: &cStrRef{
  794. F: v.Categories,
  795. },
  796. }
  797. chartSeriesCat := map[string]*cCat{Scatter: nil, Bubble: nil, Bubble3D: nil}
  798. if _, ok := chartSeriesCat[formatSet.Type]; ok || v.Categories == "" {
  799. return nil
  800. }
  801. return cat
  802. }
  803. // drawChartSeriesVal provides a function to draw the c:val element by given
  804. // chart series and format sets.
  805. func (f *File) drawChartSeriesVal(v formatChartSeries, formatSet *formatChart) *cVal {
  806. val := &cVal{
  807. NumRef: &cNumRef{
  808. F: v.Values,
  809. },
  810. }
  811. chartSeriesVal := map[string]*cVal{Scatter: nil, Bubble: nil, Bubble3D: nil}
  812. if _, ok := chartSeriesVal[formatSet.Type]; ok {
  813. return nil
  814. }
  815. return val
  816. }
  817. // drawChartSeriesMarker provides a function to draw the c:marker element by
  818. // given data index and format sets.
  819. func (f *File) drawChartSeriesMarker(i int, formatSet *formatChart) *cMarker {
  820. marker := &cMarker{
  821. Symbol: &attrValString{Val: stringPtr("circle")},
  822. Size: &attrValInt{Val: intPtr(5)},
  823. }
  824. if i < 6 {
  825. marker.SpPr = &cSpPr{
  826. SolidFill: &aSolidFill{
  827. SchemeClr: &aSchemeClr{
  828. Val: "accent" + strconv.Itoa(i+1),
  829. },
  830. },
  831. Ln: &aLn{
  832. W: 9252,
  833. SolidFill: &aSolidFill{
  834. SchemeClr: &aSchemeClr{
  835. Val: "accent" + strconv.Itoa(i+1),
  836. },
  837. },
  838. },
  839. }
  840. }
  841. chartSeriesMarker := map[string]*cMarker{Scatter: marker}
  842. return chartSeriesMarker[formatSet.Type]
  843. }
  844. // drawChartSeriesXVal provides a function to draw the c:xVal element by given
  845. // chart series and format sets.
  846. func (f *File) drawChartSeriesXVal(v formatChartSeries, formatSet *formatChart) *cCat {
  847. cat := &cCat{
  848. StrRef: &cStrRef{
  849. F: v.Categories,
  850. },
  851. }
  852. chartSeriesXVal := map[string]*cCat{Scatter: cat}
  853. return chartSeriesXVal[formatSet.Type]
  854. }
  855. // drawChartSeriesYVal provides a function to draw the c:yVal element by given
  856. // chart series and format sets.
  857. func (f *File) drawChartSeriesYVal(v formatChartSeries, formatSet *formatChart) *cVal {
  858. val := &cVal{
  859. NumRef: &cNumRef{
  860. F: v.Values,
  861. },
  862. }
  863. chartSeriesYVal := map[string]*cVal{Scatter: val, Bubble: val, Bubble3D: val}
  864. return chartSeriesYVal[formatSet.Type]
  865. }
  866. // drawCharSeriesBubbleSize provides a function to draw the c:bubbleSize
  867. // element by given chart series and format sets.
  868. func (f *File) drawCharSeriesBubbleSize(v formatChartSeries, formatSet *formatChart) *cVal {
  869. if _, ok := map[string]bool{Bubble: true, Bubble3D: true}[formatSet.Type]; !ok {
  870. return nil
  871. }
  872. return &cVal{
  873. NumRef: &cNumRef{
  874. F: v.Values,
  875. },
  876. }
  877. }
  878. // drawCharSeriesBubble3D provides a function to draw the c:bubble3D element
  879. // by given format sets.
  880. func (f *File) drawCharSeriesBubble3D(formatSet *formatChart) *attrValBool {
  881. if _, ok := map[string]bool{Bubble3D: true}[formatSet.Type]; !ok {
  882. return nil
  883. }
  884. return &attrValBool{Val: boolPtr(true)}
  885. }
  886. // drawChartDLbls provides a function to draw the c:dLbls element by given
  887. // format sets.
  888. func (f *File) drawChartDLbls(formatSet *formatChart) *cDLbls {
  889. return &cDLbls{
  890. ShowLegendKey: &attrValBool{Val: boolPtr(formatSet.Legend.ShowLegendKey)},
  891. ShowVal: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowVal)},
  892. ShowCatName: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowCatName)},
  893. ShowSerName: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowSerName)},
  894. ShowBubbleSize: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowBubbleSize)},
  895. ShowPercent: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowPercent)},
  896. ShowLeaderLines: &attrValBool{Val: boolPtr(formatSet.Plotarea.ShowLeaderLines)},
  897. }
  898. }
  899. // drawChartSeriesDLbls provides a function to draw the c:dLbls element by
  900. // given format sets.
  901. func (f *File) drawChartSeriesDLbls(formatSet *formatChart) *cDLbls {
  902. dLbls := f.drawChartDLbls(formatSet)
  903. chartSeriesDLbls := map[string]*cDLbls{Scatter: nil, Surface3D: nil, WireframeSurface3D: nil, Contour: nil, WireframeContour: nil, Bubble: nil, Bubble3D: nil}
  904. if _, ok := chartSeriesDLbls[formatSet.Type]; ok {
  905. return nil
  906. }
  907. return dLbls
  908. }
  909. // drawPlotAreaCatAx provides a function to draw the c:catAx element.
  910. func (f *File) drawPlotAreaCatAx(formatSet *formatChart) []*cAxs {
  911. min := &attrValFloat{Val: float64Ptr(formatSet.XAxis.Minimum)}
  912. max := &attrValFloat{Val: float64Ptr(formatSet.XAxis.Maximum)}
  913. if formatSet.XAxis.Minimum == 0 {
  914. min = nil
  915. }
  916. if formatSet.XAxis.Maximum == 0 {
  917. max = nil
  918. }
  919. axs := []*cAxs{
  920. {
  921. AxID: &attrValInt{Val: intPtr(754001152)},
  922. Scaling: &cScaling{
  923. Orientation: &attrValString{Val: stringPtr(orientation[formatSet.XAxis.ReverseOrder])},
  924. Max: max,
  925. Min: min,
  926. },
  927. Delete: &attrValBool{Val: boolPtr(false)},
  928. AxPos: &attrValString{Val: stringPtr(catAxPos[formatSet.XAxis.ReverseOrder])},
  929. NumFmt: &cNumFmt{
  930. FormatCode: "General",
  931. SourceLinked: true,
  932. },
  933. MajorTickMark: &attrValString{Val: stringPtr("none")},
  934. MinorTickMark: &attrValString{Val: stringPtr("none")},
  935. TickLblPos: &attrValString{Val: stringPtr("nextTo")},
  936. SpPr: f.drawPlotAreaSpPr(),
  937. TxPr: f.drawPlotAreaTxPr(),
  938. CrossAx: &attrValInt{Val: intPtr(753999904)},
  939. Crosses: &attrValString{Val: stringPtr("autoZero")},
  940. Auto: &attrValBool{Val: boolPtr(true)},
  941. LblAlgn: &attrValString{Val: stringPtr("ctr")},
  942. LblOffset: &attrValInt{Val: intPtr(100)},
  943. NoMultiLvlLbl: &attrValBool{Val: boolPtr(false)},
  944. },
  945. }
  946. if formatSet.XAxis.MajorGridlines {
  947. axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  948. }
  949. if formatSet.XAxis.MinorGridlines {
  950. axs[0].MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  951. }
  952. if formatSet.XAxis.TickLabelSkip != 0 {
  953. axs[0].TickLblSkip = &attrValInt{Val: intPtr(formatSet.XAxis.TickLabelSkip)}
  954. }
  955. return axs
  956. }
  957. // drawPlotAreaValAx provides a function to draw the c:valAx element.
  958. func (f *File) drawPlotAreaValAx(formatSet *formatChart) []*cAxs {
  959. min := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Minimum)}
  960. max := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Maximum)}
  961. if formatSet.YAxis.Minimum == 0 {
  962. min = nil
  963. }
  964. if formatSet.YAxis.Maximum == 0 {
  965. max = nil
  966. }
  967. var logBase *attrValFloat
  968. if formatSet.YAxis.LogBase >= 2 && formatSet.YAxis.LogBase <= 1000 {
  969. logBase = &attrValFloat{Val: float64Ptr(formatSet.YAxis.LogBase)}
  970. }
  971. axs := []*cAxs{
  972. {
  973. AxID: &attrValInt{Val: intPtr(753999904)},
  974. Scaling: &cScaling{
  975. LogBase: logBase,
  976. Orientation: &attrValString{Val: stringPtr(orientation[formatSet.YAxis.ReverseOrder])},
  977. Max: max,
  978. Min: min,
  979. },
  980. Delete: &attrValBool{Val: boolPtr(false)},
  981. AxPos: &attrValString{Val: stringPtr(valAxPos[formatSet.YAxis.ReverseOrder])},
  982. NumFmt: &cNumFmt{
  983. FormatCode: chartValAxNumFmtFormatCode[formatSet.Type],
  984. SourceLinked: true,
  985. },
  986. MajorTickMark: &attrValString{Val: stringPtr("none")},
  987. MinorTickMark: &attrValString{Val: stringPtr("none")},
  988. TickLblPos: &attrValString{Val: stringPtr("nextTo")},
  989. SpPr: f.drawPlotAreaSpPr(),
  990. TxPr: f.drawPlotAreaTxPr(),
  991. CrossAx: &attrValInt{Val: intPtr(754001152)},
  992. Crosses: &attrValString{Val: stringPtr("autoZero")},
  993. CrossBetween: &attrValString{Val: stringPtr(chartValAxCrossBetween[formatSet.Type])},
  994. },
  995. }
  996. if formatSet.YAxis.MajorGridlines {
  997. axs[0].MajorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  998. }
  999. if formatSet.YAxis.MinorGridlines {
  1000. axs[0].MinorGridlines = &cChartLines{SpPr: f.drawPlotAreaSpPr()}
  1001. }
  1002. if pos, ok := valTickLblPos[formatSet.Type]; ok {
  1003. axs[0].TickLblPos.Val = stringPtr(pos)
  1004. }
  1005. if formatSet.YAxis.MajorUnit != 0 {
  1006. axs[0].MajorUnit = &attrValFloat{Val: float64Ptr(formatSet.YAxis.MajorUnit)}
  1007. }
  1008. return axs
  1009. }
  1010. // drawPlotAreaSerAx provides a function to draw the c:serAx element.
  1011. func (f *File) drawPlotAreaSerAx(formatSet *formatChart) []*cAxs {
  1012. min := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Minimum)}
  1013. max := &attrValFloat{Val: float64Ptr(formatSet.YAxis.Maximum)}
  1014. if formatSet.YAxis.Minimum == 0 {
  1015. min = nil
  1016. }
  1017. if formatSet.YAxis.Maximum == 0 {
  1018. max = nil
  1019. }
  1020. return []*cAxs{
  1021. {
  1022. AxID: &attrValInt{Val: intPtr(832256642)},
  1023. Scaling: &cScaling{
  1024. Orientation: &attrValString{Val: stringPtr(orientation[formatSet.YAxis.ReverseOrder])},
  1025. Max: max,
  1026. Min: min,
  1027. },
  1028. Delete: &attrValBool{Val: boolPtr(false)},
  1029. AxPos: &attrValString{Val: stringPtr(catAxPos[formatSet.XAxis.ReverseOrder])},
  1030. TickLblPos: &attrValString{Val: stringPtr("nextTo")},
  1031. SpPr: f.drawPlotAreaSpPr(),
  1032. TxPr: f.drawPlotAreaTxPr(),
  1033. CrossAx: &attrValInt{Val: intPtr(753999904)},
  1034. },
  1035. }
  1036. }
  1037. // drawPlotAreaSpPr provides a function to draw the c:spPr element.
  1038. func (f *File) drawPlotAreaSpPr() *cSpPr {
  1039. return &cSpPr{
  1040. Ln: &aLn{
  1041. W: 9525,
  1042. Cap: "flat",
  1043. Cmpd: "sng",
  1044. Algn: "ctr",
  1045. SolidFill: &aSolidFill{
  1046. SchemeClr: &aSchemeClr{
  1047. Val: "tx1",
  1048. LumMod: &attrValInt{Val: intPtr(15000)},
  1049. LumOff: &attrValInt{Val: intPtr(85000)},
  1050. },
  1051. },
  1052. },
  1053. }
  1054. }
  1055. // drawPlotAreaTxPr provides a function to draw the c:txPr element.
  1056. func (f *File) drawPlotAreaTxPr() *cTxPr {
  1057. return &cTxPr{
  1058. BodyPr: aBodyPr{
  1059. Rot: -60000000,
  1060. SpcFirstLastPara: true,
  1061. VertOverflow: "ellipsis",
  1062. Vert: "horz",
  1063. Wrap: "square",
  1064. Anchor: "ctr",
  1065. AnchorCtr: true,
  1066. },
  1067. P: aP{
  1068. PPr: &aPPr{
  1069. DefRPr: aRPr{
  1070. Sz: 900,
  1071. B: false,
  1072. I: false,
  1073. U: "none",
  1074. Strike: "noStrike",
  1075. Kern: 1200,
  1076. Baseline: 0,
  1077. SolidFill: &aSolidFill{
  1078. SchemeClr: &aSchemeClr{
  1079. Val: "tx1",
  1080. LumMod: &attrValInt{Val: intPtr(15000)},
  1081. LumOff: &attrValInt{Val: intPtr(85000)},
  1082. },
  1083. },
  1084. Latin: &aLatin{Typeface: "+mn-lt"},
  1085. Ea: &aEa{Typeface: "+mn-ea"},
  1086. Cs: &aCs{Typeface: "+mn-cs"},
  1087. },
  1088. },
  1089. EndParaRPr: &aEndParaRPr{Lang: "en-US"},
  1090. },
  1091. }
  1092. }
  1093. // drawingParser provides a function to parse drawingXML. In order to solve
  1094. // the problem that the label structure is changed after serialization and
  1095. // deserialization, two different structures: decodeWsDr and encodeWsDr are
  1096. // defined.
  1097. func (f *File) drawingParser(path string) (*xlsxWsDr, int) {
  1098. var (
  1099. err error
  1100. ok bool
  1101. )
  1102. if f.Drawings[path] == nil {
  1103. content := xlsxWsDr{}
  1104. content.A = NameSpaceDrawingML.Value
  1105. content.Xdr = NameSpaceDrawingMLSpreadSheet.Value
  1106. if _, ok = f.XLSX[path]; ok { // Append Model
  1107. decodeWsDr := decodeWsDr{}
  1108. if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(path)))).
  1109. Decode(&decodeWsDr); err != nil && err != io.EOF {
  1110. log.Printf("xml decode error: %s", err)
  1111. }
  1112. content.R = decodeWsDr.R
  1113. for _, v := range decodeWsDr.OneCellAnchor {
  1114. content.OneCellAnchor = append(content.OneCellAnchor, &xdrCellAnchor{
  1115. EditAs: v.EditAs,
  1116. GraphicFrame: v.Content,
  1117. })
  1118. }
  1119. for _, v := range decodeWsDr.TwoCellAnchor {
  1120. content.TwoCellAnchor = append(content.TwoCellAnchor, &xdrCellAnchor{
  1121. EditAs: v.EditAs,
  1122. GraphicFrame: v.Content,
  1123. })
  1124. }
  1125. }
  1126. f.Drawings[path] = &content
  1127. }
  1128. wsDr := f.Drawings[path]
  1129. return wsDr, len(wsDr.OneCellAnchor) + len(wsDr.TwoCellAnchor) + 2
  1130. }
  1131. // addDrawingChart provides a function to add chart graphic frame by given
  1132. // sheet, drawingXML, cell, width, height, relationship index and format sets.
  1133. func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rID int, formatSet *formatPicture) error {
  1134. col, row, err := CellNameToCoordinates(cell)
  1135. if err != nil {
  1136. return err
  1137. }
  1138. colIdx := col - 1
  1139. rowIdx := row - 1
  1140. width = int(float64(width) * formatSet.XScale)
  1141. height = int(float64(height) * formatSet.YScale)
  1142. colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
  1143. f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.OffsetX, formatSet.OffsetY, width, height)
  1144. content, cNvPrID := f.drawingParser(drawingXML)
  1145. twoCellAnchor := xdrCellAnchor{}
  1146. twoCellAnchor.EditAs = formatSet.Positioning
  1147. from := xlsxFrom{}
  1148. from.Col = colStart
  1149. from.ColOff = formatSet.OffsetX * EMU
  1150. from.Row = rowStart
  1151. from.RowOff = formatSet.OffsetY * EMU
  1152. to := xlsxTo{}
  1153. to.Col = colEnd
  1154. to.ColOff = x2 * EMU
  1155. to.Row = rowEnd
  1156. to.RowOff = y2 * EMU
  1157. twoCellAnchor.From = &from
  1158. twoCellAnchor.To = &to
  1159. graphicFrame := xlsxGraphicFrame{
  1160. NvGraphicFramePr: xlsxNvGraphicFramePr{
  1161. CNvPr: &xlsxCNvPr{
  1162. ID: cNvPrID,
  1163. Name: "Chart " + strconv.Itoa(cNvPrID),
  1164. },
  1165. },
  1166. Graphic: &xlsxGraphic{
  1167. GraphicData: &xlsxGraphicData{
  1168. URI: NameSpaceDrawingMLChart.Value,
  1169. Chart: &xlsxChart{
  1170. C: NameSpaceDrawingMLChart.Value,
  1171. R: SourceRelationship.Value,
  1172. RID: "rId" + strconv.Itoa(rID),
  1173. },
  1174. },
  1175. },
  1176. }
  1177. graphic, _ := xml.Marshal(graphicFrame)
  1178. twoCellAnchor.GraphicFrame = string(graphic)
  1179. twoCellAnchor.ClientData = &xdrClientData{
  1180. FLocksWithSheet: formatSet.FLocksWithSheet,
  1181. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  1182. }
  1183. content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
  1184. f.Drawings[drawingXML] = content
  1185. return err
  1186. }
  1187. // addSheetDrawingChart provides a function to add chart graphic frame for
  1188. // chartsheet by given sheet, drawingXML, width, height, relationship index
  1189. // and format sets.
  1190. func (f *File) addSheetDrawingChart(drawingXML string, rID int, formatSet *formatPicture) {
  1191. content, cNvPrID := f.drawingParser(drawingXML)
  1192. absoluteAnchor := xdrCellAnchor{
  1193. EditAs: formatSet.Positioning,
  1194. Pos: &xlsxPoint2D{},
  1195. Ext: &xlsxExt{},
  1196. }
  1197. graphicFrame := xlsxGraphicFrame{
  1198. NvGraphicFramePr: xlsxNvGraphicFramePr{
  1199. CNvPr: &xlsxCNvPr{
  1200. ID: cNvPrID,
  1201. Name: "Chart " + strconv.Itoa(cNvPrID),
  1202. },
  1203. },
  1204. Graphic: &xlsxGraphic{
  1205. GraphicData: &xlsxGraphicData{
  1206. URI: NameSpaceDrawingMLChart.Value,
  1207. Chart: &xlsxChart{
  1208. C: NameSpaceDrawingMLChart.Value,
  1209. R: SourceRelationship.Value,
  1210. RID: "rId" + strconv.Itoa(rID),
  1211. },
  1212. },
  1213. },
  1214. }
  1215. graphic, _ := xml.Marshal(graphicFrame)
  1216. absoluteAnchor.GraphicFrame = string(graphic)
  1217. absoluteAnchor.ClientData = &xdrClientData{
  1218. FLocksWithSheet: formatSet.FLocksWithSheet,
  1219. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  1220. }
  1221. content.AbsoluteAnchor = append(content.AbsoluteAnchor, &absoluteAnchor)
  1222. f.Drawings[drawingXML] = content
  1223. return
  1224. }
  1225. // deleteDrawing provides a function to delete chart graphic frame by given by
  1226. // given coordinates and graphic type.
  1227. func (f *File) deleteDrawing(col, row int, drawingXML, drawingType string) (err error) {
  1228. var (
  1229. wsDr *xlsxWsDr
  1230. deTwoCellAnchor *decodeTwoCellAnchor
  1231. )
  1232. xdrCellAnchorFuncs := map[string]func(anchor *xdrCellAnchor) bool{
  1233. "Chart": func(anchor *xdrCellAnchor) bool { return anchor.Pic == nil },
  1234. "Pic": func(anchor *xdrCellAnchor) bool { return anchor.Pic != nil },
  1235. }
  1236. decodeTwoCellAnchorFuncs := map[string]func(anchor *decodeTwoCellAnchor) bool{
  1237. "Chart": func(anchor *decodeTwoCellAnchor) bool { return anchor.Pic == nil },
  1238. "Pic": func(anchor *decodeTwoCellAnchor) bool { return anchor.Pic != nil },
  1239. }
  1240. wsDr, _ = f.drawingParser(drawingXML)
  1241. for idx := 0; idx < len(wsDr.TwoCellAnchor); idx++ {
  1242. if err = nil; wsDr.TwoCellAnchor[idx].From != nil && xdrCellAnchorFuncs[drawingType](wsDr.TwoCellAnchor[idx]) {
  1243. if wsDr.TwoCellAnchor[idx].From.Col == col && wsDr.TwoCellAnchor[idx].From.Row == row {
  1244. wsDr.TwoCellAnchor = append(wsDr.TwoCellAnchor[:idx], wsDr.TwoCellAnchor[idx+1:]...)
  1245. idx--
  1246. }
  1247. }
  1248. }
  1249. for idx := 0; idx < len(wsDr.TwoCellAnchor); idx++ {
  1250. deTwoCellAnchor = new(decodeTwoCellAnchor)
  1251. if err = f.xmlNewDecoder(strings.NewReader("<decodeTwoCellAnchor>" + wsDr.TwoCellAnchor[idx].GraphicFrame + "</decodeTwoCellAnchor>")).
  1252. Decode(deTwoCellAnchor); err != nil && err != io.EOF {
  1253. err = fmt.Errorf("xml decode error: %s", err)
  1254. return
  1255. }
  1256. if err = nil; deTwoCellAnchor.From != nil && decodeTwoCellAnchorFuncs[drawingType](deTwoCellAnchor) {
  1257. if deTwoCellAnchor.From.Col == col && deTwoCellAnchor.From.Row == row {
  1258. wsDr.TwoCellAnchor = append(wsDr.TwoCellAnchor[:idx], wsDr.TwoCellAnchor[idx+1:]...)
  1259. idx--
  1260. }
  1261. }
  1262. }
  1263. f.Drawings[drawingXML] = wsDr
  1264. return err
  1265. }