go-chart/testserver/main.go
Will Charczuk f843d124d6 SVG!
2016-07-07 22:18:53 -07:00

60 lines
1.3 KiB
Go

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 {
rc.Response.Header().Set("Content-Type", "image/svg+xml")
now := time.Now()
c := chart.Chart{
Title: "A Test Chart",
TitleStyle: chart.Style{
Show: true,
},
Width: 640,
Height: 480,
Axes: chart.Style{
Show: true,
StrokeWidth: 1.0,
},
YRange: chart.Range{
Min: 0.0,
Max: 7.0,
},
FinalValueLabel: chart.Style{
Show: true,
},
Series: []chart.Series{
chart.TimeSeries{
Name: "a",
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},
},
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},
},
},
}
buffer := bytes.NewBuffer([]byte{})
err := c.Render(chart.SVG, buffer)
if err != nil {
return rc.API().InternalError(err)
}
return rc.Raw(buffer.Bytes())
})
log.Fatal(app.Start())
}