axis tick position
This commit is contained in:
parent
c2d680968d
commit
9b4307e186
15 changed files with 82 additions and 35 deletions
|
@ -6,7 +6,7 @@ import "math"
|
||||||
type AnnotationSeries struct {
|
type AnnotationSeries struct {
|
||||||
Name string
|
Name string
|
||||||
Style Style
|
Style Style
|
||||||
YAxis YAxisType
|
YAxis yAxisType
|
||||||
Annotations []Value2
|
Annotations []Value2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ func (as AnnotationSeries) GetStyle() Style {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetYAxis returns which YAxis the series draws on.
|
// GetYAxis returns which YAxis the series draws on.
|
||||||
func (as AnnotationSeries) GetYAxis() YAxisType {
|
func (as AnnotationSeries) GetYAxis() yAxisType {
|
||||||
return as.YAxis
|
return as.YAxis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
17
axis.go
17
axis.go
|
@ -1,13 +1,24 @@
|
||||||
package chart
|
package chart
|
||||||
|
|
||||||
|
type tickPosition int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// TickPositionUnset means to use the default tick position.
|
||||||
|
TickPositionUnset tickPosition = 0
|
||||||
|
// TickPositionBetweenTicks draws the labels for a tick between the previous and current tick.
|
||||||
|
TickPositionBetweenTicks tickPosition = 1
|
||||||
|
// TickPositionUnderTick draws the tick below the tick.
|
||||||
|
TickPositionUnderTick tickPosition = 2
|
||||||
|
)
|
||||||
|
|
||||||
// YAxisType is a type of y-axis; it can either be primary or secondary.
|
// YAxisType is a type of y-axis; it can either be primary or secondary.
|
||||||
type YAxisType int
|
type yAxisType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// YAxisPrimary is the primary axis.
|
// YAxisPrimary is the primary axis.
|
||||||
YAxisPrimary YAxisType = 0
|
YAxisPrimary yAxisType = 0
|
||||||
// YAxisSecondary is the secondary axis.
|
// YAxisSecondary is the secondary axis.
|
||||||
YAxisSecondary YAxisType = 1
|
YAxisSecondary yAxisType = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
// Axis is a chart feature detailing what values happen where.
|
// Axis is a chart feature detailing what values happen where.
|
||||||
|
|
|
@ -7,7 +7,7 @@ import "math"
|
||||||
type BollingerBandsSeries struct {
|
type BollingerBandsSeries struct {
|
||||||
Name string
|
Name string
|
||||||
Style Style
|
Style Style
|
||||||
YAxis YAxisType
|
YAxis yAxisType
|
||||||
|
|
||||||
Period int
|
Period int
|
||||||
K float64
|
K float64
|
||||||
|
@ -27,7 +27,7 @@ func (bbs BollingerBandsSeries) GetStyle() Style {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetYAxis returns which YAxis the series draws on.
|
// GetYAxis returns which YAxis the series draws on.
|
||||||
func (bbs BollingerBandsSeries) GetYAxis() YAxisType {
|
func (bbs BollingerBandsSeries) GetYAxis() yAxisType {
|
||||||
return bbs.YAxis
|
return bbs.YAxis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ type ContinuousSeries struct {
|
||||||
Name string
|
Name string
|
||||||
Style Style
|
Style Style
|
||||||
|
|
||||||
YAxis YAxisType
|
YAxis yAxisType
|
||||||
|
|
||||||
XValues []float64
|
XValues []float64
|
||||||
YValues []float64
|
YValues []float64
|
||||||
|
@ -44,7 +44,7 @@ func (cs ContinuousSeries) GetValueFormatters() (x, y ValueFormatter) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetYAxis returns which YAxis the series draws on.
|
// GetYAxis returns which YAxis the series draws on.
|
||||||
func (cs ContinuousSeries) GetYAxis() YAxisType {
|
func (cs ContinuousSeries) GetYAxis() yAxisType {
|
||||||
return cs.YAxis
|
return cs.YAxis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
3
draw.go
3
draw.go
|
@ -62,8 +62,6 @@ func (d draw) BoundedSeries(r Renderer, canvasBox Box, xrange, yrange Range, sty
|
||||||
drawOffsetIndex = drawOffsetIndexes[0]
|
drawOffsetIndex = drawOffsetIndexes[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
style.WriteToRenderer(r)
|
|
||||||
|
|
||||||
cb := canvasBox.Bottom
|
cb := canvasBox.Bottom
|
||||||
cl := canvasBox.Left
|
cl := canvasBox.Left
|
||||||
|
|
||||||
|
@ -79,6 +77,7 @@ func (d draw) BoundedSeries(r Renderer, canvasBox Box, xrange, yrange Range, sty
|
||||||
y2values := make([]float64, bbs.Len())
|
y2values := make([]float64, bbs.Len())
|
||||||
y2values[0] = v0y2
|
y2values[0] = v0y2
|
||||||
|
|
||||||
|
style.GetFillAndStrokeOptions().WriteToRenderer(r)
|
||||||
r.MoveTo(x0, y0)
|
r.MoveTo(x0, y0)
|
||||||
for i := 1; i < bbs.Len(); i++ {
|
for i := 1; i < bbs.Len(); i++ {
|
||||||
vx, vy1, vy2 = bbs.GetBoundedValue(i)
|
vx, vy1, vy2 = bbs.GetBoundedValue(i)
|
||||||
|
|
|
@ -9,7 +9,7 @@ const (
|
||||||
type EMASeries struct {
|
type EMASeries struct {
|
||||||
Name string
|
Name string
|
||||||
Style Style
|
Style Style
|
||||||
YAxis YAxisType
|
YAxis yAxisType
|
||||||
|
|
||||||
Period int
|
Period int
|
||||||
InnerSeries ValueProvider
|
InnerSeries ValueProvider
|
||||||
|
@ -28,7 +28,7 @@ func (ema EMASeries) GetStyle() Style {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetYAxis returns which YAxis the series draws on.
|
// GetYAxis returns which YAxis the series draws on.
|
||||||
func (ema EMASeries) GetYAxis() YAxisType {
|
func (ema EMASeries) GetYAxis() yAxisType {
|
||||||
return ema.YAxis
|
return ema.YAxis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||||
graph := chart.Chart{
|
graph := chart.Chart{
|
||||||
XAxis: chart.XAxis{
|
XAxis: chart.XAxis{
|
||||||
Style: chart.Style{Show: true},
|
Style: chart.Style{Show: true},
|
||||||
|
TickPosition: chart.TickPositionBetweenTicks,
|
||||||
},
|
},
|
||||||
YAxis: chart.YAxis{
|
YAxis: chart.YAxis{
|
||||||
Style: chart.Style{Show: true},
|
Style: chart.Style{Show: true},
|
||||||
|
|
|
@ -6,7 +6,7 @@ package chart
|
||||||
type HistogramSeries struct {
|
type HistogramSeries struct {
|
||||||
Name string
|
Name string
|
||||||
Style Style
|
Style Style
|
||||||
YAxis YAxisType
|
YAxis yAxisType
|
||||||
InnerSeries ValueProvider
|
InnerSeries ValueProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ func (hs HistogramSeries) GetStyle() Style {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetYAxis returns which yaxis the series is mapped to.
|
// GetYAxis returns which yaxis the series is mapped to.
|
||||||
func (hs HistogramSeries) GetYAxis() YAxisType {
|
func (hs HistogramSeries) GetYAxis() yAxisType {
|
||||||
return hs.YAxis
|
return hs.YAxis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ package chart
|
||||||
type LinearRegressionSeries struct {
|
type LinearRegressionSeries struct {
|
||||||
Name string
|
Name string
|
||||||
Style Style
|
Style Style
|
||||||
YAxis YAxisType
|
YAxis yAxisType
|
||||||
|
|
||||||
Window int
|
Window int
|
||||||
Offset int
|
Offset int
|
||||||
|
@ -28,7 +28,7 @@ func (lrs LinearRegressionSeries) GetStyle() Style {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetYAxis returns which YAxis the series draws on.
|
// GetYAxis returns which YAxis the series draws on.
|
||||||
func (lrs LinearRegressionSeries) GetYAxis() YAxisType {
|
func (lrs LinearRegressionSeries) GetYAxis() yAxisType {
|
||||||
return lrs.YAxis
|
return lrs.YAxis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ const (
|
||||||
type MACDSeries struct {
|
type MACDSeries struct {
|
||||||
Name string
|
Name string
|
||||||
Style Style
|
Style Style
|
||||||
YAxis YAxisType
|
YAxis yAxisType
|
||||||
InnerSeries ValueProvider
|
InnerSeries ValueProvider
|
||||||
|
|
||||||
PrimaryPeriod int
|
PrimaryPeriod int
|
||||||
|
@ -56,7 +56,7 @@ func (macd MACDSeries) GetStyle() Style {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetYAxis returns which YAxis the series draws on.
|
// GetYAxis returns which YAxis the series draws on.
|
||||||
func (macd MACDSeries) GetYAxis() YAxisType {
|
func (macd MACDSeries) GetYAxis() yAxisType {
|
||||||
return macd.YAxis
|
return macd.YAxis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ func (macd *MACDSeries) ensureChildSeries() {
|
||||||
type MACDSignalSeries struct {
|
type MACDSignalSeries struct {
|
||||||
Name string
|
Name string
|
||||||
Style Style
|
Style Style
|
||||||
YAxis YAxisType
|
YAxis yAxisType
|
||||||
InnerSeries ValueProvider
|
InnerSeries ValueProvider
|
||||||
|
|
||||||
PrimaryPeriod int
|
PrimaryPeriod int
|
||||||
|
@ -150,7 +150,7 @@ func (macds MACDSignalSeries) GetStyle() Style {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetYAxis returns which YAxis the series draws on.
|
// GetYAxis returns which YAxis the series draws on.
|
||||||
func (macds MACDSignalSeries) GetYAxis() YAxisType {
|
func (macds MACDSignalSeries) GetYAxis() yAxisType {
|
||||||
return macds.YAxis
|
return macds.YAxis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,7 +200,7 @@ func (macds *MACDSignalSeries) Render(r Renderer, canvasBox Box, xrange, yrange
|
||||||
type MACDLineSeries struct {
|
type MACDLineSeries struct {
|
||||||
Name string
|
Name string
|
||||||
Style Style
|
Style Style
|
||||||
YAxis YAxisType
|
YAxis yAxisType
|
||||||
InnerSeries ValueProvider
|
InnerSeries ValueProvider
|
||||||
|
|
||||||
PrimaryPeriod int
|
PrimaryPeriod int
|
||||||
|
@ -223,7 +223,7 @@ func (macdl MACDLineSeries) GetStyle() Style {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetYAxis returns which YAxis the series draws on.
|
// GetYAxis returns which YAxis the series draws on.
|
||||||
func (macdl MACDLineSeries) GetYAxis() YAxisType {
|
func (macdl MACDLineSeries) GetYAxis() yAxisType {
|
||||||
return macdl.YAxis
|
return macdl.YAxis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ package chart
|
||||||
// Series is an alias to Renderable.
|
// Series is an alias to Renderable.
|
||||||
type Series interface {
|
type Series interface {
|
||||||
GetName() string
|
GetName() string
|
||||||
GetYAxis() YAxisType
|
GetYAxis() yAxisType
|
||||||
GetStyle() Style
|
GetStyle() Style
|
||||||
Render(r Renderer, canvasBox Box, xrange, yrange Range, s Style)
|
Render(r Renderer, canvasBox Box, xrange, yrange Range, s Style)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ const (
|
||||||
type SMASeries struct {
|
type SMASeries struct {
|
||||||
Name string
|
Name string
|
||||||
Style Style
|
Style Style
|
||||||
YAxis YAxisType
|
YAxis yAxisType
|
||||||
|
|
||||||
Period int
|
Period int
|
||||||
InnerSeries ValueProvider
|
InnerSeries ValueProvider
|
||||||
|
@ -26,7 +26,7 @@ func (sma SMASeries) GetStyle() Style {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetYAxis returns which YAxis the series draws on.
|
// GetYAxis returns which YAxis the series draws on.
|
||||||
func (sma SMASeries) GetYAxis() YAxisType {
|
func (sma SMASeries) GetYAxis() yAxisType {
|
||||||
return sma.YAxis
|
return sma.YAxis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ type TimeSeries struct {
|
||||||
Name string
|
Name string
|
||||||
Style Style
|
Style Style
|
||||||
|
|
||||||
YAxis YAxisType
|
YAxis yAxisType
|
||||||
|
|
||||||
XValues []time.Time
|
XValues []time.Time
|
||||||
YValues []float64
|
YValues []float64
|
||||||
|
@ -50,7 +50,7 @@ func (ts TimeSeries) GetValueFormatters() (x, y ValueFormatter) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetYAxis returns which YAxis the series draws on.
|
// GetYAxis returns which YAxis the series draws on.
|
||||||
func (ts TimeSeries) GetYAxis() YAxisType {
|
func (ts TimeSeries) GetYAxis() yAxisType {
|
||||||
return ts.YAxis
|
return ts.YAxis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
46
xaxis.go
46
xaxis.go
|
@ -13,6 +13,8 @@ type XAxis struct {
|
||||||
Range Range
|
Range Range
|
||||||
Ticks []Tick
|
Ticks []Tick
|
||||||
|
|
||||||
|
TickPosition tickPosition
|
||||||
|
|
||||||
GridLines []GridLine
|
GridLines []GridLine
|
||||||
GridMajorStyle Style
|
GridMajorStyle Style
|
||||||
GridMinorStyle Style
|
GridMinorStyle Style
|
||||||
|
@ -28,6 +30,17 @@ func (xa XAxis) GetStyle() Style {
|
||||||
return xa.Style
|
return xa.Style
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetTickPosition returns the tick position option for the axis.
|
||||||
|
func (xa XAxis) GetTickPosition(defaults ...tickPosition) tickPosition {
|
||||||
|
if xa.TickPosition == TickPositionUnset {
|
||||||
|
if len(defaults) > 0 {
|
||||||
|
return defaults[0]
|
||||||
|
}
|
||||||
|
return TickPositionUnderTick
|
||||||
|
}
|
||||||
|
return xa.TickPosition
|
||||||
|
}
|
||||||
|
|
||||||
// GetTicks returns the ticks for a series.
|
// GetTicks returns the ticks for a series.
|
||||||
// The coalesce priority is:
|
// The coalesce priority is:
|
||||||
// - User Supplied Ticks (i.e. Ticks array on the axis itself).
|
// - User Supplied Ticks (i.e. Ticks array on the axis itself).
|
||||||
|
@ -82,25 +95,48 @@ func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, tic
|
||||||
|
|
||||||
// Render renders the axis
|
// Render renders the axis
|
||||||
func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) {
|
func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) {
|
||||||
xa.Style.InheritFrom(defaults).WriteToRenderer(r)
|
tickStyle := xa.Style.InheritFrom(defaults)
|
||||||
|
|
||||||
|
tickStyle.GetStrokeOptions().WriteToRenderer(r)
|
||||||
r.MoveTo(canvasBox.Left, canvasBox.Bottom)
|
r.MoveTo(canvasBox.Left, canvasBox.Bottom)
|
||||||
r.LineTo(canvasBox.Right, canvasBox.Bottom)
|
r.LineTo(canvasBox.Right, canvasBox.Bottom)
|
||||||
r.Stroke()
|
r.Stroke()
|
||||||
|
|
||||||
sort.Sort(Ticks(ticks))
|
sort.Sort(Ticks(ticks))
|
||||||
|
|
||||||
for _, t := range ticks {
|
tp := xa.GetTickPosition()
|
||||||
|
|
||||||
|
var tx, ty int
|
||||||
|
for index, t := range ticks {
|
||||||
v := t.Value
|
v := t.Value
|
||||||
lx := ra.Translate(v)
|
lx := ra.Translate(v)
|
||||||
tb := r.MeasureText(t.Label)
|
tb := r.MeasureText(t.Label)
|
||||||
tx := canvasBox.Left + lx
|
|
||||||
ty := canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
|
|
||||||
r.Text(t.Label, tx-tb.Width()>>1, ty)
|
|
||||||
|
|
||||||
|
tx = canvasBox.Left + lx
|
||||||
|
ty = canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
|
||||||
|
|
||||||
|
tickStyle.GetStrokeOptions().WriteToRenderer(r)
|
||||||
r.MoveTo(tx, canvasBox.Bottom)
|
r.MoveTo(tx, canvasBox.Bottom)
|
||||||
r.LineTo(tx, canvasBox.Bottom+DefaultVerticalTickHeight)
|
r.LineTo(tx, canvasBox.Bottom+DefaultVerticalTickHeight)
|
||||||
r.Stroke()
|
r.Stroke()
|
||||||
|
|
||||||
|
switch tp {
|
||||||
|
case TickPositionUnderTick:
|
||||||
|
tickStyle.GetTextOptions().WriteToRenderer(r)
|
||||||
|
r.Text(t.Label, tx-tb.Width()>>1, ty)
|
||||||
|
case TickPositionBetweenTicks:
|
||||||
|
if index > 0 {
|
||||||
|
llx := ra.Translate(ticks[index-1].Value)
|
||||||
|
ltx := canvasBox.Left + llx
|
||||||
|
Draw.TextWithin(r, t.Label, Box{
|
||||||
|
Left: ltx,
|
||||||
|
Right: tx,
|
||||||
|
Top: canvasBox.Bottom + DefaultXAxisMargin,
|
||||||
|
Bottom: canvasBox.Bottom + DefaultXAxisMargin + tb.Height(),
|
||||||
|
}, tickStyle.InheritFrom(Style{TextHorizontalAlign: TextHorizontalAlignCenter}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if xa.GridMajorStyle.Show || xa.GridMinorStyle.Show {
|
if xa.GridMajorStyle.Show || xa.GridMinorStyle.Show {
|
||||||
|
|
2
yaxis.go
2
yaxis.go
|
@ -13,7 +13,7 @@ type YAxis struct {
|
||||||
|
|
||||||
Zero GridLine
|
Zero GridLine
|
||||||
|
|
||||||
AxisType YAxisType
|
AxisType yAxisType
|
||||||
|
|
||||||
ValueFormatter ValueFormatter
|
ValueFormatter ValueFormatter
|
||||||
Range Range
|
Range Range
|
||||||
|
|
Loading…
Reference in a new issue