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"
|
|
|
|
"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{
|
|
|
|
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.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-08 02:14:25 +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())
|
|
|
|
}
|