go-chart/min_max_series.go

139 lines
3 KiB
Go
Raw Normal View History

2016-08-06 18:53:14 -04:00
package chart
2017-02-03 14:26:53 -05:00
import (
"fmt"
"math"
)
2016-08-06 18:53:14 -04:00
// MinSeries draws a horizontal line at the minimum value of the inner series.
type MinSeries struct {
Name string
Style Style
YAxis YAxisType
2017-04-28 19:07:36 -04:00
InnerSeries ValuesProvider
2016-08-06 18:53:14 -04:00
minValue *float64
}
// GetName returns the name of the time series.
func (ms MinSeries) GetName() string {
return ms.Name
}
// GetStyle returns the line style.
func (ms MinSeries) GetStyle() Style {
return ms.Style
}
// GetYAxis returns which YAxis the series draws on.
func (ms MinSeries) GetYAxis() YAxisType {
return ms.YAxis
}
// Len returns the number of elements in the series.
func (ms MinSeries) Len() int {
return ms.InnerSeries.Len()
}
2017-04-28 19:07:36 -04:00
// GetValues gets a value at a given index.
func (ms *MinSeries) GetValues(index int) (x, y float64) {
2016-08-06 18:53:14 -04:00
ms.ensureMinValue()
2017-04-28 19:07:36 -04:00
x, _ = ms.InnerSeries.GetValues(index)
2016-08-06 18:53:14 -04:00
y = *ms.minValue
return
}
// Render renders the series.
func (ms *MinSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
style := ms.Style.InheritFrom(defaults)
Draw.LineSeries(r, canvasBox, xrange, yrange, style, ms)
}
func (ms *MinSeries) ensureMinValue() {
if ms.minValue == nil {
minValue := math.MaxFloat64
var y float64
for x := 0; x < ms.InnerSeries.Len(); x++ {
2017-04-28 19:07:36 -04:00
_, y = ms.InnerSeries.GetValues(x)
2016-08-06 18:53:14 -04:00
if y < minValue {
minValue = y
}
}
ms.minValue = &minValue
}
}
2017-02-03 14:26:53 -05:00
// Validate validates the series.
func (ms *MinSeries) Validate() error {
if ms.InnerSeries == nil {
return fmt.Errorf("min series requires InnerSeries to be set")
}
return nil
}
2016-08-06 18:53:14 -04:00
// MaxSeries draws a horizontal line at the maximum value of the inner series.
type MaxSeries struct {
Name string
Style Style
YAxis YAxisType
2017-04-28 19:07:36 -04:00
InnerSeries ValuesProvider
2016-08-06 18:53:14 -04:00
maxValue *float64
}
// GetName returns the name of the time series.
func (ms MaxSeries) GetName() string {
return ms.Name
}
// GetStyle returns the line style.
func (ms MaxSeries) GetStyle() Style {
return ms.Style
}
// GetYAxis returns which YAxis the series draws on.
func (ms MaxSeries) GetYAxis() YAxisType {
return ms.YAxis
}
// Len returns the number of elements in the series.
func (ms MaxSeries) Len() int {
return ms.InnerSeries.Len()
}
2017-04-28 19:07:36 -04:00
// GetValues gets a value at a given index.
func (ms *MaxSeries) GetValues(index int) (x, y float64) {
2016-08-06 18:53:14 -04:00
ms.ensureMaxValue()
2017-04-28 19:07:36 -04:00
x, _ = ms.InnerSeries.GetValues(index)
2016-08-06 18:53:14 -04:00
y = *ms.maxValue
return
}
// Render renders the series.
func (ms *MaxSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
style := ms.Style.InheritFrom(defaults)
Draw.LineSeries(r, canvasBox, xrange, yrange, style, ms)
}
func (ms *MaxSeries) ensureMaxValue() {
if ms.maxValue == nil {
maxValue := -math.MaxFloat64
var y float64
for x := 0; x < ms.InnerSeries.Len(); x++ {
2017-04-28 19:07:36 -04:00
_, y = ms.InnerSeries.GetValues(x)
2016-08-06 18:53:14 -04:00
if y > maxValue {
maxValue = y
}
}
ms.maxValue = &maxValue
}
}
2017-02-03 14:26:53 -05:00
// Validate validates the series.
func (ms *MaxSeries) Validate() error {
if ms.InnerSeries == nil {
return fmt.Errorf("max series requires InnerSeries to be set")
}
return nil
}