go-chart/testserver/main.go

80 lines
1.6 KiB
Go
Raw Normal View History

2016-07-07 23:44:03 +02:00
package main
import (
"log"
2016-07-09 02:57:14 +02:00
"net/http"
2016-07-07 23:44:03 +02:00
"github.com/wcharczuk/go-chart"
2016-07-09 20:23:35 +02:00
"github.com/wcharczuk/go-chart/drawing"
2016-07-07 23:44:03 +02:00
"github.com/wcharczuk/go-web"
)
2016-07-09 18:11:47 +02:00
func chartHandler(rc *web.RequestContext) web.ControllerResult {
format, err := rc.RouteParameter("format")
if err != nil {
format = "png"
}
if format == "png" {
2016-07-08 09:16:02 +02:00
rc.Response.Header().Set("Content-Type", "image/png")
2016-07-09 18:11:47 +02:00
} else {
rc.Response.Header().Set("Content-Type", "image/svg+xml")
}
c := chart.Chart{
Title: "A Test Chart",
TitleStyle: chart.Style{
2016-07-09 19:27:47 +02:00
Show: true,
FontSize: 26.0,
2016-07-09 18:11:47 +02:00
},
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.ContinuousSeries{
Name: "a",
XValues: []float64{1.0, 2.0, 3.0, 4.0},
YValues: []float64{2.5, 5.0, 2.0, 3.3},
2016-07-09 19:27:47 +02:00
Style: chart.Style{
2016-07-09 20:23:35 +02:00
FillColor: drawing.Color{R: 0, G: 116, B: 217, A: 128},
2016-07-09 19:27:47 +02:00
},
},
2016-07-09 18:11:47 +02:00
chart.ContinuousSeries{
Name: "b",
XValues: []float64{3.0, 4.0, 5.0, 6.0},
YValues: []float64{6.0, 5.0, 4.0, 1.0},
2016-07-07 23:44:03 +02:00
},
2016-07-09 18:11:47 +02:00
},
}
if format == "png" {
err = c.Render(chart.PNG, rc.Response)
} else {
err = c.Render(chart.SVG, rc.Response)
}
if err != nil {
return rc.API().InternalError(err)
}
rc.Response.WriteHeader(http.StatusOK)
return nil
}
2016-07-07 23:44:03 +02:00
2016-07-09 18:11:47 +02:00
func main() {
app := web.New()
app.SetName("Chart Test Server")
app.SetLogger(web.NewStandardOutputLogger())
app.GET("/", chartHandler)
app.GET("/:format", chartHandler)
2016-07-07 23:44:03 +02:00
log.Fatal(app.Start())
}