2016-07-07 17:44:03 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/wcharczuk/go-chart"
|
|
|
|
"github.com/wcharczuk/go-web"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := web.New()
|
|
|
|
app.SetName("Chart Test Server")
|
|
|
|
app.SetLogger(web.NewStandardOutputLogger())
|
|
|
|
app.GET("/", func(rc *web.RequestContext) web.ControllerResult {
|
2016-07-08 01:18:53 -04:00
|
|
|
rc.Response.Header().Set("Content-Type", "image/svg+xml")
|
2016-07-07 17:44:03 -04:00
|
|
|
now := time.Now()
|
|
|
|
c := chart.Chart{
|
2016-07-07 22:11:30 -04:00
|
|
|
Title: "A Test Chart",
|
2016-07-07 17:44:03 -04:00
|
|
|
TitleStyle: chart.Style{
|
|
|
|
Show: true,
|
|
|
|
},
|
2016-07-07 23:26:07 -04:00
|
|
|
Width: 640,
|
|
|
|
Height: 480,
|
2016-07-07 17:44:03 -04:00
|
|
|
Axes: chart.Style{
|
|
|
|
Show: true,
|
|
|
|
StrokeWidth: 1.0,
|
|
|
|
},
|
2016-07-07 22:11:30 -04:00
|
|
|
YRange: chart.Range{
|
|
|
|
Min: 0.0,
|
|
|
|
Max: 7.0,
|
|
|
|
},
|
2016-07-07 20:14:25 -04:00
|
|
|
FinalValueLabel: chart.Style{
|
|
|
|
Show: true,
|
|
|
|
},
|
2016-07-07 17:44:03 -04:00
|
|
|
Series: []chart.Series{
|
|
|
|
chart.TimeSeries{
|
2016-07-07 22:11:30 -04:00
|
|
|
Name: "a",
|
2016-07-07 17:44:03 -04:00
|
|
|
XValues: []time.Time{now.AddDate(0, 0, -4), now.AddDate(0, 0, -3), now.AddDate(0, 0, -2), now.AddDate(0, 0, -1)},
|
|
|
|
YValues: []float64{2.5, 5.0, 2.0, 3.0},
|
|
|
|
},
|
2016-07-07 22:11:30 -04:00
|
|
|
chart.TimeSeries{
|
|
|
|
Name: "b",
|
|
|
|
XValues: []time.Time{now.AddDate(0, 0, -4), now.AddDate(0, 0, -3), now.AddDate(0, 0, -2), now.AddDate(0, 0, -1)},
|
|
|
|
YValues: []float64{6.0, 5.0, 4.0, 1.0},
|
|
|
|
},
|
2016-07-07 17:44:03 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer := bytes.NewBuffer([]byte{})
|
2016-07-08 01:18:53 -04:00
|
|
|
err := c.Render(chart.SVG, buffer)
|
2016-07-07 17:44:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return rc.API().InternalError(err)
|
|
|
|
}
|
|
|
|
return rc.Raw(buffer.Bytes())
|
|
|
|
})
|
|
|
|
log.Fatal(app.Start())
|
|
|
|
}
|