things
This commit is contained in:
parent
5e5ff104d2
commit
65c3f62ac5
2 changed files with 25 additions and 7 deletions
16
chart.go
16
chart.go
|
@ -106,10 +106,16 @@ func (c Chart) calculateFinalLabelWidth(r Renderer) int {
|
|||
if !c.FinalValueLabel.Show {
|
||||
return 0
|
||||
}
|
||||
|
||||
var finalLabelText string
|
||||
for _, s := range c.Series {
|
||||
_, 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) {
|
||||
finalLabelText = ll
|
||||
}
|
||||
|
@ -178,6 +184,9 @@ func (c Chart) initRanges(canvasBox Box) (xrange Range, yrange Range) {
|
|||
xrange.Min = c.XRange.Min
|
||||
xrange.Max = c.XRange.Max
|
||||
}
|
||||
if c.XRange.Formatter != nil {
|
||||
xrange.Formatter = c.XRange.Formatter
|
||||
}
|
||||
xrange.Domain = canvasBox.Width
|
||||
|
||||
if c.YRange.IsZero() {
|
||||
|
@ -187,6 +196,9 @@ func (c Chart) initRanges(canvasBox Box) (xrange Range, yrange Range) {
|
|||
yrange.Min = c.YRange.Min
|
||||
yrange.Max = c.YRange.Max
|
||||
}
|
||||
if c.YRange.Formatter != nil {
|
||||
yrange.Formatter = c.YRange.Formatter
|
||||
}
|
||||
yrange.Domain = canvasBox.Height
|
||||
|
||||
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) {
|
||||
if c.FinalValueLabel.Show {
|
||||
_, lv := s.GetValue(s.Len() - 1)
|
||||
ll := s.GetYFormatter()(lv)
|
||||
ll := yrange.Format(lv)
|
||||
|
||||
py := canvasBox.Top
|
||||
ly := yrange.Translate(lv) + py
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/blendlabs/go-util"
|
||||
"github.com/wcharczuk/go-chart"
|
||||
"github.com/wcharczuk/go-web"
|
||||
)
|
||||
|
@ -13,7 +14,7 @@ func main() {
|
|||
app.SetName("Chart Test Server")
|
||||
app.SetLogger(web.NewStandardOutputLogger())
|
||||
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{
|
||||
Title: "A Test Chart",
|
||||
TitleStyle: chart.Style{
|
||||
|
@ -28,6 +29,12 @@ func main() {
|
|||
YRange: chart.Range{
|
||||
Min: 0.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{
|
||||
Show: true,
|
||||
|
@ -46,12 +53,11 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
buffer := bytes.NewBuffer([]byte{})
|
||||
err := c.Render(chart.SVG, buffer)
|
||||
err := c.Render(chart.PNG, rc.Response)
|
||||
if err != nil {
|
||||
return rc.API().InternalError(err)
|
||||
}
|
||||
return rc.Raw(buffer.Bytes())
|
||||
return nil
|
||||
})
|
||||
log.Fatal(app.Start())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue