2016-07-07 17:44:03 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2016-07-08 20:57:14 -04:00
|
|
|
"net/http"
|
2016-07-07 17:44:03 -04:00
|
|
|
|
|
|
|
"github.com/wcharczuk/go-chart"
|
2016-07-09 14:23:35 -04:00
|
|
|
"github.com/wcharczuk/go-chart/drawing"
|
2016-07-07 17:44:03 -04:00
|
|
|
"github.com/wcharczuk/go-web"
|
|
|
|
)
|
|
|
|
|
2016-07-09 12:11:47 -04:00
|
|
|
func chartHandler(rc *web.RequestContext) web.ControllerResult {
|
|
|
|
format, err := rc.RouteParameter("format")
|
|
|
|
if err != nil {
|
|
|
|
format = "png"
|
|
|
|
}
|
|
|
|
|
|
|
|
if format == "png" {
|
2016-07-08 03:16:02 -04:00
|
|
|
rc.Response.Header().Set("Content-Type", "image/png")
|
2016-07-09 12:11:47 -04:00
|
|
|
} else {
|
|
|
|
rc.Response.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
}
|
|
|
|
|
|
|
|
c := chart.Chart{
|
|
|
|
Title: "A Test Chart",
|
|
|
|
TitleStyle: chart.Style{
|
2016-07-09 13:27:47 -04:00
|
|
|
Show: true,
|
|
|
|
FontSize: 26.0,
|
2016-07-09 12:11:47 -04: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 13:27:47 -04:00
|
|
|
Style: chart.Style{
|
2016-07-09 14:23:35 -04:00
|
|
|
FillColor: drawing.Color{R: 0, G: 116, B: 217, A: 128},
|
2016-07-09 13:27:47 -04:00
|
|
|
},
|
2016-07-07 20:14:25 -04:00
|
|
|
},
|
2016-07-09 12:11:47 -04: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 17:44:03 -04:00
|
|
|
},
|
2016-07-09 12:11:47 -04: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 17:44:03 -04:00
|
|
|
|
2016-07-09 12:11:47 -04: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 17:44:03 -04:00
|
|
|
log.Fatal(app.Start())
|
|
|
|
}
|