chart.go 28 KB

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