This commit is contained in:
Will Charczuk 2016-07-08 00:16:02 -07:00
parent 5e5ff104d2
commit 65c3f62ac5
2 changed files with 25 additions and 7 deletions

View File

@ -106,10 +106,16 @@ func (c Chart) calculateFinalLabelWidth(r Renderer) int {
if !c.FinalValueLabel.Show { if !c.FinalValueLabel.Show {
return 0 return 0
} }
var finalLabelText string var finalLabelText string
for _, s := range c.Series { for _, s := range c.Series {
_, lv := s.GetValue(s.Len() - 1) _, lv := s.GetValue(s.Len() - 1)
ll := s.GetYFormatter()(lv) var ll string
if c.YRange.Formatter != nil {
ll = c.YRange.Formatter(lv)
} else {
ll = s.GetYFormatter()(lv)
}
if len(finalLabelText) < len(ll) { if len(finalLabelText) < len(ll) {
finalLabelText = ll finalLabelText = ll
} }
@ -178,6 +184,9 @@ func (c Chart) initRanges(canvasBox Box) (xrange Range, yrange Range) {
xrange.Min = c.XRange.Min xrange.Min = c.XRange.Min
xrange.Max = c.XRange.Max xrange.Max = c.XRange.Max
} }
if c.XRange.Formatter != nil {
xrange.Formatter = c.XRange.Formatter
}
xrange.Domain = canvasBox.Width xrange.Domain = canvasBox.Width
if c.YRange.IsZero() { if c.YRange.IsZero() {
@ -187,6 +196,9 @@ func (c Chart) initRanges(canvasBox Box) (xrange Range, yrange Range) {
yrange.Min = c.YRange.Min yrange.Min = c.YRange.Min
yrange.Max = c.YRange.Max yrange.Max = c.YRange.Max
} }
if c.YRange.Formatter != nil {
yrange.Formatter = c.YRange.Formatter
}
yrange.Domain = canvasBox.Height yrange.Domain = canvasBox.Height
return return
@ -330,7 +342,7 @@ func (c Chart) drawSeries(r Renderer, canvasBox Box, index int, s Series, xrange
func (c Chart) drawFinalValueLabel(r Renderer, canvasBox Box, index int, s Series, yrange Range) { func (c Chart) drawFinalValueLabel(r Renderer, canvasBox Box, index int, s Series, yrange Range) {
if c.FinalValueLabel.Show { if c.FinalValueLabel.Show {
_, lv := s.GetValue(s.Len() - 1) _, lv := s.GetValue(s.Len() - 1)
ll := s.GetYFormatter()(lv) ll := yrange.Format(lv)
py := canvasBox.Top py := canvasBox.Top
ly := yrange.Translate(lv) + py ly := yrange.Translate(lv) + py

View File

@ -1,9 +1,10 @@
package main package main
import ( import (
"bytes" "fmt"
"log" "log"
"github.com/blendlabs/go-util"
"github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-web" "github.com/wcharczuk/go-web"
) )
@ -13,7 +14,7 @@ func main() {
app.SetName("Chart Test Server") app.SetName("Chart Test Server")
app.SetLogger(web.NewStandardOutputLogger()) app.SetLogger(web.NewStandardOutputLogger())
app.GET("/", func(rc *web.RequestContext) web.ControllerResult { app.GET("/", func(rc *web.RequestContext) web.ControllerResult {
rc.Response.Header().Set("Content-Type", "image/svg+xml") rc.Response.Header().Set("Content-Type", "image/png")
c := chart.Chart{ c := chart.Chart{
Title: "A Test Chart", Title: "A Test Chart",
TitleStyle: chart.Style{ TitleStyle: chart.Style{
@ -28,6 +29,12 @@ func main() {
YRange: chart.Range{ YRange: chart.Range{
Min: 0.0, Min: 0.0,
Max: 7.0, Max: 7.0,
Formatter: func(v interface{}) string {
if typed, isTyped := v.(float64); isTyped {
return fmt.Sprintf("%.4f", typed)
}
return util.StringEmpty
},
}, },
FinalValueLabel: chart.Style{ FinalValueLabel: chart.Style{
Show: true, Show: true,
@ -46,12 +53,11 @@ func main() {
}, },
} }
buffer := bytes.NewBuffer([]byte{}) err := c.Render(chart.PNG, rc.Response)
err := c.Render(chart.SVG, buffer)
if err != nil { if err != nil {
return rc.API().InternalError(err) return rc.API().InternalError(err)
} }
return rc.Raw(buffer.Bytes()) return nil
}) })
log.Fatal(app.Start()) log.Fatal(app.Start())
} }