Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions axis.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,37 +462,52 @@ func (ts ConstantTicks) Ticks(float64, float64) []Tick {
return ts
}

// UnixTimeTicks is suitable for axes representing time values.
// UnixTimeTicks expects values in Unix time seconds.
type UnixTimeTicks struct {
// UnixTimeIn returns a time conversion function for the given location.
func UnixTimeIn(loc *time.Location) func(t float64) time.Time {
return func(t float64) time.Time {
return time.Unix(int64(t), 0).In(loc)
}
}

// UTCUnixTime is the default time conversion for TimeTicks.
var UTCUnixTime = UnixTimeIn(time.UTC)

// TimeTicks is suitable for axes representing time values.
type TimeTicks struct {
// Ticker is used to generate a set of ticks.
// If nil, DefaultTicks will be used.
Ticker Ticker

// Format is the textual representation of the time value.
// If empty, time.RFC3339 will be used
Format string

// Time takes a float64 value and converts it into a time.Time.
// If nil, UTCUnixTime is used.
Time func(t float64) time.Time
}

var _ Ticker = UnixTimeTicks{}
var _ Ticker = TimeTicks{}

// Ticks implements plot.Ticker.
func (utt UnixTimeTicks) Ticks(min, max float64) []Tick {
if utt.Ticker == nil {
utt.Ticker = DefaultTicks{}
func (t TimeTicks) Ticks(min, max float64) []Tick {
if t.Ticker == nil {
t.Ticker = DefaultTicks{}
}
if t.Format == "" {
t.Format = time.RFC3339
}
if utt.Format == "" {
utt.Format = time.RFC3339
if t.Time == nil {
t.Time = UTCUnixTime
}

ticks := utt.Ticker.Ticks(min, max)
ticks := t.Ticker.Ticks(min, max)
for i := range ticks {
tick := &ticks[i]
if tick.Label == "" {
continue
}
t := time.Unix(int64(tick.Value), 0)
tick.Label = t.Format(utt.Format)
tick.Label = t.Time(tick.Value).Format(t.Format)
}
return ticks
}
Expand Down
Binary file modified plotter/testdata/timeseries_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion plotter/timeseries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
func Example_timeSeries() {
rnd := rand.New(rand.NewSource(1))

// xticks defines how we convert and display time.Time values.
xticks := plot.TimeTicks{Format: "2006-01-02\n15:04"}

// randomPoints returns some random x, y points
// with some interesting kind of trend.
randomPoints := func(n int) XYs {
Expand Down Expand Up @@ -48,7 +51,7 @@ func Example_timeSeries() {
log.Panic(err)
}
p.Title.Text = "Time Series"
p.X.Tick.Marker = plot.UnixTimeTicks{Format: "2006-01-02"}
p.X.Tick.Marker = xticks
p.Y.Label.Text = "Number of Gophers\n(Billions)"
p.Add(NewGrid())

Expand Down