forked from p30928647/excelize
Custom chart size.
Added helper functions to set the chart size. Added the unit test Signed-off-by: Eugene Dzhurinsky <jdevelop@gmail.com>
This commit is contained in:
parent
e8961f0aff
commit
e09e47d988
|
@ -109,7 +109,7 @@ func main() {
|
|||
for k, v := range values {
|
||||
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"}}`)
|
||||
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))
|
||||
// Save xlsx file by the given path.
|
||||
err := xlsx.SaveAs("./Book1.xlsx")
|
||||
if err != nil {
|
||||
|
|
38
chart.go
38
chart.go
|
@ -351,7 +351,13 @@ func parseFormatChartSet(formatSet string) *formatChart {
|
|||
// 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.
|
||||
//
|
||||
func (f *File) AddChart(sheet, cell, format string) {
|
||||
func (f *File) AddChart(sheet, cell, format string, opts ...chartOpts) {
|
||||
|
||||
var defOpts = defaultChartOptions
|
||||
for _, optF := range opts {
|
||||
optF(&defOpts)
|
||||
}
|
||||
|
||||
formatSet := parseFormatChartSet(format)
|
||||
// Read sheet data.
|
||||
xlsx := f.workSheetReader(sheet)
|
||||
|
@ -361,12 +367,40 @@ func (f *File) AddChart(sheet, cell, format string) {
|
|||
drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
|
||||
drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
|
||||
drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
|
||||
f.addDrawingChart(sheet, drawingXML, cell, 480, 290, drawingRID, &formatSet.Format)
|
||||
f.addDrawingChart(sheet, drawingXML, cell, defOpts.width, defOpts.height, drawingRID, &formatSet.Format)
|
||||
f.addChart(formatSet)
|
||||
f.addContentTypePart(chartID, "chart")
|
||||
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
|
||||
// folder xl/charts.
|
||||
func (f *File) countCharts() int {
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
package excelize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"testing"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
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))
|
||||
// Save xlsx file by the given path.
|
||||
err := xlsx.Write(&buffer)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
newFile, err := OpenReader(&buffer)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
chartsNum := newFile.countCharts()
|
||||
if chartsNum != 1 {
|
||||
t.Fatalf("Expected 1 chart, actual %d", chartsNum)
|
||||
}
|
||||
|
||||
var (
|
||||
workdir decodeWsDr
|
||||
anchor decodeTwoCellAnchor
|
||||
)
|
||||
|
||||
content, ok := newFile.XLSX["xl/drawings/drawing1.xml"]
|
||||
if !ok {
|
||||
t.Fatal("Can't open the chart")
|
||||
}
|
||||
|
||||
err = xml.Unmarshal([]byte(content), &workdir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = xml.Unmarshal([]byte("<decodeTwoCellAnchor>"+workdir.TwoCellAnchor[0].Content+"</decodeTwoCellAnchor>"), &anchor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if anchor.From.Col != 4 || anchor.From.Row != 3 {
|
||||
t.Fatalf("From: Expected column 4, row 3, actual column %d, row %d", anchor.From.Col, anchor.From.Row)
|
||||
}
|
||||
if anchor.To.Col != 14 || anchor.To.Row != 27 {
|
||||
t.Fatalf("To: Expected column 14, row 27, actual column %d, row %d", anchor.To.Col, anchor.To.Row)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue