go-chart/seq/linear.go

74 lines
1.7 KiB
Go
Raw Normal View History

2017-05-12 17:17:43 -04:00
package seq
2017-04-30 00:12:39 -04:00
2017-05-12 17:17:43 -04:00
// Range returns the array values of a linear seq with a given start, end and optional step.
func Range(start, end float64) []float64 {
2017-05-02 01:33:49 -04:00
return Seq{NewLinear().WithStart(start).WithEnd(end).WithStep(1.0)}.Array()
2017-04-30 03:39:38 -04:00
}
2017-05-12 17:17:43 -04:00
// RangeWithStep returns the array values of a linear seq with a given start, end and optional step.
func RangeWithStep(start, end, step float64) []float64 {
2017-05-02 01:33:49 -04:00
return Seq{NewLinear().WithStart(start).WithEnd(end).WithStep(step)}.Array()
2017-04-30 03:39:38 -04:00
}
2017-04-30 00:12:39 -04:00
// NewLinear returns a new linear generator.
func NewLinear() *Linear {
2017-05-12 17:17:43 -04:00
return &Linear{step: 1.0}
2017-04-30 00:12:39 -04:00
}
// Linear is a stepwise generator.
type Linear struct {
2017-05-02 01:33:49 -04:00
start float64
end float64
step float64
2017-04-30 00:12:39 -04:00
}
2017-05-12 17:17:43 -04:00
// Start returns the start value.
func (lg Linear) Start() float64 {
return lg.start
}
// End returns the end value.
func (lg Linear) End() float64 {
return lg.end
}
// Step returns the step value.
func (lg Linear) Step() float64 {
return lg.step
}
// Len returns the number of elements in the seq.
2017-04-30 00:12:39 -04:00
func (lg Linear) Len() int {
2017-05-02 01:33:49 -04:00
if lg.start < lg.end {
return int((lg.end-lg.start)/lg.step) + 1
}
return int((lg.start-lg.end)/lg.step) + 1
2017-04-30 00:12:39 -04:00
}
// GetValue returns the value at a given index.
func (lg Linear) GetValue(index int) float64 {
2017-05-02 01:51:23 -04:00
fi := float64(index)
2017-05-02 01:33:49 -04:00
if lg.start < lg.end {
2017-05-02 01:51:23 -04:00
return lg.start + (fi * lg.step)
2017-05-02 01:33:49 -04:00
}
2017-05-02 01:51:23 -04:00
return lg.start - (fi * lg.step)
2017-04-30 00:12:39 -04:00
}
2017-05-02 01:33:49 -04:00
// WithStart sets the start and returns the linear generator.
func (lg *Linear) WithStart(start float64) *Linear {
lg.start = start
2017-04-30 00:12:39 -04:00
return lg
}
2017-05-02 01:33:49 -04:00
// WithEnd sets the end and returns the linear generator.
func (lg *Linear) WithEnd(end float64) *Linear {
lg.end = end
2017-04-30 00:12:39 -04:00
return lg
}
// WithStep sets the step and returns the linear generator.
func (lg *Linear) WithStep(step float64) *Linear {
lg.step = step
return lg
}