Skip to content

Commit b5655ce

Browse files
authored
Merge pull request qax-os#219 from jdevelop/feature/chart-size
Added helper functions to set the chart size.
2 parents 18aa606 + 1787c35 commit b5655ce

File tree

4 files changed

+85
-9
lines changed

4 files changed

+85
-9
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","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

+5-1
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,
@@ -361,7 +365,7 @@ func (f *File) AddChart(sheet, cell, format string) {
361365
drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
362366
drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
363367
drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
364-
f.addDrawingChart(sheet, drawingXML, cell, 480, 290, drawingRID, &formatSet.Format)
368+
f.addDrawingChart(sheet, drawingXML, cell, formatSet.Dimension.Width, formatSet.Dimension.Height, drawingRID, &formatSet.Format)
365369
f.addChart(formatSet)
366370
f.addContentTypePart(chartID, "chart")
367371
f.addContentTypePart(drawingID, "drawings")

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","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"}}`)
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+
}

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)