secondary axis works.

This commit is contained in:
Will Charczuk 2016-07-10 11:19:56 -07:00
parent abfdc3e0d9
commit d78930a08f
3 changed files with 76 additions and 34 deletions

View file

@ -155,6 +155,14 @@ func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
yrange.Min = globalMinY yrange.Min = globalMinY
yrange.Max = globalMaxY yrange.Max = globalMaxY
} }
if !c.YAxisSecondary.Range.IsZero() {
yrangeAlt.Min = c.YAxisSecondary.Range.Min
yrangeAlt.Max = c.YAxisSecondary.Range.Max
} else {
yrangeAlt.Min = globalMinYA
yrangeAlt.Max = globalMaxYA
}
return return
} }
@ -207,7 +215,7 @@ func (c Chart) getAxesTicks(r Renderer, xr, yr, yar Range, xf, yf, yfa ValueForm
yticks = c.YAxis.GetTicks(r, yr, yf) yticks = c.YAxis.GetTicks(r, yr, yf)
} }
if c.YAxisSecondary.Style.Show { if c.YAxisSecondary.Style.Show {
yticksAlt = c.YAxisSecondary.GetTicks(r, yr, yf) yticksAlt = c.YAxisSecondary.GetTicks(r, yar, yf)
} }
return return
} }
@ -348,7 +356,7 @@ func (c Chart) drawSeries(r Renderer, canvasBox Box, xrange, yrange, yrangeAlt R
if s.GetYAxis() == YAxisPrimary { if s.GetYAxis() == YAxisPrimary {
s.Render(r, canvasBox, xrange, yrange, c.getSeriesStyleDefaults(seriesIndex)) s.Render(r, canvasBox, xrange, yrange, c.getSeriesStyleDefaults(seriesIndex))
} else if s.GetYAxis() == YAxisSecondary { } else if s.GetYAxis() == YAxisSecondary {
s.Render(r, canvasBox, xrange, yrange, c.getSeriesStyleDefaults(seriesIndex)) s.Render(r, canvasBox, xrange, yrangeAlt, c.getSeriesStyleDefaults(seriesIndex))
} }
} }

View file

