chart.go 27 KB

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