From d455b775dae82055e3c3a4c73e4a81289bbb4eaf Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Sat, 16 Jul 2016 23:45:28 -0700 Subject: [PATCH] small bugfix for edge case. --- chart.go | 4 ++++ examples/timeseries/main.go | 39 +++++++++++++++++++++++++++++++++++++ xaxis.go | 2 ++ 3 files changed, 45 insertions(+) diff --git a/chart.go b/chart.go index bf82a57..98079a0 100644 --- a/chart.go +++ b/chart.go @@ -104,7 +104,11 @@ func (c Chart) Render(rp RendererProvider, w io.Writer) error { xt, yt, yta = c.getAxesTicks(r, xr, yr, yra, xf, yf, yfa) canvasBox = c.getAxisAdjustedCanvasBox(r, canvasBox, xr, yr, yra, xt, yt, yta) xr, yr, yra = c.setRangeDomains(canvasBox, xr, yr, yra) + + // do a second pass in case things haven't settled yet. xt, yt, yta = c.getAxesTicks(r, xr, yr, yra, xf, yf, yfa) + canvasBox = c.getAxisAdjustedCanvasBox(r, canvasBox, xr, yr, yra, xt, yt, yta) + xr, yr, yra = c.setRangeDomains(canvasBox, xr, yr, yra) } if c.hasAnnotationSeries() { diff --git a/examples/timeseries/main.go b/examples/timeseries/main.go index aade79a..9303ab7 100644 --- a/examples/timeseries/main.go +++ b/examples/timeseries/main.go @@ -42,7 +42,46 @@ func drawChart(res http.ResponseWriter, req *http.Request) { graph.Render(chart.PNG, res) } +func drawCustomChart(res http.ResponseWriter, req *http.Request) { + /* + This is basically the other timeseries example, except we switch to hour intervals and specify a different formatter from default for the xaxis tick labels. + */ + graph := chart.Chart{ + XAxis: chart.XAxis{ + Style: chart.Style{ + Show: true, + }, + ValueFormatter: chart.TimeHourValueFormatter, + }, + Series: []chart.Series{ + chart.TimeSeries{ + XValues: []time.Time{ + time.Now().Add(-10 * time.Hour), + time.Now().Add(-9 * time.Hour), + time.Now().Add(-8 * time.Hour), + time.Now().Add(-7 * time.Hour), + time.Now().Add(-6 * time.Hour), + time.Now().Add(-5 * time.Hour), + time.Now().Add(-4 * time.Hour), + time.Now().Add(-3 * time.Hour), + time.Now().Add(-2 * time.Hour), + time.Now().Add(-1 * time.Hour), + time.Now(), + }, + YValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0}, + }, + }, + } + + res.Header().Set("Content-Type", "image/png") + graph.Render(chart.PNG, res) +} + func main() { http.HandleFunc("/", drawChart) + http.HandleFunc("/favico.ico", func(res http.ResponseWriter, req *http.Request) { + res.Write([]byte{}) + }) + http.HandleFunc("/custom", drawCustomChart) http.ListenAndServe(":8080", nil) } diff --git a/xaxis.go b/xaxis.go index 8df875f..0f3650e 100644 --- a/xaxis.go +++ b/xaxis.go @@ -78,6 +78,7 @@ func (xa XAxis) GetGridLines(ticks []Tick) []GridLine { func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) Box { r.SetStrokeColor(xa.Style.GetStrokeColor(defaults.StrokeColor)) r.SetStrokeWidth(xa.Style.GetStrokeWidth(defaults.StrokeWidth)) + r.SetStrokeDashArray(xa.Style.GetStrokeDashArray()) r.SetFont(xa.Style.GetFont(defaults.GetFont())) r.SetFontColor(xa.Style.GetFontColor(DefaultAxisColor)) r.SetFontSize(xa.Style.GetFontSize(defaults.GetFontSize())) @@ -111,6 +112,7 @@ func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, tic func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) { r.SetStrokeColor(xa.Style.GetStrokeColor(defaults.StrokeColor)) r.SetStrokeWidth(xa.Style.GetStrokeWidth(defaults.StrokeWidth)) + r.SetStrokeDashArray(xa.Style.GetStrokeDashArray()) r.SetFont(xa.Style.GetFont(defaults.GetFont())) r.SetFontColor(xa.Style.GetFontColor(DefaultAxisColor)) r.SetFontSize(xa.Style.GetFontSize(defaults.GetFontSize()))