fixing examples

This commit is contained in:
Will Charczuk 2017-05-12 17:06:45 -07:00
parent 4d93883953
commit 50b025e273
32 changed files with 87 additions and 67 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -20,12 +20,18 @@ func drawLargeChart(res http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
numSeriesInt64 = int64(1) numSeriesInt64 = int64(1)
} }
if numSeriesInt64 == 0 {
numSeriesInt64 = 1
}
numSeries := int(numSeriesInt64) numSeries := int(numSeriesInt64)
numValuesInt64, err := strconv.ParseInt(r.FormValue("values"), 10, 64) numValuesInt64, err := strconv.ParseInt(r.FormValue("values"), 10, 64)
if err != nil { if err != nil {
numValuesInt64 = int64(100) numValuesInt64 = int64(100)
} }
if numValuesInt64 == 0 {
numValuesInt64 = int64(100)
}
numValues := int(numValuesInt64) numValues := int(numValuesInt64)
series := make([]chart.Series, numSeries) series := make([]chart.Series, numSeries)

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

View File

@ -32,7 +32,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
Series: []chart.Series{ Series: []chart.Series{
chart.ContinuousSeries{ chart.ContinuousSeries{
XValues: seq.Range(1.0, 100.0), XValues: seq.Range(1.0, 100.0),
YValues: seq.New(seq.NewRandom().WithLen(100).WithAverage(256)).Array(), YValues: seq.RandomValuesWithMax(100, 512),
}, },
}, },
} }
@ -59,7 +59,7 @@ func drawChartDefault(res http.ResponseWriter, req *http.Request) {
Series: []chart.Series{ Series: []chart.Series{
chart.ContinuousSeries{ chart.ContinuousSeries{
XValues: seq.Range(1.0, 100.0), XValues: seq.Range(1.0, 100.0),
YValues: seq.New(seq.NewRandom().WithLen(100).WithAverage(256)).Array(), YValues: seq.RandomValuesWithMax(100, 512),
}, },
}, },
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 67 KiB

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 24 KiB

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/seq"
) )
func drawChart(res http.ResponseWriter, req *http.Request) { func drawChart(res http.ResponseWriter, req *http.Request) {
@ -15,8 +16,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
mainSeries := chart.ContinuousSeries{ mainSeries := chart.ContinuousSeries{
Name: "A test series", Name: "A test series",
XValues: chart.Sequence.Float64(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements. XValues: seq.Range(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements.
YValues: chart.Sequence.Random(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements. YValues: seq.RandomValuesWithAverage(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements.
} }
// note we create a LinearRegressionSeries series by assignin the inner series. // note we create a LinearRegressionSeries series by assignin the inner series.

View File

@ -4,13 +4,15 @@ import (
"net/http" "net/http"
"github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/seq"
"github.com/wcharczuk/go-chart/util"
) )
func drawChart(res http.ResponseWriter, req *http.Request) { func drawChart(res http.ResponseWriter, req *http.Request) {
start := chart.Date.Date(2016, 7, 01, chart.Date.Eastern()) start := util.Date.Date(2016, 7, 01, util.Date.Eastern())
end := chart.Date.Date(2016, 07, 21, chart.Date.Eastern()) end := util.Date.Date(2016, 07, 21, util.Date.Eastern())
xv := chart.Sequence.MarketHours(start, end, chart.NYSEOpen, chart.NYSEClose, chart.Date.IsNYSEHoliday) xv := seq.Time.MarketHours(start, end, util.NYSEOpen(), util.NYSEClose(), util.Date.IsNYSEHoliday)
yv := chart.Sequence.RandomWithAverage(len(xv), 200, 10) yv := seq.New(seq.NewRandom().WithLen(len(xv)).WithAverage(200).WithScale(10)).Array()
graph := chart.Chart{ graph := chart.Chart{
XAxis: chart.XAxis{ XAxis: chart.XAxis{
@ -18,9 +20,9 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
TickPosition: chart.TickPositionBetweenTicks, TickPosition: chart.TickPositionBetweenTicks,
ValueFormatter: chart.TimeHourValueFormatter, ValueFormatter: chart.TimeHourValueFormatter,
Range: &chart.MarketHoursRange{ Range: &chart.MarketHoursRange{
MarketOpen: chart.NYSEOpen, MarketOpen: util.NYSEOpen(),
MarketClose: chart.NYSEClose, MarketClose: util.NYSEClose(),
HolidayProvider: chart.Date.IsNYSEHoliday, HolidayProvider: util.Date.IsNYSEHoliday,
}, },
}, },
YAxis: chart.YAxis{ YAxis: chart.YAxis{

View File

@ -4,13 +4,14 @@ import (
"net/http" "net/http"
"github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/seq"
) )
func drawChart(res http.ResponseWriter, req *http.Request) { func drawChart(res http.ResponseWriter, req *http.Request) {
mainSeries := chart.ContinuousSeries{ mainSeries := chart.ContinuousSeries{
Name: "A test series", Name: "A test series",
XValues: chart.Sequence.Float64(1.0, 100.0), XValues: seq.Range(1.0, 100.0),
YValues: chart.Sequence.RandomWithAverage(100, 100, 50), YValues: seq.New(seq.NewRandom().WithLen(100).WithAverage(100).WithScale(50)).Array(),
} }
minSeries := &chart.MinSeries{ minSeries := &chart.MinSeries{

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 34 KiB

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/seq"
) )
func drawChart(res http.ResponseWriter, req *http.Request) { func drawChart(res http.ResponseWriter, req *http.Request) {
@ -15,8 +16,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
mainSeries := chart.ContinuousSeries{ mainSeries := chart.ContinuousSeries{
Name: "A test series", Name: "A test series",
XValues: chart.Sequence.Float64(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements. XValues: seq.Range(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements.
YValues: chart.Sequence.Random(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements. YValues: seq.RandomValuesWithAverage(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements.
} }
polyRegSeries := &chart.PolynomialRegressionSeries{ polyRegSeries := &chart.PolynomialRegressionSeries{

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

View File

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart"
util "github.com/wcharczuk/go-chart/util"
) )
func parseInt(str string) int { func parseInt(str string) int {
@ -23,7 +24,7 @@ func parseFloat64(str string) float64 {
func readData() ([]time.Time, []float64) { func readData() ([]time.Time, []float64) {
var xvalues []time.Time var xvalues []time.Time
var yvalues []float64 var yvalues []float64
err := chart.File.ReadByLines("requests.csv", func(line string) { err := util.File.ReadByLines("requests.csv", func(line string) error {
parts := strings.Split(line, ",") parts := strings.Split(line, ",")
year := parseInt(parts[0]) year := parseInt(parts[0])
month := parseInt(parts[1]) month := parseInt(parts[1])
@ -32,6 +33,7 @@ func readData() ([]time.Time, []float64) {
elapsedMillis := parseFloat64(parts[4]) elapsedMillis := parseFloat64(parts[4])
xvalues = append(xvalues, time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.UTC)) xvalues = append(xvalues, time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.UTC))
yvalues = append(yvalues, elapsedMillis) yvalues = append(yvalues, elapsedMillis)
return nil
}) })
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
@ -41,12 +43,12 @@ func readData() ([]time.Time, []float64) {
func releases() []chart.GridLine { func releases() []chart.GridLine {
return []chart.GridLine{ return []chart.GridLine{
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 1, 9, 30, 0, 0, time.UTC))}, {Value: util.Time.ToFloat64(time.Date(2016, 8, 1, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 2, 9, 30, 0, 0, time.UTC))}, {Value: util.Time.ToFloat64(time.Date(2016, 8, 2, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 2, 15, 30, 0, 0, time.UTC))}, {Value: util.Time.ToFloat64(time.Date(2016, 8, 2, 15, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 4, 9, 30, 0, 0, time.UTC))}, {Value: util.Time.ToFloat64(time.Date(2016, 8, 4, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 5, 9, 30, 0, 0, time.UTC))}, {Value: util.Time.ToFloat64(time.Date(2016, 8, 5, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 6, 9, 30, 0, 0, time.UTC))}, {Value: util.Time.ToFloat64(time.Date(2016, 8, 6, 9, 30, 0, 0, time.UTC))},
} }
} }
@ -125,8 +127,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
graph.Elements = []chart.Renderable{chart.LegendThin(&graph)} graph.Elements = []chart.Renderable{chart.LegendThin(&graph)}
res.Header().Set("Content-Type", chart.ContentTypeSVG) res.Header().Set("Content-Type", chart.ContentTypePNG)
graph.Render(chart.SVG, res) graph.Render(chart.PNG, res)
} }
func main() { func main() {

View File

@ -8,6 +8,7 @@ import (
"github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing" "github.com/wcharczuk/go-chart/drawing"
"github.com/wcharczuk/go-chart/seq"
) )
func drawChart(res http.ResponseWriter, req *http.Request) { func drawChart(res http.ResponseWriter, req *http.Request) {
@ -25,8 +26,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
DotWidth: 5, DotWidth: 5,
DotColorProvider: viridisByY, DotColorProvider: viridisByY,
}, },
XValues: chart.Sequence.Random(128, 1024), XValues: seq.Range(0, 127),
YValues: chart.Sequence.Random(128, 1024), YValues: seq.New(seq.NewRandom().WithLen(128).WithMax(1024)).Array(),
}, },
}, },
} }
@ -50,8 +51,8 @@ func unit(res http.ResponseWriter, req *http.Request) {
}, },
Series: []chart.Series{ Series: []chart.Series{
chart.ContinuousSeries{ chart.ContinuousSeries{
XValues: chart.Sequence.Float64(0, 4, 1), XValues: seq.RangeWithStep(0, 4, 1),
YValues: chart.Sequence.Float64(0, 4, 1), YValues: seq.RangeWithStep(0, 4, 1),
}, },
}, },
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View File

@ -4,19 +4,15 @@ import (
"net/http" "net/http"
"github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/seq"
) )
func drawChart(res http.ResponseWriter, req *http.Request) { func drawChart(res http.ResponseWriter, req *http.Request) {
/*
In this example we add a new type of series, a `SimpleMovingAverageSeries` that takes another series as a required argument.
InnerSeries only needs to implement `ValuesProvider`, so really you could chain `SimpleMovingAverageSeries` together if you wanted.
*/
mainSeries := chart.ContinuousSeries{ mainSeries := chart.ContinuousSeries{
Name: "A test series", Name: "A test series",
XValues: chart.Sequence.Float64(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements. XValues: seq.Range(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements.
YValues: chart.Sequence.Random(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements. YValues: seq.RandomValuesWithMax(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements.
} }
// note we create a SimpleMovingAverage series by assignin the inner series. // note we create a SimpleMovingAverage series by assignin the inner series.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

After

Width:  |  Height:  |  Size: 36 KiB

View File

@ -4,8 +4,8 @@ import (
"bytes" "bytes"
"log" "log"
"os" "os"
//"time"
"github.com/wcharczuk/go-chart" //exposes "chart" "github.com/wcharczuk/go-chart"
) )
func main() { func main() {
@ -18,8 +18,6 @@ func main() {
Style: chart.Style{ Style: chart.Style{
Show: true, Show: true,
}, },
//XValues: []time.Time{time.Unix(3*b,0),time.Unix(4*b,0),time.Unix(5*b,0),time.Unix(6*b,0),time.Unix(7*b,0),time.Unix(8*b,0),time.Unix(9*b,0),time.Unix(10*b,0)},
XValues: []float64{10 * b, 20 * b, 30 * b, 40 * b, 50 * b, 60 * b, 70 * b, 80 * b}, XValues: []float64{10 * b, 20 * b, 30 * b, 40 * b, 50 * b, 60 * b, 70 * b, 80 * b},
YValues: []float64{1.0, 2.0, 30.0, 4.0, 50.0, 6.0, 7.0, 88.0}, YValues: []float64{1.0, 2.0, 30.0, 4.0, 50.0, 6.0, 7.0, 88.0},
} }

View File

@ -14,7 +14,7 @@ func TestBollingerBandSeries(t *testing.T) {
s1 := mockValuesProvider{ s1 := mockValuesProvider{
X: seq.Range(1.0, 100.0), X: seq.Range(1.0, 100.0),
Y: seq.RandomValuesWithAverage(1024, 100), Y: seq.RandomValuesWithMax(100, 1024),
} }
bbs := &BollingerBandsSeries{ bbs := &BollingerBandsSeries{

View File

@ -12,8 +12,8 @@ func RandomValues(count int) []float64 {
} }
// RandomValuesWithAverage returns an array of random values with a given average. // RandomValuesWithAverage returns an array of random values with a given average.
func RandomValuesWithAverage(average float64, count int) []float64 { func RandomValuesWithMax(count int, max float64) []float64 {
return Seq{NewRandom().WithAverage(average).WithLen(count)}.Array() return Seq{NewRandom().WithMax(max).WithLen(count)}.Array()
} }
// NewRandom creates a new random seq. // NewRandom creates a new random seq.
@ -25,10 +25,10 @@ func NewRandom() *Random {
// Random is a random number seq generator. // Random is a random number seq generator.
type Random struct { type Random struct {
rnd *rand.Rand rnd *rand.Rand
scale *float64 max *float64
average *float64 min *float64
len *int len *int
} }
// Len returns the number of elements that will be generated. // Len returns the number of elements that will be generated.
@ -41,10 +41,20 @@ func (r *Random) Len() int {
// GetValue returns the value. // GetValue returns the value.
func (r *Random) GetValue(_ int) float64 { func (r *Random) GetValue(_ int) float64 {
if r.average != nil && r.scale != nil { if r.min != nil && r.max != nil {
return *r.average + *r.scale - (r.rnd.Float64() * (2 * *r.scale)) var delta float64
} else if r.scale != nil {
return r.rnd.Float64() * *r.scale if *r.max > *r.min {
delta = *r.max - *r.min
} else {
delta = *r.min - *r.max
}
return *r.min + (r.rnd.Float64() * delta)
} else if r.max != nil {
return r.rnd.Float64() * *r.max
} else if r.min != nil {
return *r.min + (r.rnd.Float64())
} }
return r.rnd.Float64() return r.rnd.Float64()
} }
@ -55,24 +65,24 @@ func (r *Random) WithLen(length int) *Random {
return r return r
} }
// Scale returns the scale. // Min returns the minimum value.
func (r Random) Scale() *float64 { func (r Random) Min() *float64 {
return r.scale return r.min
} }
// WithScale sets the scale and returns the Random. // WithMin sets the scale and returns the Random.
func (r *Random) WithScale(scale float64) *Random { func (r *Random) WithMin(min float64) *Random {
r.scale = &scale r.min = &min
return r return r
} }
// Average returns the average. // Max returns the maximum value.
func (r Random) Average() *float64 { func (r Random) Max() *float64 {
return r.average return r.max
} }
// WithAverage sets the average and returns the Random. // WithMax sets the average and returns the Random.
func (r *Random) WithAverage(average float64) *Random { func (r *Random) WithMax(max float64) *Random {
r.average = &average r.max = &max
return r return r
} }

View File

@ -9,10 +9,12 @@ import (
func TestRandomRegression(t *testing.T) { func TestRandomRegression(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
randomProvider := NewRandom().WithLen(100).WithAverage(256) randomProvider := NewRandom().WithLen(4096).WithMax(256)
assert.Equal(100, randomProvider.Len()) assert.Equal(4096, randomProvider.Len())
assert.Equal(256, *randomProvider.Average()) assert.Equal(256, *randomProvider.Max())
randomValues := New(randomProvider).Array() randomSequence := New(randomProvider)
assert.Len(randomValues, 100) randomValues := randomSequence.Array()
assert.Len(randomValues, 4096)
assert.InDelta(128, randomSequence.Average(), 10.0)
} }