Ver Fonte

Use format string for chart dimension.

Signed-off-by: Eugene Dzhurinsky <jdevelop@gmail.com>
Eugene Dzhurinsky há 7 anos atrás
pai
commit
1787c3533b
4 ficheiros alterados com 21 adições e 45 exclusões
  1. 1 1
      README.md
  2. 6 36
      chart.go
  3. 1 1
      chart_test.go
  4. 13 7
      xmlChart.go

+ 1 - 1
README.md

@@ -109,7 +109,7 @@ func main() {
     for k, v := range values {
     for k, v := range values {
         xlsx.SetCellValue("Sheet1", k, v)
         xlsx.SetCellValue("Sheet1", k, v)
     }
     }
-    xlsx.AddChart("Sheet1", "E1", `{"type":"col3DClustered","series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`,excelize.ChartWidth(800), excelize.ChartHeight(600))
+    xlsx.AddChart("Sheet1", "E1", `{"type":"col3DClustered","dimension":{"width":640, "height":480},"series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`)
     // Save xlsx file by the given path.
     // Save xlsx file by the given path.
     err := xlsx.SaveAs("./Book1.xlsx")
     err := xlsx.SaveAs("./Book1.xlsx")
     if err != nil {
     if err != nil {

+ 6 - 36
chart.go

@@ -194,6 +194,10 @@ var (
 // chart with default value.
 // chart with default value.
 func parseFormatChartSet(formatSet string) *formatChart {
 func parseFormatChartSet(formatSet string) *formatChart {
 	format := formatChart{
 	format := formatChart{
+		Dimension: formatChartDimension{
+			Width:  480,
+			Height: 290,
+		},
 		Format: formatPicture{
 		Format: formatPicture{
 			FPrintsWithSheet: true,
 			FPrintsWithSheet: true,
 			FLocksWithSheet:  false,
 			FLocksWithSheet:  false,
@@ -351,13 +355,7 @@ func parseFormatChartSet(formatSet string) *formatChart {
 // maximum: Specifies that the fixed maximum, 0 is auto. The maximum property is optional. The default value is auto.
 // maximum: Specifies that the fixed maximum, 0 is auto. The maximum property is optional. The default value is auto.
 // minimum: Specifies that the fixed minimum, 0 is auto. The minimum property is optional. The default value is auto.
 // minimum: Specifies that the fixed minimum, 0 is auto. The minimum property is optional. The default value is auto.
 //
 //
-func (f *File) AddChart(sheet, cell, format string, opts ...chartOpts) {
-
-	var defOpts = defaultChartOptions
-	for _, optF := range opts {
-		optF(&defOpts)
-	}
-
+func (f *File) AddChart(sheet, cell, format string) {
 	formatSet := parseFormatChartSet(format)
 	formatSet := parseFormatChartSet(format)
 	// Read sheet data.
 	// Read sheet data.
 	xlsx := f.workSheetReader(sheet)
 	xlsx := f.workSheetReader(sheet)
@@ -367,40 +365,12 @@ func (f *File) AddChart(sheet, cell, format string, opts ...chartOpts) {
 	drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
 	drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
 	drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
 	drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
 	drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
 	drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
-	f.addDrawingChart(sheet, drawingXML, cell, defOpts.width, defOpts.height, drawingRID, &formatSet.Format)
+	f.addDrawingChart(sheet, drawingXML, cell, formatSet.Dimension.Width, formatSet.Dimension.Height, drawingRID, &formatSet.Format)
 	f.addChart(formatSet)
 	f.addChart(formatSet)
 	f.addContentTypePart(chartID, "chart")
 	f.addContentTypePart(chartID, "chart")
 	f.addContentTypePart(drawingID, "drawings")
 	f.addContentTypePart(drawingID, "drawings")
 }
 }
 
 
-type chartOptions struct {
-	width  int
-	height int
-}
-
-var defaultChartOptions = chartOptions{
-	width:  480,
-	height: 290,
-}
-
-type chartOpts func(opts *chartOptions)
-
-// ChartWidth sets the chart width.
-func ChartWidth(width int) chartOpts {
-	return func(opts *chartOptions) {
-		opts.width = width
-		return
-	}
-}
-
-// ChartHeight sets the chart height.
-func ChartHeight(height int) chartOpts {
-	return func(opts *chartOptions) {
-		opts.height = height
-		return
-	}
-}
-
 // countCharts provides function to get chart files count storage in the
 // countCharts provides function to get chart files count storage in the
 // folder xl/charts.
 // folder xl/charts.
 func (f *File) countCharts() int {
 func (f *File) countCharts() int {

+ 1 - 1
chart_test.go

@@ -19,7 +19,7 @@ func TestChartSize(t *testing.T) {
 	for k, v := range values {
 	for k, v := range values {
 		xlsx.SetCellValue("Sheet1", k, v)
 		xlsx.SetCellValue("Sheet1", k, v)
 	}
 	}
-	xlsx.AddChart("Sheet1", "E4", `{"type":"col3DClustered","series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`, ChartWidth(640), ChartHeight(480))
+	xlsx.AddChart("Sheet1", "E4", `{"type":"col3DClustered","dimension":{"width":640, "height":480},"series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`)
 	// Save xlsx file by the given path.
 	// Save xlsx file by the given path.
 	err := xlsx.Write(&buffer)
 	err := xlsx.Write(&buffer)
 	if err != nil {
 	if err != nil {

+ 13 - 7
xmlChart.go

@@ -506,15 +506,21 @@ type formatChartAxis struct {
 	NameLayout formatLayout `json:"name_layout"`
 	NameLayout formatLayout `json:"name_layout"`
 }
 }
 
 
+type formatChartDimension struct {
+	Width  int `json:"width"`
+	Height int `json:"height"`
+}
+
 // formatChart directly maps the format settings of the chart.
 // formatChart directly maps the format settings of the chart.
 type formatChart struct {
 type formatChart struct {
-	Type      string              `json:"type"`
-	Series    []formatChartSeries `json:"series"`
-	Format    formatPicture       `json:"format"`
-	Legend    formatChartLegend   `json:"legend"`
-	Title     formatChartTitle    `json:"title"`
-	XAxis     formatChartAxis     `json:"x_axis"`
-	YAxis     formatChartAxis     `json:"y_axis"`
+	Type      string               `json:"type"`
+	Series    []formatChartSeries  `json:"series"`
+	Format    formatPicture        `json:"format"`
+	Dimension formatChartDimension `json:"dimension"`
+	Legend    formatChartLegend    `json:"legend"`
+	Title     formatChartTitle     `json:"title"`
+	XAxis     formatChartAxis      `json:"x_axis"`
+	YAxis     formatChartAxis      `json:"y_axis"`
 	Chartarea struct {
 	Chartarea struct {
 		Border struct {
 		Border struct {
 			None bool `json:"none"`
 			None bool `json:"none"`