diff --git a/chart.go b/chart.go index acd7b7c..850daf6 100644 --- a/chart.go +++ b/chart.go @@ -137,6 +137,8 @@ func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) { var miny, maxy float64 = math.MaxFloat64, 0 var minya, maxya float64 = math.MaxFloat64, 0 + hasSecondaryAxis := false + // note: a possible future optimization is to not scan the series values if // all axis are represented by either custom ticks or custom ranges. for _, s := range c.Series { @@ -160,6 +162,7 @@ func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) { minya = math.Min(minya, vy2) maxya = math.Max(maxya, vy1) maxya = math.Max(maxya, vy2) + hasSecondaryAxis = true } } } else if vp, isValueProvider := s.(ValueProvider); isValueProvider { @@ -226,7 +229,7 @@ func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) { } else if !c.YAxisSecondary.Range.IsZero() { yrangeAlt.Min = c.YAxisSecondary.Range.Min yrangeAlt.Max = c.YAxisSecondary.Range.Max - } else { + } else if hasSecondaryAxis { yrangeAlt.Min = minya yrangeAlt.Max = maxya yrangeAlt.Min, yrangeAlt.Max = yrangeAlt.GetRoundedRangeBounds() diff --git a/examples/stock_analysis.go/main.go b/examples/stock_analysis.go/main.go new file mode 100644 index 0000000..06aaaa8 --- /dev/null +++ b/examples/stock_analysis.go/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "net/http" + + "github.com/wcharczuk/go-chart" +) + +func drawChart(res http.ResponseWriter, req *http.Request) { + xvalues, yvalues := getMockChartData() + + priceSeries := chart.TimeSeries{ + XValues: xvalues, + YValues: yvalues, + } + + graph := chart.Chart{ + Series: []chart.Series{ + priceSeries, + }, + } + + res.Header().Set("Content-Type", "image/svg+xml") + graph.Render(chart.SVG, res) +} + +func main() { + http.HandleFunc("/", drawChart) + http.ListenAndServe(":8080", nil) +}