2016-07-07 23:44:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-07-08 09:16:02 +02:00
|
|
|
"fmt"
|
2016-07-07 23:44:03 +02:00
|
|
|
"log"
|
|
|
|
|
2016-07-08 09:16:02 +02:00
|
|
|
"github.com/blendlabs/go-util"
|
2016-07-07 23:44:03 +02:00
|
|
|
"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 {
|
2016-07-08 09:16:02 +02:00
|
|
|
rc.Response.Header().Set("Content-Type", "image/png")
|
2016-07-07 23:44:03 +02:00
|
|
|
c := chart.Chart{
|
2016-07-08 04:11:30 +02:00
|
|
|
Title: "A Test Chart",
|
2016-07-07 23:44:03 +02:00
|
|
|
TitleStyle: chart.Style{
|
|
|
|
Show: true,
|
|
|
|
},
|
2016-07-08 05:26:07 +02:00
|
|
|
Width: 640,
|
|
|
|
Height: 480,
|
2016-07-07 23:44:03 +02:00
|
|
|
Axes: chart.Style{
|
|
|
|
Show: true,
|
|
|
|
StrokeWidth: 1.0,
|
|
|
|
},
|
2016-07-08 04:11:30 +02:00
|
|
|
YRange: chart.Range{
|
|
|
|
Min: 0.0,
|
|
|
|
Max: 7.0,
|
2016-07-08 09:16:02 +02:00
|
|
|
Formatter: func(v interface{}) string {
|
|
|
|
if typed, isTyped := v.(float64); isTyped {
|
|
|
|
return fmt.Sprintf("%.4f", typed)
|
|
|
|
}
|
|
|
|
return util.StringEmpty
|
|
|
|
},
|
2016-07-08 04:11:30 +02:00
|
|
|
},
|
2016-07-08 02:14:25 +02:00
|
|
|
FinalValueLabel: chart.Style{
|
|
|
|
Show: true,
|
|
|
|
},
|
2016-07-07 23:44:03 +02:00
|
|
|
Series: []chart.Series{
|
2016-07-08 08:49:31 +02:00
|
|
|
chart.ContinuousSeries{
|
2016-07-08 04:11:30 +02:00
|
|
|
Name: "a",
|
2016-07-08 08:49:31 +02:00
|
|
|
XValues: []float64{1.0, 2.0, 3.0, 4.0},
|
|
|
|
YValues: []float64{2.5, 5.0, 2.0, 3.3},
|
2016-07-07 23:44:03 +02:00
|
|
|
},
|
2016-07-08 08:49:31 +02:00
|
|
|
chart.ContinuousSeries{
|
2016-07-08 04:11:30 +02:00
|
|
|
Name: "b",
|
2016-07-08 08:49:31 +02:00
|
|
|
XValues: []float64{3.0, 4.0, 5.0, 6.0},
|
2016-07-08 04:11:30 +02:00
|
|
|
YValues: []float64{6.0, 5.0, 4.0, 1.0},
|
|
|
|
},
|
2016-07-07 23:44:03 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-07-08 09:16:02 +02:00
|
|
|
err := c.Render(chart.PNG, rc.Response)
|
2016-07-07 23:44:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return rc.API().InternalError(err)
|
|
|
|
}
|
2016-07-08 09:16:02 +02:00
|
|
|
return nil
|
2016-07-07 23:44:03 +02:00
|
|
|
})
|
|
|
|
log.Fatal(app.Start())
|
|
|
|
}
|