From 744babf1bde09f03b1e9a22251b8c333d2a8171f Mon Sep 17 00:00:00 2001 From: Todd Date: Thu, 7 Sep 2017 09:27:41 -0400 Subject: [PATCH] spreadsheet: add support for scatter charts --- _examples/spreadsheet/line-chart/main.go | 2 +- chart/chart.go | 11 +++ chart/linechartseries.go | 1 + chart/scatterchart.go | 48 +++++++++++ chart/scatterchartseries.go | 105 +++++++++++++++++++++++ 5 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 chart/scatterchart.go create mode 100644 chart/scatterchartseries.go diff --git a/_examples/spreadsheet/line-chart/main.go b/_examples/spreadsheet/line-chart/main.go index 5cc372b0..9a69828a 100644 --- a/_examples/spreadsheet/line-chart/main.go +++ b/_examples/spreadsheet/line-chart/main.go @@ -32,7 +32,7 @@ func main() { // make it a bit wider than the default anc.BottomRight().SetCol(15) - lc := chart.AddLineChart() + lc := chart.AddScatterChart() priceSeries := lc.AddSeries() priceSeries.SetText("Price") // Set a category axis reference on the first series to pull the product names diff --git a/chart/chart.go b/chart/chart.go index ea5b6908..dde1fe30 100644 --- a/chart/chart.go +++ b/chart/chart.go @@ -191,6 +191,17 @@ func (c Chart) AddSurface3DChart() Surface3DChart { return b } +// AddScatterChart adds a scatter (X/Y) chart. +func (c Chart) AddScatterChart() ScatterChart { + chc := crt.NewCT_PlotAreaChoice() + c.x.Chart.PlotArea.Choice = append(c.x.Chart.PlotArea.Choice, chc) + chc.ScatterChart = crt.NewCT_ScatterChart() + + b := ScatterChart{x: chc.ScatterChart} + b.InitializeDefaults() + return b +} + // Properties returns the chart's shape properties. func (c Chart) Properties() drawing.ShapeProperties { if c.x.SpPr == nil { diff --git a/chart/linechartseries.go b/chart/linechartseries.go index 22c9347c..aa02a441 100644 --- a/chart/linechartseries.go +++ b/chart/linechartseries.go @@ -15,6 +15,7 @@ import ( crt "baliance.com/gooxml/schema/schemas.openxmlformats.org/drawingml/2006/chart" ) +// LineChartSeries is the data series for a line chart. type LineChartSeries struct { x *crt.CT_LineSer } diff --git a/chart/scatterchart.go b/chart/scatterchart.go new file mode 100644 index 00000000..cb893aa1 --- /dev/null +++ b/chart/scatterchart.go @@ -0,0 +1,48 @@ +// 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 sales@baliance.com. + +package chart + +import ( + crt "baliance.com/gooxml/schema/schemas.openxmlformats.org/drawingml/2006/chart" +) + +type ScatterChart struct { + chartBase + x *crt.CT_ScatterChart +} + +// X returns the inner wrapped XML type. +func (c ScatterChart) X() *crt.CT_ScatterChart { + return c.x +} + +func (c ScatterChart) InitializeDefaults() { + c.x.ScatterStyle.ValAttr = crt.ST_ScatterStyleMarker +} + +// AddSeries adds a default series to a Scatter chart. +func (c ScatterChart) AddSeries() ScatterChartSeries { + color := c.nextColor(len(c.x.Ser)) + ser := crt.NewCT_ScatterSer() + 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) + + ls := ScatterChartSeries{ser} + ls.InitializeDefaults() + ls.Marker().Properties().LineProperties().SetSolidFill(color) + ls.Marker().Properties().SetSolidFill(color) + return ls +} + +// AddAxis adds an axis to a Scatter chart. +func (c ScatterChart) AddAxis(axis Axis) { + axisID := crt.NewCT_UnsignedInt() + axisID.ValAttr = axis.AxisID() + c.x.AxId = append(c.x.AxId, axisID) +} diff --git a/chart/scatterchartseries.go b/chart/scatterchartseries.go new file mode 100644 index 00000000..c1e8b0d8 --- /dev/null +++ b/chart/scatterchartseries.go @@ -0,0 +1,105 @@ +// 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 sales@baliance.com. + +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" +) + +// ScatterChartSeries is the data series for a scatter chart. +type ScatterChartSeries struct { + x *crt.CT_ScatterSer +} + +// X returns the inner wrapped XML type. +func (c ScatterChartSeries) X() *crt.CT_ScatterSer { + return c.x +} + +// Index returns the index of the series +func (c ScatterChartSeries) Index() uint32 { + return c.x.Idx.ValAttr +} + +// SetIndex sets the index of the series +func (c ScatterChartSeries) SetIndex(idx uint32) { + c.x.Idx.ValAttr = idx +} + +// Order returns the order of the series +func (c ScatterChartSeries) Order() uint32 { + return c.x.Order.ValAttr +} + +// SetOrder sets the order of the series +func (c ScatterChartSeries) SetOrder(idx uint32) { + c.x.Order.ValAttr = idx +} + +// SetText sets the series text +func (c ScatterChartSeries) SetText(s string) { + c.x.Tx = crt.NewCT_SerTx() + c.x.Tx.Choice.V = &s +} + +// Properties returns the line chart series shape properties. +func (c ScatterChartSeries) Properties() drawing.ShapeProperties { + if c.x.SpPr == nil { + c.x.SpPr = dml.NewCT_ShapeProperties() + } + return drawing.MakeShapeProperties(c.x.SpPr) +} + +// Marker returns the marker properties. +func (c ScatterChartSeries) Marker() Marker { + if c.x.Marker == nil { + c.x.Marker = crt.NewCT_Marker() + } + return MakeMarker(c.x.Marker) +} + +// Labels returns the data label properties. +func (c ScatterChartSeries) Labels() DataLabels { + if c.x.DLbls == nil { + c.x.DLbls = crt.NewCT_DLbls() + } + return MakeDataLabels(c.x.DLbls) +} + +func (c ScatterChartSeries) CategoryAxis() CategoryAxisDataSource { + if c.x.XVal == nil { + c.x.XVal = crt.NewCT_AxDataSource() + } + return MakeAxisDataSource(c.x.XVal) +} + +func (c ScatterChartSeries) Values() NumberDataSource { + if c.x.YVal == nil { + c.x.YVal = crt.NewCT_NumDataSource() + } + return MakeNumberDataSource(c.x.YVal) +} + +func (c ScatterChartSeries) SetSmooth(b bool) { + c.x.Smooth = crt.NewCT_Boolean() + c.x.Smooth.ValAttr = &b +} + +func (c ScatterChartSeries) InitializeDefaults() { + // turn off the line + c.Properties().LineProperties().SetNoFill() + c.Marker().SetSymbol(crt.ST_MarkerStyleAuto) + c.Labels().SetShowLegendKey(false) + c.Labels().SetShowValue(true) + c.Labels().SetShowPercent(false) + c.Labels().SetShowCategoryName(false) + c.Labels().SetShowSeriesName(false) + c.Labels().SetShowLeaderLines(false) +}