2018-05-06 09:03:03 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
|
|
|
"testing"
|
2018-12-27 18:51:44 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-05-06 09:03:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestChartSize(t *testing.T) {
|
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
|
|
|
categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
|
|
|
|
values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
|
|
|
|
xlsx := NewFile()
|
|
|
|
for k, v := range categories {
|
|
|
|
xlsx.SetCellValue("Sheet1", k, v)
|
|
|
|
}
|
|
|
|
for k, v := range values {
|
|
|
|
xlsx.SetCellValue("Sheet1", k, v)
|
|
|
|
}
|
2018-05-09 19:44:04 +08:00
|
|
|
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"}}`)
|
2018-05-06 09:03:03 +08:00
|
|
|
// Save xlsx file by the given path.
|
|
|
|
err := xlsx.Write(&buffer)
|
2018-12-27 18:51:44 +08:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
2018-05-06 09:03:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
newFile, err := OpenReader(&buffer)
|
2018-12-27 18:51:44 +08:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
2018-05-06 09:03:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
chartsNum := newFile.countCharts()
|
2018-12-27 18:51:44 +08:00
|
|
|
if !assert.Equal(t, 1, chartsNum, "Expected 1 chart, actual %d", chartsNum) {
|
|
|
|
t.FailNow()
|
2018-05-06 09:03:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
workdir decodeWsDr
|
|
|
|
anchor decodeTwoCellAnchor
|
|
|
|
)
|
|
|
|
|
|
|
|
content, ok := newFile.XLSX["xl/drawings/drawing1.xml"]
|
2018-12-27 18:51:44 +08:00
|
|
|
assert.True(t, ok, "Can't open the chart")
|
2018-05-06 09:03:03 +08:00
|
|
|
|
|
|
|
err = xml.Unmarshal([]byte(content), &workdir)
|
2018-12-27 18:51:44 +08:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
2018-05-06 09:03:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = xml.Unmarshal([]byte("<decodeTwoCellAnchor>"+workdir.TwoCellAnchor[0].Content+"</decodeTwoCellAnchor>"), &anchor)
|
2018-12-27 18:51:44 +08:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
2018-05-06 09:03:03 +08:00
|
|
|
}
|
|
|
|
|
2018-12-27 18:51:44 +08:00
|
|
|
if !assert.Equal(t, 4, anchor.From.Col, "Expected 'from' column 4") ||
|
|
|
|
!assert.Equal(t, 3, anchor.From.Row, "Expected 'from' row 3") {
|
|
|
|
|
|
|
|
t.FailNow()
|
2018-05-06 09:03:03 +08:00
|
|
|
}
|
|
|
|
|
2018-12-27 18:51:44 +08:00
|
|
|
if !assert.Equal(t, 14, anchor.To.Col, "Expected 'to' column 14") ||
|
|
|
|
!assert.Equal(t, 27, anchor.To.Row, "Expected 'to' row 27") {
|
|
|
|
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2018-05-06 09:03:03 +08:00
|
|
|
}
|