slightly more rigorous bounds checking and auto-fit

This commit is contained in:
Will Charczuk 2016-07-12 16:47:52 -07:00
parent bba75e5d4c
commit 28f01842de
13 changed files with 475 additions and 195 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"bytes"
"fmt"
"log"
"math/rand"
@ -8,6 +9,7 @@ import (
"time"
"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
"github.com/wcharczuk/go-web"
)
@ -36,7 +38,7 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
c := chart.Chart{
Title: "A Test Chart",
TitleStyle: chart.Style{
Show: true,
Show: false,
},
Width: 1024,
Height: 400,
@ -91,6 +93,104 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
return nil
}
func boxHandler(rc *web.RequestContext) web.ControllerResult {
r, err := chart.PNG(1024, 1024)
if err != nil {
rc.API().InternalError(err)
}
f, err := chart.GetDefaultFont()
if err != nil {
return rc.API().InternalError(err)
}
//1:1 128wx128h @ 64,64
a := chart.Box{Top: 64, Left: 64, Right: 192, Bottom: 192}
// 3:2 256x170 @ 16, 16
//b := chart.Box{Top: 16, Left: 16, Right: 256, Bottom: 170}
// 2:3 170x256 @ 16, 16
c := chart.Box{Top: 16, Left: 16, Right: 170, Bottom: 256}
//fitb := a.Fit(b)
fitc := a.Fit(c)
//growb := a.Grow(b)
//growc := a.Grow(c)
//grow := a.Grow(b).Grow(c)
conc := a.Constrain(c)
boxStyle := chart.Style{
StrokeColor: drawing.ColorBlack,
StrokeWidth: 1.0,
Font: f,
FontSize: 18.0,
}
computedBoxStyle := chart.Style{
StrokeColor: drawing.ColorRed,
StrokeWidth: 1.0,
Font: f,
FontSize: 18.0,
}
chart.DrawBox(r, a, boxStyle)
//chart.DrawBox(r, b, boxStyle)
chart.DrawBox(r, c, boxStyle)
//chart.DrawBox(r, fitb, computedBoxStyle)
chart.DrawBox(r, fitc, computedBoxStyle)
/*chart.DrawBox(r, growb, computedBoxStyle)
chart.DrawBox(r, growc, computedBoxStyle)
chart.DrawBox(r, grow, computedBoxStyle)*/
chart.DrawBox(r, conc, computedBoxStyle)
ax, ay := a.Center()
chart.DrawTextCentered(r, "a", ax, ay, boxStyle.WithDefaultsFrom(chart.Style{
FillColor: boxStyle.StrokeColor,
}))
/*bx, by := b.Center()
chart.DrawTextCentered(r, "b", bx, by, boxStyle.WithDefaultsFrom(chart.Style{
FillColor: boxStyle.StrokeColor,
}))*/
cx, cy := c.Center()
chart.DrawTextCentered(r, "c", cx, cy, boxStyle.WithDefaultsFrom(chart.Style{
FillColor: boxStyle.StrokeColor,
}))
/*fbx, fby := fitb.Center()
chart.DrawTextCentered(r, "a fit b", fbx, fby, computedBoxStyle.WithDefaultsFrom(chart.Style{
FillColor: computedBoxStyle.StrokeColor,
}))*/
fcx, fcy := fitc.Center()
chart.DrawTextCentered(r, "a fit c", fcx, fcy, computedBoxStyle.WithDefaultsFrom(chart.Style{
FillColor: computedBoxStyle.StrokeColor,
}))
/*gbx, gby := growb.Center()
chart.DrawTextCentered(r, "a grow b", gbx, gby, computedBoxStyle.WithDefaultsFrom(chart.Style{
FillColor: computedBoxStyle.StrokeColor,
}))
gcx, gcy := growc.Center()
chart.DrawTextCentered(r, "a grow c", gcx, gcy, computedBoxStyle.WithDefaultsFrom(chart.Style{
FillColor: computedBoxStyle.StrokeColor,
}))*/
ccx, ccy := conc.Center()
chart.DrawTextCentered(r, "a const c", ccx, ccy, computedBoxStyle.WithDefaultsFrom(chart.Style{
FillColor: computedBoxStyle.StrokeColor,
}))
rc.Response.Header().Set("Content-Type", "image/png")
buffer := bytes.NewBuffer([]byte{})
err = r.Save(buffer)
return rc.Raw(buffer.Bytes())
}
func main() {
app := web.New()
app.SetName("Chart Test Server")
@ -100,5 +200,6 @@ func main() {
app.GET("/favico.ico", func(rc *web.RequestContext) web.ControllerResult {
return rc.Raw([]byte{})
})
app.GET("/box", boxHandler)
log.Fatal(app.Start())
}