ticks refactor.
This commit is contained in:
parent
78645130e4
commit
a6b6097c20
21
date/util.go
21
date/util.go
|
@ -73,6 +73,7 @@ var (
|
|||
// HolidayProvider is a function that returns if a given time falls on a holiday.
|
||||
type HolidayProvider func(time.Time) bool
|
||||
|
||||
// DefaultHolidayProvider implements `HolidayProvider` and just returns false.
|
||||
func DefaultHolidayProvider(_ time.Time) bool { return false }
|
||||
|
||||
// IsNYSEHoliday returns if a date was/is on a nyse holiday day.
|
||||
|
@ -212,6 +213,16 @@ func Eastern() *time.Location {
|
|||
return _eastern
|
||||
}
|
||||
|
||||
// ClockTime returns a new time.Time for the given clock components.
|
||||
func ClockTime(hour, min, sec, nsec int, loc *time.Location) time.Time {
|
||||
return time.Date(0, 0, 0, hour, min, sec, nsec, loc)
|
||||
}
|
||||
|
||||
// On returns the clock components of clock (hour,minute,second) on the date components of d.
|
||||
func On(clock, d time.Time) time.Time {
|
||||
return time.Date(d.Year(), d.Month(), d.Day(), clock.Hour(), clock.Minute(), clock.Second(), clock.Nanosecond(), clock.Location())
|
||||
}
|
||||
|
||||
// Optional returns a pointer reference to a given time.
|
||||
func Optional(t time.Time) *time.Time {
|
||||
return &t
|
||||
|
@ -324,16 +335,6 @@ func CalculateMarketSecondsBetween(start, end, marketOpen, marketClose time.Time
|
|||
return
|
||||
}
|
||||
|
||||
// ClockTime returns a new time.Time for the given clock components.
|
||||
func ClockTime(hour, min, sec, nsec int, loc *time.Location) time.Time {
|
||||
return time.Date(0, 0, 0, hour, min, sec, nsec, loc)
|
||||
}
|
||||
|
||||
// On returns the clock components of clock (hour,minute,second) on the date components of d.
|
||||
func On(clock, d time.Time) time.Time {
|
||||
return time.Date(d.Year(), d.Month(), d.Day(), clock.Hour(), clock.Minute(), clock.Second(), clock.Nanosecond(), clock.Location())
|
||||
}
|
||||
|
||||
// Format returns a string representation of a date.
|
||||
func format(t time.Time) string {
|
||||
return t.Format("2006-01-02")
|
||||
|
|
57
grid_line.go
57
grid_line.go
|
@ -1,31 +1,8 @@
|
|||
package chart
|
||||
|
||||
// GenerateGridLines generates grid lines.
|
||||
func GenerateGridLines(ticks []Tick, isVertical bool) []GridLine {
|
||||
var gl []GridLine
|
||||
isMinor := false
|
||||
minorStyle := Style{
|
||||
StrokeColor: DefaultGridLineColor.WithAlpha(100),
|
||||
StrokeWidth: 1.0,
|
||||
}
|
||||
majorStyle := Style{
|
||||
StrokeColor: DefaultGridLineColor,
|
||||
StrokeWidth: 1.0,
|
||||
}
|
||||
for _, t := range ticks {
|
||||
s := majorStyle
|
||||
if isMinor {
|
||||
s = minorStyle
|
||||
}
|
||||
gl = append(gl, GridLine{
|
||||
Style: s,
|
||||
IsMinor: isMinor,
|
||||
IsVertical: isVertical,
|
||||
Value: t.Value,
|
||||
})
|
||||
isMinor = !isMinor
|
||||
}
|
||||
return gl
|
||||
// GridLineProvider is a type that provides grid lines.
|
||||
type GridLineProvider interface {
|
||||
GetGridLines(ticks []Tick, isVertical bool) []GridLine
|
||||
}
|
||||
|
||||
// GridLine is a line on a graph canvas.
|
||||
|
@ -82,3 +59,31 @@ func (gl GridLine) Render(r Renderer, canvasBox Box, ra Range) {
|
|||
r.Stroke()
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateGridLines generates grid lines.
|
||||
func GenerateGridLines(ticks []Tick, isVertical bool) []GridLine {
|
||||
var gl []GridLine
|
||||
isMinor := false
|
||||
minorStyle := Style{
|
||||
StrokeColor: DefaultGridLineColor.WithAlpha(100),
|
||||
StrokeWidth: 1.0,
|
||||
}
|
||||
majorStyle := Style{
|
||||
StrokeColor: DefaultGridLineColor,
|
||||
StrokeWidth: 1.0,
|
||||
}
|
||||
for _, t := range ticks {
|
||||
s := majorStyle
|
||||
if isMinor {
|
||||
s = minorStyle
|
||||
}
|
||||
gl = append(gl, GridLine{
|
||||
Style: s,
|
||||
IsMinor: isMinor,
|
||||
IsVertical: isVertical,
|
||||
Value: t.Value,
|
||||
})
|
||||
isMinor = !isMinor
|
||||
}
|
||||
return gl
|
||||
}
|
||||
|
|
|
@ -63,6 +63,38 @@ func (mhr *MarketHoursRange) SetDomain(domain int) {
|
|||
mhr.Domain = domain
|
||||
}
|
||||
|
||||
// GetHolidayProvider coalesces a userprovided holiday provider and the date.DefaultHolidayProvider.
|
||||
func (mhr MarketHoursRange) GetHolidayProvider() date.HolidayProvider {
|
||||
if mhr.HolidayProvider == nil {
|
||||
return date.DefaultHolidayProvider
|
||||
}
|
||||
return mhr.HolidayProvider
|
||||
}
|
||||
|
||||
// GetTicks returns the ticks for the range.
|
||||
// This is to override the default continous ticks that would be generated for the range.
|
||||
func (mhr *MarketHoursRange) GetTicks(vf ValueFormatter) []Tick {
|
||||
// return one tick per day
|
||||
// figure out how to advance one ticke per market day.
|
||||
var ticks []Tick
|
||||
|
||||
cursor := date.On(mhr.MarketOpen, mhr.Min)
|
||||
maxClose := date.On(mhr.MarketClose, mhr.Max)
|
||||
|
||||
for date.BeforeDate(cursor, maxClose) {
|
||||
if date.IsWeekDay(cursor.Weekday()) && !mhr.GetHolidayProvider()(cursor) {
|
||||
ticks = append(ticks, Tick{
|
||||
Value: TimeToFloat64(cursor),
|
||||
Label: vf(cursor),
|
||||
})
|
||||
}
|
||||
|
||||
cursor = cursor.AddDate(0, 0, 1)
|
||||
}
|
||||
|
||||
return ticks
|
||||
}
|
||||
|
||||
func (mhr MarketHoursRange) String() string {
|
||||
return fmt.Sprintf("MarketHoursRange [%s, %s] => %d", mhr.Min.Format(DefaultDateFormat), mhr.Max.Format(DefaultDateFormat), mhr.Domain)
|
||||
}
|
||||
|
|
62
tick.go
62
tick.go
|
@ -1,21 +1,10 @@
|
|||
package chart
|
||||
|
||||
// GenerateTicksWithStep generates a set of ticks.
|
||||
func GenerateTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick {
|
||||
var ticks []Tick
|
||||
min, max := ra.GetMin(), ra.GetMax()
|
||||
for cursor := min; cursor <= max; cursor += step {
|
||||
ticks = append(ticks, Tick{
|
||||
Value: cursor,
|
||||
Label: vf(cursor),
|
||||
})
|
||||
import "math"
|
||||
|
||||
// this guard is in place in case step is super, super small.
|
||||
if len(ticks) > DefaultTickCountSanityCheck {
|
||||
return ticks
|
||||
}
|
||||
}
|
||||
return ticks
|
||||
// TicksProvider is a type that provides ticks.
|
||||
type TicksProvider interface {
|
||||
GetTicks(vf ValueFormatter) []Tick
|
||||
}
|
||||
|
||||
// Tick represents a label on an axis.
|
||||
|
@ -41,3 +30,46 @@ func (t Ticks) Swap(i, j int) {
|
|||
func (t Ticks) Less(i, j int) bool {
|
||||
return t[i].Value < t[j].Value
|
||||
}
|
||||
|
||||
// GenerateContinuousTicksWithStep generates a set of ticks.
|
||||
func GenerateContinuousTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick {
|
||||
var ticks []Tick
|
||||
min, max := ra.GetMin(), ra.GetMax()
|
||||
for cursor := min; cursor <= max; cursor += step {
|
||||
ticks = append(ticks, Tick{
|
||||
Value: cursor,
|
||||
Label: vf(cursor),
|
||||
})
|
||||
|
||||
// this guard is in place in case step is super, super small.
|
||||
if len(ticks) > DefaultTickCountSanityCheck {
|
||||
return ticks
|
||||
}
|
||||
}
|
||||
return ticks
|
||||
}
|
||||
|
||||
// CalculateContinuousTickStep calculates the continous range interval between ticks.
|
||||
func CalculateContinuousTickStep(r Renderer, ra Range, isVertical bool, style Style, vf ValueFormatter) float64 {
|
||||
r.SetFont(style.GetFont())
|
||||
r.SetFontSize(style.GetFontSize())
|
||||
if isVertical {
|
||||
label := vf(ra.GetMin())
|
||||
tb := r.MeasureText(label)
|
||||
count := int(math.Ceil(float64(ra.GetDomain()) / float64(tb.Height()+DefaultMinimumTickVerticalSpacing)))
|
||||
return ra.GetDelta() / float64(count)
|
||||
}
|
||||
|
||||
// take a cut at determining the 'widest' value.
|
||||
l0 := vf(ra.GetMin())
|
||||
ln := vf(ra.GetMax())
|
||||
ll := l0
|
||||
if len(ln) > len(l0) {
|
||||
ll = ln
|
||||
}
|
||||
llb := r.MeasureText(ll)
|
||||
textWidth := llb.Width()
|
||||
width := textWidth + DefaultMinimumTickHorizontalSpacing
|
||||
count := int(math.Ceil(float64(ra.GetDomain()) / float64(width)))
|
||||
return ra.GetDelta() / float64(count)
|
||||
}
|
||||
|
|
|
@ -9,6 +9,6 @@ import (
|
|||
func TestGenerateTicksWithStep(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
ticks := GenerateTicksWithStep(&ContinuousRange{Min: 1.0, Max: 10.0, Domain: 100}, 1.0, FloatValueFormatter)
|
||||
ticks := GenerateContinuousTicksWithStep(&ContinuousRange{Min: 1.0, Max: 10.0, Domain: 100}, 1.0, FloatValueFormatter)
|
||||
assert.Len(ticks, 10)
|
||||
}
|
||||
|
|
33
xaxis.go
33
xaxis.go
|
@ -34,36 +34,11 @@ func (xa XAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter
|
|||
if len(xa.Ticks) > 0 {
|
||||
return xa.Ticks
|
||||
}
|
||||
return xa.generateTicks(r, ra, defaults, vf)
|
||||
}
|
||||
|
||||
func (xa XAxis) generateTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter) []Tick {
|
||||
step := xa.getTickStep(r, ra, defaults, vf)
|
||||
return GenerateTicksWithStep(ra, step, vf)
|
||||
}
|
||||
|
||||
func (xa XAxis) getTickStep(r Renderer, ra Range, defaults Style, vf ValueFormatter) float64 {
|
||||
tickCount := xa.getTickCount(r, ra, defaults, vf)
|
||||
step := ra.GetDelta() / float64(tickCount)
|
||||
return step
|
||||
}
|
||||
|
||||
func (xa XAxis) getTickCount(r Renderer, ra Range, defaults Style, vf ValueFormatter) int {
|
||||
r.SetFont(xa.Style.GetFont(defaults.GetFont()))
|
||||
r.SetFontSize(xa.Style.GetFontSize(defaults.GetFontSize(DefaultFontSize)))
|
||||
|
||||
// take a cut at determining the 'widest' value.
|
||||
l0 := vf(ra.GetMin())
|
||||
ln := vf(ra.GetMax())
|
||||
ll := l0
|
||||
if len(ln) > len(l0) {
|
||||
ll = ln
|
||||
if tp, isTickProvider := ra.(TicksProvider); isTickProvider {
|
||||
return tp.GetTicks(vf)
|
||||
}
|
||||
llb := r.MeasureText(ll)
|
||||
textWidth := llb.Width()
|
||||
width := textWidth + DefaultMinimumTickHorizontalSpacing
|
||||
count := int(math.Ceil(float64(ra.GetDomain()) / float64(width)))
|
||||
return count
|
||||
step := CalculateContinuousTickStep(r, ra, false, xa.Style.InheritFrom(defaults), vf)
|
||||
return GenerateContinuousTicksWithStep(ra, step, vf)
|
||||
}
|
||||
|
||||
// GetGridLines returns the gridlines for the axis.
|
||||
|
|
|
@ -6,46 +6,6 @@ import (
|
|||
"github.com/blendlabs/go-assert"
|
||||
)
|
||||
|
||||
func TestXAxisGetTickCount(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
r, err := PNG(1024, 1024)
|
||||
assert.Nil(err)
|
||||
|
||||
f, err := GetDefaultFont()
|
||||
assert.Nil(err)
|
||||
|
||||
xa := XAxis{}
|
||||
xr := &ContinuousRange{Min: 10, Max: 100, Domain: 1024}
|
||||
styleDefaults := Style{
|
||||
Font: f,
|
||||
FontSize: 10.0,
|
||||
}
|
||||
vf := FloatValueFormatter
|
||||
count := xa.getTickCount(r, xr, styleDefaults, vf)
|
||||
assert.Equal(16, count)
|
||||
}
|
||||
|
||||
func TestXAxisGetTickStep(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
r, err := PNG(1024, 1024)
|
||||
assert.Nil(err)
|
||||
|
||||
f, err := GetDefaultFont()
|
||||
assert.Nil(err)
|
||||
|
||||
xa := XAxis{}
|
||||
xr := &ContinuousRange{Min: 10, Max: 100, Domain: 1024}
|
||||
styleDefaults := Style{
|
||||
Font: f,
|
||||
FontSize: 10.0,
|
||||
}
|
||||
vf := FloatValueFormatter
|
||||
step := xa.getTickStep(r, xr, styleDefaults, vf)
|
||||
assert.Equal(xr.GetDelta()/16.0, step)
|
||||
}
|
||||
|
||||
func TestXAxisGetTicks(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
|
|
28
yaxis.go
28
yaxis.go
|
@ -41,29 +41,11 @@ func (ya YAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter
|
|||
if len(ya.Ticks) > 0 {
|
||||
return ya.Ticks
|
||||
}
|
||||
return ya.generateTicks(r, ra, defaults, vf)
|
||||
}
|
||||
|
||||
func (ya YAxis) generateTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter) []Tick {
|
||||
step := ya.getTickStep(r, ra, defaults, vf)
|
||||
ticks := GenerateTicksWithStep(ra, step, vf)
|
||||
return ticks
|
||||
}
|
||||
|
||||
func (ya YAxis) getTickStep(r Renderer, ra Range, defaults Style, vf ValueFormatter) float64 {
|
||||
tickCount := ya.getTickCount(r, ra, defaults, vf)
|
||||
step := ra.GetDelta() / float64(tickCount)
|
||||
return step
|
||||
}
|
||||
|
||||
func (ya YAxis) getTickCount(r Renderer, ra Range, defaults Style, vf ValueFormatter) int {
|
||||
r.SetFont(ya.Style.GetFont(defaults.GetFont()))
|
||||
r.SetFontSize(ya.Style.GetFontSize(defaults.GetFontSize(DefaultFontSize)))
|
||||
//given the domain, figure out how many ticks we can draw ...
|
||||
label := vf(ra.GetMin())
|
||||
tb := r.MeasureText(label)
|
||||
count := int(math.Ceil(float64(ra.GetDomain()) / float64(tb.Height()+DefaultMinimumTickVerticalSpacing)))
|
||||
return count
|
||||
if tp, isTickProvider := ra.(TicksProvider); isTickProvider {
|
||||
return tp.GetTicks(vf)
|
||||
}
|
||||
step := CalculateContinuousTickStep(r, ra, true, ya.Style.InheritFrom(defaults), vf)
|
||||
return GenerateContinuousTicksWithStep(ra, step, vf)
|
||||
}
|
||||
|
||||
// GetGridLines returns the gridlines for the axis.
|
||||
|
|
|
@ -6,46 +6,6 @@ import (
|
|||
"github.com/blendlabs/go-assert"
|
||||
)
|
||||
|
||||
func TestYAxisGetTickCount(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
r, err := PNG(1024, 1024)
|
||||
assert.Nil(err)
|
||||
|
||||
f, err := GetDefaultFont()
|
||||
assert.Nil(err)
|
||||
|
||||
ya := YAxis{}
|
||||
yr := &ContinuousRange{Min: 10, Max: 100, Domain: 1024}
|
||||
styleDefaults := Style{
|
||||
Font: f,
|
||||
FontSize: 10.0,
|
||||
}
|
||||
vf := FloatValueFormatter
|
||||
count := ya.getTickCount(r, yr, styleDefaults, vf)
|
||||
assert.Equal(34, count)
|
||||
}
|
||||
|
||||
func TestYAxisGetTickStep(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
r, err := PNG(1024, 1024)
|
||||
assert.Nil(err)
|
||||
|
||||
f, err := GetDefaultFont()
|
||||
assert.Nil(err)
|
||||
|
||||
ya := YAxis{}
|
||||
yr := &ContinuousRange{Min: 10, Max: 100, Domain: 1024}
|
||||
styleDefaults := Style{
|
||||
Font: f,
|
||||
FontSize: 10.0,
|
||||
}
|
||||
vf := FloatValueFormatter
|
||||
step := ya.getTickStep(r, yr, styleDefaults, vf)
|
||||
assert.Equal(yr.GetDelta()/34.0, step)
|
||||
}
|
||||
|
||||
func TestYAxisGetTicks(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user