chart.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. package excelize
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. )
  9. // This section defines the currently supported chart types.
  10. const (
  11. Bar = "bar"
  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{Bar: 0, Bar3D: 15, Doughnut: 0, Line: 0, Pie: 0, Pie3D: 30, Radar: 0, Scatter: 0}
  23. chartView3DRotY = map[string]int{Bar: 0, Bar3D: 20, Doughnut: 0, Line: 0, Pie: 0, Pie3D: 0, Radar: 0, Scatter: 0}
  24. chartView3DDepthPercent = map[string]int{Bar: 100, Bar3D: 100, Doughnut: 100, Line: 100, Pie: 100, Pie3D: 100, Radar: 100, Scatter: 100}
  25. chartView3DRAngAx = map[string]int{Bar: 0, Bar3D: 1, Doughnut: 0, Line: 0, Pie: 0, Pie3D: 0, Radar: 0, Scatter: 0}
  26. chartLegendPosition = map[string]string{"bottom": "b", "left": "l", "right": "r", "top": "t", "top_right": "tr"}
  27. )
  28. // parseFormatChartSet provides function to parse the format settings of the
  29. // chart with default value.
  30. func parseFormatChartSet(formatSet string) *formatChart {
  31. format := formatChart{
  32. Format: formatPicture{
  33. FPrintsWithSheet: true,
  34. FLocksWithSheet: false,
  35. NoChangeAspect: false,
  36. OffsetX: 0,
  37. OffsetY: 0,
  38. XScale: 1.0,
  39. YScale: 1.0,
  40. },
  41. Legend: formatChartLegend{
  42. Position: "bottom",
  43. ShowLegendKey: false,
  44. },
  45. Title: formatChartTitle{
  46. Name: " ",
  47. },
  48. ShowBlanksAs: "gap",
  49. }
  50. json.Unmarshal([]byte(formatSet), &format)
  51. return &format
  52. }
  53. // AddChart provides the method to add chart in a sheet by given chart format
  54. // set (such as offset, scale, aspect ratio setting and print settings) and
  55. // properties set. Only support "pie" and "3Dpie" type chart currently. For
  56. // example, create 3D bar chart with data Sheet1!$A$29:$D$32:
  57. //
  58. // package main
  59. //
  60. // import (
  61. // "fmt"
  62. // "os"
  63. //
  64. // "github.com/Luxurioust/excelize"
  65. // )
  66. //
  67. // func main() {
  68. // xlsx := excelize.CreateFile()
  69. // xlsx.SetCellValue("SHEET1", "A30", "Small")
  70. // xlsx.SetCellValue("SHEET1", "A31", "Normal")
  71. // xlsx.SetCellValue("SHEET1", "A32", "Large")
  72. // xlsx.SetCellValue("SHEET1", "B29", "Apple")
  73. // xlsx.SetCellValue("SHEET1", "C29", "Prange")
  74. // xlsx.SetCellValue("SHEET1", "D29", "Pear")
  75. // xlsx.SetCellValue("SHEET1", "B30", 2)
  76. // xlsx.SetCellValue("SHEET1", "C30", 3)
  77. // xlsx.SetCellValue("SHEET1", "D30", 3)
  78. // xlsx.SetCellValue("SHEET1", "B31", 5)
  79. // xlsx.SetCellValue("SHEET1", "C31", 2)
  80. // xlsx.SetCellValue("SHEET1", "D31", 4)
  81. // xlsx.SetCellValue("SHEET1", "B32", 6)
  82. // xlsx.SetCellValue("SHEET1", "C32", 7)
  83. // xlsx.SetCellValue("SHEET1", "D32", 8)
  84. // 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"}`)
  85. // // Save xlsx file by the given path.
  86. // err := xlsx.WriteTo("./tmp/Workbook.xlsx")
  87. // if err != nil {
  88. // fmt.Println(err)
  89. // os.Exit(1)
  90. // }
  91. // }
  92. //
  93. // The following shows the type of chart supported by excelize:
  94. //
  95. // +---------------------------+
  96. // | Type | Chart |
  97. // +==========+================+
  98. // | bar | bar chart |
  99. // +----------+----------------+
  100. // | bar3D | 3D bar chart |
  101. // +----------+----------------+
  102. // | doughnut | doughnut chart |
  103. // +----------+----------------+
  104. // | line | line chart |
  105. // +----------+----------------+
  106. // | pie | pie chart |
  107. // +----------+----------------+
  108. // | pie3D | 3D pie chart |
  109. // +----------+----------------+
  110. // | radar | radar chart |
  111. // +----------+----------------+
  112. // | scatter | scatter chart |
  113. // +----------+----------------+
  114. //
  115. // In Excel a chart series is a collection of information that defines which data is plotted such as values, axis labels and formatting.
  116. //
  117. // The series options that can be set are:
  118. //
  119. // name
  120. // categories
  121. // values
  122. //
  123. // 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
  124. //
  125. // 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.
  126. //
  127. // 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.
  128. //
  129. // Set properties of the chart legend. The options that can be set are:
  130. //
  131. // position
  132. // show_legend_key
  133. //
  134. // position: Set the position of the chart legend. The default legend position is right. The available positions are:
  135. //
  136. // top
  137. // bottom
  138. // left
  139. // right
  140. // top_right
  141. //
  142. // show_legend_key: Set the legend keys shall be shown in data labels. The default value is false.
  143. //
  144. // Set properties of the chart title. The properties that can be set are:
  145. //
  146. // title
  147. //
  148. // 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.
  149. //
  150. // 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:
  151. //
  152. // gap
  153. // span
  154. // zero
  155. //
  156. // gap: Specifies that blank values shall be left as a gap.
  157. //
  158. // sapn: Specifies that blank values shall be spanned with a line.
  159. //
  160. // zero: Specifies that blank values shall be treated as zero.
  161. //
  162. // Set chart offset, scale, aspect ratio setting and print settings by format, same as function AddPicture.
  163. //
  164. // Set the position of the chart plot area by plotarea. The properties that can be set are:
  165. //
  166. // show_bubble_size
  167. // show_cat_name
  168. // show_leader_lines
  169. // show_percent
  170. // show_series_name
  171. // show_val
  172. //
  173. // 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.
  174. // 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.
  175. // show_leader_lines: Specifies leader lines shall be shown for data labels. The show_leader_lines property is optional. The default value is false.
  176. // show_percent: Specifies that the percentage shall be shown in a data label. The show_percent property is optional. The default value is false.
  177. // 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.
  178. // show_val: Specifies that the value shall be shown in a data label. The show_val property is optional. The default value is false.
  179. //
  180. func (f *File) AddChart(sheet, cell, format string) {
  181. formatSet := parseFormatChartSet(format)
  182. // Read sheet data.
  183. xlsx := f.workSheetReader(sheet)
  184. // Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
  185. drawingID := f.countDrawings() + 1
  186. chartID := f.countCharts() + 1
  187. drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  188. sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
  189. var drawingRID int
  190. if xlsx.Drawing != nil {
  191. // The worksheet already has a picture or chart relationships, use the relationships drawing ../drawings/drawing%d.xml.
  192. sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID)
  193. drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml"))
  194. drawingXML = strings.Replace(sheetRelationshipsDrawingXML, "..", "xl", -1)
  195. } else {
  196. // Add first picture for given sheet.
  197. rID := f.addSheetRelationships(sheet, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
  198. f.addSheetDrawing(sheet, rID)
  199. }
  200. drawingRID = f.addDrawingRelationships(drawingID, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml")
  201. f.addDrawingChart(sheet, drawingXML, cell, 480, 290, drawingRID, &formatSet.Format)
  202. f.addChart(formatSet)
  203. f.addChartContentTypePart(chartID)
  204. f.addDrawingContentTypePart(drawingID)
  205. }
  206. // countCharts provides function to get chart files count storage in the
  207. // folder xl/charts.
  208. func (f *File) countCharts() int {
  209. count := 0
  210. for k := range f.XLSX {
  211. if strings.Contains(k, "xl/charts/chart") {
  212. count++
  213. }
  214. }
  215. return count
  216. }
  217. // addChartContentTypePart provides function to add chart part relationships in
  218. // the file [Content_Types].xml by given chart index.
  219. func (f *File) addChartContentTypePart(index int) {
  220. content := f.contentTypesReader()
  221. for _, v := range content.Overrides {
  222. if v.PartName == "/xl/charts/chart"+strconv.Itoa(index)+".xml" {
  223. return
  224. }
  225. }
  226. content.Overrides = append(content.Overrides, xlsxOverride{
  227. PartName: "/xl/charts/chart" + strconv.Itoa(index) + ".xml",
  228. ContentType: "application/vnd.openxmlformats-officedocument.drawingml.chart+xml",
  229. })
  230. }
  231. // addChart provides function to create chart as xl/charts/chart%d.xml by given
  232. // format sets.
  233. func (f *File) addChart(formatSet *formatChart) {
  234. count := f.countCharts()
  235. xlsxChartSpace := xlsxChartSpace{
  236. ChartSpace: cChartSpace{
  237. XMLNSc: NameSpaceDrawingMLChart,
  238. XMLNSa: NameSpaceDrawingML,
  239. XMLNSr: SourceRelationship,
  240. XMLNSc16r2: SourceRelationshipChart201506,
  241. Date1904: &attrValBool{Val: false},
  242. Lang: &attrValString{Val: "en-US"},
  243. RoundedCorners: &attrValBool{Val: false},
  244. Chart: cChart{
  245. Title: &cTitle{
  246. Tx: cTx{
  247. Rich: &cRich{
  248. P: aP{
  249. PPr: aPPr{
  250. DefRPr: aDefRPr{
  251. Kern: 1200,
  252. Strike: "noStrike",
  253. U: "none",
  254. Sz: 1400,
  255. SolidFill: &aSolidFill{
  256. SchemeClr: &aSchemeClr{
  257. Val: "tx1",
  258. LumMod: &attrValInt{
  259. Val: 65000,
  260. },
  261. LumOff: &attrValInt{
  262. Val: 35000,
  263. },
  264. },
  265. },
  266. Ea: &aEa{
  267. Typeface: "+mn-ea",
  268. },
  269. Cs: &aCs{
  270. Typeface: "+mn-cs",
  271. },
  272. Latin: &aLatin{
  273. Typeface: "+mn-lt",
  274. },
  275. },
  276. },
  277. R: &aR{
  278. RPr: aRPr{
  279. Lang: "en-US",
  280. AltLang: "en-US",
  281. },
  282. T: formatSet.Title.Name,
  283. },
  284. },
  285. },
  286. },
  287. TxPr: cTxPr{
  288. P: aP{
  289. PPr: aPPr{
  290. DefRPr: aDefRPr{
  291. Kern: 1200,
  292. U: "none",
  293. Sz: 14000,
  294. Strike: "noStrike",
  295. },
  296. },
  297. EndParaRPr: &aEndParaRPr{
  298. Lang: "en-US",
  299. },
  300. },
  301. },
  302. },
  303. View3D: &cView3D{
  304. RotX: &attrValInt{Val: chartView3DRotX[formatSet.Type]},
  305. RotY: &attrValInt{Val: chartView3DRotY[formatSet.Type]},
  306. DepthPercent: &attrValInt{Val: chartView3DDepthPercent[formatSet.Type]},
  307. RAngAx: &attrValInt{Val: chartView3DRAngAx[formatSet.Type]},
  308. },
  309. Floor: &cThicknessSpPr{
  310. Thickness: &attrValInt{Val: 0},
  311. },
  312. SideWall: &cThicknessSpPr{
  313. Thickness: &attrValInt{Val: 0},
  314. },
  315. BackWall: &cThicknessSpPr{
  316. Thickness: &attrValInt{Val: 0},
  317. },
  318. PlotArea: &cPlotArea{},
  319. Legend: &cLegend{
  320. LegendPos: &attrValString{Val: chartLegendPosition[formatSet.Legend.Position]},
  321. Overlay: &attrValBool{Val: false},
  322. },
  323. PlotVisOnly: &attrValBool{Val: false},
  324. DispBlanksAs: &attrValString{Val: formatSet.ShowBlanksAs},
  325. ShowDLblsOverMax: &attrValBool{Val: false},
  326. },
  327. SpPr: &cSpPr{
  328. SolidFill: &aSolidFill{
  329. SchemeClr: &aSchemeClr{Val: "bg1"},
  330. },
  331. Ln: &aLn{
  332. W: 9525,
  333. Cap: "flat",
  334. Cmpd: "sng",
  335. Algn: "ctr",
  336. SolidFill: &aSolidFill{
  337. SchemeClr: &aSchemeClr{Val: "tx1",
  338. LumMod: &attrValInt{
  339. Val: 15000,
  340. },
  341. LumOff: &attrValInt{
  342. Val: 85000,
  343. },
  344. },
  345. },
  346. },
  347. },
  348. PrintSettings: &cPrintSettings{
  349. PageMargins: &cPageMargins{
  350. B: 0.75,
  351. L: 0.7,
  352. R: 0.7,
  353. T: 0.7,
  354. Header: 0.3,
  355. Footer: 0.3,
  356. },
  357. },
  358. },
  359. }
  360. plotAreaFunc := map[string]func(*formatChart) *cPlotArea{
  361. Bar: f.drawBarChart,
  362. Bar3D: f.drawBar3DChart,
  363. Doughnut: f.drawDoughnutChart,
  364. Line: f.drawLineChart,
  365. Pie3D: f.drawPie3DChart,
  366. Pie: f.drawPieChart,
  367. Radar: f.drawRadarChart,
  368. Scatter: f.drawScatterChart,
  369. }
  370. xlsxChartSpace.ChartSpace.Chart.PlotArea = plotAreaFunc[formatSet.Type](formatSet)
  371. chart, _ := xml.Marshal(xlsxChartSpace)
  372. media := "xl/charts/chart" + strconv.Itoa(count+1) + ".xml"
  373. f.saveFileList(media, string(chart[16:len(chart)-17]))
  374. }
  375. // drawBarChart provides function to draw the c:plotArea element for bar chart
  376. // by given format sets.
  377. func (f *File) drawBarChart(formatSet *formatChart) *cPlotArea {
  378. return &cPlotArea{
  379. BarChart: &cCharts{
  380. BarDir: &attrValString{
  381. Val: "col",
  382. },
  383. Grouping: &attrValString{
  384. Val: "clustered",
  385. },
  386. VaryColors: &attrValBool{
  387. Val: true,
  388. },
  389. Ser: f.drawChartSeries(formatSet),
  390. DLbls: f.drawChartDLbls(formatSet),
  391. AxID: []*attrValInt{
  392. &attrValInt{Val: 754001152},
  393. &attrValInt{Val: 753999904},
  394. },
  395. },
  396. CatAx: f.drawPlotAreaCatAx(),
  397. ValAx: f.drawPlotAreaValAx(),
  398. }
  399. }
  400. // drawBar3DChart provides function to draw the c:plotArea element for 3D bar
  401. // chart by given format sets.
  402. func (f *File) drawBar3DChart(formatSet *formatChart) *cPlotArea {
  403. return &cPlotArea{
  404. Bar3DChart: &cCharts{
  405. BarDir: &attrValString{
  406. Val: "col",
  407. },
  408. Grouping: &attrValString{
  409. Val: "clustered",
  410. },
  411. VaryColors: &attrValBool{
  412. Val: true,
  413. },
  414. Ser: f.drawChartSeries(formatSet),
  415. DLbls: f.drawChartDLbls(formatSet),
  416. AxID: []*attrValInt{
  417. &attrValInt{Val: 754001152},
  418. &attrValInt{Val: 753999904},
  419. },
  420. },
  421. CatAx: f.drawPlotAreaCatAx(),
  422. ValAx: f.drawPlotAreaValAx(),
  423. }
  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. &attrValInt{Val: 754001152},
  456. &attrValInt{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. &attrValInt{Val: 754001152},
  502. &attrValInt{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. &attrValInt{Val: 754001152},
  524. &attrValInt{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, v := 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: v.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(v, formatSet),
  549. Val: f.drawChartSeriesVal(v, formatSet),
  550. XVal: f.drawChartSeriesXVal(v, formatSet),
  551. YVal: f.drawChartSeriesYVal(v, 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{&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 := &cDLbls{
  691. ShowLegendKey: &attrValBool{Val: formatSet.Legend.ShowLegendKey},
  692. ShowVal: &attrValBool{Val: formatSet.Plotarea.ShowVal},
  693. ShowCatName: &attrValBool{Val: formatSet.Plotarea.ShowCatName},
  694. ShowSerName: &attrValBool{Val: formatSet.Plotarea.ShowSerName},
  695. ShowBubbleSize: &attrValBool{Val: formatSet.Plotarea.ShowBubbleSize},
  696. ShowPercent: &attrValBool{Val: formatSet.Plotarea.ShowPercent},
  697. ShowLeaderLines: &attrValBool{Val: formatSet.Plotarea.ShowLeaderLines},
  698. }
  699. chartSeriesDLbls := map[string]*cDLbls{Bar: dLbls, Bar3D: dLbls, Doughnut: dLbls, Line: dLbls, Pie: dLbls, Pie3D: dLbls, Radar: dLbls, Scatter: nil}
  700. return chartSeriesDLbls[formatSet.Type]
  701. }
  702. // drawPlotAreaCatAx provides function to draw the c:catAx element.
  703. func (f *File) drawPlotAreaCatAx() []*cAxs {
  704. return []*cAxs{
  705. &cAxs{
  706. AxID: &attrValInt{Val: 754001152},
  707. Scaling: &cScaling{
  708. Orientation: &attrValString{Val: "minMax"},
  709. },
  710. Delete: &attrValBool{Val: false},
  711. AxPos: &attrValString{Val: "b"},
  712. NumFmt: &cNumFmt{
  713. FormatCode: "General",
  714. SourceLinked: true,
  715. },
  716. MajorTickMark: &attrValString{Val: "none"},
  717. MinorTickMark: &attrValString{Val: "none"},
  718. TickLblPos: &attrValString{Val: "nextTo"},
  719. SpPr: f.drawPlotAreaSpPr(),
  720. TxPr: f.drawPlotAreaTxPr(),
  721. CrossAx: &attrValInt{Val: 753999904},
  722. Crosses: &attrValString{Val: "autoZero"},
  723. Auto: &attrValBool{Val: true},
  724. LblAlgn: &attrValString{Val: "ctr"},
  725. LblOffset: &attrValInt{Val: 100},
  726. NoMultiLvlLbl: &attrValBool{Val: false},
  727. },
  728. }
  729. }
  730. // drawPlotAreaCatAx provides function to draw the c:valAx element.
  731. func (f *File) drawPlotAreaValAx() []*cAxs {
  732. return []*cAxs{
  733. &cAxs{
  734. AxID: &attrValInt{Val: 753999904},
  735. Scaling: &cScaling{
  736. Orientation: &attrValString{Val: "minMax"},
  737. },
  738. Delete: &attrValBool{Val: false},
  739. AxPos: &attrValString{Val: "l"},
  740. NumFmt: &cNumFmt{
  741. FormatCode: "General",
  742. SourceLinked: true,
  743. },
  744. MajorTickMark: &attrValString{Val: "none"},
  745. MinorTickMark: &attrValString{Val: "none"},
  746. TickLblPos: &attrValString{Val: "nextTo"},
  747. SpPr: f.drawPlotAreaSpPr(),
  748. TxPr: f.drawPlotAreaTxPr(),
  749. CrossAx: &attrValInt{Val: 754001152},
  750. Crosses: &attrValString{Val: "autoZero"},
  751. CrossBetween: &attrValString{Val: "between"},
  752. },
  753. }
  754. }
  755. // drawPlotAreaSpPr provides function to draw the c:spPr element.
  756. func (f *File) drawPlotAreaSpPr() *cSpPr {
  757. return &cSpPr{
  758. Ln: &aLn{
  759. W: 9525,
  760. Cap: "flat",
  761. Cmpd: "sng",
  762. Algn: "ctr",
  763. SolidFill: &aSolidFill{
  764. SchemeClr: &aSchemeClr{
  765. Val: "tx1",
  766. LumMod: &attrValInt{Val: 15000},
  767. LumOff: &attrValInt{Val: 85000},
  768. },
  769. },
  770. },
  771. }
  772. }
  773. // drawPlotAreaTxPr provides function to draw the c:txPr element.
  774. func (f *File) drawPlotAreaTxPr() *cTxPr {
  775. return &cTxPr{
  776. BodyPr: aBodyPr{
  777. Rot: -60000000,
  778. SpcFirstLastPara: true,
  779. VertOverflow: "ellipsis",
  780. Vert: "horz",
  781. Wrap: "square",
  782. Anchor: "ctr",
  783. AnchorCtr: true,
  784. },
  785. P: aP{
  786. PPr: aPPr{
  787. DefRPr: aDefRPr{
  788. Sz: 900,
  789. B: false,
  790. I: false,
  791. U: "none",
  792. Strike: "noStrike",
  793. Kern: 1200,
  794. Baseline: 0,
  795. SolidFill: &aSolidFill{
  796. SchemeClr: &aSchemeClr{
  797. Val: "tx1",
  798. LumMod: &attrValInt{Val: 15000},
  799. LumOff: &attrValInt{Val: 85000},
  800. },
  801. },
  802. Latin: &aLatin{Typeface: "+mn-lt"},
  803. Ea: &aEa{Typeface: "+mn-ea"},
  804. Cs: &aCs{Typeface: "+mn-cs"},
  805. },
  806. },
  807. EndParaRPr: &aEndParaRPr{Lang: "en-US"},
  808. },
  809. }
  810. }
  811. // addDrawingChart provides function to add chart graphic frame by given sheet,
  812. // drawingXML, cell, width, height, relationship index and format sets.
  813. func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rID int, formatSet *formatPicture) {
  814. cell = strings.ToUpper(cell)
  815. fromCol := string(strings.Map(letterOnlyMapF, cell))
  816. fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
  817. row := fromRow - 1
  818. col := titleToNumber(fromCol)
  819. width = int(float64(width) * formatSet.XScale)
  820. height = int(float64(height) * formatSet.YScale)
  821. colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, col, row, formatSet.OffsetX, formatSet.OffsetY, width, height)
  822. content := encodeWsDr{}
  823. content.WsDr.A = NameSpaceDrawingML
  824. content.WsDr.Xdr = NameSpaceSpreadSheetDrawing
  825. cNvPrID := 1
  826. _, ok := f.XLSX[drawingXML]
  827. if ok { // Append Model
  828. decodeWsDr := decodeWsDr{}
  829. xml.Unmarshal([]byte(f.readXML(drawingXML)), &decodeWsDr)
  830. cNvPrID = len(decodeWsDr.TwoCellAnchor) + 1
  831. for _, v := range decodeWsDr.OneCellAnchor {
  832. content.WsDr.OneCellAnchor = append(content.WsDr.OneCellAnchor, &xlsxCellAnchor{
  833. EditAs: v.EditAs,
  834. GraphicFrame: v.Content,
  835. })
  836. }
  837. for _, v := range decodeWsDr.TwoCellAnchor {
  838. content.WsDr.TwoCellAnchor = append(content.WsDr.TwoCellAnchor, &xlsxCellAnchor{
  839. EditAs: v.EditAs,
  840. GraphicFrame: v.Content,
  841. })
  842. }
  843. }
  844. twoCellAnchor := xlsxCellAnchor{}
  845. twoCellAnchor.EditAs = "oneCell"
  846. from := xlsxFrom{}
  847. from.Col = colStart
  848. from.ColOff = formatSet.OffsetX * EMU
  849. from.Row = rowStart
  850. from.RowOff = formatSet.OffsetY * EMU
  851. to := xlsxTo{}
  852. to.Col = colEnd
  853. to.ColOff = x2 * EMU
  854. to.Row = rowEnd
  855. to.RowOff = y2 * EMU
  856. twoCellAnchor.From = &from
  857. twoCellAnchor.To = &to
  858. graphicFrame := graphicFrame{
  859. GraphicFrame: &xlsxGraphicFrame{
  860. NvGraphicFramePr: xlsxNvGraphicFramePr{
  861. CNvPr: &xlsxCNvPr{
  862. ID: cNvPrID,
  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. }
  878. graphic, _ := xml.Marshal(graphicFrame)
  879. twoCellAnchor.GraphicFrame = string(graphic[14 : len(graphic)-15])
  880. twoCellAnchor.ClientData = &xlsxClientData{
  881. FLocksWithSheet: formatSet.FLocksWithSheet,
  882. FPrintsWithSheet: formatSet.FPrintsWithSheet,
  883. }
  884. content.WsDr.TwoCellAnchor = append(content.WsDr.TwoCellAnchor, &twoCellAnchor)
  885. output, err := xml.Marshal(content)
  886. if err != nil {
  887. fmt.Println(err)
  888. }
  889. // Create replacer with pairs as arguments and replace all pairs.
  890. r := strings.NewReplacer("<encodeWsDr>", "", "</encodeWsDr>", "")
  891. result := r.Replace(string(output))
  892. f.saveFileList(drawingXML, result)
  893. }