chart.go 28 KB

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