This commit is contained in:
Will Charczuk 2019-02-13 19:13:29 -08:00
parent 5f42a580a9
commit fa93bd8abb
4 changed files with 84 additions and 14 deletions

View file

@ -12,10 +12,15 @@ import (
var (
outputPath = flag.String("output", "", "The output file")
inputFormat = flag.String("format", "csv", "The input format, either 'csv' or 'tsv' (defaults to 'csv')")
inputPath = flag.String("f", "", "The input file")
disableLinreg = flag.Bool("disable-linreg", false, "If we should omit linear regressions")
disableLastValues = flag.Bool("disable-last-values", false, "If we should omit last values")
reverse = flag.Bool("reverse", false, "If we should reverse the inputs")
hideLegend = flag.Bool("hide-legend", false, "If we should omit the chart legend")
hideSMA = flag.Bool("hide-sma", false, "If we should omit simple moving average")
hideLinreg = flag.Bool("hide-linreg", false, "If we should omit linear regressions")
hideLastValues = flag.Bool("hide-last-values", false, "If we should omit last values")
)
func main() {
@ -58,6 +63,10 @@ func main() {
log.FatalErr(err)
}
if *reverse {
yvalues = chart.ValueSequence(yvalues...).Reverse().Values()
}
var series []chart.Series
mainSeries := chart.ContinuousSeries{
Name: "Values",
@ -66,17 +75,57 @@ func main() {
}
series = append(series, mainSeries)
if !*disableLinreg {
smaSeries := &chart.SMASeries{
Name: "SMA",
Style: chart.Style{
Hidden: *hideSMA,
StrokeColor: chart.ColorRed,
StrokeDashArray: []float64{5.0, 5.0},
},
InnerSeries: mainSeries,
}
series = append(series, smaSeries)
linRegSeries := &chart.LinearRegressionSeries{
Name: "Values - Lin. Reg.",
Style: chart.Style{
Hidden: *hideLinreg,
},
InnerSeries: mainSeries,
}
series = append(series, linRegSeries)
mainLastValue := chart.LastValueAnnotationSeries(mainSeries)
mainLastValue.Style = chart.Style{
Hidden: *hideLastValues,
}
series = append(series, mainLastValue)
linregLastValue := chart.LastValueAnnotationSeries(linRegSeries)
linregLastValue.Style = chart.Style{
Hidden: (*hideLastValues || *hideLinreg),
}
series = append(series, linregLastValue)
smaLastValue := chart.LastValueAnnotationSeries(smaSeries)
smaLastValue.Style = chart.Style{
Hidden: (*hideLastValues || *hideSMA),
}
series = append(series, smaLastValue)
graph := chart.Chart{
Background: chart.Style{
Padding: chart.Box{
Top: 50,
},
},
Series: series,
}
if !*hideLegend {
graph.Elements = []chart.Renderable{chart.LegendThin(&graph)}
}
var output *os.File
if *outputPath != "" {
output, err = os.Create(*outputPath)

View file

@ -2,8 +2,8 @@ package chart
import "fmt"
// LastValueAnnotation returns an annotation series of just the last value of a value provider.
func LastValueAnnotation(innerSeries ValuesProvider, vfs ...ValueFormatter) AnnotationSeries {
// LastValueAnnotationSeries returns an annotation series of just the last value of a value provider.
func LastValueAnnotationSeries(innerSeries ValuesProvider, vfs ...ValueFormatter) AnnotationSeries {
var vf ValueFormatter
if len(vfs) > 0 {
vf = vfs[0]

View file

@ -6,7 +6,7 @@ import (
"github.com/blend/go-sdk/assert"
)
func TestLastValueAnnotation(t *testing.T) {
func TestLastValueAnnotationSeries(t *testing.T) {
assert := assert.New(t)
series := ContinuousSeries{
@ -14,7 +14,7 @@ func TestLastValueAnnotation(t *testing.T) {
YValues: []float64{5.0, 3.0, 3.0, 2.0, 1.0},
}
lva := LastValueAnnotation(series)
lva := LastValueAnnotationSeries(series)
assert.NotEmpty(lva.Annotations)
lvaa := lva.Annotations[0]
assert.Equal(5, lvaa.XValue)

21
seq.go
View file

@ -148,6 +148,27 @@ func (s Seq) Sort() Seq {
return Seq{Array(values)}
}
// Reverse reverses the sequence
func (s Seq) Reverse() Seq {
if s.Len() == 0 {
return s
}
values := s.Values()
valuesLen := len(values)
valuesLen1 := len(values) - 1
valuesLen2 := valuesLen >> 1
var i, j float64
for index := 0; index < valuesLen2; index++ {
i = values[index]
j = values[valuesLen1-index]
values[index] = j
values[valuesLen1-index] = i
}
return Seq{Array(values)}
}
// Median returns the median or middle value in the sorted seq.
func (s Seq) Median() (median float64) {
l := s.Len()