Skip to content

Commit

Permalink
spreadsheet: add pie chart support
Browse files Browse the repository at this point in the history
  • Loading branch information
tbaliance committed Sep 6, 2017
1 parent 38db3f5 commit 144a590
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
54 changes: 54 additions & 0 deletions _examples/spreadsheet/pie-chart/main.go
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 added _examples/spreadsheet/pie-chart/pie-chart.xlsx
Binary file not shown.
11 changes: 11 additions & 0 deletions chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ func (c Chart) AddRadarChart() RadarChart {
return b
}

// AddPieChart adds a new pie chart to a chart.
func (c Chart) AddPieChart() PieChart {
chc := crt.NewCT_PlotAreaChoice()
c.x.Chart.PlotArea.Choice = append(c.x.Chart.PlotArea.Choice, chc)
chc.PieChart = crt.NewCT_PieChart()

b := PieChart{x: chc.PieChart}
b.InitializeDefaults()
return b
}

// Properties returns the chart's shape properties.
func (c Chart) Properties() drawing.ShapeProperties {
if c.x.SpPr == nil {
Expand Down
40 changes: 40 additions & 0 deletions chart/piechart.go
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
}
59 changes: 59 additions & 0 deletions chart/piechartseries.go
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)
}

0 comments on commit 144a590

Please sign in to comment.