| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"time" |
| 6 |
|
| 7 |
chart "github.com/wcharczuk/go-chart/v2" |
| 8 |
) |
| 9 |
|
| 10 |
func drawChart(res http.ResponseWriter, req *http.Request) { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
graph := chart.Chart{ |
| 16 |
Series: []chart.Series{ |
| 17 |
chart.TimeSeries{ |
| 18 |
XValues: []time.Time{ |
| 19 |
time.Now().AddDate(0, 0, -10), |
| 20 |
time.Now().AddDate(0, 0, -9), |
| 21 |
time.Now().AddDate(0, 0, -8), |
| 22 |
time.Now().AddDate(0, 0, -7), |
| 23 |
time.Now().AddDate(0, 0, -6), |
| 24 |
time.Now().AddDate(0, 0, -5), |
| 25 |
time.Now().AddDate(0, 0, -4), |
| 26 |
time.Now().AddDate(0, 0, -3), |
| 27 |
time.Now().AddDate(0, 0, -2), |
| 28 |
time.Now().AddDate(0, 0, -1), |
| 29 |
time.Now(), |
| 30 |
}, |
| 31 |
YValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0}, |
| 32 |
}, |
| 33 |
}, |
| 34 |
} |
| 35 |
|
| 36 |
res.Header().Set("Content-Type", "image/png") |
| 37 |
graph.Render(chart.PNG, res) |
| 38 |
} |
| 39 |
|
| 40 |
func drawCustomChart(res http.ResponseWriter, req *http.Request) { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
graph := chart.Chart{ |
| 45 |
XAxis: chart.XAxis{ |
| 46 |
ValueFormatter: chart.TimeHourValueFormatter, |
| 47 |
}, |
| 48 |
Series: []chart.Series{ |
| 49 |
chart.TimeSeries{ |
| 50 |
XValues: []time.Time{ |
| 51 |
time.Now().Add(-10 * time.Hour), |
| 52 |
time.Now().Add(-9 * time.Hour), |
| 53 |
time.Now().Add(-8 * time.Hour), |
| 54 |
time.Now().Add(-7 * time.Hour), |
| 55 |
time.Now().Add(-6 * time.Hour), |
| 56 |
time.Now().Add(-5 * time.Hour), |
| 57 |
time.Now().Add(-4 * time.Hour), |
| 58 |
time.Now().Add(-3 * time.Hour), |
| 59 |
time.Now().Add(-2 * time.Hour), |
| 60 |
time.Now().Add(-1 * time.Hour), |
| 61 |
time.Now(), |
| 62 |
}, |
| 63 |
YValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0}, |
| 64 |
}, |
| 65 |
}, |
| 66 |
} |
| 67 |
|
| 68 |
res.Header().Set("Content-Type", "image/png") |
| 69 |
graph.Render(chart.PNG, res) |
| 70 |
} |
| 71 |
|
| 72 |
func main() { |
| 73 |
http.HandleFunc("/", drawChart) |
| 74 |
http.HandleFunc("/favicon.ico", func(res http.ResponseWriter, req *http.Request) { |
| 75 |
res.Write([]byte{}) |
| 76 |
}) |
| 77 |
http.HandleFunc("/custom", drawCustomChart) |
| 78 |
http.ListenAndServe(":8080", nil) |
| 79 |
} |