@ -20,11 +20,11 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
rc.Response.Header().Set("Content-Type", "image/svg+xml") rc.Response.Header().Set("Content-Type", "image/svg+xml")
} }
s1x := []float64{1.0, 2.0, 3.0, 4.0} s1x := []float64{2.0, 3.0, 4.0, 5.0}
s1y := []float64{2.5, 5.0, 2.0, 3.3} s1y := []float64{2.5, 5.0, 2.0, 3.3}
s2x := []float64{3.0, 4.0, 5.0, 6.0} s2x := []float64{0.0, 0.5, 1.0, 1.5}
s2y := []float64{6.0, 5.0, 4.0, 1.0} s2y := []float64{1.1, 1.2, 1.0, 1.3}
c := chart.Chart{ c := chart.Chart{
Title: "A Test Chart", Title: "A Test Chart",
@ -35,20 +35,27 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
Height: 400, Height: 400,
XAxis: chart.XAxis{ XAxis: chart.XAxis{
Style: chart.Style{ Style: chart.Style{
Show: true, Show: true,
StrokeWidth: 1.0,
}, },
}, },
YAxis: chart.YAxis{ YAxis: chart.YAxis{
Style: chart.Style{ Style: chart.Style{
Show: true, Show: true,
StrokeWidth: 1.0,
}, },
Range: chart.Range{ Range: chart.Range{
Min: 0.0, Min: 0.0,
Max: 7.0, Max: 7.0,
}, },
}, },
YAxisSecondary: chart.YAxis{
Style: chart.Style{
Show: true,
},
Range: chart.Range{
Min: 0.8,
Max: 1.5,
},
},
Series: []chart.Series{ Series: []chart.Series{
chart.ContinuousSeries{ chart.ContinuousSeries{
Name: "a", Name: "a",
@ -57,6 +64,7 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
}, },
chart.ContinuousSeries{ chart.ContinuousSeries{
Name: "b", Name: "b",
YAxis: chart.YAxisSecondary,
XValues: s2x, XValues: s2x,
YValues: s2y, YValues: s2y,
}, },
@ -75,7 +83,8 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
}, },
}, },
chart.AnnotationSeries{ chart.AnnotationSeries{
Name: "b - last value", Name: "b - last value",
YAxis: chart.YAxisSecondary,
Style: chart.Style{ Style: chart.Style{
Show: true, Show: true,
StrokeColor: chart.GetDefaultSeriesStrokeColor(1), StrokeColor: chart.GetDefaultSeriesStrokeColor(1),

View file

@ -1,6 +1,7 @@
package chart package chart
import ( import (
"fmt"
"math" "math"
"sort" "sort"
) )
@ -54,6 +55,7 @@ func (ya YAxis) getTickStep(r Renderer, ra Range, vf ValueFormatter) float64 {
func (ya YAxis) generateTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick { func (ya YAxis) generateTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick {
var ticks []Tick var ticks []Tick
for cursor := ra.Min; cursor < ra.Max; cursor += step { for cursor := ra.Min; cursor < ra.Max; cursor += step {
println("yaxis tick:", fmt.Sprintf("%.2f", cursor))
ticks = append(ticks, Tick{ ticks = append(ticks, Tick{
Value: cursor, Value: cursor,
Label: vf(cursor), Label: vf(cursor),
@ -64,39 +66,62 @@ func (ya YAxis) generateTicksWithStep(ra Range, step float64, vf ValueFormatter)
// Render renders the axis. // Render renders the axis.
func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, axisType YAxisType, ticks []Tick) { func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, axisType YAxisType, ticks []Tick) {
var lx int
var tx int
if axisType == YAxisPrimary {
lx = canvasBox.Right
tx = canvasBox.Right + DefaultYAxisMargin
} else if axisType == YAxisSecondary {
lx = canvasBox.Left
tx = canvasBox.Left - DefaultYAxisMargin
}
r.SetStrokeColor(ya.Style.GetStrokeColor(DefaultAxisColor)) r.SetStrokeColor(ya.Style.GetStrokeColor(DefaultAxisColor))
r.SetStrokeWidth(ya.Style.GetStrokeWidth(DefaultAxisLineWidth)) r.SetStrokeWidth(ya.Style.GetStrokeWidth(DefaultAxisLineWidth))
r.MoveTo(lx, canvasBox.Bottom)
r.LineTo(lx, canvasBox.Top)
r.Stroke()
r.SetFontColor(ya.Style.GetFontColor(DefaultAxisColor)) r.SetFontColor(ya.Style.GetFontColor(DefaultAxisColor))
fontSize := ya.Style.GetFontSize(DefaultFontSize) fontSize := ya.Style.GetFontSize(DefaultFontSize)
r.SetFontSize(fontSize) r.SetFontSize(fontSize)
sort.Sort(Ticks(ticks)) sort.Sort(Ticks(ticks))
for _, t := range ticks {
v := t.Value
ly := ra.Translate(v) + canvasBox.Top
th := int(fontSize) >> 1 var lx int
ty := ly + th var tx int
if axisType == YAxisPrimary {
lx = canvasBox.Right
tx = canvasBox.Right + DefaultYAxisMargin
r.Text(t.Label, tx, ty) r.MoveTo(lx, canvasBox.Bottom)
r.LineTo(lx, canvasBox.Top)
r.MoveTo(lx, ly)
r.LineTo(lx+DefaultVerticalTickWidth, ly)
r.Stroke() r.Stroke()
for _, t := range ticks {
v := t.Value
ly := ra.Translate(v) + canvasBox.Top
th := int(fontSize) >> 1
ty := ly + th
r.Text(t.Label, tx, ty)
r.MoveTo(lx, ly)
r.LineTo(lx+DefaultVerticalTickWidth, ly)
r.Stroke()
}
} else if axisType == YAxisSecondary {
lx = canvasBox.Left
r.MoveTo(lx, canvasBox.Bottom)
r.LineTo(lx, canvasBox.Top)
r.Stroke()
for _, t := range ticks {
v := t.Value
ly := ra.Translate(v) + canvasBox.Top
ptw, _ := r.MeasureText(t.Label)
tw := ptw
th := int(fontSize)
tx = lx - (int(tw) + (DefaultYAxisMargin >> 1))
ty := ly + th>>1
r.Text(t.Label, tx, ty)
r.MoveTo(lx, ly)
r.LineTo(lx-DefaultVerticalTickWidth, ly)
r.Stroke()
}
} }
} }