go-chart/testserver/main.go

149 lines
2.8 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-11 08:55:57 +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")
}
2016-07-10 20:19:56 +02:00
s1x := []float64{2.0, 3.0, 4.0, 5.0}
2016-07-10 19:43:04 +02:00
s1y := []float64{2.5, 5.0, 2.0, 3.3}
2016-07-10 20:19:56 +02:00
s2x := []float64{0.0, 0.5, 1.0, 1.5}
s2y := []float64{1.1, 1.2, 1.0, 1.3}
2016-07-10 19:43:04 +02:00
2016-07-09 18:11:47 +02:00
c := chart.Chart{
Title: "A Test Chart",
TitleStyle: chart.Style{
2016-07-10 19:43:04 +02:00
Show: true,
2016-07-09 18:11:47 +02:00
},
2016-07-10 19:43:04 +02:00
Width: 1024,
Height: 400,
XAxis: chart.XAxis{
Style: chart.Style{
2016-07-11 03:09:41 +02:00
Show: true,
2016-07-10 19:43:04 +02:00
},
2016-07-09 18:11:47 +02:00
},
2016-07-10 19:43:04 +02:00
YAxis: chart.YAxis{
Style: chart.Style{
2016-07-11 03:09:41 +02:00
Show: true,
2016-07-10 19:43:04 +02:00
},
Range: chart.Range{
Min: 0.0,
Max: 7.0,
},
2016-07-09 18:11:47 +02:00
},
2016-07-10 20:19:56 +02:00
YAxisSecondary: chart.YAxis{
Style: chart.Style{
2016-07-11 03:09:41 +02:00
Show: true,
2016-07-10 20:19:56 +02:00
},
Range: chart.Range{
Min: 0.8,
Max: 1.5,
},
},
2016-07-09 18:11:47 +02:00
Series: []chart.Series{
chart.ContinuousSeries{
Name: "a",
2016-07-10 19:43:04 +02:00
XValues: s1x,
YValues: s1y,
},
2016-07-09 18:11:47 +02:00
chart.ContinuousSeries{
Name: "b",
2016-07-10 20:19:56 +02:00
YAxis: chart.YAxisSecondary,
2016-07-10 19:43:04 +02:00
XValues: s2x,
YValues: s2y,
},
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-11 08:55:57 +02:00
func measureTestHandler(rc *web.RequestContext) web.ControllerResult {
format, err := rc.RouteParameter("format")
if err != nil {
format = "png"
}
if format == "png" {
rc.Response.Header().Set("Content-Type", "image/png")
} else {
rc.Response.Header().Set("Content-Type", "image/svg+xml")
}
var r chart.Renderer
if format == "png" {
r, err = chart.PNG(512, 512)
} else {
r, err = chart.SVG(512, 512)
}
if err != nil {
return rc.API().InternalError(err)
}
f, err := chart.GetDefaultFont()
if err != nil {
return rc.API().InternalError(err)
}
r.SetDPI(96)
r.SetFont(f)
2016-07-11 09:02:31 +02:00
r.SetFontSize(10.0)
2016-07-11 08:55:57 +02:00
r.SetFontColor(drawing.ColorBlack)
r.SetStrokeColor(drawing.ColorBlack)
2016-07-11 09:02:31 +02:00
label := "goog - 702.23"
2016-07-11 08:55:57 +02:00
tx, ty := 64, 64
2016-07-11 09:02:31 +02:00
tw, th := r.MeasureText(label)
2016-07-11 08:55:57 +02:00
r.MoveTo(tx, ty)
r.LineTo(tx+tw, ty)
r.LineTo(tx+tw, ty-th)
r.LineTo(tx, ty-th)
r.LineTo(tx, ty)
r.Stroke()
2016-07-11 09:02:31 +02:00
r.Text(label, tx, ty)
2016-07-11 08:55:57 +02:00
r.Save(rc.Response)
return nil
}
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)
2016-07-10 19:43:04 +02:00
app.GET("/format/:format", chartHandler)
app.GET("/favico.ico", func(rc *web.RequestContext) web.ControllerResult {
return rc.Raw([]byte{})
})
2016-07-11 08:55:57 +02:00
app.GET("/measure", measureTestHandler)
2016-07-07 23:44:03 +02:00
log.Fatal(app.Start())
}