Skip to content

Commit 1787c35

Browse files
committed
Use format string for chart dimension.
Signed-off-by: Eugene Dzhurinsky <jdevelop@gmail.com>
1 parent e09e47d commit 1787c35

File tree

4 files changed

+21
-45
lines changed

4 files changed

+21
-45
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func main() {
109109
for k, v := range values {
110110
xlsx.SetCellValue("Sheet1", k, v)
111111
}
112-
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))
112+
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"}}`)
113113
// Save xlsx file by the given path.
114114
err := xlsx.SaveAs("./Book1.xlsx")
115115
if err != nil {

chart.go

+6-36
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ var (
194194
// chart with default value.
195195
func parseFormatChartSet(formatSet string) *formatChart {
196196
format := formatChart{
197+
Dimension: formatChartDimension{
198+
Width: 480,
199+
Height: 290,
200+
},
197201
Format: formatPicture{
198202
FPrintsWithSheet: true,
199203
FLocksWithSheet: false,
@@ -351,13 +355,7 @@ func parseFormatChartSet(formatSet string) *formatChart {
351355
// maximum: Specifies that the fixed maximum, 0 is auto. The maximum property is optional. The default value is auto.
352356
// minimum: Specifies that the fixed minimum, 0 is auto. The minimum property is optional. The default value is auto.
353357
//
354-
func (f *File) AddChart(sheet, cell, format string, opts ...chartOpts) {
355-
356-
var defOpts = defaultChartOptions
357-
for _, optF := range opts {
358-
optF(&defOpts)
359-
}
360-
358+
func (f *File) AddChart(sheet, cell, format string) {
361359
formatSet := parseFormatChartSet(format)
362360
// Read sheet data.
363361
xlsx := f.workSheetReader(sheet)
@@ -367,40 +365,12 @@ func (f *File) AddChart(sheet, cell, format string, opts ...chartOpts) {
367365
drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
368366
drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
369367
drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
370-
f.addDrawingChart(sheet, drawingXML, cell, defOpts.width, defOpts.height, drawingRID, &formatSet.Format)
368+
f.addDrawingChart(sheet, drawingXML, cell, formatSet.Dimension.Width, formatSet.Dimension.Height, drawingRID, &formatSet.Format)
371369
f.addChart(formatSet)
372370
f.addContentTypePart(chartID, "chart")
373371
f.addContentTypePart(drawingID, "drawings")
374372
}
375373

376-
type chartOptions struct {
377-
width int
378-
height int
379-
}
380-
381-
var defaultChartOptions = chartOptions{
382-
width: 480,
383-
height: 290,
384-
}
385-
386-
type chartOpts func(opts *chartOptions)
387-
388-
// ChartWidth sets the chart width.
389-
func ChartWidth(width int) chartOpts {
390-
return func(opts *chartOptions) {
391-
opts.width = width
392-
return
393-
}
394-
}
395-
396-
// ChartHeight sets the chart height.
397-
func ChartHeight(height int) chartOpts {
398-
return func(opts *chartOptions) {
399-
opts.height = height
400-
return
401-
}
402-
}
403-
404374
// countCharts provides function to get chart files count storage in the
405375
// folder xl/charts.
406376
func (f *File) countCharts() int {

chart_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestChartSize(t *testing.T) {
1919
for k, v := range values {
2020
xlsx.SetCellValue("Sheet1", k, v)
2121
}
22-
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))
22+
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"}}`)
2323
// Save xlsx file by the given path.
2424
err := xlsx.Write(&buffer)
2525
if err != nil {

xmlChart.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -506,15 +506,21 @@ type formatChartAxis struct {
506506
NameLayout formatLayout `json:"name_layout"`
507507
}
508508

509+
type formatChartDimension struct {
510+
Width int `json:"width"`
511+
Height int `json:"height"`
512+
}
513+
509514
// formatChart directly maps the format settings of the chart.
510515
type formatChart struct {
511-
Type string `json:"type"`
512-
Series []formatChartSeries `json:"series"`
513-
Format formatPicture `json:"format"`
514-
Legend formatChartLegend `json:"legend"`
515-
Title formatChartTitle `json:"title"`
516-
XAxis formatChartAxis `json:"x_axis"`
517-
YAxis formatChartAxis `json:"y_axis"`
516+
Type string `json:"type"`
517+
Series []formatChartSeries `json:"series"`
518+
Format formatPicture `json:"format"`
519+
Dimension formatChartDimension `json:"dimension"`
520+
Legend formatChartLegend `json:"legend"`
521+
Title formatChartTitle `json:"title"`
522+
XAxis formatChartAxis `json:"x_axis"`
523+
YAxis formatChartAxis `json:"y_axis"`
518524
Chartarea struct {
519525
Border struct {
520526
None bool `json:"none"`

0 commit comments

Comments
 (0)