Skip to content

Commit

Permalink
spreadsheet: working line chart
Browse files Browse the repository at this point in the history
- needs major cleanup, but it displays in Libre
  • Loading branch information
tbaliance committed Sep 4, 2017
1 parent 28e4b68 commit f959cb0
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 11 deletions.
24 changes: 24 additions & 0 deletions chart/numberdatasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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"

type NumberDataSource struct {
x *crt.CT_NumDataSource
}

func MakeNumberDataSource(x *crt.CT_NumDataSource) NumberDataSource {
return NumberDataSource{x}
}

func (n NumberDataSource) SetReference(s string) {
n.x.Choice = crt.NewCT_NumDataSourceChoice()
n.x.Choice.NumRef = crt.NewCT_NumRef()
n.x.Choice.NumRef.F = s
}
10 changes: 5 additions & 5 deletions filenames.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ func RelativeFilename(dt DocType, typ string, index int) string {

case StylesType:
return "styles.xml"
case ChartType:
case ChartType, ChartContentType:
return fmt.Sprintf("../charts/chart%d.xml", index)
case DrawingType:
return fmt.Sprintf("drawings/drawing%d.xml", index)
case DrawingType, DrawingContentType:
return fmt.Sprintf("../drawings/drawing%d.xml", index)

case ThemeType, ThemeContentType:
return fmt.Sprintf("theme/theme%d.xml", index)
Expand Down Expand Up @@ -96,15 +96,15 @@ func AbsoluteFilename(dt DocType, typ string, index int) string {
return "xl/styles.xml"
}

case ChartType:
case ChartType, ChartContentType:
switch dt {
case DocTypeSpreadsheet:
return fmt.Sprintf("xl/charts/chart%d.xml", index)
default:
log.Printf("unsupported type %s pair and %v", typ, dt)
}

case DrawingType:
case DrawingType, DrawingContentType:
switch dt {
case DocTypeSpreadsheet:
return fmt.Sprintf("xl/drawings/drawing%d.xml", index)
Expand Down
2 changes: 2 additions & 0 deletions schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const (
CommentsType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"
ThumbnailType = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail"
DrawingType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"
DrawingContentType = "application/vnd.openxmlformats-officedocument.drawing+xml"
ChartType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"
ChartContentType = "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"

ExtendedPropertiesType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"
CorePropertiesType = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"
Expand Down
10 changes: 10 additions & 0 deletions spreadsheet/drawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func (d Drawing) AddChart() Chart {
chart := crt.NewChartSpace()
d.wb.charts = append(d.wb.charts, chart)

fn := gooxml.AbsoluteFilename(gooxml.DocTypeSpreadsheet, gooxml.ChartContentType, len(d.wb.charts))
d.wb.ContentTypes.AddOverride(fn, gooxml.ChartContentType)

var chartID string
// add relationship from drawing to the chart
for i, dr := range d.wb.drawings {
Expand All @@ -42,7 +45,14 @@ func (d Drawing) AddChart() Chart {

// maybe use a one cell anchor?
tca := sd.NewCT_TwoCellAnchor()
tca.EditAsAttr = sd.ST_EditAsOneCell
tca.From.Col = 5
tca.From.Row = 0
tca.To.Col = 10
tca.To.Row = 20

d.x.EG_Anchor = []*sd.EG_Anchor{&sd.EG_Anchor{TwoCellAnchor: tca}}

tca.Choice = &sd.EG_ObjectChoicesChoice{}
tca.Choice.GraphicFrame = sd.NewCT_GraphicalObjectFrame()
tca.Choice.GraphicFrame.Graphic = dml.NewGraphic()
Expand Down
12 changes: 12 additions & 0 deletions spreadsheet/linechartseries.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,15 @@ func (c LineChartSeries) Axis() chart.AxisDataSource {
}
return chart.MakeAxisDataSource(c.x.Cat)
}

func (c LineChartSeries) Values() chart.NumberDataSource {
if c.x.Val == nil {
c.x.Val = crt.NewCT_NumDataSource()
}
return chart.MakeNumberDataSource(c.x.Val)
}

func (c LineChartSeries) SetSmooth(b bool) {
c.x.Smooth = crt.NewCT_Boolean()
c.x.Smooth.ValAttr = &b
}
24 changes: 24 additions & 0 deletions spreadsheet/sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package spreadsheet

import (
"baliance.com/gooxml"
"baliance.com/gooxml/common"
sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml"
)

Expand Down Expand Up @@ -56,3 +57,26 @@ func (s Sheet) Rows() []Row {
}
return ret
}

// SetDrawing sets the worksheet drawing. A worksheet can have a reference to a
// single drawing, but the drawing can have many charts.
func (s Sheet) SetDrawing(d Drawing) {
var rel common.Relationships
for i, wks := range s.w.xws {
if wks == s.ws {
rel = s.w.xwsRels[i]
break
}
}
// add relationship from drawing to the sheet
var drawingID string
for i, dr := range d.wb.drawings {
if dr == d.x {
rel := rel.AddAutoRelationship(gooxml.DocTypeSpreadsheet, i+1, gooxml.DrawingType)
drawingID = rel.ID()
break
}
}
s.ws.Drawing = sml.NewCT_Drawing()
s.ws.Drawing.IdAttr = drawingID
}
13 changes: 7 additions & 6 deletions spreadsheet/workbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func (wb *Workbook) AddSheet() Sheet {
ws.Dimension = sml.NewCT_SheetDimension()
ws.Dimension.RefAttr = "A1"
wb.xws = append(wb.xws, ws)
wb.xwsRels = append(wb.xwsRels, common.NewRelationships())
wsRel := common.NewRelationships()
wb.xwsRels = append(wb.xwsRels, wsRel)
ws.SheetData = sml.NewCT_SheetData()

dt := gooxml.DocTypeSpreadsheet
Expand All @@ -78,7 +79,6 @@ func (wb *Workbook) AddSheet() Sheet {
rs.IdAttr = rid.ID()

// add the content type

wb.ContentTypes.AddOverride(gooxml.AbsoluteFilename(dt, gooxml.WorksheetContentType, len(wb.x.Sheets.Sheet)),
gooxml.WorksheetContentType)

Expand Down Expand Up @@ -126,9 +126,6 @@ func (wb *Workbook) Save(w io.Writer) error {
return err
}

if err := zippkg.MarshalXMLByType(z, dt, gooxml.SharedStingsType, wb.SharedStrings.X()); err != nil {
return err
}
for i, thm := range wb.themes {
if err := zippkg.MarshalXMLByTypeIndex(z, dt, gooxml.ThemeType, i+1, thm); err != nil {
return err
Expand All @@ -139,6 +136,10 @@ func (wb *Workbook) Save(w io.Writer) error {
zippkg.MarshalXML(z, fn, sheet)
zippkg.MarshalXML(z, zippkg.RelationsPathFor(fn), wb.xwsRels[i].X())
}
if err := zippkg.MarshalXMLByType(z, dt, gooxml.SharedStingsType, wb.SharedStrings.X()); err != nil {
return err
}

if wb.Thumbnail != nil {
fn := gooxml.AbsoluteFilename(dt, gooxml.ThumbnailType, 0)
tn, err := z.Create(fn)
Expand Down Expand Up @@ -300,7 +301,7 @@ func (wb *Workbook) AddDrawing() Drawing {
drawing := sd.NewWsDr()
wb.drawings = append(wb.drawings, drawing)
fn := gooxml.AbsoluteFilename(gooxml.DocTypeSpreadsheet, gooxml.DrawingType, len(wb.drawings))
wb.ContentTypes.AddOverride(fn, gooxml.DrawingType)
wb.ContentTypes.AddOverride(fn, gooxml.DrawingContentType)
wb.drawingRels = append(wb.drawingRels, common.NewRelationships())
return Drawing{wb, drawing}
}

0 comments on commit f959cb0

Please sign in to comment.