updates
This commit is contained in:
parent
59a5b2cbac
commit
7bb82ae691
6 changed files with 324 additions and 267 deletions
192
series.go
192
series.go
|
|
@ -1,121 +1,117 @@
|
|||
package chart
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
import "math"
|
||||
|
||||
"github.com/blendlabs/go-util"
|
||||
)
|
||||
|
||||
// Series is a entity data set.
|
||||
// Series is a entity data set. It constitutes an item to draw on the chart.
|
||||
// The series interface is the bare minimum you need to implement to draw something on a chart.
|
||||
type Series interface {
|
||||
GetName() string
|
||||
GetStyle() Style
|
||||
Render(c *Chart, r Renderer, canvasBox Box, xrange, yrange Range) error
|
||||
}
|
||||
|
||||
// ValueProvider is a series that is a set of values.
|
||||
type ValueProvider interface {
|
||||
Len() int
|
||||
|
||||
GetValue(index int) (float64, float64)
|
||||
}
|
||||
|
||||
// FormatterProvider is a series that has custom formatters.
|
||||
type FormatterProvider interface {
|
||||
GetXFormatter() Formatter
|
||||
GetYFormatter() Formatter
|
||||
}
|
||||
|
||||
// TimeSeries is a line on a chart.
|
||||
type TimeSeries struct {
|
||||
Name string
|
||||
Style Style
|
||||
|
||||
XValues []time.Time
|
||||
YValues []float64
|
||||
}
|
||||
|
||||
// GetName returns the name of the time series.
|
||||
func (ts TimeSeries) GetName() string {
|
||||
return ts.Name
|
||||
}
|
||||
|
||||
// GetStyle returns the line style.
|
||||
func (ts TimeSeries) GetStyle() Style {
|
||||
return ts.Style
|
||||
}
|
||||
|
||||
// Len returns the number of elements in the series.
|
||||
func (ts TimeSeries) Len() int {
|
||||
return len(ts.XValues)
|
||||
}
|
||||
|
||||
// GetValue gets a value at a given index.
|
||||
func (ts TimeSeries) GetValue(index int) (x float64, y float64) {
|
||||
x = float64(ts.XValues[index].Unix())
|
||||
y = ts.YValues[index]
|
||||
return
|
||||
}
|
||||
|
||||
// GetXFormatter returns the x value formatter.
|
||||
func (ts TimeSeries) GetXFormatter() Formatter {
|
||||
return func(v interface{}) string {
|
||||
if typed, isTyped := v.(time.Time); isTyped {
|
||||
return typed.Format(DefaultDateFormat)
|
||||
}
|
||||
if typed, isTyped := v.(int64); isTyped {
|
||||
return time.Unix(typed, 0).Format(DefaultDateFormat)
|
||||
}
|
||||
if typed, isTyped := v.(float64); isTyped {
|
||||
return time.Unix(int64(typed), 0).Format(DefaultDateFormat)
|
||||
}
|
||||
return util.StringEmpty
|
||||
// DrawLineSeries draws a line series with a renderer.
|
||||
func DrawLineSeries(c *Chart, r Renderer, canvasBox Box, xrange, yrange Range, vs ValueProvider) error {
|
||||
if vs.Len() == 0 {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// GetYFormatter returns the y value formatter.
|
||||
func (ts TimeSeries) GetYFormatter() Formatter {
|
||||
return func(v interface{}) string {
|
||||
if typed, isTyped := v.(float64); isTyped {
|
||||
return fmt.Sprintf("%0.2f", typed)
|
||||
cx := canvasBox.Left
|
||||
cy := canvasBox.Top
|
||||
cb := canvasBox.Bottom
|
||||
cw := canvasBox.Width
|
||||
|
||||
v0x, v0y := vs.GetValue(0)
|
||||
x0 := cw - xrange.Translate(v0x)
|
||||
y0 := yrange.Translate(v0y)
|
||||
|
||||
var vx, vy float64
|
||||
var x, y int
|
||||
|
||||
fill := s.GetStyle().GetFillColor()
|
||||
if !fill.IsZero() {
|
||||
r.SetFillColor(fill)
|
||||
r.MoveTo(x0+cx, y0+cy)
|
||||
for i := 1; i < vs.Len(); i++ {
|
||||
vx, vy = vs.GetValue(i)
|
||||
x = cw - xrange.Translate(vx)
|
||||
y = yrange.Translate(vy)
|
||||
r.LineTo(x+cx, y+cy)
|
||||
}
|
||||
return util.StringEmpty
|
||||
r.LineTo(x+cx, cb)
|
||||
r.LineTo(x0+cx, cb)
|
||||
r.Close()
|
||||
r.Fill()
|
||||
}
|
||||
}
|
||||
|
||||
// ContinuousSeries represents a line on a chart.
|
||||
type ContinuousSeries struct {
|
||||
Name string
|
||||
Style Style
|
||||
stroke := s.GetStyle().GetStrokeColor(GetDefaultSeriesStrokeColor(index))
|
||||
r.SetStrokeColor(stroke)
|
||||
r.SetStrokeWidth(s.GetStyle().GetStrokeWidth(DefaultStrokeWidth))
|
||||
|
||||
XValues []float64
|
||||
YValues []float64
|
||||
}
|
||||
|
||||
// GetName returns the name of the time series.
|
||||
func (cs ContinuousSeries) GetName() string {
|
||||
return cs.Name
|
||||
}
|
||||
|
||||
// GetStyle returns the line style.
|
||||
func (cs ContinuousSeries) GetStyle() Style {
|
||||
return cs.Style
|
||||
}
|
||||
|
||||
// Len returns the number of elements in the series.
|
||||
func (cs ContinuousSeries) Len() int {
|
||||
return len(cs.XValues)
|
||||
}
|
||||
|
||||
// GetValue gets a value at a given index.
|
||||
func (cs ContinuousSeries) GetValue(index int) (float64, float64) {
|
||||
return cs.XValues[index], cs.YValues[index]
|
||||
}
|
||||
|
||||
// GetXFormatter returns the xs value formatter.
|
||||
func (cs ContinuousSeries) GetXFormatter() Formatter {
|
||||
return func(v interface{}) string {
|
||||
if typed, isTyped := v.(float64); isTyped {
|
||||
return fmt.Sprintf("%0.2f", typed)
|
||||
}
|
||||
return util.StringEmpty
|
||||
r.MoveTo(x0+cx, y0+cy)
|
||||
for i := 1; i < vs.Len(); i++ {
|
||||
vx, vy = vs.GetValue(i)
|
||||
x = cw - xrange.Translate(vx)
|
||||
y = yrange.Translate(vy)
|
||||
r.LineTo(x+cx, y+cy)
|
||||
}
|
||||
r.Stroke()
|
||||
}
|
||||
|
||||
// GetYFormatter returns the y value formatter.
|
||||
func (cs ContinuousSeries) GetYFormatter() Formatter {
|
||||
return cs.GetXFormatter()
|
||||
// DrawAnnotation draws an anotation with a renderer.
|
||||
func DrawAnnotation(c *Chart, r Renderer, canvasBox Box, xrange, yrange, s Style, lx, ly int, lv string) {
|
||||
py := canvasBox.Top
|
||||
|
||||
r.SetFontSize(s.GetFontSize(DefaultFinalLabelFontSize))
|
||||
textWidth, _ := r.MeasureText(ll)
|
||||
textHeight := int(math.Floor(DefaultFinalLabelFontSize))
|
||||
halfTextHeight := textHeight >> 1
|
||||
|
||||
pt := s.Padding.GetTop(DefaultFinalLabelPadding.Top)
|
||||
pl := s.Padding.GetLeft(DefaultFinalLabelPadding.Left)
|
||||
pr := s.Padding.GetRight(DefaultFinalLabelPadding.Right)
|
||||
pb := s.Padding.GetBottom(DefaultFinalLabelPadding.Bottom)
|
||||
|
||||
textX := lx + pl + DefaultFinalLabelDeltaWidth
|
||||
textY := ly + halfTextHeight
|
||||
|
||||
ltlx := lx + pl + DefaultFinalLabelDeltaWidth
|
||||
ltly := ly - (pt + halfTextHeight)
|
||||
|
||||
ltrx := lx + pl + pr + textWidth
|
||||
ltry := ly - (pt + halfTextHeight)
|
||||
|
||||
lbrx := lx + pl + pr + textWidth
|
||||
lbry := ly + (pb + halfTextHeight)
|
||||
|
||||
lblx := lx + DefaultFinalLabelDeltaWidth
|
||||
lbly := ly + (pb + halfTextHeight)
|
||||
|
||||
//draw the shape...
|
||||
r.SetFillColor(s.GetFillColor(DefaultAnnotationFillColor))
|
||||
r.SetStrokeColor(s.GetStrokeColor())
|
||||
r.SetStrokeWidth(s.GetStrokeWidth())
|
||||
r.MoveTo(lx, ly)
|
||||
r.LineTo(ltlx, ltly)
|
||||
r.LineTo(ltrx, ltry)
|
||||
r.LineTo(lbrx, lbry)
|
||||
r.LineTo(lblx, lbly)
|
||||
r.LineTo(cx, ly)
|
||||
r.Close()
|
||||
r.FillStroke()
|
||||
|
||||
r.SetFontColor(s.GetFontColor(DefaultTextColor))
|
||||
r.Text(ll, textX, textY)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue