chart.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. package excelize
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "strconv"
  6. "strings"
  7. )
  8. // This section defines the currently supported chart types.
  9. const (
  10. Bar = "bar"
  11. BarStacked = "barStacked"
  12. Bar3D = "bar3D"
  13. Bar3DColumn = "bar3DColumn"
  14. Bar3DPercentStacked = "bar3DPercentStacked"
  15. Doughnut = "doughnut"
  16. Line = "line"
  17. Pie = "pie"
  18. Pie3D = "pie3D"
  19. Radar = "radar"
  20. Scatter = "scatter"
  21. )
  22. // This section defines the default value of chart properties.
  23. var (
  24. chartView3DRotX = map[string]int{
  25. Bar: 0,
  26. BarStacked: 0,
  27. Bar3D: 15,
  28. Bar3DColumn: 15,
  29. Bar3DPercentStacked: 15,
  30. Doughnut: 0,
  31. Line: 0,
  32. Pie: 0,
  33. Pie3D: 30,
  34. Radar: 0,
  35. Scatter: 0,
  36. }
  37. chartView3DRotY = map[string]int{
  38. Bar: 0,
  39. BarStacked: 0,
  40. Bar3D: 20,
  41. Bar3DColumn: 20,
  42. Bar3DPercentStacked: 20,
  43. Doughnut: 0,
  44. Line: 0,
  45. Pie: 0,
  46. Pie3D: 0,
  47. Radar: 0,
  48. Scatter: 0,
  49. }
  50. chartView3DDepthPercent = map[string]int{
  51. Bar: 100,
  52. BarStacked: 100,
  53. Bar3D: 100,
  54. Bar3DColumn: 100,
  55. Bar3DPercentStacked: 100,
  56. Doughnut: 100,
  57. Line: 100,
  58. Pie: 100,
  59. Pie3D: 100,
  60. Radar: 100,
  61. Scatter: 100,
  62. }
  63. chartView3DRAngAx = map[string]int{
  64. Bar: 0,
  65. BarStacked: 0,
  66. Bar3D: 1,
  67. Bar3DColumn: 1,
  68. Bar3DPercentStacked: 1,
  69. Doughnut: 0,
  70. Line: 0,
  71. Pie: 0,
  72. Pie3D: 0,
  73. Radar: 0,
  74. Scatter: 0,
  75. }
  76. chartLegendPosition = map[string]string{
  77. "bottom": "b",
  78. "left": "l",
  79. "right": "r",
  80. "top": "t",
  81. "top_right": "tr",
  82. }
  83. chartValAxNumFmtFormatCode = map[string]string{
  84. Bar: "General",
  85. BarStacked: "General",
  86. Bar3D: "General",
  87. Bar3DColumn: "General",
  88. Bar3DPercentStacked: "0%",
  89. Doughnut: "General",
  90. Line: "General",
  91. Pie: "General",
  92. Pie3D: "General",
  93. Radar: "General",
  94. Scatter: "General",
  95. }
  96. plotAreaChartGrouping = map[string]string{
  97. Bar: "clustered",
  98. BarStacked: "stacked",
  99. Bar3D: "clustered",
  100. Bar3DColumn: "standard",
  101. Bar3DPercentStacked: "percentStacked",
  102. Line: "standard",
  103. }
  104. )
  105. // parseFormatChartSet provides function to parse the format settings of the
  106. // chart with default value.
  107. func parseFormatChartSet(formatSet string) *formatChart {
  108. format := formatChart{
  109. Format: formatPicture{
  110. FPrintsWithSheet: true,
  111. FLocksWithSheet: false,
  112. NoChangeAspect: false,
  113. OffsetX: 0,
  114. OffsetY: 0,
  115. XScale: 1.0,
  116. YScale: 1.0,
  117. },
  118. Legend: formatChartLegend{
  119. Position: "bottom",
  120. ShowLegendKey: false,
  121. },
  122. Title: formatChartTitle{
  123. Name: " ",
  124. },
  125. ShowBlanksAs: "gap",
  126. }
  127. json.Unmarshal([]byte(formatSet), &format)
  128. return &format
  129. }
  130. // AddChart provides the method to add chart in a sheet by given chart format
  131. // set (such as offset, scale, aspect ratio setting and print settings) and
  132. // properties set. For example, create 3D bar chart with data
  133. // Sheet1!$A$29:$D$32:
  134. //
  135. // package main
  136. //
  137. // import (
  138. // "fmt"
  139. //
  140. // "github.com/360EntSecGroup-Skylar/excelize"
  141. // )
  142. //
  143. // func main() {
  144. // categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  145. // values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  146. // xlsx := excelize.NewFile()
  147. // for k, v := range categories {
  148. // xlsx.SetCellValue("Sheet1", k, v)
  149. // }
  150. // for k, v := range values {
  151. // xlsx.SetCellValue("Sheet1", k, v)
  152. // }
  153. // xlsx.AddChart("SHEET1", "F2", `{"type":"bar3D","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit Line Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`)
  154. // // Save xlsx file by the given path.
  155. // err := xlsx.SaveAs("./Workbook.xlsx")
  156. // if err != nil {
  157. // fmt.Println(err)
  158. // }
  159. // }
  160. //
  161. // The following shows the type of chart supported by excelize:
  162. //
  163. // Type | Chart
  164. // ---------------------+---------------------------
  165. // bar | bar chart
  166. // barStacked | stacked bar chart
  167. // bar3D | 3D bar chart
  168. // bar3DColumn | 3D column bar chart
  169. // bar3DPercentStacked | 3D 100% stacked bar chart
  170. // doughnut | doughnut chart
  171. // line | line chart
  172. // pie | pie chart
  173. // pie3D | 3D pie chart
  174. // radar | radar chart
  175. // scatter | scatter chart
  176. //
  177. // In Excel a chart series is a collection of information that defines which data is plotted such as values, axis labels and formatting.
  178. //
  179. // The series options that can be set are:
  180. //
  181. // name
  182. // categories
  183. // values
  184. //
  185. // name: Set the name for the series. The name is displayed in the chart legend and in the formula bar. The name property is optional and if it isn't supplied it will default to Series 1..n. The name can also be a formula such as =Sheet1!$A$1
  186. //
  187. // categories: This sets the chart category labels. The category is more or less the same as the X axis. In most chart types the categories property is optional and the chart will just assume a sequential series from 1..n.
  188. //
  189. // values: This is the most important property of a series and is the only mandatory option for every chart object. This option links the chart with the worksheet data that it displays.
  190. //
  191. // Set properties of the chart legend. The options that can be set are:
  192. //
  193. // position
  194. // show_legend_key
  195. //
  196. // position: Set the position of the chart legend. The default legend position is right. The available positions are:
  197. //
  198. // top
  199. // bottom
  200. // left
  201. // right
  202. // top_right
  203. //
  204. // show_legend_key: Set the legend keys shall be shown in data labels. The default value is false.
  205. //
  206. // Set properties of the chart title. The properties that can be set are:
  207. //
  208. // title
  209. //
  210. // name: Set the name (title) for the chart. The name is displayed above the chart. The name can also be a formula such as =Sheet1!$A$1 or a list with a sheetname. The name property is optional. The default is to have no chart title.
  211. //
  212. // Specifies how blank cells are plotted on the chart by show_blanks_as. The default value is gap. The options that can be set are:
  213. //
  214. // gap
  215. // span
  216. // zero
  217. //
  218. // gap: Specifies that blank values shall be left as a gap.
  219. //
  220. // sapn: Specifies that blank values shall be spanned with a line.
  221. //
  222. // zero: Specifies that blank values shall be treated as zero.
  223. //
  224. // Set chart offset, scale, aspect ratio setting and print settings by format, same as function AddPicture.
  225. //
  226. // Set the position of the chart plot area by plotarea. The properties that can be set are:
  227. //
  228. // show_bubble_size
  229. // show_cat_name
  230. // show_leader_lines
  231. // show_percent
  232. // show_series_name
  233. // show_val
  234. //
  235. // show_bubble_size: Specifies the bubble size shall be shown in a data label. The show_bubble_size property is optional. The default value is false.
  236. //
  237. // show_cat_name: Specifies that the category name shall be shown in the data label. The show_cat_name property is optional. The default value is true.
  238. //
  239. // show_leader_lines: Specifies leader lines shall be shown for data labels. The show_leader_lines property is optional. The default value is false.
  240. //
  241. // show_percent: Specifies that the percentage shall be shown in a data label. The show_percent property is optional. The default value is false.
  242. //
  243. // show_series_name: Specifies that the series name shall be shown in a data label. The show_series_name property is optional. The default value is false.
  244. //
  245. // show_val: Specifies that the value shall be shown in a data label. The show_val property is optional. The default value is false.
  246. //
  247. func (f *File) AddChart(sheet, cell, format string) {
  248. formatSet := parseFormatChartSet(format)
  249. // Read sheet data.
  250. xlsx := f.workSheetReader(sheet)
  251. // Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
  252. drawingID := f.countDrawings() + 1
  253. chartID := f.countCharts() + 1
  254. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  255. drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
  256. drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
  257. f.addDrawingChart(sheet, drawingXML, cell, 480, 290, drawingRID, &formatSet.Format)
  258. f.addChart(formatSet)
  259. f.addContentTypePart(chartID, "chart")
  260. f.addContentTypePart(drawingID, "drawings")
  261. }
  262. // countCharts provides function to get chart files count storage in the
  263. // folder xl/charts.
  264. func (f *File) countCharts() int {
  265. count := 0
  266. for k := range f.XLSX {
  267. if strings.Contains(k, "xl/charts/chart") {
  268. count++
  269. }
  270. }
  271. return count
  272. }
  273. // prepareDrawing provides function to prepare drawing ID and XML by given
  274. // drawingID, worksheet name and default drawingXML.
  275. func (f *File) prepareDrawing(xlsx *xlsxWorksheet, drawingID int, sheet, drawingXML string) (int, string) {
  276. sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  277. if xlsx.Drawing != nil {
  278. // The worksheet already has a picture or chart relationships, use the relationships drawing ../drawings/drawing%d.xml.
  279. sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID)
  280. drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml"))
  281. drawingXML = strings.Replace(sheetRelationshipsDrawingXML, "..", "xl", -1)
  282. } else {
  283. // Add first picture for given sheet.
  284. rID := f.addSheetRelationships(sheet, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
  285. f.addSheetDrawing(sheet, rID)
  286. }
  287. return drawingID, drawingXML
  288. }
  289. // addChart provides function to create chart as xl/charts/chart%d.xml by given
  290. // format sets.
  291. func (f *File) addChart(formatSet *formatChart) {
  292. count := f.countCharts()
  293. xlsxChartSpace := xlsxChartSpace{
  294. XMLNSc: NameSpaceDrawingMLChart,
  295. XMLNSa: NameSpaceDrawingML,
  296. XMLNSr: SourceRelationship,
  297. XMLNSc16r2: SourceRelationshipChart201506,
  298. Date1904: &attrValBool{Val: false},
  299. Lang: &attrValString{Val: "en-US"},
  300. RoundedCorners: &attrValBool{Val: false},
  301. Chart: cChart{
  302. Title: &cTitle{
  303. Tx: cTx{
  304. Rich: &cRich{
  305. P: aP{
  306. PPr: &aPPr{
  307. DefRPr: aRPr{
  308. Kern: 1200,
  309. Strike: "noStrike",
  310. U: "none",
  311. Sz: 1400,
  312. SolidFill: &aSolidFill{
  313. SchemeClr: &aSchemeClr{
  314. Val: "tx1",
  315. LumMod: &attrValInt{
  316. Val: 65000,
  317. },
  318. LumOff: &attrValInt{
  319. Val: 35000,
  320. },
  321. },
  322. },
  323. Ea: &aEa{
  324. Typeface: "+mn-ea",
  325. },
  326. Cs: &aCs{
  327. Typeface: "+mn-cs",
  328. },
  329. Latin: &aLatin{
  330. Typeface: "+mn-lt",
  331. },
  332. },
  333. },
  334. R: &aR{
  335. RPr: aRPr{
  336. Lang: "en-US",
  337. AltLang: "en-US",
  338. },
  339. T: formatSet.Title.Name,
  340. },
  341. },
  342. },
  343. },
  344. TxPr: cTxPr{
  345. P: aP{
  346. PPr: &aPPr{
  347. DefRPr: aRPr{
  348. Kern: 1200,
  349. U: "none",
  350. Sz: 14000,
  351. Strike: "noStrike",
  352. },
  353. },
  354. EndParaRPr: &aEndParaRPr{
  355. Lang: "en-US",
  356. },
  357. },
  358. },
  359. },
  360. View3D: &cView3D{
  361. RotX: &attrValInt{Val: chartView3DRotX[formatSet.Type]},
  362. RotY: &attrValInt{Val: chartView3DRotY[formatSet.Type]},
  363. DepthPercent: &attrValInt{Val: chartView3DDepthPercent[formatSet.Type]},
  364. RAngAx: &attrValInt{Val: chartView3DRAngAx[formatSet.Type]},
  365. },
  366. Floor: &cThicknessSpPr{
  367. Thickness: &attrValInt{Val: 0},
  368. },
  369. SideWall: &cThicknessSpPr{
  370. Thickness: &attrValInt{Val: 0},
  371. },
  372. BackWall: &cThicknessSpPr{
  373. Thickness: &attrValInt{Val: 0},
  374. },
  375. PlotArea: &cPlotArea{},
  376. Legend: &cLegend{
  377. LegendPos: &attrValString{Val: chartLegendPosition[formatSet.Legend.Position]},
  378. Overlay: &attrValBool{Val: false},
  379. },
  380. PlotVisOnly: &attrValBool{Val: false},
  381. DispBlanksAs: &attrValString{Val: formatSet.ShowBlanksAs},
  382. ShowDLblsOverMax: &attrValBool{Val: false},
  383. },
  384. SpPr: &cSpPr{
  385. SolidFill: &aSolidFill{
  386. SchemeClr: &aSchemeClr{Val: "bg1"},
  387. },
  388. Ln: &aLn{
  389. W: 9525,
  390. Cap: "flat",
  391. Cmpd: "sng",
  392. Algn: "ctr",
  393. SolidFill: &aSolidFill{
  394. SchemeClr: &aSchemeClr{Val: "tx1",
  395. LumMod: &attrValInt{
  396. Val: 15000,
  397. },
  398. LumOff: &attrValInt{
  399. Val: 85000,
  400. },
  401. },
  402. },
  403. },
  404. },
  405. PrintSettings: &cPrintSettings{
  406. PageMargins: &cPageMargins{
  407. B: 0.75,
  408. L: 0.7,
  409. R: 0.7,
  410. T: 0.7,
  411. Header: 0.3,
  412. Footer: 0.3,
  413. },
  414. },
  415. }
  416. plotAreaFunc := map[string]func(*formatChart) *cPlotArea{
  417. Bar: f.drawBarChart,
  418. BarStacked: f.drawBarChart,
  419. Bar3D: f.drawBarChart,
  420. Bar3DColumn: f.drawBarChart,
  421. Bar3DPercentStacked: f.drawBarChart,
  422. Doughnut: f.drawDoughnutChart,
  423. Line: f.drawLineChart,
  424. Pie3D: f.drawPie3DChart,
  425. Pie: f.drawPieChart,
  426. Radar: f.drawRadarChart,
  427. Scatter: f.drawScatterChart,
  428. }
  429. xlsxChartSpace.Chart.PlotArea = plotAreaFunc[formatSet.Type](formatSet)
  430. chart, _ := xml.Marshal(xlsxChartSpace)
  431. media := "xl/charts/chart" + strconv.Itoa(count+1) + ".xml"
  432. f.saveFileList(media, string(chart))
  433. }
  434. // drawBarChart provides function to draw the c:plotArea element for bar, barStacked and
  435. // bar3D chart by given format sets.
  436. func (f *File) drawBarChart(formatSet *formatChart) *cPlotArea {
  437. c := cCharts{
  438. BarDir: &attrValString{
  439. Val: "col",
  440. },
  441. Grouping: &attrValString{
  442. Val: "clustered",
  443. },
  444. VaryColors: &attrValBool{
  445. Val: true,
  446. },
  447. Ser: f.drawChartSeries(formatSet),
  448. DLbls: f.drawChartDLbls(formatSet),
  449. AxID: []*attrValInt{
  450. {Val: 754001152},
  451. {Val: 753999904},
  452. },
  453. }
  454. c.Grouping.Val = plotAreaChartGrouping[formatSet.Type]
  455. if formatSet.Type == "barStacked" {
  456. c.Overlap = &attrValInt{Val: 100}
  457. }
  458. catAx := f.drawPlotAreaCatAx(formatSet)
  459. valAx := f.drawPlotAreaValAx(formatSet)
  460. charts := map[string]*cPlotArea{
  461. "bar": {
  462. BarChart: &c,
  463. CatAx: catAx,
  464. ValAx: valAx,
  465. },
  466. "barStacked": {
  467. BarChart: &c,
  468. CatAx: catAx,
  469. ValAx: valAx,
  470. },
  471. "bar3D": {
  472. Bar3DChart: &c,
  473. CatAx: catAx,
  474. ValAx: valAx,
  475. },
  476. "bar3DColumn": {
  477. Bar3DChart: &c,
  478. CatAx: catAx,
  479. ValAx: valAx,
  480. },
  481. "bar3DPercentStacked": {
  482. Bar3DChart: &c,
  483. CatAx: catAx,
  484. ValAx: valAx,
  485. },
  486. }
  487. return charts[formatSet.Type]
  488. }
  489. // drawDoughnutChart provides function to draw the c:plotArea element for
  490. // doughnut chart by given format sets.
  491. func (f *File) drawDoughnutChart(formatSet *formatChart) *cPlotArea {
  492. return &cPlotArea{
  493. DoughnutChart: &cCharts{
  494. VaryColors: &attrValBool{
  495. Val: true,
  496. },
  497. Ser: f.drawChartSeries(formatSet),
  498. HoleSize: &attrValInt{Val: 75},
  499. },
  500. }
  501. }
  502. // drawLineChart provides function to draw the c:plotArea element for line chart
  503. // by given format sets.
  504. func (f *File) drawLineChart(formatSet *formatChart) *cPlotArea {
  505. return &cPlotArea{
  506. LineChart: &cCharts{
  507. Grouping: &attrValString{
  508. Val: plotAreaChartGrouping[formatSet.Type],
  509. },
  510. VaryColors: &attrValBool{
  511. Val: false,
  512. },
  513. Ser: f.drawChartSeries(formatSet),
  514. DLbls: f.drawChartDLbls(formatSet),
  515. Smooth: &attrValBool{
  516. Val: false,
  517. },
  518. AxID: []*attrValInt{
  519. {Val: 754001152},
  520. {Val: 753999904},
  521. },
  522. },
  523. CatAx: f.drawPlotAreaCatAx(formatSet),
  524. ValAx: f.drawPlotAreaValAx(formatSet),
  525. }
  526. }
  527. // drawPieChart provides function to draw the c:plotArea element for pie chart
  528. // by given format sets.
  529. func (f *File) drawPieChart(formatSet *formatChart) *cPlotArea {
  530. return &cPlotArea{
  531. PieChart: &cCharts{
  532. VaryColors: &attrValBool{
  533. Val: true,
  534. },
  535. Ser: f.drawChartSeries(formatSet),
  536. },
  537. }
  538. }
  539. // drawPie3DChart provides function to draw the c:plotArea element for 3D pie
  540. // chart by given format sets.
  541. func (f *File) drawPie3DChart(formatSet *formatChart) *cPlotArea {
  542. return &cPlotArea{
  543. Pie3DChart: &cCharts{
  544. VaryColors: &attrValBool{
  545. Val: true,
  546. },
  547. Ser: f.drawChartSeries(formatSet),
  548. },
  549. }
  550. }
  551. // drawRadarChart provides function to draw the c:plotArea element for radar
  552. // chart by given format sets.
  553. func (f *File) drawRadarChart(formatSet *formatChart) *cPlotArea {
  554. return &cPlotArea{
  555. RadarChart: &cCharts{
  556. RadarStyle: &attrValString{
  557. Val: "marker",
  558. },
  559. VaryColors: &attrValBool{
  560. Val: false,
  561. },
  562. Ser: f.drawChartSeries(formatSet),
  563. DLbls: f.drawChartDLbls(formatSet),
  564. AxID: []*attrValInt{
  565. {Val: 754001152},
  566. {Val: 753999904},
  567. },
  568. },
  569. CatAx: f.drawPlotAreaCatAx(formatSet),
  570. ValAx: f.drawPlotAreaValAx(formatSet),
  571. }
  572. }
  573. // drawScatterChart provides function to draw the c:plotArea element for scatter
  574. // chart by given format sets.
  575. func (f *File) drawScatterChart(formatSet *formatChart) *cPlotArea {
  576. return &cPlotArea{
  577. ScatterChart: &cCharts{
  578. ScatterStyle: &attrValString{
  579. Val: "smoothMarker", // line,lineMarker,marker,none,smooth,smoothMarker
  580. },
  581. VaryColors: &attrValBool{
  582. Val: false,
  583. },
  584. Ser: f.drawChartSeries(formatSet),
  585. DLbls: f.drawChartDLbls(formatSet),
  586. AxID: []*attrValInt{
  587. {Val: 754001152},
  588. {Val: 753999904},
  589. },
  590. },
  591. CatAx: f.drawPlotAreaCatAx(formatSet),
  592. ValAx: f.drawPlotAreaValAx(formatSet),
  593. }
  594. }
  595. // drawChartSeries provides function to draw the c:ser element by given format
  596. // sets.
  597. func (f *File) drawChartSeries(formatSet *formatChart) *[]cSer {
  598. ser := []cSer{}
  599. for k := range formatSet.Series {
  600. ser = append(ser, cSer{
  601. IDx: &attrValInt{Val: k},
  602. Order: &attrValInt{Val: k},
  603. Tx: &cTx{
  604. StrRef: &cStrRef{
  605. F: formatSet.Series[k].Name,
  606. },
  607. },
  608. SpPr: f.drawChartSeriesSpPr(k, formatSet),
  609. Marker: f.drawChartSeriesMarker(k, formatSet),
  610. DPt: f.drawChartSeriesDPt(k, formatSet),
  611. DLbls: f.drawChartSeriesDLbls(formatSet),
  612. Cat: f.drawChartSeriesCat(formatSet.Series[k], formatSet),
  613. Val: f.drawChartSeriesVal(formatSet.Series[k], formatSet),
  614. XVal: f.drawChartSeriesXVal(formatSet.Series[k], formatSet),
  615. YVal: f.drawChartSeriesYVal(formatSet.Series[k], formatSet),
  616. })
  617. }
  618. return &ser
  619. }
  620. // drawChartSeriesSpPr provides function to draw the c:spPr element by given
  621. // format sets.
  622. func (f *File) drawChartSeriesSpPr(i int, formatSet *formatChart) *cSpPr {
  623. spPrScatter := &cSpPr{
  624. Ln: &aLn{
  625. W: 25400,
  626. NoFill: " ",
  627. },
  628. }
  629. spPrLine := &cSpPr{
  630. Ln: &aLn{
  631. W: 25400,
  632. Cap: "rnd", // rnd, sq, flat
  633. SolidFill: &aSolidFill{
  634. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
  635. },
  636. },
  637. }
  638. chartSeriesSpPr := map[string]*cSpPr{Bar: nil, BarStacked: nil, Bar3D: nil, Bar3DColumn: nil, Bar3DPercentStacked: nil, Doughnut: nil, Line: spPrLine, Pie: nil, Pie3D: nil, Radar: nil, Scatter: spPrScatter}
  639. return chartSeriesSpPr[formatSet.Type]
  640. }
  641. // drawChartSeriesDPt provides function to draw the c:dPt element by given data
  642. // index and format sets.
  643. func (f *File) drawChartSeriesDPt(i int, formatSet *formatChart) []*cDPt {
  644. dpt := []*cDPt{{
  645. IDx: &attrValInt{Val: i},
  646. Bubble3D: &attrValBool{Val: false},
  647. SpPr: &cSpPr{
  648. SolidFill: &aSolidFill{
  649. SchemeClr: &aSchemeClr{Val: "accent" + strconv.Itoa(i+1)},
  650. },
  651. Ln: &aLn{
  652. W: 25400,
  653. Cap: "rnd",
  654. SolidFill: &aSolidFill{
  655. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  656. },
  657. },
  658. Sp3D: &aSp3D{
  659. ContourW: 25400,
  660. ContourClr: &aContourClr{
  661. SchemeClr: &aSchemeClr{Val: "lt" + strconv.Itoa(i+1)},
  662. },
  663. },
  664. },
  665. }}
  666. chartSeriesDPt := map[string][]*cDPt{Bar: nil, BarStacked: nil, Bar3D: nil, Bar3DColumn: nil, Bar3DPercentStacked: nil, Doughnut: nil, Line: nil, Pie: dpt, Pie3D: dpt, Radar: nil, Scatter: nil}
  667. return chartSeriesDPt[formatSet.Type]
  668. }
  669. // drawChartSeriesCat provides function to draw the c:cat element by given chart
  670. // series and format sets.
  671. func (f *File) drawChartSeriesCat(v formatChartSeries, formatSet *formatChart) *cCat {
  672. cat := &cCat{
  673. StrRef: &cStrRef{
  674. F: v.Categories,
  675. },
  676. }
  677. chartSeriesCat := map[string]*cCat{Bar: cat, BarStacked: cat, Bar3D: cat, Bar3DColumn: cat, Bar3DPercentStacked: cat, Doughnut: cat, Line: cat, Pie: cat, Pie3D: cat, Radar: cat, Scatter: nil}
  678. return chartSeriesCat[formatSet.Type]
  679. }
  680. // drawChartSeriesVal provides function to draw the c:val element by given chart
  681. // series and format sets.
  682. func (f *File) drawChartSeriesVal(v formatChartSeries, formatSet *formatChart) *cVal {
  683. val := &cVal{
  684. NumRef: &cNumRef{
  685. F: v.Values,
  686. },
  687. }
  688. chartSeriesVal := map[string]*cVal{Bar: val, BarStacked: val, Bar3D: val, Bar3DColumn: val, Bar3DPercentStacked: val, Doughnut: val, Line: val, Pie: val, Pie3D: val, Radar: val, Scatter: nil}
  689. return chartSeriesVal[formatSet.Type]
  690. }
  691. // drawChartSeriesMarker provides function to draw the c:marker element by given
  692. // data index and format sets.
  693. func (f *File) drawChartSeriesMarker(i int, formatSet *formatChart) *cMarker {
  694. marker := &cMarker{
  695. Symbol: &attrValString{Val: "circle"},
  696. Size: &attrValInt{Val: 5},
  697. SpPr: &cSpPr{
  698. SolidFill: &aSolidFill{
  699. SchemeClr: &aSchemeClr{
  700. Val: "accent" + strconv.Itoa(i+1),
  701. },
  702. },
  703. Ln: &aLn{
  704. W: 9252,
  705. SolidFill: &aSolidFill{
  706. SchemeClr: &aSchemeClr{
  707. Val: "accent" + strconv.Itoa(i+1),
  708. },
  709. },
  710. },
  711. },
  712. }
  713. chartSeriesMarker := map[string]*cMarker{Bar: nil, BarStacked: nil, Bar3D: nil, Bar3DColumn: nil, Bar3DPercentStacked: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: marker}
  714. return chartSeriesMarker[formatSet.Type]
  715. }
  716. // drawChartSeriesXVal provides function to draw the c:xVal element by given
  717. // chart series and format sets.
  718. func (f *File) drawChartSeriesXVal(v formatChartSeries, formatSet *formatChart) *cCat {
  719. cat := &cCat{
  720. StrRef: &cStrRef{
  721. F: v.Categories,
  722. },
  723. }
  724. chartSeriesXVal := map[string]*cCat{Bar: nil, BarStacked: nil, Bar3D: nil, Bar3DColumn: nil, Bar3DPercentStacked: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: cat}
  725. return chartSeriesXVal[formatSet.Type]
  726. }
  727. // drawChartSeriesYVal provides function to draw the c:yVal element by given
  728. // chart series and format sets.
  729. func (f *File) drawChartSeriesYVal(v formatChartSeries, formatSet *formatChart) *cVal {
  730. val := &cVal{
  731. NumRef: &cNumRef{
  732. F: v.Values,
  733. },
  734. }
  735. chartSeriesYVal := map[string]*cVal{Bar: nil, BarStacked: nil, Bar3D: nil, Bar3DColumn: nil, Bar3DPercentStacked: nil, Doughnut: nil, Line: nil, Pie: nil, Pie3D: nil, Radar: nil, Scatter: val}
  736. return chartSeriesYVal[formatSet.Type]
  737. }
  738. // drawChartDLbls provides function to draw the c:dLbls element by given format
  739. // sets.
  740. func (f *File) drawChartDLbls(formatSet *formatChart) *cDLbls {
  741. return &cDLbls{
  742. ShowLegendKey: &attrValBool{Val: formatSet.Legend.ShowLegendKey},
  743. ShowVal: &attrValBool{Val: formatSet.Plotarea.ShowVal},
  744. ShowCatName: &attrValBool{Val: formatSet.Plotarea.ShowCatName},
  745. ShowSerName: &attrValBool{Val: formatSet.Plotarea.ShowSerName},
  746. ShowBubbleSize: &attrValBool{Val: formatSet.Plotarea.ShowBubbleSize},
  747. ShowPercent: &attrValBool{Val: formatSet.Plotarea.ShowPercent},
  748. ShowLeaderLines: &attrValBool{Val: formatSet.Plotarea.ShowLeaderLines},
  749. }
  750. }
  751. // drawChartSeriesDLbls provides function to draw the c:dLbls element by given
  752. // format sets.
  753. func (f *File) drawChartSeriesDLbls(formatSet *formatChart) *cDLbls {
  754. dLbls := f.drawChartDLbls(formatSet)
  755. chartSeriesDLbls := map[string]*cDLbls{Bar: dLbls, BarStacked: dLbls, Bar3D: dLbls, Bar3DColumn: dLbls, Bar3DPercentStacked: dLbls, Doughnut: dLbls, Line: dLbls, Pie: dLbls, Pie3D: dLbls, Radar: dLbls, Scatter: nil}
  756. return chartSeriesDLbls[formatSet.Type]
  757. }
  758. // drawPlotAreaCatAx provides function to draw the c:catAx element.
  759. func (f *File) drawPlotAreaCatAx(formatSet *formatChart) []*cAxs {
  760. return []*cAxs{
  761. {
  762. AxID: &attrValInt{Val: 754001152},
  763. Scaling: &cScaling{
  764. Orientation: &attrValString{Val: "minMax"},
  765. },
  766. Delete: &attrValBool{Val: false},
  767. AxPos: &attrValString{Val: "b"},
  768. NumFmt: &cNumFmt{
  769. FormatCode: "General",
  770. SourceLinked: true,
  771. },
  772. MajorTickMark: &attrValString{Val: "none"},
  773. MinorTickMark: &attrValString{Val: "none"},
  774. TickLblPos: &attrValString{Val: "nextTo"},
  775. SpPr: f.drawPlotAreaSpPr(),
  776. TxPr: f.drawPlotAreaTxPr(),
  777. CrossAx: &attrValInt{Val: 753999904},
  778. Crosses: &attrValString{Val: "autoZero"},
  779. Auto: &attrValBool{Val: true},
  780. LblAlgn: &attrValString{Val: "ctr"},
  781. LblOffset: &attrValInt{Val: 100},
  782. NoMultiLvlLbl: &attrValBool{Val: false},
  783. },
  784. }
  785. }
  786. // drawPlotAreaValAx provides function to draw the c:valAx element.
  787. func (f *File) drawPlotAreaValAx(formatSet *formatChart) []*cAxs {
  788. return []*cAxs{
  789. {
  790. AxID: &attrValInt{Val: 753999904},
  791. Scaling: &cScaling{
  792. Orientation: &attrValString{Val: "minMax"},
  793. },
  794. Delete: &attrValBool{Val: false},
  795. AxPos: &attrValString{Val: "l"},
  796. NumFmt: &cNumFmt{
  797. FormatCode: chartValAxNumFmtFormatCode[formatSet.Type],
  798. SourceLinked: true,
  799. },
  800. MajorTickMark: &attrValString{Val: "none"},
  801. MinorTickMark: &attrValString{Val: "none"},
  802. TickLblPos: &attrValString{Val: "nextTo"},
  803. SpPr: f.drawPlotAreaSpPr(),
  804. TxPr: f.drawPlotAreaTxPr(),
  805. CrossAx: &attrValInt{Val: 754001152},
  806. Crosses: &attrValString{Val: "autoZero"},
  807. CrossBetween: &attrValString{Val: "between"},
  808. },
  809. }
  810. }
  811. // drawPlotAreaSpPr provides function to draw the c:spPr element.
  812. func (f *File) drawPlotAreaSpPr() *cSpPr {
  813. return &cSpPr{
  814. Ln: &aLn{
  815. W: 9525,
  816. Cap: "flat",
  817. Cmpd: "sng",
  818. Algn: "ctr",
  819. SolidFill: &aSolidFill{
  820. SchemeClr: &aSchemeClr{
  821. Val: "tx1",
  822. LumMod: &attrValInt{Val: 15000},
  823. LumOff: &attrValInt{Val: 85000},
  824. },
  825. },
  826. },
  827. }
  828. }
  829. // drawPlotAreaTxPr provides function to draw the c:txPr element.
  830. func (f *File) drawPlotAreaTxPr() *cTxPr {
  831. return &cTxPr{
  832. BodyPr: aBodyPr{
  833. Rot: -60000000,
  834. SpcFirstLastPara: true,
  835. VertOverflow: "ellipsis",
  836. Vert: "horz",
  837. Wrap: "square",
  838. Anchor: "ctr",
  839. AnchorCtr: true,
  840. },
  841. P: aP{
  842. PPr: &aPPr{
  843. DefRPr: aRPr{
  844. Sz: 900,
  845. B: false,
  846. I: false,
  847. U: "none",
  848. Strike: "noStrike",
  849. Kern: 1200,
  850. Baseline: 0,
  851. SolidFill: &aSolidFill{
  852. SchemeClr: &aSchemeClr{
  853. Val: "tx1",
  854. LumMod: &attrValInt{Val: 15000},
  855. LumOff: &attrValInt{Val: 85000},
  856. },
  857. },
  858. Latin: &aLatin{Typeface: "+mn-lt"},
  859. Ea: &aEa{Typeface: "+mn-ea"},
  860. Cs: &aCs{Typeface: "+mn-cs"},
  861. },
  862. },
  863. EndParaRPr: &aEndParaRPr{Lang: "en-US"},
  864. },
  865. }
  866. }
  867. // drawingParser provides function to parse drawingXML. In order to solve the
  868. // problem that the label structure is changed after serialization and
  869. // deserialization, two different structures: decodeWsDr and encodeWsDr are
  870. // defined.
  871. func (f *File) drawingParser(drawingXML string, content *xlsxWsDr) int {
  872. cNvPrID := 1
  873. _, ok := f.XLSX[drawingXML]
  874. if ok { // Append Model
  875. decodeWsDr := decodeWsDr{}
  876. xml.Unmarshal([]byte(f.readXML(drawingXML)), &decodeWsDr)
  877. content.R = decodeWsDr.R
  878. cNvPrID = len(decodeWsDr.OneCellAnchor) + len(decodeWsDr.TwoCellAnchor) + 1
  879. for _, v := range decodeWsDr.OneCellAnchor {
  880. content.OneCellAnchor = append(content.OneCellAnchor, &xdrCellAnchor{
  881. EditAs: v.EditAs,
  882. GraphicFrame: v.Content,
  883. })
  884. }
  885. for _, v := range decodeWsDr.TwoCellAnchor {
  886. content.TwoCellAnchor = append(content.TwoCellAnchor, &xdrCellAnchor{
  887. EditAs: v.EditAs,
  888. GraphicFrame: v.Content,
  889. })
  890. }
  891. }
  892. return cNvPrID
  893. }
  894. // addDrawingChart provides function to add chart graphic frame by given sheet,
  895. // drawingXML, cell, width, height, relationship index and format sets.
  896. func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rID int, formatSet *formatPicture) {
  897. cell = strings.ToUpper(cell)
  898. fromCol := string(strings.Map(letterOnlyMapF, cell))
  899. fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
  900. row := fromRow - 1
  901. col := TitleToNumber(fromCol)
  902. width = int(float64(width) * formatSet.XScale)
  903. height = int(float64(height) * formatSet.YScale)
  904. colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, col, row, formatSet.OffsetX, formatSet.OffsetY, width, height)
  905. content := xlsxWsDr{}
  906. content.A = NameSpaceDrawingML
  907. content.Xdr = NameSpaceDrawingMLSpreadSheet
  908. cNvPrID := f.drawingParser(drawingXML, &content)
  909. twoCellAnchor := xdrCellAnchor{}
  910. twoCellAnchor.EditAs = "oneCell"
  911. from := xlsxFrom{}
  912. from.Col = colStart
  913. from.ColOff = formatSet.OffsetX * EMU
  914. from.Row = rowStart
  915. from.RowOff = formatSet.OffsetY * EMU
  916. to := xlsxTo{}
  917. to.Col = colEnd
  918. to.ColOff = x2 * EMU
  919. to.Row = rowEnd
  920. to.RowOff = y2 * EMU
  921. twoCellAnchor.From = &from
  922. twoCellAnchor.To = &to
  923. graphicFrame := xlsxGraphicFrame{
  924. NvGraphicFramePr: xlsxNvGraphicFramePr{
  925. CNvPr: &xlsxCNvPr{
  926. ID: f.countCharts() + f.countMedia() + 1,
  927. Name: "Chart " + strconv.Itoa(cNvPrID),
  928. },
  929. },
  930. Graphic: &xlsxGraphic{
  931. GraphicData: &xlsxGraphicData{
  932. URI: NameSpaceDrawingMLChart,
  933. Chart: &xlsxChart{
  934. C: NameSpaceDrawingMLChart,
  935. R: SourceRelationship,
  936. RID: "rId" + strconv.Itoa(rID),
  937. },
  938. },
  939. },
  940. }
  941. graphic, _ := xml.Marshal(graphicFrame)
  942. twoCellAnchor.GraphicFrame = string(graphic)
  943. twoCellAnchor.ClientData = &xdrClientData{
  944. FLocksWithSheet: formatSet.FLocksWithSheet,
  945. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  946. }
  947. content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
  948. output, _ := xml.Marshal(content)
  949. f.saveFileList(drawingXML, string(output))
  950. }