diff --git a/examples/timeseries/main.go b/examples/timeseries/main.go new file mode 100644 index 0000000..aade79a --- /dev/null +++ b/examples/timeseries/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "net/http" + "time" + + "github.com/wcharczuk/go-chart" +) + +func drawChart(res http.ResponseWriter, req *http.Request) { + /* + This is an example of using the `TimeSeries` to automatically coerce time.Time values into a continuous xrange. + Note: chart.TimeSeries implements `ValueFormatterProvider` and as a result gives the XAxis the appropariate formatter to use for the ticks. + */ + graph := chart.Chart{ + XAxis: chart.XAxis{ + Style: chart.Style{ + Show: true, + }, + }, + Series: []chart.Series{ + chart.TimeSeries{ + XValues: []time.Time{ + time.Now().AddDate(0, 0, -10), + time.Now().AddDate(0, 0, -9), + time.Now().AddDate(0, 0, -8), + time.Now().AddDate(0, 0, -7), + time.Now().AddDate(0, 0, -6), + time.Now().AddDate(0, 0, -5), + time.Now().AddDate(0, 0, -4), + time.Now().AddDate(0, 0, -3), + time.Now().AddDate(0, 0, -2), + time.Now().AddDate(0, 0, -1), + time.Now(), + }, + YValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0}, + }, + }, + } + + res.Header().Set("Content-Type", "image/png") + graph.Render(chart.PNG, res) +} + +func main() { + http.HandleFunc("/", drawChart) + http.ListenAndServe(":8080", nil) +} diff --git a/util.go b/util.go index 1e4e6ca..8dbc0d8 100644 --- a/util.go +++ b/util.go @@ -156,6 +156,15 @@ func SeqRand(samples int, scale float64) []float64 { return values } +// SeqDays generates a sequence of timestamps by day, from -days to today. +func SeqDays(days int) []time.Time { + var values []time.Time + for day := days; day >= 0; day-- { + values = append(values, time.Now().AddDate(0, 0, -day)) + } + return values +} + // PercentDifference computes the percentage difference between two values. // The formula is (v2-v1)/v1. func PercentDifference(v1, v2 float64) float64 {