fixing svg circles

This commit is contained in:
Will Charczuk 2017-03-05 23:52:13 -08:00
parent 10de6fa9bf
commit 66b99eb8e3
4 changed files with 33 additions and 19 deletions

View File

@ -125,8 +125,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
graph.Elements = []chart.Renderable{chart.LegendThin(&graph)} graph.Elements = []chart.Renderable{chart.LegendThin(&graph)}
res.Header().Set("Content-Type", "image/png") res.Header().Set("Content-Type", chart.ContentTypeSVG)
graph.Render(chart.PNG, res) graph.Render(chart.SVG, res)
} }
func main() { func main() {

View File

@ -24,8 +24,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
}, },
} }
res.Header().Set("Content-Type", "image/png") res.Header().Set("Content-Type", chart.ContentTypeSVG)
err := graph.Render(chart.PNG, res) err := graph.Render(chart.SVG, res)
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
} }
@ -49,8 +49,8 @@ func unit(res http.ResponseWriter, req *http.Request) {
}, },
} }
res.Header().Set("Content-Type", "image/png") res.Header().Set("Content-Type", chart.ContentTypeSVG)
err := graph.Render(chart.PNG, res) err := graph.Render(chart.SVG, res)
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
} }

View File

@ -225,3 +225,11 @@ func GetDefaultFont() (*truetype.Font, error) {
} }
return _defaultFont, nil return _defaultFont, nil
} }
const (
// ContentTypePNG is the png mime type.
ContentTypePNG = "image/png"
// ContentTypeSVG is the svg mime type.
ContentTypeSVG = "image/svg+xml"
)

View File

@ -222,7 +222,7 @@ func (c *canvas) Path(d string, style Style) {
if len(style.StrokeDashArray) > 0 { if len(style.StrokeDashArray) > 0 {
strokeDashArrayProperty = c.getStrokeDashArray(style) strokeDashArrayProperty = c.getStrokeDashArray(style)
} }
c.w.Write([]byte(fmt.Sprintf(`<path %s d="%s" style="%s"/>\n`, strokeDashArrayProperty, d, c.styleAsSVG(style)))) c.w.Write([]byte(fmt.Sprintf(`<path %s d="%s" style="%s"/>`, strokeDashArrayProperty, d, c.styleAsSVG(style))))
} }
func (c *canvas) Text(x, y int, body string, style Style) { func (c *canvas) Text(x, y int, body string, style Style) {
@ -235,7 +235,7 @@ func (c *canvas) Text(x, y int, body string, style Style) {
} }
func (c *canvas) Circle(x, y, r int, style Style) { func (c *canvas) Circle(x, y, r int, style Style) {
c.w.Write([]byte(fmt.Sprintf(`<circle cx="%d" cy="%d" r="%d" style="%s">`, x, y, r, c.styleAsSVG(style)))) c.w.Write([]byte(fmt.Sprintf(`<circle cx="%d" cy="%d" r="%d" style="%s"/>`, x, y, r, c.styleAsSVG(style))))
} }
func (c *canvas) End() { func (c *canvas) End() {
@ -274,30 +274,36 @@ func (c *canvas) styleAsSVG(s Style) string {
fs := s.FontSize fs := s.FontSize
fnc := s.FontColor fnc := s.FontColor
strokeWidthText := "stroke-width:0" var pieces []string
if sw != 0 { if sw != 0 {
strokeWidthText = "stroke-width:" + fmt.Sprintf("%d", int(sw)) pieces = append(pieces, "stroke-width:"+fmt.Sprintf("%d", int(sw)))
} else {
pieces = append(pieces, "stroke-width:0")
} }
strokeText := "stroke:none"
if !sc.IsZero() { if !sc.IsZero() {
strokeText = "stroke:" + sc.String() pieces = append(pieces, "stroke:"+sc.String())
} else {
pieces = append(pieces, "stroke:none")
} }
fillText := "fill:none"
if !fc.IsZero() { if !fc.IsZero() {
fillText = "fill:" + fc.String() pieces = append(pieces, "fill:"+fc.String())
} else {
pieces = append(pieces, "fill:none")
} }
fontSizeText := ""
if fs != 0 { if fs != 0 {
fontSizeText = "font-size:" + fmt.Sprintf("%.1fpx", drawing.PointsToPixels(c.dpi, fs)) pieces = append(pieces, "font-size:"+fmt.Sprintf("%.1fpx", drawing.PointsToPixels(c.dpi, fs)))
} }
if !fnc.IsZero() { if !fnc.IsZero() {
fillText = "fill:" + fnc.String() pieces = append(pieces, "fill:"+fnc.String())
} }
fontText := c.getFontFace(s) if s.Font != nil {
return strings.Join([]string{strokeWidthText, strokeText, fillText, fontSizeText, fontText}, ";") pieces = append(pieces, c.getFontFace(s))
}
return strings.Join(pieces, ";")
} }