2016-07-17 02:03:01 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
chart "github.com/wcharczuk/go-chart"
|
2016-07-17 02:03:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
2018-09-05 11:43:52 -04:00
|
|
|
Note: chart.TimeSeries implements `ValueFormatterProvider` and as a result gives the XAxis the appropriate formatter to use for the ticks.
|
2016-07-17 02:03:01 -04:00
|
|
|
*/
|
|
|
|
graph := chart.Chart{
|
|
|
|
XAxis: chart.XAxis{
|
2018-10-11 20:20:44 -04:00
|
|
|
Style: chart.StyleShow(),
|
2016-07-17 02:03:01 -04:00
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-07-17 02:45:28 -04:00
|
|
|
func drawCustomChart(res http.ResponseWriter, req *http.Request) {
|
|
|
|
/*
|
|
|
|
This is basically the other timeseries example, except we switch to hour intervals and specify a different formatter from default for the xaxis tick labels.
|
|
|
|
*/
|
|
|
|
graph := chart.Chart{
|
|
|
|
XAxis: chart.XAxis{
|
2018-10-11 20:20:44 -04:00
|
|
|
Style: chart.StyleShow(),
|
2016-07-17 02:45:28 -04:00
|
|
|
ValueFormatter: chart.TimeHourValueFormatter,
|
|
|
|
},
|
|
|
|
Series: []chart.Series{
|
|
|
|
chart.TimeSeries{
|
|
|
|
XValues: []time.Time{
|
|
|
|
time.Now().Add(-10 * time.Hour),
|
|
|
|
time.Now().Add(-9 * time.Hour),
|
|
|
|
time.Now().Add(-8 * time.Hour),
|
|
|
|
time.Now().Add(-7 * time.Hour),
|
|
|
|
time.Now().Add(-6 * time.Hour),
|
|
|
|
time.Now().Add(-5 * time.Hour),
|
|
|
|
time.Now().Add(-4 * time.Hour),
|
|
|
|
time.Now().Add(-3 * time.Hour),
|
|
|
|
time.Now().Add(-2 * time.Hour),
|
|
|
|
time.Now().Add(-1 * time.Hour),
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-07-17 02:03:01 -04:00
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/", drawChart)
|
2018-09-05 11:43:35 -04:00
|
|
|
http.HandleFunc("/favicon.ico", func(res http.ResponseWriter, req *http.Request) {
|
2016-07-17 02:45:28 -04:00
|
|
|
res.Write([]byte{})
|
|
|
|
})
|
|
|
|
http.HandleFunc("/custom", drawCustomChart)
|
2016-07-17 02:03:01 -04:00
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|