From d78ac4108c2fd8ccf4a0d150302a70031d6f889b Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Sun, 23 Apr 2017 00:39:14 +0800 Subject: [PATCH] - Update doc of function `AddChart()`; - Readme updated; - go test update --- README.md | 4 ++-- chart.go | 49 +++++++++++++++++++++--------------------------- excelize_test.go | 29 +++++++++++----------------- 3 files changed, 34 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 3f714548..0e131388 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ With Excelize chart generation and management is as easy as a few lines of code. ![Excelize](./test/images/chart.png "Excelize") -``` +```go package main import ( @@ -107,7 +107,7 @@ import ( ) func main() { - categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Large", "C1": "Apple", "D1": "Pear"} + 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 := excelize.CreateFile() for k, v := range categories { diff --git a/chart.go b/chart.go index c5ef6b8b..526544ba 100644 --- a/chart.go +++ b/chart.go @@ -70,22 +70,15 @@ func parseFormatChartSet(formatSet string) *formatChart { // ) // // func main() { +// 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 := excelize.CreateFile() -// xlsx.SetCellValue("SHEET1", "A30", "Small") -// xlsx.SetCellValue("SHEET1", "A31", "Normal") -// xlsx.SetCellValue("SHEET1", "A32", "Large") -// xlsx.SetCellValue("SHEET1", "B29", "Apple") -// xlsx.SetCellValue("SHEET1", "C29", "Prange") -// xlsx.SetCellValue("SHEET1", "D29", "Pear") -// xlsx.SetCellValue("SHEET1", "B30", 2) -// xlsx.SetCellValue("SHEET1", "C30", 3) -// xlsx.SetCellValue("SHEET1", "D30", 3) -// xlsx.SetCellValue("SHEET1", "B31", 5) -// xlsx.SetCellValue("SHEET1", "C31", 2) -// xlsx.SetCellValue("SHEET1", "D31", 4) -// xlsx.SetCellValue("SHEET1", "B32", 6) -// xlsx.SetCellValue("SHEET1", "C32", 7) -// xlsx.SetCellValue("SHEET1", "D32", 8) +// for k, v := range categories { +// xlsx.SetCellValue("Sheet1", k, v) +// } +// for k, v := range values { +// xlsx.SetCellValue("Sheet1", k, v) +// } // xlsx.AddChart("SHEET1", "F2", `{"type":"bar3D","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit Line Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`) // // Save xlsx file by the given path. // err := xlsx.WriteTo("./tmp/Workbook.xlsx") @@ -401,8 +394,8 @@ func (f *File) drawBarChart(formatSet *formatChart) *cPlotArea { Ser: f.drawChartSeries(formatSet), DLbls: f.drawChartDLbls(formatSet), AxID: []*attrValInt{ - &attrValInt{Val: 754001152}, - &attrValInt{Val: 753999904}, + {Val: 754001152}, + {Val: 753999904}, }, }, CatAx: f.drawPlotAreaCatAx(), @@ -427,8 +420,8 @@ func (f *File) drawBar3DChart(formatSet *formatChart) *cPlotArea { Ser: f.drawChartSeries(formatSet), DLbls: f.drawChartDLbls(formatSet), AxID: []*attrValInt{ - &attrValInt{Val: 754001152}, - &attrValInt{Val: 753999904}, + {Val: 754001152}, + {Val: 753999904}, }, }, CatAx: f.drawPlotAreaCatAx(), @@ -467,8 +460,8 @@ func (f *File) drawLineChart(formatSet *formatChart) *cPlotArea { Val: false, }, AxID: []*attrValInt{ - &attrValInt{Val: 754001152}, - &attrValInt{Val: 753999904}, + {Val: 754001152}, + {Val: 753999904}, }, }, CatAx: f.drawPlotAreaCatAx(), @@ -516,8 +509,8 @@ func (f *File) drawRadarChart(formatSet *formatChart) *cPlotArea { Ser: f.drawChartSeries(formatSet), DLbls: f.drawChartDLbls(formatSet), AxID: []*attrValInt{ - &attrValInt{Val: 754001152}, - &attrValInt{Val: 753999904}, + {Val: 754001152}, + {Val: 753999904}, }, }, CatAx: f.drawPlotAreaCatAx(), @@ -539,8 +532,8 @@ func (f *File) drawScatterChart(formatSet *formatChart) *cPlotArea { Ser: f.drawChartSeries(formatSet), DLbls: f.drawChartDLbls(formatSet), AxID: []*attrValInt{ - &attrValInt{Val: 754001152}, - &attrValInt{Val: 753999904}, + {Val: 754001152}, + {Val: 753999904}, }, }, CatAx: f.drawPlotAreaCatAx(), @@ -599,7 +592,7 @@ func (f *File) drawChartSeriesSpPr(i int, formatSet *formatChart) *cSpPr { // drawChartSeriesDPt provides function to draw the c:dPt element by given data // index and format sets. func (f *File) drawChartSeriesDPt(i int, formatSet *formatChart) []*cDPt { - dpt := []*cDPt{&cDPt{ + dpt := []*cDPt{{ IDx: &attrValInt{Val: i}, Bubble3D: &attrValBool{Val: false}, SpPr: &cSpPr{ @@ -732,7 +725,7 @@ func (f *File) drawChartSeriesDLbls(formatSet *formatChart) *cDLbls { // drawPlotAreaCatAx provides function to draw the c:catAx element. func (f *File) drawPlotAreaCatAx() []*cAxs { return []*cAxs{ - &cAxs{ + { AxID: &attrValInt{Val: 754001152}, Scaling: &cScaling{ Orientation: &attrValString{Val: "minMax"}, @@ -761,7 +754,7 @@ func (f *File) drawPlotAreaCatAx() []*cAxs { // drawPlotAreaCatAx provides function to draw the c:valAx element. func (f *File) drawPlotAreaValAx() []*cAxs { return []*cAxs{ - &cAxs{ + { AxID: &attrValInt{Val: 753999904}, Scaling: &cScaling{ Orientation: &attrValString{Val: "minMax"}, diff --git a/excelize_test.go b/excelize_test.go index 6c484e30..6d47a72e 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -432,27 +432,20 @@ func TestAddChart(t *testing.T) { if err != nil { t.Log(err) } - xlsx.SetCellValue("SHEET1", "A30", "Small") - xlsx.SetCellValue("SHEET1", "A31", "Normal") - xlsx.SetCellValue("SHEET1", "A32", "Large") - xlsx.SetCellValue("SHEET1", "B29", "Apple") - xlsx.SetCellValue("SHEET1", "C29", "Prange") - xlsx.SetCellValue("SHEET1", "D29", "Pear") - xlsx.SetCellValue("SHEET1", "B30", 2) - xlsx.SetCellValue("SHEET1", "C30", 3) - xlsx.SetCellValue("SHEET1", "D30", 3) - xlsx.SetCellValue("SHEET1", "B31", 5) - xlsx.SetCellValue("SHEET1", "C31", 2) - xlsx.SetCellValue("SHEET1", "D31", 4) - xlsx.SetCellValue("SHEET1", "B32", 6) - xlsx.SetCellValue("SHEET1", "C32", 7) - xlsx.SetCellValue("SHEET1", "D32", 8) + categories := map[string]string{"A30": "Small", "A31": "Normal", "A32": "Large", "B29": "Apple", "C29": "Orange", "D29": "Pear"} + values := map[string]int{"B30": 2, "C30": 3, "D30": 3, "B31": 5, "C31": 2, "D31": 4, "B32": 6, "C32": 7, "D32": 8} + for k, v := range categories { + xlsx.SetCellValue("Sheet1", k, v) + } + for k, v := range values { + xlsx.SetCellValue("Sheet1", k, v) + } xlsx.AddChart("SHEET1", "P1", `{"type":"bar3D","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit 3D Bar Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`) xlsx.AddChart("SHEET1", "X1", `{"type":"bar","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"Fruit Bar Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`) - xlsx.AddChart("SHEET1", "P16", `{"type":"doughnut","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"right","show_legend_key":false},"title":{"name":"Fruit Doughnut Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`) + xlsx.AddChart("SHEET1", "P16", `{"type":"doughnut","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"right","show_legend_key":false},"title":{"name":"Fruit Doughnut Chart"},"plotarea":{"show_bubble_size":false,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":false,"show_val":false},"show_blanks_as":"zero"}`) xlsx.AddChart("SHEET1", "X16", `{"type":"line","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"top","show_legend_key":false},"title":{"name":"Fruit Line Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`) - xlsx.AddChart("SHEET1", "P30", `{"type":"pie3D","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit 3D Pie Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`) - xlsx.AddChart("SHEET1", "X30", `{"type":"pie","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit Pie Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"gap"}`) + xlsx.AddChart("SHEET1", "P30", `{"type":"pie3D","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit 3D Pie Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":false,"show_val":false},"show_blanks_as":"zero"}`) + xlsx.AddChart("SHEET1", "X30", `{"type":"pie","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit Pie Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":false,"show_val":false},"show_blanks_as":"gap"}`) xlsx.AddChart("SHEET2", "P1", `{"type":"radar","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"top_right","show_legend_key":false},"title":{"name":"Fruit Radar Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"span"}`) xlsx.AddChart("SHEET2", "X1", `{"type":"scatter","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit Scatter Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`) // Save xlsx file by the given path.