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"
|
|
|
|
"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")
|
|
|
|
}
|
|
|
|
|
2016-07-10 13:43:04 -04:00
|
|
|
s1x := []float64{1.0, 2.0, 3.0, 4.0}
|
|
|
|
s1y := []float64{2.5, 5.0, 2.0, 3.3}
|
|
|
|
|
|
|
|
s2x := []float64{3.0, 4.0, 5.0, 6.0}
|
|
|
|
s2y := []float64{6.0, 5.0, 4.0, 1.0}
|
|
|
|
|
2016-07-09 12:11:47 -04:00
|
|
|
c := chart.Chart{
|
|
|
|
Title: "A Test Chart",
|
|
|
|
TitleStyle: chart.Style{
|
2016-07-10 13:43:04 -04:00
|
|
|
Show: true,
|
2016-07-09 12:11:47 -04:00
|
|
|
},
|
2016-07-10 13:43:04 -04:00
|
|
|
Width: 1024,
|
|
|
|
Height: 400,
|
|
|
|
XAxis: chart.XAxis{
|
|
|
|
Style: chart.Style{
|
|
|
|
Show: true,
|
|
|
|
StrokeWidth: 1.0,
|
|
|
|
},
|
2016-07-09 12:11:47 -04:00
|
|
|
},
|
2016-07-10 13:43:04 -04:00
|
|
|
YAxis: chart.YAxis{
|
|
|
|
Style: chart.Style{
|
|
|
|
Show: true,
|
|
|
|
StrokeWidth: 1.0,
|
|
|
|
},
|
|
|
|
Range: chart.Range{
|
|
|
|
Min: 0.0,
|
|
|
|
Max: 7.0,
|
|
|
|
},
|
2016-07-09 12:11:47 -04:00
|
|
|
},
|
|
|
|
Series: []chart.Series{
|
|
|
|
chart.ContinuousSeries{
|
|
|
|
Name: "a",
|
2016-07-10 13:43:04 -04:00
|
|
|
XValues: s1x,
|
|
|
|
YValues: s1y,
|
2016-07-07 20:14:25 -04:00
|
|
|
},
|
2016-07-09 12:11:47 -04:00
|
|
|
chart.ContinuousSeries{
|
|
|
|
Name: "b",
|
2016-07-10 13:43:04 -04:00
|
|
|
XValues: s2x,
|
|
|
|
YValues: s2y,
|
|
|
|
},
|
|
|
|
chart.AnnotationSeries{
|
|
|
|
Name: "a - last value",
|
|
|
|
Style: chart.Style{
|
|
|
|
Show: true,
|
|
|
|
StrokeColor: chart.GetDefaultSeriesStrokeColor(0),
|
|
|
|
},
|
|
|
|
Annotations: []chart.Annotation{
|
|
|
|
chart.Annotation{
|
|
|
|
X: s1x[len(s1x)-1],
|
|
|
|
Y: s1y[len(s1y)-1],
|
|
|
|
Label: chart.FloatValueFormatter(s1y[len(s1y)-1]),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
chart.AnnotationSeries{
|
|
|
|
Name: "b - last value",
|
|
|
|
Style: chart.Style{
|
|
|
|
Show: true,
|
|
|
|
StrokeColor: chart.GetDefaultSeriesStrokeColor(1),
|
|
|
|
},
|
|
|
|
Annotations: []chart.Annotation{
|
|
|
|
chart.Annotation{
|
|
|
|
X: s2x[len(s2x)-1],
|
|
|
|
Y: s2y[len(s2y)-1],
|
|
|
|
Label: chart.FloatValueFormatter(s2y[len(s2y)-1]),
|
|
|
|
},
|
|
|
|
},
|
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)
|
2016-07-10 13:43:04 -04:00
|
|
|
app.GET("/format/:format", chartHandler)
|
|
|
|
app.GET("/favico.ico", func(rc *web.RequestContext) web.ControllerResult {
|
|
|
|
return rc.Raw([]byte{})
|
|
|
|
})
|
2016-07-07 17:44:03 -04:00
|
|
|
log.Fatal(app.Start())
|
|
|
|
}
|