mostly works.

This commit is contained in:
Will Charczuk 2016-07-10 10:43:04 -07:00
parent e9a36274ac
commit abfdc3e0d9
14 changed files with 465 additions and 159 deletions

View File

@ -14,13 +14,35 @@ type AnnotationSeries struct {
Annotations []Annotation
}
// GetName returns the name of the time series.
func (as AnnotationSeries) GetName() string {
return as.Name
}
// GetStyle returns the line style.
func (as AnnotationSeries) GetStyle() Style {
return as.Style
}
// GetYAxis returns which YAxis the series draws on.
func (as AnnotationSeries) GetYAxis() YAxisType {
return as.YAxis
}
// Render draws the series.
func (as AnnotationSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range) {
func (as AnnotationSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
if as.Style.Show {
style := as.Style.WithDefaultsFrom(Style{
FillColor: DefaultAnnotationFillColor,
FontSize: DefaultAnnotationFontSize,
StrokeColor: defaults.StrokeColor,
StrokeWidth: defaults.StrokeWidth,
Padding: DefaultAnnotationPadding,
})
for _, a := range as.Annotations {
lx := xrange.Translate(a.X) + canvasBox.Left
lx := canvasBox.Right - xrange.Translate(a.X)
ly := yrange.Translate(a.Y) + canvasBox.Top
DrawAnnotation(r, canvasBox, xrange, yrange, as.Style, lx, ly, a.Label)
DrawAnnotation(r, canvasBox, xrange, yrange, style, lx, ly, a.Label)
}
}
}

44
axis.go
View File

@ -14,46 +14,6 @@ const (
type Axis interface {
GetName() string
GetStyle() Style
Render(c *Chart, r Renderer, canvasBox Box, ra Range)
}
// axis represents the basics of an axis implementation.
type axis struct {
Name string
Style Style
ValueFormatter ValueFormatter
Range Range
Ticks []Tick
}
func (a axis) GetName() string {
return a.Name
}
func (a axis) GetStyle() Style {
return a.Style
}
func (a axis) getTicks(ra Range) []Tick {
if len(a.Ticks) > 0 {
return a.Ticks
}
return a.generateTicks(ra)
}
func (a axis) generateTicks(ra Range) []Tick {
step := a.getTickStep(ra)
return a.generateTicksWithStep(ra, step)
}
func (a axis) getTickCount(ra Range) int {
return 0
}
func (a axis) getTickStep(ra Range) float64 {
return 0.0
}
func (a axis) generateTicksWithStep(ra Range, step float64) []Tick {
return []Tick{}
GetTicks(r Renderer, ra Range, vf ValueFormatter) []Tick
Render(c *Chart, r Renderer, canvasBox Box, ra Range, ticks []Tick)
}

179
chart.go
View File

@ -5,6 +5,7 @@ import (
"io"
"github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/drawing"
)
// Chart is what we're drawing.
@ -51,7 +52,7 @@ func (c Chart) GetFont() (*truetype.Font, error) {
}
// Render renders the chart with the given renderer to the given io.Writer.
func (c *Chart) Render(rp RendererProvider, w io.Writer) error {
func (c Chart) Render(rp RendererProvider, w io.Writer) error {
if len(c.Series) == 0 {
return errors.New("Please provide at least one series")
}
@ -64,16 +65,26 @@ func (c *Chart) Render(rp RendererProvider, w io.Writer) error {
if err != nil {
return err
}
c.Font = font
r.SetFont(font)
r.SetDPI(c.GetDPI(DefaultDPI))
xrange, yrange, yrangeAlt := c.getRanges()
canvasBox := c.getCanvasBox(r, xrange, yrange, yrangeAlt)
xrange, yrange, yrangeAlt = c.getRangeDomains(canvasBox, xrange, yrange, yrangeAlt)
canvasBox := c.getDefaultCanvasBox()
xf, yf, yfa := c.getValueFormatters()
xrange, yrange, yrangeAlt = c.setRangeDomains(canvasBox, xrange, yrange, yrangeAlt)
xticks, yticks, yticksAlt := c.getAxesTicks(r, xrange, yrange, yrangeAlt, xf, yf, yfa)
canvasBox = c.getAdjustedCanvasBox(r, canvasBox, xticks, yticks, yticksAlt)
// we do a second pass to take the updated domains into account
xrange, yrange, yrangeAlt = c.setRangeDomains(canvasBox, xrange, yrange, yrangeAlt)
xticks, yticks, yticksAlt = c.getAxesTicks(r, xrange, yrange, yrangeAlt, xf, yf, yfa)
canvasBox = c.getAdjustedCanvasBox(r, canvasBox, xticks, yticks, yticksAlt)
c.drawBackground(r)
c.drawCanvas(r, canvasBox)
c.drawAxes(r, canvasBox, xrange, yrange, yrangeAlt)
c.drawAxes(r, canvasBox, xrange, yrange, yrangeAlt, xticks, yticks, yticksAlt)
for index, series := range c.Series {
c.drawSeries(r, canvasBox, xrange, yrange, yrangeAlt, series, index)
}
@ -147,21 +158,11 @@ func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
return
}
func (c Chart) getCanvasBox(r Renderer, xrange, yrange, yrangeAlt Range) Box {
func (c Chart) getDefaultCanvasBox() Box {
dpl := c.Background.Padding.GetLeft(DefaultBackgroundPadding.Left)
dpr := c.Background.Padding.GetRight(DefaultBackgroundPadding.Right)
dpb := c.Background.Padding.GetBottom(DefaultBackgroundPadding.Bottom)
if c.YAxisSecondary.Style.Show {
dpl = c.getYAxisSecondaryWidth(r, yrangeAlt)
}
if c.YAxis.Style.Show {
dpr = c.getYAxisWidth(r, yrange)
}
if c.XAxis.Style.Show {
dpb = c.getXAxisHeight(r, xrange)
}
cb := Box{
Top: c.Background.Padding.GetTop(DefaultBackgroundPadding.Top),
Left: dpl,
@ -173,23 +174,95 @@ func (c Chart) getCanvasBox(r Renderer, xrange, yrange, yrangeAlt Range) Box {
return cb
}
func (c Chart) getYAxisSecondaryWidth(r Renderer, ra Range) int {
var ll string
ticks := c.YAxisSecondary.getTicks(ra)
for _, t := range ticks {
if len(t.Label) > len(ll) {
ll = t.Label
func (c Chart) getValueFormatters() (x, y, ya ValueFormatter) {
for _, s := range c.Series {
if vfp, isVfp := s.(ValueFormatterProvider); isVfp {
sx, sy := vfp.GetValueFormatters()
if s.GetYAxis() == YAxisPrimary {
x = sx
y = sy
} else if s.GetYAxis() == YAxisSecondary {
x = sx
ya = sy
}
}
r.SetFontSize(c.YAxisSecondary.Style.GetFontSize(DefaultFontSize))
r.SetFont(c.YAxisSecondary.Style.GetFont(c.Font))
tw, _ := r.MeasureText(ll)
return tw + DefaultYAxisMargin
}
if c.XAxis.ValueFormatter != nil {
x = c.XAxis.ValueFormatter
}
if c.YAxis.ValueFormatter != nil {
y = c.YAxis.ValueFormatter
}
if c.YAxisSecondary.ValueFormatter != nil {
ya = c.YAxisSecondary.ValueFormatter
}
return
}
func (c Chart) getYAxisWidth(r Renderer, ra Range) int {
func (c Chart) getAxesTicks(r Renderer, xr, yr, yar Range, xf, yf, yfa ValueFormatter) (xticks, yticks, yticksAlt []Tick) {
if c.XAxis.Style.Show {
xticks = c.XAxis.GetTicks(r, xr, xf)
}
if c.YAxis.Style.Show {
yticks = c.YAxis.GetTicks(r, yr, yf)
}
if c.YAxisSecondary.Style.Show {
yticksAlt = c.YAxisSecondary.GetTicks(r, yr, yf)
}
return
}
func (c Chart) getAdjustedCanvasBox(r Renderer, defaults Box, xticks, yticks, yticksAlt []Tick) Box {
canvasBox := Box{}
var dpl, dpr, dpb int
if c.XAxis.Style.Show {
dpb = c.getXAxisHeight(r, xticks)
}
if c.YAxis.Style.Show {
dpr = c.getYAxisWidth(r, yticks)
}
if c.YAxisSecondary.Style.Show {
dpl = c.getYAxisSecondaryWidth(r, yticksAlt)
}
canvasBox.Top = defaults.Top
if dpl != 0 {
canvasBox.Left = c.Canvas.Padding.GetLeft(dpl)
} else {
canvasBox.Left = defaults.Left
}
if dpr != 0 {
canvasBox.Right = c.Width - c.Canvas.Padding.GetRight(dpr)
} else {
canvasBox.Right = defaults.Right
}
if dpb != 0 {
canvasBox.Bottom = c.Height - c.Canvas.Padding.GetBottom(dpb)
} else {
canvasBox.Bottom = defaults.Bottom
}
canvasBox.Width = canvasBox.Right - canvasBox.Left
canvasBox.Height = canvasBox.Bottom - canvasBox.Top
return canvasBox
}
func (c Chart) getXAxisHeight(r Renderer, ticks []Tick) int {
r.SetFontSize(c.XAxis.Style.GetFontSize(DefaultFontSize))
r.SetFont(c.XAxis.Style.GetFont(c.Font))
var tl int
for _, t := range ticks {
_, lh := r.MeasureText(t.Label)
if lh > tl {
tl = lh
}
}
return tl + DefaultXAxisMargin
}
func (c Chart) getYAxisWidth(r Renderer, ticks []Tick) int {
var ll string
ticks := c.YAxis.getTicks(ra)
for _, t := range ticks {
if len(t.Label) > len(ll) {
ll = t.Label
@ -201,21 +274,21 @@ func (c Chart) getYAxisWidth(r Renderer, ra Range) int {
return tw + DefaultYAxisMargin
}
func (c Chart) getXAxisHeight(r Renderer, ra Range) int {
r.SetFontSize(c.XAxis.Style.GetFontSize(DefaultFontSize))
r.SetFont(c.XAxis.Style.GetFont(c.Font))
var tl int
ticks := c.YAxis.getTicks(ra)
func (c Chart) getYAxisSecondaryWidth(r Renderer, ticks []Tick) int {
var ll string
for _, t := range ticks {
_, lh := r.MeasureText(t.Label)
if lh > tl {
tl = lh
if len(t.Label) > len(ll) {
ll = t.Label
}
}
return tl + DefaultXAxisMargin
r.SetFontSize(c.YAxisSecondary.Style.GetFontSize(DefaultFontSize))
r.SetFont(c.YAxisSecondary.Style.GetFont(c.Font))
tw, _ := r.MeasureText(ll)
return tw + DefaultYAxisMargin
}
func (c Chart) getRangeDomains(canvasBox Box, xrange, yrange, yrangeAlt Range) (Range, Range, Range) {
func (c Chart) setRangeDomains(canvasBox Box, xrange, yrange, yrangeAlt Range) (Range, Range, Range) {
xrange.Domain = canvasBox.Width
yrange.Domain = canvasBox.Height
yrangeAlt.Domain = canvasBox.Height
@ -248,19 +321,19 @@ func (c Chart) drawCanvas(r Renderer, canvasBox Box) {
r.FillStroke()
}
func (c Chart) drawAxes(r Renderer, canvasBox Box, xrange, yrange, yrangeAlt Range) {
func (c Chart) drawAxes(r Renderer, canvasBox Box, xrange, yrange, yrangeAlt Range, xticks, yticks, yticksAlt []Tick) {
if c.XAxis.Style.Show {
c.XAxis.Render(r, canvasBox, xrange)
c.XAxis.Render(r, canvasBox, xrange, xticks)
}
if c.YAxis.Style.Show {
c.YAxis.Render(r, canvasBox, yrange, YAxisPrimary)
c.YAxis.Render(r, canvasBox, yrange, YAxisPrimary, yticks)
}
if c.YAxisSecondary.Style.Show {
c.YAxisSecondary.Render(r, canvasBox, yrangeAlt, YAxisSecondary)
c.YAxisSecondary.Render(r, canvasBox, yrangeAlt, YAxisSecondary, yticksAlt)
}
}
func (c Chart) getSeriesDefaults(seriesIndex int) Style {
func (c Chart) getSeriesStyleDefaults(seriesIndex int) Style {
strokeColor := GetDefaultSeriesStrokeColor(seriesIndex)
return Style{
StrokeColor: strokeColor,
@ -273,21 +346,27 @@ func (c Chart) getSeriesDefaults(seriesIndex int) Style {
func (c Chart) drawSeries(r Renderer, canvasBox Box, xrange, yrange, yrangeAlt Range, s Series, seriesIndex int) {
if s.GetYAxis() == YAxisPrimary {
s.Render(r, canvasBox, xrange, yrange, c.getSeriesDefaults(seriesIndex))
s.Render(r, canvasBox, xrange, yrange, c.getSeriesStyleDefaults(seriesIndex))
} else if s.GetYAxis() == YAxisSecondary {
s.Render(r, canvasBox, xrange, yrange, c.getSeriesDefaults(seriesIndex))
s.Render(r, canvasBox, xrange, yrange, c.getSeriesStyleDefaults(seriesIndex))
}
}
func (c Chart) drawTitle(r Renderer) error {
func (c Chart) drawTitle(r Renderer) {
if len(c.Title) > 0 && c.TitleStyle.Show {
r.SetFontColor(c.Canvas.GetFontColor(DefaultTextColor))
titleFontSize := c.Canvas.GetFontSize(DefaultTitleFontSize)
r.SetFont(c.TitleStyle.GetFont(c.Font))
r.SetFontColor(c.TitleStyle.GetFontColor(DefaultTextColor))
titleFontSize := c.TitleStyle.GetFontSize(DefaultTitleFontSize)
r.SetFontSize(titleFontSize)
textWidth, _ := r.MeasureText(c.Title)
textWidthPoints, textHeightPoints := r.MeasureText(c.Title)
textWidth := int(drawing.PointsToPixels(r.GetDPI(), float64(textWidthPoints)))
textHeight := int(drawing.PointsToPixels(r.GetDPI(), float64(textHeightPoints)))
titleX := (c.Width >> 1) - (textWidth >> 1)
titleY := c.TitleStyle.Padding.GetTop(DefaultTitleTop) + int(titleFontSize)
titleY := c.TitleStyle.Padding.GetTop(DefaultTitleTop) + textHeight
r.Text(c.Title, titleX, titleY)
}
return nil
}

View File

@ -31,6 +31,18 @@ func (cs ContinuousSeries) GetValue(index int) (float64, float64) {
return cs.XValues[index], cs.YValues[index]
}
// GetValueFormatters returns value formatter defaults for the series.
func (cs ContinuousSeries) GetValueFormatters() (x, y ValueFormatter) {
x = FloatValueFormatter
y = FloatValueFormatter
return
}
// GetYAxis returns which YAxis the series draws on.
func (cs ContinuousSeries) GetYAxis() YAxisType {
return cs.YAxis
}
// Render renders the series.
func (cs ContinuousSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
style := cs.Style.WithDefaultsFrom(defaults)

View File

@ -32,18 +32,25 @@ const (
DefaultAxisFontSize = 10.0
// DefaultTitleTop is the default distance from the top of the chart to put the title.
DefaultTitleTop = 10
// DefaultYAxisMargin is the default distance from the right of the canvas to the y axis labels.
DefaultYAxisMargin = 5
DefaultYAxisMargin = 10
// DefaultXAxisMargin is the default distance from bottom of the canvas to the x axis labels.
DefaultXAxisMargin = 10
//DefaultVerticalTickWidth is half the margin.
DefaultVerticalTickWidth = DefaultYAxisMargin >> 1
//DefaultHorizontalTickWidth is half the margin.
DefaultHorizontalTickWidth = DefaultXAxisMargin >> 1
// DefaultMinimumTickHorizontalSpacing is the minimum distance between horizontal ticks.
DefaultMinimumTickHorizontalSpacing = 20
// DefaultMinimumTickVerticalSpacing is the minimum distance between vertical ticks.
DefaultMinimumTickVerticalSpacing = 20
// DefaultDateFormat is the default date format.
DefaultDateFormat = "2006-01-02"
// DefaultMaxTickCount is the maximum number of ticks to draw
DefaultMaxTickCount = 7
)
var (

View File

@ -8,14 +8,13 @@ func DrawLineSeries(r Renderer, canvasBox Box, xrange, yrange Range, s Style, vs
return
}
cx := canvasBox.Left
cy := canvasBox.Top
ct := canvasBox.Top
cb := canvasBox.Bottom
cw := canvasBox.Width
cr := canvasBox.Right
v0x, v0y := vs.GetValue(0)
x0 := cw - xrange.Translate(v0x)
y0 := yrange.Translate(v0y)
x0 := cr - xrange.Translate(v0x)
y0 := yrange.Translate(v0y) + ct
var vx, vy float64
var x, y int
@ -23,15 +22,15 @@ func DrawLineSeries(r Renderer, canvasBox Box, xrange, yrange Range, s Style, vs
fill := s.GetFillColor()
if !fill.IsZero() {
r.SetFillColor(fill)
r.MoveTo(x0+cx, y0+cy)
r.MoveTo(x0, y0)
for i := 1; i < vs.Len(); i++ {
vx, vy = vs.GetValue(i)
x = cw - xrange.Translate(vx)
y = yrange.Translate(vy)
r.LineTo(x+cx, y+cy)
x = cr - xrange.Translate(vx)
y = yrange.Translate(vy) + ct
r.LineTo(x, y)
}
r.LineTo(x+cx, cb)
r.LineTo(x0+cx, cb)
r.LineTo(x, cb)
r.LineTo(x0, cb)
r.Close()
r.Fill()
}
@ -40,12 +39,12 @@ func DrawLineSeries(r Renderer, canvasBox Box, xrange, yrange Range, s Style, vs
r.SetStrokeColor(stroke)
r.SetStrokeWidth(s.GetStrokeWidth(DefaultStrokeWidth))
r.MoveTo(x0+cx, y0+cy)
r.MoveTo(x0, y0)
for i := 1; i < vs.Len(); i++ {
vx, vy = vs.GetValue(i)
x = cw - xrange.Translate(vx)
y = yrange.Translate(vy)
r.LineTo(x+cx, y+cy)
x = cr - xrange.Translate(vx)
y = yrange.Translate(vy) + ct
r.LineTo(x, y)
}
r.Stroke()
}
@ -81,6 +80,7 @@ func DrawAnnotation(r Renderer, canvasBox Box, xrange, yrange Range, s Style, lx
r.SetFillColor(s.GetFillColor(DefaultAnnotationFillColor))
r.SetStrokeColor(s.GetStrokeColor())
r.SetStrokeWidth(s.GetStrokeWidth())
r.MoveTo(lx, ly)
r.LineTo(ltlx, ltly)
r.LineTo(ltrx, ltry)

View File

@ -9,7 +9,7 @@ import (
// Renderer represents the basic methods required to draw a chart.
type Renderer interface {
// GetDPI returns the dpi for the renderer.
// GetDPI gets the DPI for the renderer.
GetDPI() float64
// SetDPI sets the DPI for the renderer.

30
series_test.go Normal file
View File

@ -0,0 +1,30 @@
package chart
import (
"testing"
"time"
"github.com/blendlabs/go-assert"
)
func TestTimeSeriesGetValue(t *testing.T) {
assert := assert.New(t)
ts := TimeSeries{
Name: "Test",
XValues: []time.Time{
time.Now().AddDate(0, 0, -5),
time.Now().AddDate(0, 0, -4),
time.Now().AddDate(0, 0, -3),
time.Now().AddDate(0, 0, -2),
time.Now().AddDate(0, 0, -1),
},
YValues: []float64{
1.0, 2.0, 3.0, 4.0, 5.0,
},
}
x0, y0 := ts.GetValue(0)
assert.NotZero(x0)
assert.Equal(1.0, y0)
}

View File

@ -5,7 +5,6 @@ import (
"net/http"
"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
"github.com/wcharczuk/go-web"
)
@ -21,38 +20,73 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
rc.Response.Header().Set("Content-Type", "image/svg+xml")
}
s1x := []float64{1.0, 2.0, 3.0, 4.0}
s1y := []float64{2.5, 5.0, 2.0, 3.3}
s2x := []float64{3.0, 4.0, 5.0, 6.0}
s2y := []float64{6.0, 5.0, 4.0, 1.0}
c := chart.Chart{
Title: "A Test Chart",
TitleStyle: chart.Style{
Show: true,
FontSize: 26.0,
},
Width: 640,
Height: 480,
Axes: chart.Style{
Width: 1024,
Height: 400,
XAxis: chart.XAxis{
Style: chart.Style{
Show: true,
StrokeWidth: 1.0,
},
YRange: chart.Range{
},
YAxis: chart.YAxis{
Style: chart.Style{
Show: true,
StrokeWidth: 1.0,
},
Range: chart.Range{
Min: 0.0,
Max: 7.0,
},
FinalValueLabel: chart.Style{
Show: true,
},
Series: []chart.Series{
chart.ContinuousSeries{
Name: "a",
XValues: []float64{1.0, 2.0, 3.0, 4.0},
YValues: []float64{2.5, 5.0, 2.0, 3.3},
Style: chart.Style{
FillColor: drawing.Color{R: 0, G: 116, B: 217, A: 128},
},
XValues: s1x,
YValues: s1y,
},
chart.ContinuousSeries{
Name: "b",
XValues: []float64{3.0, 4.0, 5.0, 6.0},
YValues: []float64{6.0, 5.0, 4.0, 1.0},
XValues: s2x,
YValues: s2y,
},
chart.AnnotationSeries{
Name: "a - last value",
Style: chart.Style{
Show: true,
StrokeColor: chart.GetDefaultSeriesStrokeColor(0),
},
Annotations: []chart.Annotation{
chart.Annotation{
X: s1x[len(s1x)-1],
Y: s1y[len(s1y)-1],
Label: chart.FloatValueFormatter(s1y[len(s1y)-1]),
},
},
},
chart.AnnotationSeries{
Name: "b - last value",
Style: chart.Style{
Show: true,
StrokeColor: chart.GetDefaultSeriesStrokeColor(1),
},
Annotations: []chart.Annotation{
chart.Annotation{
X: s2x[len(s2x)-1],
Y: s2y[len(s2y)-1],
Label: chart.FloatValueFormatter(s2y[len(s2y)-1]),
},
},
},
},
}
@ -74,6 +108,9 @@ func main() {
app.SetName("Chart Test Server")
app.SetLogger(web.NewStandardOutputLogger())
app.GET("/", chartHandler)
app.GET("/:format", chartHandler)
app.GET("/format/:format", chartHandler)
app.GET("/favico.ico", func(rc *web.RequestContext) web.ControllerResult {
return rc.Raw([]byte{})
})
log.Fatal(app.Start())
}

View File

@ -28,11 +28,6 @@ func (ts TimeSeries) Len() int {
return len(ts.XValues)
}
// GetYAxis returns which YAxis the series draws on.
func (ts TimeSeries) GetYAxis() YAxisType {
return ts.YAxis
}
// GetValue gets a value at a given index.
func (ts TimeSeries) GetValue(index int) (x float64, y float64) {
x = float64(ts.XValues[index].Unix())
@ -40,6 +35,18 @@ func (ts TimeSeries) GetValue(index int) (x float64, y float64) {
return
}
// GetValueFormatters returns value formatter defaults for the series.
func (ts TimeSeries) GetValueFormatters() (x, y ValueFormatter) {
x = TimeValueFormatter
y = FloatValueFormatter
return
}
// GetYAxis returns which YAxis the series draws on.
func (ts TimeSeries) GetYAxis() YAxisType {
return ts.YAxis
}
// Render renders the series.
func (ts TimeSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
style := ts.Style.WithDefaultsFrom(defaults)

View File

@ -3,8 +3,6 @@ package chart
import (
"fmt"
"time"
"github.com/blendlabs/go-util"
)
// ValueFormatter is a function that takes a value and produces a string.
@ -26,7 +24,7 @@ func TimeValueFormatterWithFormat(v interface{}, dateFormat string) string {
if typed, isTyped := v.(float64); isTyped {
return time.Unix(int64(typed), 0).Format(dateFormat)
}
return util.StringEmpty
return ""
}
// FloatValueFormatter is a ValueFormatter for float64.
@ -39,5 +37,5 @@ func FloatValueFormatterWithFormat(v interface{}, floatFormat string) string {
if typed, isTyped := v.(float64); isTyped {
return fmt.Sprintf(floatFormat, typed)
}
return util.StringEmpty
return ""
}

View File

@ -0,0 +1,6 @@
package chart
// ValueFormatterProvider is a series that has custom formatters.
type ValueFormatterProvider interface {
GetValueFormatters() (x, y ValueFormatter)
}

View File

@ -1,27 +1,101 @@
package chart
import "github.com/wcharczuk/go-chart/drawing"
import (
"math"
"sort"
"github.com/wcharczuk/go-chart/drawing"
)
// XAxis represents the horizontal axis.
type XAxis struct {
axis
Name string
Style Style
ValueFormatter ValueFormatter
Range Range
Ticks []Tick
}
// GetName returns the name.
func (xa XAxis) GetName() string {
return xa.Name
}
// GetStyle returns the style.
func (xa XAxis) GetStyle() Style {
return xa.Style
}
// GetTicks returns the ticks for a series. It coalesces between user provided ticks and
// generated ticks.
func (xa XAxis) GetTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
if len(xa.Ticks) > 0 {
return xa.Ticks
}
return xa.generateTicks(r, ra, vf)
}
func (xa XAxis) generateTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
step := xa.getTickStep(r, ra, vf)
return xa.generateTicksWithStep(ra, step, vf)
}
func (xa XAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int {
fontSize := xa.Style.GetFontSize(DefaultFontSize)
r.SetFontSize(fontSize)
// take a cut at determining the 'widest' value.
l0 := vf(ra.Min)
ln := vf(ra.Max)
ll := l0
if len(ln) > len(l0) {
ll = ln
}
llw, _ := r.MeasureText(ll)
textWidth := drawing.PointsToPixels(r.GetDPI(), float64(llw))
width := textWidth + DefaultMinimumTickHorizontalSpacing
count := int(math.Ceil(float64(ra.Domain) / float64(width)))
return count
}
func (xa XAxis) getTickStep(r Renderer, ra Range, vf ValueFormatter) float64 {
tickCount := xa.getTickCount(r, ra, vf)
step := ra.Delta() / float64(tickCount)
return step
}
func (xa XAxis) generateTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick {
var ticks []Tick
for cursor := ra.Min; cursor < ra.Max; cursor += step {
ticks = append(ticks, Tick{
Value: cursor,
Label: vf(cursor),
})
}
return ticks
}
// Render renders the axis
func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range) {
func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, ticks []Tick) {
tickFontSize := xa.Style.GetFontSize(DefaultFontSize)
tickHeight := drawing.PointsToPixels(r.GetDPI(), tickFontSize)
ty := canvasBox.Bottom + DefaultXAxisMargin + int(tickHeight)
r.SetStrokeColor(xa.Style.GetStrokeColor(DefaultAxisColor))
r.SetStrokeWidth(xa.Style.GetStrokeWidth(DefaultAxisLineWidth))
r.MoveTo(canvasBox.Left, canvasBox.Bottom)
r.LineTo(canvasBox.Right, canvasBox.Bottom)
r.Stroke()
r.SetFontColor(xa.Style.GetFontColor(DefaultAxisColor))
r.SetFontSize(tickFontSize)
ticks := xa.getTicks(ra)
sort.Sort(Ticks(ticks))
for _, t := range ticks {
v := t.Value
x := ra.Translate(v)
tx := canvasBox.Left + int(x)
tx := canvasBox.Right - x
r.Text(t.Label, tx, ty)
}
}

View File

@ -1,28 +1,102 @@
package chart
import (
"math"
"sort"
)
// YAxis is a veritcal rule of the range.
// There can be (2) y-axes; a primary and secondary.
type YAxis struct {
axis
Name string
Style Style
ValueFormatter ValueFormatter
Range Range
Ticks []Tick
}
// GetName returns the name.
func (ya YAxis) GetName() string {
return ya.Name
}
// GetStyle returns the style.
func (ya YAxis) GetStyle() Style {
return ya.Style
}
// GetTicks returns the ticks for a series. It coalesces between user provided ticks and
// generated ticks.
func (ya YAxis) GetTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
if len(ya.Ticks) > 0 {
return ya.Ticks
}
return ya.generateTicks(r, ra, vf)
}
func (ya YAxis) generateTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
step := ya.getTickStep(r, ra, vf)
return ya.generateTicksWithStep(ra, step, vf)
}
func (ya YAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int {
textHeight := int(ya.Style.GetFontSize(DefaultFontSize))
height := textHeight + DefaultMinimumTickVerticalSpacing
count := int(math.Ceil(float64(ra.Domain) / float64(height)))
return count
}
func (ya YAxis) getTickStep(r Renderer, ra Range, vf ValueFormatter) float64 {
tickCount := ya.getTickCount(r, ra, vf)
return ra.Delta() / float64(tickCount)
}
func (ya YAxis) generateTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick {
var ticks []Tick
for cursor := ra.Min; cursor < ra.Max; cursor += step {
ticks = append(ticks, Tick{
Value: cursor,
Label: vf(cursor),
})
}
return ticks
}
// Render renders the axis.
func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, axisType YAxisType) {
func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, axisType YAxisType, ticks []Tick) {
var lx int
var tx int
if axisType == YAxisPrimary {
lx = canvasBox.Right
tx = canvasBox.Right + DefaultYAxisMargin
} else if axisType == YAxisSecondary {
lx = canvasBox.Left
tx = canvasBox.Left - DefaultYAxisMargin
}
r.SetFontColor(ya.Style.GetFontColor(DefaultAxisColor))
r.SetFontSize(ya.Style.GetFontSize(DefaultFontSize))
r.SetStrokeColor(ya.Style.GetStrokeColor(DefaultAxisColor))
r.SetStrokeWidth(ya.Style.GetStrokeWidth(DefaultAxisLineWidth))
ticks := ya.getTicks(ra)
r.MoveTo(lx, canvasBox.Bottom)
r.LineTo(lx, canvasBox.Top)
r.Stroke()
r.SetFontColor(ya.Style.GetFontColor(DefaultAxisColor))
fontSize := ya.Style.GetFontSize(DefaultFontSize)
r.SetFontSize(fontSize)
sort.Sort(Ticks(ticks))
for _, t := range ticks {
v := t.Value
y := ra.Translate(v)
ty := int(y)
ly := ra.Translate(v) + canvasBox.Top
th := int(fontSize) >> 1
ty := ly + th
r.Text(t.Label, tx, ty)
r.MoveTo(lx, ly)
r.LineTo(lx+DefaultVerticalTickWidth, ly)
r.Stroke()
}
}