chart.go 28 KB

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