drawing.go 38 KB

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