drawing.go 39 KB

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