Skip to content

Commit e09e47d

Browse files
committed
Custom chart size.
Added helper functions to set the chart size. Added the unit test Signed-off-by: Eugene Dzhurinsky <jdevelop@gmail.com>
1 parent e8961f0 commit e09e47d

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
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"}}`)
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))
113113
// Save xlsx file by the given path.
114114
err := xlsx.SaveAs("./Book1.xlsx")
115115
if err != nil {

chart.go

+36-2
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,13 @@ func parseFormatChartSet(formatSet string) *formatChart {
351351
// maximum: Specifies that the fixed maximum, 0 is auto. The maximum property is optional. The default value is auto.
352352
// minimum: Specifies that the fixed minimum, 0 is auto. The minimum property is optional. The default value is auto.
353353
//
354-
func (f *File) AddChart(sheet, cell, format string) {
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+
355361
formatSet := parseFormatChartSet(format)
356362
// Read sheet data.
357363
xlsx := f.workSheetReader(sheet)
@@ -361,12 +367,40 @@ func (f *File) AddChart(sheet, cell, format string) {
361367
drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
362368
drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
363369
drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
364-
f.addDrawingChart(sheet, drawingXML, cell, 480, 290, drawingRID, &formatSet.Format)
370+
f.addDrawingChart(sheet, drawingXML, cell, defOpts.width, defOpts.height, drawingRID, &formatSet.Format)
365371
f.addChart(formatSet)
366372
f.addContentTypePart(chartID, "chart")
367373
f.addContentTypePart(drawingID, "drawings")
368374
}
369375

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+
370404
// countCharts provides function to get chart files count storage in the
371405
// folder xl/charts.
372406
func (f *File) countCharts() int {

chart_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package excelize
2+
3+
import (
4+
"bytes"
5+
"encoding/xml"
6+
"testing"
7+
)
8+
9+
func TestChartSize(t *testing.T) {
10+
11+
var buffer bytes.Buffer
12+
13+
categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
14+
values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
15+
xlsx := NewFile()
16+
for k, v := range categories {
17+
xlsx.SetCellValue("Sheet1", k, v)
18+
}
19+
for k, v := range values {
20+
xlsx.SetCellValue("Sheet1", k, v)
21+
}
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))
23+
// Save xlsx file by the given path.
24+
err := xlsx.Write(&buffer)
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
29+
newFile, err := OpenReader(&buffer)
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
chartsNum := newFile.countCharts()
35+
if chartsNum != 1 {
36+
t.Fatalf("Expected 1 chart, actual %d", chartsNum)
37+
}
38+
39+
var (
40+
workdir decodeWsDr
41+
anchor decodeTwoCellAnchor
42+
)
43+
44+
content, ok := newFile.XLSX["xl/drawings/drawing1.xml"]
45+
if !ok {
46+
t.Fatal("Can't open the chart")
47+
}
48+
49+
err = xml.Unmarshal([]byte(content), &workdir)
50+
if err != nil {
51+
t.Fatal(err)
52+
}
53+
54+
err = xml.Unmarshal([]byte("<decodeTwoCellAnchor>"+workdir.TwoCellAnchor[0].Content+"</decodeTwoCellAnchor>"), &anchor)
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
if anchor.From.Col != 4 || anchor.From.Row != 3 {
60+
t.Fatalf("From: Expected column 4, row 3, actual column %d, row %d", anchor.From.Col, anchor.From.Row)
61+
}
62+
if anchor.To.Col != 14 || anchor.To.Row != 27 {
63+
t.Fatalf("To: Expected column 14, row 27, actual column %d, row %d", anchor.To.Col, anchor.To.Row)
64+
}
65+
66+
}

0 commit comments

Comments
 (0)