forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2017 Baliance. All rights reserved. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"baliance.com/gooxml/spreadsheet" | ||
) | ||
|
||
func main() { | ||
ss := spreadsheet.New() | ||
sheet := ss.AddSheet() | ||
|
||
// Create all of our data | ||
row := sheet.AddRow() | ||
row.AddCell().SetString("Item") | ||
row.AddCell().SetString("Price") | ||
row.AddCell().SetString("# Sold") | ||
row.AddCell().SetString("Total") | ||
for r := 0; r < 5; r++ { | ||
row := sheet.AddRow() | ||
row.AddCell().SetString(fmt.Sprintf("Product %d", r+1)) | ||
row.AddCell().SetNumber(1.23 * float64(r+1)) | ||
row.AddCell().SetNumber(float64(r%3 + 1)) | ||
row.AddCell().SetFormulaRaw(fmt.Sprintf("C%d*B%d", r+2, r+2)) | ||
} | ||
|
||
// Charts need to reside in a drawing | ||
dwng := ss.AddDrawing() | ||
chart, anc := dwng.AddChart() | ||
// make it a bit wider than the default | ||
anc.BottomRight().SetCol(15) | ||
|
||
lc := chart.AddPieChart() | ||
priceSeries := lc.AddSeries() | ||
priceSeries.SetText("Price") | ||
// Set a category axis reference on the first series to pull the product names | ||
priceSeries.CategoryAxis().SetReference(`'Sheet 1'!A2:A6`) | ||
priceSeries.Values().SetReference(`'Sheet 1'!B2:B6`) | ||
|
||
// add a title and legend | ||
title := chart.AddTitle() | ||
title.SetText("Items Sold") | ||
chart.AddLegend() | ||
|
||
// and finally add the chart to the sheet | ||
sheet.SetDrawing(dwng) | ||
|
||
if err := ss.Validate(); err != nil { | ||
log.Fatalf("error validating sheet: %s", err) | ||
} | ||
ss.SaveToFile("pie-chart.xlsx") | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2017 Baliance. All rights reserved. | ||
// | ||
// Use of this source code is governed by the terms of the Affero GNU General | ||
// Public License version 3.0 as published by the Free Software Foundation and | ||
// appearing in the file LICENSE included in the packaging of this file. A | ||
// commercial license can be purchased by contacting [email protected]. | ||
|
||
package chart | ||
|
||
import crt "baliance.com/gooxml/schema/schemas.openxmlformats.org/drawingml/2006/chart" | ||
import "baliance.com/gooxml" | ||
|
||
// PieChart is a Pie chart. | ||
type PieChart struct { | ||
chartBase | ||
x *crt.CT_PieChart | ||
} | ||
|
||
// X returns the inner wrapped XML type. | ||
func (c PieChart) X() *crt.CT_PieChart { | ||
return c.x | ||
} | ||
|
||
// InitializeDefaults the bar chart to its defaults | ||
func (c PieChart) InitializeDefaults() { | ||
c.x.VaryColors = crt.NewCT_Boolean() | ||
c.x.VaryColors.ValAttr = gooxml.Bool(true) | ||
} | ||
|
||
// AddSeries adds a default series to an Pie chart. | ||
func (c PieChart) AddSeries() PieChartSeries { | ||
ser := crt.NewCT_PieSer() | ||
c.x.Ser = append(c.x.Ser, ser) | ||
ser.Idx.ValAttr = uint32(len(c.x.Ser) - 1) | ||
ser.Order.ValAttr = uint32(len(c.x.Ser) - 1) | ||
|
||
bs := PieChartSeries{ser} | ||
bs.InitializeDefaults() | ||
return bs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2017 Baliance. All rights reserved. | ||
// | ||
// Use of this source code is governed by the terms of the Affero GNU General | ||
// Public License version 3.0 as published by the Free Software Foundation and | ||
// appearing in the file LICENSE included in the packaging of this file. A | ||
// commercial license can be purchased by contacting [email protected]. | ||
|
||
package chart | ||
|
||
import ( | ||
"baliance.com/gooxml/drawing" | ||
dml "baliance.com/gooxml/schema/schemas.openxmlformats.org/drawingml" | ||
crt "baliance.com/gooxml/schema/schemas.openxmlformats.org/drawingml/2006/chart" | ||
) | ||
|
||
// PieChartSeries is a series to be used on an Pie chart. | ||
type PieChartSeries struct { | ||
x *crt.CT_PieSer | ||
} | ||
|
||
// X returns the inner wrapped XML type. | ||
func (c PieChartSeries) X() *crt.CT_PieSer { | ||
return c.x | ||
} | ||
|
||
// InitializeDefaults initializes an Pie series to the default values. | ||
func (c PieChartSeries) InitializeDefaults() { | ||
|
||
} | ||
|
||
// SetText sets the series text. | ||
func (c PieChartSeries) SetText(s string) { | ||
c.x.Tx = crt.NewCT_SerTx() | ||
c.x.Tx.Choice.V = &s | ||
} | ||
|
||
// CategoryAxis returns the category data source. | ||
func (c PieChartSeries) CategoryAxis() CategoryAxisDataSource { | ||
if c.x.Cat == nil { | ||
c.x.Cat = crt.NewCT_AxDataSource() | ||
} | ||
return MakeAxisDataSource(c.x.Cat) | ||
} | ||
|
||
// Values returns the value data source. | ||
func (c PieChartSeries) Values() NumberDataSource { | ||
if c.x.Val == nil { | ||
c.x.Val = crt.NewCT_NumDataSource() | ||
} | ||
return MakeNumberDataSource(c.x.Val) | ||
} | ||
|
||
// Properties returns the bar chart series shape properties. | ||
func (c PieChartSeries) Properties() drawing.ShapeProperties { | ||
if c.x.SpPr == nil { | ||
c.x.SpPr = dml.NewCT_ShapeProperties() | ||
} | ||
return drawing.MakeShapeProperties(c.x.SpPr) | ||
} |