This commit is contained in:
Will Charczuk 2016-07-07 17:50:16 -07:00
parent 04bef9cad8
commit b10ead7cc1
4 changed files with 56 additions and 62 deletions

View File

@ -29,32 +29,36 @@ type Chart struct {
// GetCanvasTop gets the top corner pixel. // GetCanvasTop gets the top corner pixel.
func (c Chart) GetCanvasTop() int { func (c Chart) GetCanvasTop() int {
return c.Canvas.Padding.GetTop(DefaultCanvasPadding.Top) return c.Background.Padding.GetTop(DefaultBackgroundPadding.Top)
} }
// GetCanvasLeft gets the left corner pixel. // GetCanvasLeft gets the left corner pixel.
func (c Chart) GetCanvasLeft() int { func (c Chart) GetCanvasLeft() int {
return c.Canvas.Padding.GetLeft(DefaultCanvasPadding.Left) return c.Background.Padding.GetLeft(DefaultBackgroundPadding.Left)
} }
// GetCanvasBottom gets the bottom corner pixel. // GetCanvasBottom gets the bottom corner pixel.
func (c Chart) GetCanvasBottom() int { func (c Chart) GetCanvasBottom() int {
return c.Height - c.Canvas.Padding.GetBottom(DefaultCanvasPadding.Bottom) return c.Height - c.Background.Padding.GetBottom(DefaultBackgroundPadding.Bottom)
} }
// GetCanvasRight gets the right corner pixel. // GetCanvasRight gets the right corner pixel.
func (c Chart) GetCanvasRight() int { func (c Chart) GetCanvasRight() int {
return c.Width - c.Canvas.Padding.GetRight(DefaultCanvasPadding.Right) return c.Width - c.Background.Padding.GetRight(DefaultBackgroundPadding.Right)
} }
// GetCanvasWidth returns the width of the canvas. // GetCanvasWidth returns the width of the canvas.
func (c Chart) GetCanvasWidth() int { func (c Chart) GetCanvasWidth() int {
return c.Width - (c.Canvas.Padding.GetLeft(DefaultCanvasPadding.Left) + c.Canvas.Padding.GetRight(DefaultCanvasPadding.Right)) pl := c.Background.Padding.GetLeft(DefaultBackgroundPadding.Left)
pr := c.Background.Padding.GetRight(DefaultBackgroundPadding.Right)
return c.Width - (pl + pr)
} }
// GetCanvasHeight returns the height of the canvas. // GetCanvasHeight returns the height of the canvas.
func (c Chart) GetCanvasHeight() int { func (c Chart) GetCanvasHeight() int {
return c.Height - (c.Canvas.Padding.GetTop(DefaultCanvasPadding.Top) + c.Canvas.Padding.GetBottom(DefaultCanvasPadding.Bottom)) pt := c.Background.Padding.GetTop(DefaultBackgroundPadding.Top)
pb := c.Background.Padding.GetBottom(DefaultBackgroundPadding.Bottom)
return c.Height - (pt + pb)
} }
// GetFont returns the text font. // GetFont returns the text font.
@ -79,8 +83,8 @@ func (c *Chart) Render(provider RendererProvider, w io.Writer) error {
c.drawBackground(r) c.drawBackground(r)
c.drawCanvas(r) c.drawCanvas(r)
c.drawAxes(r, xrange, yrange) c.drawAxes(r, xrange, yrange)
for _, series := range c.Series { for index, series := range c.Series {
c.drawSeries(r, series, xrange, yrange) c.drawSeries(r, index, series, xrange, yrange)
} }
c.drawTitle(r) c.drawTitle(r)
return r.Save(w) return r.Save(w)
@ -162,7 +166,7 @@ func (c Chart) drawCanvas(r Renderer) {
func (c Chart) drawAxes(r Renderer, xrange, yrange Range) { func (c Chart) drawAxes(r Renderer, xrange, yrange Range) {
if c.Axes.Show { if c.Axes.Show {
r.SetStrokeColor(c.Axes.GetStrokeColor(DefaultAxisColor)) r.SetStrokeColor(c.Axes.GetStrokeColor(DefaultAxisColor))
r.SetLineWidth(c.Axes.GetStrokeWidth(DefaultLineWidth)) r.SetLineWidth(c.Axes.GetStrokeWidth(DefaultStrokeWidth))
r.MoveTo(c.GetCanvasLeft(), c.GetCanvasBottom()) r.MoveTo(c.GetCanvasLeft(), c.GetCanvasBottom())
r.LineTo(c.GetCanvasRight(), c.GetCanvasBottom()) r.LineTo(c.GetCanvasRight(), c.GetCanvasBottom())
r.LineTo(c.GetCanvasRight(), c.GetCanvasTop()) r.LineTo(c.GetCanvasRight(), c.GetCanvasTop())
@ -173,46 +177,46 @@ func (c Chart) drawAxes(r Renderer, xrange, yrange Range) {
} }
func (c Chart) drawAxesLabels(r Renderer, xrange, yrange Range) { func (c Chart) drawAxesLabels(r Renderer, xrange, yrange Range) {
// do x axis
// do y axis
} }
func (c Chart) drawSeries(r Renderer, s Series, xrange, yrange Range) { func (c Chart) drawSeries(r Renderer, index int, s Series, xrange, yrange Range) {
r.SetStrokeColor(s.GetStyle().GetStrokeColor(DefaultLineColor)) r.SetStrokeColor(s.GetStyle().GetStrokeColor(GetDefaultSeriesStrokeColor(index)))
r.SetLineWidth(s.GetStyle().GetStrokeWidth(DefaultLineWidth)) r.SetLineWidth(s.GetStyle().GetStrokeWidth(DefaultStrokeWidth))
if s.Len() == 0 { if s.Len() == 0 {
return return
} }
px := c.Canvas.Padding.GetLeft(DefaultCanvasPadding.Left) cx := c.GetCanvasLeft()
py := c.Canvas.Padding.GetTop(DefaultCanvasPadding.Top) cy := c.GetCanvasTop()
cw := c.GetCanvasWidth() cw := c.GetCanvasWidth()
v0x, v0y := s.GetValue(0) v0x, v0y := s.GetValue(0)
x0 := cw - xrange.Translate(v0x) x0 := cw - xrange.Translate(v0x)
y0 := yrange.Translate(v0y) y0 := yrange.Translate(v0y)
r.MoveTo(x0+px, y0+py) r.MoveTo(x0+cx, y0+cy)
var vx, vy float64 var vx, vy float64
var x, y int var x, y int
for index := 1; index < s.Len(); index++ { for i := 1; i < s.Len(); i++ {
vx, vy = s.GetValue(index) vx, vy = s.GetValue(i)
x = cw - xrange.Translate(vx) x = cw - xrange.Translate(vx)
y = yrange.Translate(vy) y = yrange.Translate(vy)
r.LineTo(x+px, y+py) r.LineTo(x+cx, y+cy)
} }
r.Stroke() r.Stroke()
c.drawFinalValueLabel(r, s, yrange) c.drawFinalValueLabel(r, index, s, yrange)
} }
func (c Chart) drawFinalValueLabel(r Renderer, s Series, yrange Range) { func (c Chart) drawFinalValueLabel(r Renderer, index int, s Series, yrange Range) {
if c.FinalValueLabel.Show { if c.FinalValueLabel.Show {
_, lv := s.GetValue(s.Len() - 1) _, lv := s.GetValue(s.Len() - 1)
_, ll := s.GetLabel(s.Len() - 1) _, ll := s.GetLabel(s.Len() - 1)
py := c.Canvas.Padding.GetTop(DefaultCanvasPadding.Top) py := c.GetCanvasTop()
ly := yrange.Translate(lv) + py ly := yrange.Translate(lv) + py
r.SetFontSize(c.FinalValueLabel.GetFontSize(DefaultFinalLabelFontSize)) r.SetFontSize(c.FinalValueLabel.GetFontSize(DefaultFinalLabelFontSize))
@ -245,7 +249,7 @@ func (c Chart) drawFinalValueLabel(r Renderer, s Series, yrange Range) {
//draw the shape... //draw the shape...
r.SetFillColor(c.FinalValueLabel.GetFillColor(DefaultFinalLabelBackgroundColor)) r.SetFillColor(c.FinalValueLabel.GetFillColor(DefaultFinalLabelBackgroundColor))
r.SetStrokeColor(c.FinalValueLabel.GetStrokeColor(s.GetStyle().GetStrokeColor(DefaultLineColor))) r.SetStrokeColor(c.FinalValueLabel.GetStrokeColor(s.GetStyle().GetStrokeColor(GetDefaultSeriesStrokeColor(index))))
r.SetLineWidth(c.FinalValueLabel.GetStrokeWidth(DefaultAxisLineWidth)) r.SetLineWidth(c.FinalValueLabel.GetStrokeWidth(DefaultAxisLineWidth))
r.MoveTo(cx, ly) r.MoveTo(cx, ly)
r.LineTo(ltlx, ltly) r.LineTo(ltlx, ltly)

View File

@ -12,8 +12,8 @@ const (
DefaultChartHeight = 400 DefaultChartHeight = 400
// DefaultChartWidth is the default chart width. // DefaultChartWidth is the default chart width.
DefaultChartWidth = 200 DefaultChartWidth = 200
// DefaultLineWidth is the default chart line width. // DefaultStrokeWidth is the default chart line/stroke width.
DefaultLineWidth = 2.0 DefaultStrokeWidth = 1.0
// DefaultAxisLineWidth is the line width of the axis lines. // DefaultAxisLineWidth is the line width of the axis lines.
DefaultAxisLineWidth = 1.0 DefaultAxisLineWidth = 1.0
//DefaultDPI is the default dots per inch for the chart. //DefaultDPI is the default dots per inch for the chart.
@ -24,26 +24,18 @@ const (
DefaultFontSize = 10.0 DefaultFontSize = 10.0
// DefaultTitleFontSize is the default title font size. // DefaultTitleFontSize is the default title font size.
DefaultTitleFontSize = 18.0 DefaultTitleFontSize = 18.0
// DefaultDateFormat is the default date format.
DefaultDateFormat = "2006-01-02"
)
const (
// DefaultFinalLabelDeltaWidth is the width of the left triangle out of the final label. // DefaultFinalLabelDeltaWidth is the width of the left triangle out of the final label.
DefaultFinalLabelDeltaWidth = 10 DefaultFinalLabelDeltaWidth = 10
// DefaultFinalLabelFontSize is the font size of the final label. // DefaultFinalLabelFontSize is the font size of the final label.
DefaultFinalLabelFontSize = 10.0 DefaultFinalLabelFontSize = 10.0
) // DefaultDateFormat is the default date format.
DefaultDateFormat = "2006-01-02"
var (
// DefaultFinalLabelPadding is the padding around the final label.
DefaultFinalLabelPadding = Box{Top: 5, Left: 0, Right: 5, Bottom: 5}
) )
var ( var (
// DefaultBackgroundColor is the default chart background color. // DefaultBackgroundColor is the default chart background color.
// It is equivalent to css color:white. // It is equivalent to css color:white.
DefaultBackgroundColor = color.RGBA{R: 239, G: 239, B: 239, A: 255} //color.RGBA{R: 255, G: 255, B: 255, A: 255} DefaultBackgroundColor = color.RGBA{R: 255, G: 255, B: 255, A: 255}
// DefaultCanvasColor is the default chart canvas color. // DefaultCanvasColor is the default chart canvas color.
// It is equivalent to css color:white. // It is equivalent to css color:white.
DefaultCanvasColor = color.RGBA{R: 255, G: 255, B: 255, A: 255} DefaultCanvasColor = color.RGBA{R: 255, G: 255, B: 255, A: 255}
@ -56,9 +48,6 @@ var (
// DefaultStrokeColor is the default chart border color. // DefaultStrokeColor is the default chart border color.
// It is equivalent to #efefef. // It is equivalent to #efefef.
DefaultStrokeColor = color.RGBA{R: 239, G: 239, B: 239, A: 255} DefaultStrokeColor = color.RGBA{R: 239, G: 239, B: 239, A: 255}
// DefaultLineColor is the default (1st) series line color.
// It is equivalent to #0074d9.
DefaultLineColor = color.RGBA{R: 0, G: 116, B: 217, A: 255}
// DefaultFillColor is the default fill color. // DefaultFillColor is the default fill color.
// It is equivalent to #0074d9. // It is equivalent to #0074d9.
DefaultFillColor = color.RGBA{R: 0, G: 217, B: 116, A: 255} DefaultFillColor = color.RGBA{R: 0, G: 217, B: 116, A: 255}
@ -67,8 +56,26 @@ var (
) )
var ( var (
// DefaultCanvasPadding is the default canvas padding config. // DefaultSeriesStrokeColors are a couple default series colors.
DefaultCanvasPadding = Box{Top: 5, Left: 5, Right: 15, Bottom: 15} DefaultSeriesStrokeColors = []color.RGBA{
color.RGBA{R: 0, G: 116, B: 217, A: 255},
color.RGBA{R: 0, G: 217, B: 116, A: 255},
color.RGBA{R: 217, G: 0, B: 116, A: 255},
}
)
// GetDefaultSeriesStrokeColor returns a color from the default list by index.
// NOTE: the index will wrap around (using a modulo).g
func GetDefaultSeriesStrokeColor(index int) color.RGBA {
finalIndex := index % len(DefaultSeriesStrokeColors)
return DefaultSeriesStrokeColors[finalIndex]
}
var (
// DefaultFinalLabelPadding is the padding around the final label.
DefaultFinalLabelPadding = Box{Top: 5, Left: 0, Right: 5, Bottom: 5}
// DefaultBackgroundPadding is the default canvas padding config.
DefaultBackgroundPadding = Box{Top: 5, Left: 5, Right: 40, Bottom: 40}
) )
var ( var (

View File

@ -24,7 +24,7 @@ func (s Style) GetStrokeColor(defaults ...color.RGBA) color.RGBA {
if len(defaults) > 0 { if len(defaults) > 0 {
return defaults[0] return defaults[0]
} }
return DefaultLineColor return DefaultStrokeColor
} }
return s.StrokeColor return s.StrokeColor
} }
@ -46,7 +46,7 @@ func (s Style) GetStrokeWidth(defaults ...float64) float64 {
if len(defaults) > 0 { if len(defaults) > 0 {
return defaults[0] return defaults[0]
} }
return DefaultLineWidth return DefaultStrokeWidth
} }
return s.StrokeWidth return s.StrokeWidth
} }

View File

@ -2,7 +2,6 @@ package main
import ( import (
"bytes" "bytes"
"image/color"
"log" "log"
"time" "time"
@ -34,31 +33,15 @@ func main() {
Show: true, Show: true,
StrokeWidth: 1.0, StrokeWidth: 1.0,
}, },
YRange: chart.Range{
Min: 0.0,
Max: 7.0,
},
FinalValueLabel: chart.Style{ FinalValueLabel: chart.Style{
Show: true, Show: true,
}, },
Series: []chart.Series{ Series: []chart.Series{
chart.TimeSeries{ chart.TimeSeries{
Name: "goog", Name: "goog",
Style: chart.Style{
StrokeWidth: 1.0,
},
XValues: []time.Time{now.AddDate(0, 0, -4), now.AddDate(0, 0, -3), now.AddDate(0, 0, -2), now.AddDate(0, 0, -1)}, XValues: []time.Time{now.AddDate(0, 0, -4), now.AddDate(0, 0, -3), now.AddDate(0, 0, -2), now.AddDate(0, 0, -1)},
YValues: []float64{2.5, 5.0, 2.0, 3.0}, YValues: []float64{2.5, 5.0, 2.0, 3.0},
}, },
chart.TimeSeries{
Name: "aapl",
Style: chart.Style{
StrokeWidth: 1.0,
StrokeColor: color.RGBA{R: 0, G: 217, B: 116, A: 255},
},
XValues: []time.Time{now.AddDate(0, 0, -5), now.AddDate(0, 0, -4), now.AddDate(0, 0, -2), now.AddDate(0, 0, -1)},
YValues: []float64{3.0, 2.7, 2.0, 1.1},
},
}, },
} }