chart.go 29 KB

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