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.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
}
@ -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)
}
if c.YAxisSecondary.Style.Show {
yticksAlt = c.YAxisSecondary.GetTicks(r, yr, yf)
yticksAlt = c.YAxisSecondary.GetTicks(r, yar, yf)
}
return
}
@ -348,7 +356,7 @@ func (c Chart) drawSeries(r Renderer, canvasBox Box, xrange, yrange, yrangeAlt R
if s.GetYAxis() == YAxisPrimary {
s.Render(r, canvasBox, xrange, yrange, c.getSeriesStyleDefaults(seriesIndex))
} 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")
}
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}
s2x := []float64{3.0, 4.0, 5.0, 6.0}
s2y := []float64{6.0, 5.0, 4.0, 1.0}
s2x := []float64{0.0, 0.5, 1.0, 1.5}
s2y := []float64{1.1, 1.2, 1.0, 1.3}
c := chart.Chart{
Title: "A Test Chart",
@ -35,20 +35,27 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
Height: 400,
XAxis: chart.XAxis{
Style: chart.Style{
Show: true,
StrokeWidth: 1.0,
Show: true,
},
},
YAxis: chart.YAxis{
Style: chart.Style{
Show: true,
StrokeWidth: 1.0,
Show: true,
},
Range: chart.Range{
Min: 0.0,
Max: 7.0,
},
},
YAxisSecondary: chart.YAxis{
Style: chart.Style{
Show: true,
},
Range: chart.Range{
Min: 0.8,
Max: 1.5,
},
},
Series: []chart.Series{
chart.ContinuousSeries{
Name: "a",
@ -57,6 +64,7 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
},
chart.ContinuousSeries{
Name: "b",
YAxis: chart.YAxisSecondary,
XValues: s2x,
YValues: s2y,
},
@ -75,7 +83,8 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
},
},
chart.AnnotationSeries{
Name: "b - last value",
Name: "b - last value",
YAxis: chart.YAxisSecondary,
Style: chart.Style{
Show: true,
StrokeColor: chart.GetDefaultSeriesStrokeColor(1),

View File

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