finalizing work on text rotation.

This commit is contained in:
Will Charczuk 2016-10-20 15:21:52 -07:00
parent 8595962d99
commit 53280b9258
10 changed files with 56 additions and 28 deletions

18
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "test",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceRoot}",
"env": {},
"args": [],
"showLog": true
}
]
}

View File

@ -14,11 +14,11 @@ func readData() ([]time.Time, []float64) {
var yvalues []float64
util.ReadFileByLines("requests.csv", func(line string) {
parts := strings.Split(line, ",")
year := util.ParseInt(parts[0])
month := util.ParseInt(parts[1])
day := util.ParseInt(parts[2])
hour := util.ParseInt(parts[3])
elapsedMillis := util.ParseFloat64(parts[4])
year := util.String.ParseInt(parts[0])
month := util.String.ParseInt(parts[1])
day := util.String.ParseInt(parts[2])
hour := util.String.ParseInt(parts[3])
elapsedMillis := util.String.ParseFloat64(parts[4])
xvalues = append(xvalues, time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.UTC))
yvalues = append(yvalues, elapsedMillis)
})

BIN
go-chart.test Executable file

Binary file not shown.

View File

@ -33,12 +33,12 @@ func (mhr MarketHoursRange) IsZero() bool {
// GetMin returns the min value.
func (mhr MarketHoursRange) GetMin() float64 {
return TimeToFloat64(mhr.Min)
return Time.ToFloat64(mhr.Min)
}
// GetMax returns the max value.
func (mhr MarketHoursRange) GetMax() float64 {
return TimeToFloat64(mhr.GetEffectiveMax())
return Time.ToFloat64(mhr.GetEffectiveMax())
}
// GetEffectiveMax gets either the close on the max, or the max itself.
@ -52,13 +52,13 @@ func (mhr MarketHoursRange) GetEffectiveMax() time.Time {
// SetMin sets the min value.
func (mhr *MarketHoursRange) SetMin(min float64) {
mhr.Min = Float64ToTime(min)
mhr.Min = Time.FromFloat64(min)
mhr.Min = mhr.Min.In(mhr.GetTimezone())
}
// SetMax sets the max value.
func (mhr *MarketHoursRange) SetMax(max float64) {
mhr.Max = Float64ToTime(max)
mhr.Max = Time.FromFloat64(max)
mhr.Max = mhr.Max.In(mhr.GetTimezone())
}
@ -159,7 +159,7 @@ func (mhr *MarketHoursRange) makeTicks(vf ValueFormatter, times []time.Time) []T
ticks := make([]Tick, len(times))
for index, t := range times {
ticks[index] = Tick{
Value: TimeToFloat64(t),
Value: Time.ToFloat64(t),
Label: vf(t),
}
}
@ -172,7 +172,7 @@ func (mhr MarketHoursRange) String() string {
// Translate maps a given value into the ContinuousRange space.
func (mhr MarketHoursRange) Translate(value float64) int {
valueTime := Float64ToTime(value)
valueTime := Time.FromFloat64(value)
valueTimeEastern := valueTime.In(Date.Eastern())
totalSeconds := Date.CalculateMarketSecondsBetween(mhr.Min, mhr.GetEffectiveMax(), mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.HolidayProvider)
valueDelta := Date.CalculateMarketSecondsBetween(mhr.Min, valueTimeEastern, mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.HolidayProvider)

View File

@ -35,9 +35,9 @@ func TestMarketHoursRangeTranslate(t *testing.T) {
weds := time.Date(2016, 07, 20, 9, 30, 0, 0, Date.Eastern())
assert.Equal(0, r.Translate(TimeToFloat64(r.Min)))
assert.Equal(400, r.Translate(TimeToFloat64(weds)))
assert.Equal(1000, r.Translate(TimeToFloat64(r.Max)))
assert.Equal(0, r.Translate(Time.ToFloat64(r.Min)))
assert.Equal(400, r.Translate(Time.ToFloat64(weds)))
assert.Equal(1000, r.Translate(Time.ToFloat64(r.Max)))
}
func TestMarketHoursRangeGetTicks(t *testing.T) {
@ -67,6 +67,6 @@ func TestMarketHoursRangeGetTicks(t *testing.T) {
ticks := ra.GetTicks(r, defaults, TimeValueFormatter)
assert.NotEmpty(ticks)
assert.Len(ticks, 5)
assert.NotEqual(TimeToFloat64(ra.Min), ticks[0].Value)
assert.NotEqual(Time.ToFloat64(ra.Min), ticks[0].Value)
assert.NotEmpty(ticks[0].Label)
}

View File

@ -19,16 +19,6 @@ const (
_r2d = (180.0 / math.Pi)
)
// TimeToFloat64 returns a float64 representation of a time.
func TimeToFloat64(t time.Time) float64 {
return float64(t.UnixNano())
}
// Float64ToTime returns a time from a float64.
func Float64ToTime(tf float64) time.Time {
return time.Unix(0, int64(tf))
}
var (
// Math contains helper methods for common math operations.
Math = &mathUtil{}

View File

@ -30,14 +30,14 @@ func (ts TimeSeries) Len() int {
// GetValue gets a value at a given index.
func (ts TimeSeries) GetValue(index int) (x, y float64) {
x = TimeToFloat64(ts.XValues[index])
x = Time.ToFloat64(ts.XValues[index])
y = ts.YValues[index]
return
}
// GetLastValue gets the last value.
func (ts TimeSeries) GetLastValue() (x, y float64) {
x = TimeToFloat64(ts.XValues[len(ts.XValues)-1])
x = Time.ToFloat64(ts.XValues[len(ts.XValues)-1])
y = ts.YValues[len(ts.YValues)-1]
return
}

20
time_util.go Normal file
View File

@ -0,0 +1,20 @@
package chart
import "time"
var (
// Time contains time utility functions.
Time = timeUtil{}
)
type timeUtil struct{}
// TimeToFloat64 returns a float64 representation of a time.
func (tu timeUtil) ToFloat64(t time.Time) float64 {
return float64(t.UnixNano())
}
// Float64ToTime returns a time from a float64.
func (tu timeUtil) FromFloat64(tf float64) time.Time {
return time.Unix(0, int64(tf))
}

View File

@ -11,7 +11,7 @@ func TestTimeValueFormatterWithFormat(t *testing.T) {
assert := assert.New(t)
d := time.Now()
di := TimeToFloat64(d)
di := Time.ToFloat64(d)
df := float64(di)
s := TimeValueFormatterWithFormat(d, DefaultDateFormat)