you can now fill series.

This commit is contained in:
Will Charczuk 2016-07-09 10:27:47 -07:00
parent 093a50f8b1
commit 0da2b41a9d
4 changed files with 44 additions and 16 deletions

View file

@ -333,24 +333,43 @@ func (c Chart) drawXAxisLabels(r Renderer, canvasBox Box, xrange Range) {
} }
func (c Chart) drawSeries(r Renderer, canvasBox Box, index int, s Series, xrange, yrange Range) { func (c Chart) drawSeries(r Renderer, canvasBox Box, index int, s Series, xrange, yrange Range) {
r.SetStrokeColor(s.GetStyle().GetStrokeColor(GetDefaultSeriesStrokeColor(index)))
r.SetStrokeWidth(s.GetStyle().GetStrokeWidth(DefaultStrokeWidth))
if s.Len() == 0 { if s.Len() == 0 {
return return
} }
cx := canvasBox.Left cx := canvasBox.Left
cy := canvasBox.Top cy := canvasBox.Top
cb := canvasBox.Bottom
cw := canvasBox.Width cw := canvasBox.Width
v0x, v0y := s.GetValue(0) v0x, v0y := s.GetValue(0)
x0 := cw - xrange.Translate(v0x) x0 := cw - xrange.Translate(v0x)
y0 := yrange.Translate(v0y) y0 := yrange.Translate(v0y)
r.MoveTo(x0+cx, y0+cy)
var vx, vy float64 var vx, vy float64
var x, y int var x, y int
fill := s.GetStyle().GetFillColor()
if !ColorIsZero(fill) {
r.SetFillColor(fill)
r.MoveTo(x0+cx, y0+cy)
for i := 1; i < s.Len(); i++ {
vx, vy = s.GetValue(i)
x = cw - xrange.Translate(vx)
y = yrange.Translate(vy)
r.LineTo(x+cx, y+cy)
}
r.LineTo(x+cx, cb)
r.LineTo(x0+cx, cb)
r.Close()
r.Fill()
}
stroke := s.GetStyle().GetStrokeColor(GetDefaultSeriesStrokeColor(index))
r.SetStrokeColor(stroke)
r.SetStrokeWidth(s.GetStyle().GetStrokeWidth(DefaultStrokeWidth))
r.MoveTo(x0+cx, y0+cy)
for i := 1; i < s.Len(); i++ { for i := 1; i < s.Len(); i++ {
vx, vy = s.GetValue(i) vx, vy = s.GetValue(i)
x = cw - xrange.Translate(vx) x = cw - xrange.Translate(vx)

View file

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"image/color" "image/color"
"strings" "strings"
"github.com/wcharczuk/go-chart/drawing"
) )
// Style is a simple style set. // Style is a simple style set.
@ -28,7 +30,7 @@ func (s Style) GetStrokeColor(defaults ...color.RGBA) color.RGBA {
if len(defaults) > 0 { if len(defaults) > 0 {
return defaults[0] return defaults[0]
} }
return DefaultStrokeColor return color.RGBA{}
} }
return s.StrokeColor return s.StrokeColor
} }
@ -39,7 +41,7 @@ func (s Style) GetFillColor(defaults ...color.RGBA) color.RGBA {
if len(defaults) > 0 { if len(defaults) > 0 {
return defaults[0] return defaults[0]
} }
return DefaultFillColor return color.RGBA{}
} }
return s.FillColor return s.FillColor
} }
@ -72,13 +74,13 @@ func (s Style) GetFontColor(defaults ...color.RGBA) color.RGBA {
if len(defaults) > 0 { if len(defaults) > 0 {
return defaults[0] return defaults[0]
} }
return DefaultTextColor return color.RGBA{}
} }
return s.FontColor return s.FontColor
} }
// SVG returns the style as a svg style string. // SVG returns the style as a svg style string.
func (s Style) SVG() string { func (s Style) SVG(dpi float64) string {
sw := s.StrokeWidth sw := s.StrokeWidth
sc := s.StrokeColor sc := s.StrokeColor
fc := s.FillColor fc := s.FillColor
@ -102,7 +104,7 @@ func (s Style) SVG() string {
fontSizeText := "" fontSizeText := ""
if fs != 0 { if fs != 0 {
fontSizeText = "font-size:" + fmt.Sprintf("%.1fpx", fs) fontSizeText = "font-size:" + fmt.Sprintf("%.1fpx", drawing.PointsToPixels(dpi, fs))
} }
if !ColorIsZero(fnc) { if !ColorIsZero(fnc) {

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"image/color"
"log" "log"
"net/http" "net/http"
@ -23,7 +24,8 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
c := chart.Chart{ c := chart.Chart{
Title: "A Test Chart", Title: "A Test Chart",
TitleStyle: chart.Style{ TitleStyle: chart.Style{
Show: true, Show: true,
FontSize: 26.0,
}, },
Width: 640, Width: 640,
Height: 480, Height: 480,
@ -43,6 +45,9 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
Name: "a", Name: "a",
XValues: []float64{1.0, 2.0, 3.0, 4.0}, XValues: []float64{1.0, 2.0, 3.0, 4.0},
YValues: []float64{2.5, 5.0, 2.0, 3.3}, YValues: []float64{2.5, 5.0, 2.0, 3.3},
Style: chart.Style{
FillColor: color.RGBA{R: 0, G: 116, B: 217, A: 255},
},
}, },
chart.ContinuousSeries{ chart.ContinuousSeries{
Name: "b", Name: "b",

View file

@ -5,12 +5,12 @@ import (
"fmt" "fmt"
"image/color" "image/color"
"io" "io"
"math"
"strings" "strings"
"golang.org/x/image/font" "golang.org/x/image/font"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/drawing"
) )
// SVG returns a new png/raster renderer. // SVG returns a new png/raster renderer.
@ -93,13 +93,13 @@ func (vr *vectorRenderer) FillStroke() {
} }
func (vr *vectorRenderer) drawPath() { func (vr *vectorRenderer) drawPath() {
vr.c.Path(strings.Join(vr.p, "\n"), vr.s.SVG()) vr.c.Path(strings.Join(vr.p, "\n"), vr.s.SVG(vr.dpi))
vr.p = []string{} vr.p = []string{}
} }
// Circle implements the interface method. // Circle implements the interface method.
func (vr *vectorRenderer) Circle(radius float64, x, y int) { func (vr *vectorRenderer) Circle(radius float64, x, y int) {
vr.c.Circle(x, y, int(radius), vr.s.SVG()) vr.c.Circle(x, y, int(radius), vr.s.SVG(vr.dpi))
} }
// SetFont implements the interface method. // SetFont implements the interface method.
@ -133,7 +133,7 @@ func (vr *vectorRenderer) Text(body string, x, y int) {
vr.s.FillColor = color.RGBA{} vr.s.FillColor = color.RGBA{}
vr.s.StrokeColor = color.RGBA{} vr.s.StrokeColor = color.RGBA{}
vr.s.StrokeWidth = 0 vr.s.StrokeWidth = 0
vr.c.Text(x, y, body, vr.s.SVG()+";"+vr.svgFontFace()) vr.c.Text(x, y, body, vr.s.SVG(vr.dpi)+";"+vr.svgFontFace())
} }
// MeasureText uses the truetype font drawer to measure the width of text. // MeasureText uses the truetype font drawer to measure the width of text.
@ -145,8 +145,10 @@ func (vr *vectorRenderer) MeasureText(body string) (width, height int) {
Size: vr.s.FontSize, Size: vr.s.FontSize,
}), }),
} }
width = vr.fc.MeasureString(body).Ceil() w := vr.fc.MeasureString(body).Ceil()
height = int(math.Ceil(vr.s.FontSize))
width = int(drawing.PointsToPixels(vr.dpi, float64(w)))
height = int(drawing.PointsToPixels(vr.dpi, vr.s.FontSize))
} }
return return
} }