From b10ead7cc1cd5c1156513573394ecec093970a8f Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Thu, 7 Jul 2016 17:50:16 -0700 Subject: [PATCH] tweaks. --- chart.go | 52 +++++++++++++++++++++++++--------------------- defaults.go | 43 ++++++++++++++++++++++---------------- style.go | 4 ++-- testserver/main.go | 19 +---------------- 4 files changed, 56 insertions(+), 62 deletions(-) diff --git a/chart.go b/chart.go index 07a6d31..e6eb641 100644 --- a/chart.go +++ b/chart.go @@ -29,32 +29,36 @@ type Chart struct { // GetCanvasTop gets the top corner pixel. 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. 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. 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. 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. 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. 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. @@ -79,8 +83,8 @@ func (c *Chart) Render(provider RendererProvider, w io.Writer) error { c.drawBackground(r) c.drawCanvas(r) c.drawAxes(r, xrange, yrange) - for _, series := range c.Series { - c.drawSeries(r, series, xrange, yrange) + for index, series := range c.Series { + c.drawSeries(r, index, series, xrange, yrange) } c.drawTitle(r) return r.Save(w) @@ -162,7 +166,7 @@ func (c Chart) drawCanvas(r Renderer) { func (c Chart) drawAxes(r Renderer, xrange, yrange Range) { if c.Axes.Show { 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.LineTo(c.GetCanvasRight(), c.GetCanvasBottom()) 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) { - + // do x axis + // do y axis } -func (c Chart) drawSeries(r Renderer, s Series, xrange, yrange Range) { - r.SetStrokeColor(s.GetStyle().GetStrokeColor(DefaultLineColor)) - r.SetLineWidth(s.GetStyle().GetStrokeWidth(DefaultLineWidth)) +func (c Chart) drawSeries(r Renderer, index int, s Series, xrange, yrange Range) { + r.SetStrokeColor(s.GetStyle().GetStrokeColor(GetDefaultSeriesStrokeColor(index))) + r.SetLineWidth(s.GetStyle().GetStrokeWidth(DefaultStrokeWidth)) if s.Len() == 0 { return } - px := c.Canvas.Padding.GetLeft(DefaultCanvasPadding.Left) - py := c.Canvas.Padding.GetTop(DefaultCanvasPadding.Top) - + cx := c.GetCanvasLeft() + cy := c.GetCanvasTop() cw := c.GetCanvasWidth() v0x, v0y := s.GetValue(0) x0 := cw - xrange.Translate(v0x) y0 := yrange.Translate(v0y) - r.MoveTo(x0+px, y0+py) + r.MoveTo(x0+cx, y0+cy) var vx, vy float64 var x, y int - for index := 1; index < s.Len(); index++ { - vx, vy = s.GetValue(index) + for i := 1; i < s.Len(); i++ { + vx, vy = s.GetValue(i) x = cw - xrange.Translate(vx) y = yrange.Translate(vy) - r.LineTo(x+px, y+py) + r.LineTo(x+cx, y+cy) } 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 { _, lv := s.GetValue(s.Len() - 1) _, ll := s.GetLabel(s.Len() - 1) - py := c.Canvas.Padding.GetTop(DefaultCanvasPadding.Top) + py := c.GetCanvasTop() ly := yrange.Translate(lv) + py r.SetFontSize(c.FinalValueLabel.GetFontSize(DefaultFinalLabelFontSize)) @@ -245,7 +249,7 @@ func (c Chart) drawFinalValueLabel(r Renderer, s Series, yrange Range) { //draw the shape... 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.MoveTo(cx, ly) r.LineTo(ltlx, ltly) diff --git a/defaults.go b/defaults.go index 8958443..9cd1839 100644 --- a/defaults.go +++ b/defaults.go @@ -12,8 +12,8 @@ const ( DefaultChartHeight = 400 // DefaultChartWidth is the default chart width. DefaultChartWidth = 200 - // DefaultLineWidth is the default chart line width. - DefaultLineWidth = 2.0 + // DefaultStrokeWidth is the default chart line/stroke width. + DefaultStrokeWidth = 1.0 // DefaultAxisLineWidth is the line width of the axis lines. DefaultAxisLineWidth = 1.0 //DefaultDPI is the default dots per inch for the chart. @@ -24,26 +24,18 @@ const ( DefaultFontSize = 10.0 // DefaultTitleFontSize is the default title font size. 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 = 10 // DefaultFinalLabelFontSize is the font size of the final label. DefaultFinalLabelFontSize = 10.0 -) - -var ( - // DefaultFinalLabelPadding is the padding around the final label. - DefaultFinalLabelPadding = Box{Top: 5, Left: 0, Right: 5, Bottom: 5} + // DefaultDateFormat is the default date format. + DefaultDateFormat = "2006-01-02" ) var ( // DefaultBackgroundColor is the default chart background color. // 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. // It is equivalent to css color:white. DefaultCanvasColor = color.RGBA{R: 255, G: 255, B: 255, A: 255} @@ -56,9 +48,6 @@ var ( // DefaultStrokeColor is the default chart border color. // It is equivalent to #efefef. 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. // It is equivalent to #0074d9. DefaultFillColor = color.RGBA{R: 0, G: 217, B: 116, A: 255} @@ -67,8 +56,26 @@ var ( ) var ( - // DefaultCanvasPadding is the default canvas padding config. - DefaultCanvasPadding = Box{Top: 5, Left: 5, Right: 15, Bottom: 15} + // DefaultSeriesStrokeColors are a couple default series colors. + 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 ( diff --git a/style.go b/style.go index 30a238d..2fd8dcd 100644 --- a/style.go +++ b/style.go @@ -24,7 +24,7 @@ func (s Style) GetStrokeColor(defaults ...color.RGBA) color.RGBA { if len(defaults) > 0 { return defaults[0] } - return DefaultLineColor + return DefaultStrokeColor } return s.StrokeColor } @@ -46,7 +46,7 @@ func (s Style) GetStrokeWidth(defaults ...float64) float64 { if len(defaults) > 0 { return defaults[0] } - return DefaultLineWidth + return DefaultStrokeWidth } return s.StrokeWidth } diff --git a/testserver/main.go b/testserver/main.go index ae24d05..e5eee97 100644 --- a/testserver/main.go +++ b/testserver/main.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "image/color" "log" "time" @@ -34,31 +33,15 @@ func main() { Show: true, StrokeWidth: 1.0, }, - YRange: chart.Range{ - Min: 0.0, - Max: 7.0, - }, FinalValueLabel: chart.Style{ Show: true, }, Series: []chart.Series{ chart.TimeSeries{ - Name: "goog", - Style: chart.Style{ - StrokeWidth: 1.0, - }, + Name: "goog", 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}, }, - 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}, - }, }, }