This commit is contained in:
Will Charczuk 2017-05-01 22:33:49 -07:00
parent 41d81c82db
commit 2f84185f46
5 changed files with 57 additions and 43 deletions

View file

@ -2,12 +2,12 @@ package sequence
// Values returns the array values of a linear sequence with a given start, end and optional step. // Values returns the array values of a linear sequence with a given start, end and optional step.
func Values(start, end float64) []float64 { func Values(start, end float64) []float64 {
return Seq{NewLinear().WithOffset(start).WithLimit(end).WithStep(1.0)}.Array() return Seq{NewLinear().WithStart(start).WithEnd(end).WithStep(1.0)}.Array()
} }
// ValuesWithStep returns the array values of a linear sequence with a given start, end and optional step. // ValuesWithStep returns the array values of a linear sequence with a given start, end and optional step.
func ValuesWithStep(start, end, step float64) []float64 { func ValuesWithStep(start, end, step float64) []float64 {
return Seq{NewLinear().WithOffset(start).WithLimit(end).WithStep(step)}.Array() return Seq{NewLinear().WithStart(start).WithEnd(end).WithStep(step)}.Array()
} }
// NewLinear returns a new linear generator. // NewLinear returns a new linear generator.
@ -17,30 +17,36 @@ func NewLinear() *Linear {
// Linear is a stepwise generator. // Linear is a stepwise generator.
type Linear struct { type Linear struct {
offset float64 start float64
limit float64 end float64
step float64 step float64
} }
// Len returns the number of elements in the sequence. // Len returns the number of elements in the sequence.
func (lg Linear) Len() int { func (lg Linear) Len() int {
return (int((lg.limit - lg.offset) / lg.step)) + 1 if lg.start < lg.end {
return int((lg.end-lg.start)/lg.step) + 1
}
return int((lg.start-lg.end)/lg.step) + 1
} }
// GetValue returns the value at a given index. // GetValue returns the value at a given index.
func (lg Linear) GetValue(index int) float64 { func (lg Linear) GetValue(index int) float64 {
return lg.offset + (float64(index) * lg.step) if lg.start < lg.end {
return lg.start + (float64(index) * lg.step)
}
return lg.end + (float64(index) * lg.step)
} }
// WithOffset sets the offset and returns the linear generator. // WithStart sets the start and returns the linear generator.
func (lg *Linear) WithOffset(offset float64) *Linear { func (lg *Linear) WithStart(start float64) *Linear {
lg.offset = offset lg.start = start
return lg return lg
} }
// WithLimit sets the step and returns the linear generator. // WithEnd sets the end and returns the linear generator.
func (lg *Linear) WithLimit(limit float64) *Linear { func (lg *Linear) WithEnd(end float64) *Linear {
lg.limit = limit lg.end = end
return lg return lg
} }

View file

@ -15,10 +15,18 @@ func TestValues(t *testing.T) {
assert.Equal(100, values[99]) assert.Equal(100, values[99])
} }
func TestValueWithStep(t *testing.T) { func TestValuesWithStep(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
values := ValuesWithStep(0, 100, 5) values := ValuesWithStep(0, 100, 5)
assert.Equal(100, values[20]) assert.Equal(100, values[20])
assert.Len(values, 21) assert.Len(values, 21)
} }
func TestValuesReversed(t *testing.T) {
assert := assert.New(t)
values := Values(10.0, 1.0)
assert.NotEmpty(values)
assert.Len(values, 10)
}

View file

@ -81,3 +81,31 @@ func TestSequenceHoursFill(t *testing.T) {
assert.NotZero(filledValues[16]) assert.NotZero(filledValues[16])
} }
func TestTimeStart(t *testing.T) {
assert := assert.New(t)
times := []time.Time{
time.Now().AddDate(0, 0, -4),
time.Now().AddDate(0, 0, -2),
time.Now().AddDate(0, 0, -1),
time.Now().AddDate(0, 0, -3),
time.Now().AddDate(0, 0, -5),
}
assert.InTimeDelta(Time.Start(times), times[4], time.Millisecond)
}
func TestTimeEnd(t *testing.T) {
assert := assert.New(t)
times := []time.Time{
time.Now().AddDate(0, 0, -4),
time.Now().AddDate(0, 0, -2),
time.Now().AddDate(0, 0, -1),
time.Now().AddDate(0, 0, -3),
time.Now().AddDate(0, 0, -5),
}
assert.InTimeDelta(Time.End(times), times[2], time.Millisecond)
}

View file

@ -202,7 +202,7 @@ func (sbc StackedBarChart) drawYAxis(r Renderer, canvasBox Box) {
r.LineTo(canvasBox.Right+DefaultHorizontalTickWidth, canvasBox.Bottom) r.LineTo(canvasBox.Right+DefaultHorizontalTickWidth, canvasBox.Bottom)
r.Stroke() r.Stroke()
ticks := sequence.Seq{Provider: sequence.NewLinear().WithLimit(1.0).WithStep(0.2)}.Array() ticks := sequence.ValuesWithStep(0.0, 1.0, 0.2)
for _, t := range ticks { for _, t := range ticks {
axisStyle.GetStrokeOptions().WriteToRenderer(r) axisStyle.GetStrokeOptions().WriteToRenderer(r)
ty := canvasBox.Bottom - int(t*float64(canvasBox.Height())) ty := canvasBox.Bottom - int(t*float64(canvasBox.Height()))

View file

@ -237,34 +237,6 @@ func TestDateIsNYSEHoliday(t *testing.T) {
assert.Equal(holidays, 55) assert.Equal(holidays, 55)
} }
func TestTimeStart(t *testing.T) {
assert := assert.New(t)
times := []time.Time{
time.Now().AddDate(0, 0, -4),
time.Now().AddDate(0, 0, -2),
time.Now().AddDate(0, 0, -1),
time.Now().AddDate(0, 0, -3),
time.Now().AddDate(0, 0, -5),
}
assert.InTimeDelta(Date.Start(times), times[4], time.Millisecond)
}
func TestTimeEnd(t *testing.T) {
assert := assert.New(t)
times := []time.Time{
time.Now().AddDate(0, 0, -4),
time.Now().AddDate(0, 0, -2),
time.Now().AddDate(0, 0, -1),
time.Now().AddDate(0, 0, -3),
time.Now().AddDate(0, 0, -5),
}
assert.InTimeDelta(Date.End(times), times[2], time.Millisecond)
}
func TestDateDiffDays(t *testing.T) { func TestDateDiffDays(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)