chart.go 28 KB

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