diff --git a/_examples/market_hours/main.go b/_examples/market_hours/main.go deleted file mode 100644 index d0be1d3..0000000 --- a/_examples/market_hours/main.go +++ /dev/null @@ -1,46 +0,0 @@ -package main - -import ( - "net/http" - - "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) { - start := util.Date.Date(2016, 7, 01, util.Date.Eastern()) - end := util.Date.Date(2016, 07, 21, util.Date.Eastern()) - xv := seq.Time.MarketHours(start, end, util.NYSEOpen(), util.NYSEClose(), util.Date.IsNYSEHoliday) - yv := seq.New(seq.NewRandom().WithLen(len(xv)).WithAverage(200).WithScale(10)).Array() - - graph := chart.Chart{ - XAxis: chart.XAxis{ - Style: chart.StyleShow(), - TickPosition: chart.TickPositionBetweenTicks, - ValueFormatter: chart.TimeHourValueFormatter, - Range: &chart.MarketHoursRange{ - MarketOpen: util.NYSEOpen(), - MarketClose: util.NYSEClose(), - HolidayProvider: util.Date.IsNYSEHoliday, - }, - }, - YAxis: chart.YAxis{ - Style: chart.StyleShow(), - }, - Series: []chart.Series{ - chart.TimeSeries{ - XValues: xv, - YValues: yv, - }, - }, - } - - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) -} diff --git a/_examples/market_hours/output.png b/_examples/market_hours/output.png deleted file mode 100644 index 82610ef..0000000 Binary files a/_examples/market_hours/output.png and /dev/null differ diff --git a/util/math.go b/util/math.go index 380a7f6..c064809 100644 --- a/util/math.go +++ b/util/math.go @@ -2,7 +2,6 @@ package util import ( "math" - "time" ) const ( @@ -58,27 +57,6 @@ func (m mathUtil) MinAndMax(values ...float64) (min float64, max float64) { return } -// MinAndMaxOfTime returns the min and max of a given set of times -// in one pass. -func (m mathUtil) MinAndMaxOfTime(values ...time.Time) (min time.Time, max time.Time) { - if len(values) == 0 { - return - } - - min = values[0] - max = values[0] - - for _, v := range values[1:] { - if max.Before(v) { - max = v - } - if min.After(v) { - min = v - } - } - return -} - // GetRoundToForDelta returns a `roundTo` value for a given delta. func (m mathUtil) GetRoundToForDelta(delta float64) float64 { startingDeltaBound := math.Pow(10.0, 10.0) diff --git a/util/math_test.go b/util/math_test.go index c290b12..12a450a 100644 --- a/util/math_test.go +++ b/util/math_test.go @@ -2,7 +2,6 @@ package util import ( "testing" - "time" "github.com/blend/go-sdk/assert" ) @@ -31,40 +30,6 @@ func TestMinAndMaxEmpty(t *testing.T) { assert.Equal(0.0, max) } -func TestMinAndMaxOfTime(t *testing.T) { - assert := assert.New(t) - values := []time.Time{ - time.Now().AddDate(0, 0, -1), - time.Now().AddDate(0, 0, -2), - time.Now().AddDate(0, 0, -3), - time.Now().AddDate(0, 0, -4), - } - min, max := Math.MinAndMaxOfTime(values...) - assert.Equal(values[3], min) - assert.Equal(values[0], max) -} - -func TestMinAndMaxOfTimeReversed(t *testing.T) { - assert := assert.New(t) - values := []time.Time{ - time.Now().AddDate(0, 0, -4), - time.Now().AddDate(0, 0, -2), - time.Now().AddDate(0, 0, -3), - time.Now().AddDate(0, 0, -1), - } - min, max := Math.MinAndMaxOfTime(values...) - assert.Equal(values[0], min) - assert.Equal(values[3], max) -} - -func TestMinAndMaxOfTimeEmpty(t *testing.T) { - assert := assert.New(t) - values := []time.Time{} - min, max := Math.MinAndMaxOfTime(values...) - assert.Equal(time.Time{}, min) - assert.Equal(time.Time{}, max) -} - func TestGetRoundToForDelta(t *testing.T) { assert := assert.New(t) diff --git a/util/time_test.go b/util/time_test.go index c4f9668..beeec65 100644 --- a/util/time_test.go +++ b/util/time_test.go @@ -28,3 +28,37 @@ func TestTimeDiffHours(t *testing.T) { assert.Equal(68, Time.DiffHours(t2, t1)) assert.Equal(24, Time.DiffHours(t1, t3)) } + +func TestTimeStartAndEnd(t *testing.T) { + assert := assert.New(t) + values := []time.Time{ + time.Now().AddDate(0, 0, -1), + time.Now().AddDate(0, 0, -2), + time.Now().AddDate(0, 0, -3), + time.Now().AddDate(0, 0, -4), + } + min, max := Time.StartAndEnd(values...) + assert.Equal(values[3], min) + assert.Equal(values[0], max) +} + +func TestTimeStartAndEndReversed(t *testing.T) { + assert := assert.New(t) + values := []time.Time{ + time.Now().AddDate(0, 0, -4), + time.Now().AddDate(0, 0, -2), + time.Now().AddDate(0, 0, -3), + time.Now().AddDate(0, 0, -1), + } + min, max := Time.StartAndEnd(values...) + assert.Equal(values[0], min) + assert.Equal(values[3], max) +} + +func TestTimeStartAndEndEmpty(t *testing.T) { + assert := assert.New(t) + values := []time.Time{} + min, max := Time.StartAndEnd(values...) + assert.Equal(time.Time{}, min) + assert.Equal(time.Time{}, max) +}