diff --git a/_examples/request_timings/main.go b/_examples/request_timings/main.go index aed7493..a521b1c 100644 --- a/_examples/request_timings/main.go +++ b/_examples/request_timings/main.go @@ -125,8 +125,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) { graph.Elements = []chart.Renderable{chart.LegendThin(&graph)} - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) + res.Header().Set("Content-Type", chart.ContentTypeSVG) + graph.Render(chart.SVG, res) } func main() { diff --git a/_examples/scatter/main.go b/_examples/scatter/main.go index 33edad9..780c71f 100644 --- a/_examples/scatter/main.go +++ b/_examples/scatter/main.go @@ -24,8 +24,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, } - res.Header().Set("Content-Type", "image/png") - err := graph.Render(chart.PNG, res) + res.Header().Set("Content-Type", chart.ContentTypeSVG) + err := graph.Render(chart.SVG, res) if err != nil { log.Println(err.Error()) } @@ -49,8 +49,8 @@ func unit(res http.ResponseWriter, req *http.Request) { }, } - res.Header().Set("Content-Type", "image/png") - err := graph.Render(chart.PNG, res) + res.Header().Set("Content-Type", chart.ContentTypeSVG) + err := graph.Render(chart.SVG, res) if err != nil { log.Println(err.Error()) } diff --git a/defaults.go b/defaults.go index 13213fb..17e95fa 100644 --- a/defaults.go +++ b/defaults.go @@ -225,3 +225,11 @@ func GetDefaultFont() (*truetype.Font, error) { } return _defaultFont, nil } + +const ( + // ContentTypePNG is the png mime type. + ContentTypePNG = "image/png" + + // ContentTypeSVG is the svg mime type. + ContentTypeSVG = "image/svg+xml" +) diff --git a/vector_renderer.go b/vector_renderer.go index c36f065..e6f060f 100644 --- a/vector_renderer.go +++ b/vector_renderer.go @@ -222,7 +222,7 @@ func (c *canvas) Path(d string, style Style) { if len(style.StrokeDashArray) > 0 { strokeDashArrayProperty = c.getStrokeDashArray(style) } - c.w.Write([]byte(fmt.Sprintf(`\n`, strokeDashArrayProperty, d, c.styleAsSVG(style)))) + c.w.Write([]byte(fmt.Sprintf(``, strokeDashArrayProperty, d, c.styleAsSVG(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) { - c.w.Write([]byte(fmt.Sprintf(``, x, y, r, c.styleAsSVG(style)))) + c.w.Write([]byte(fmt.Sprintf(``, x, y, r, c.styleAsSVG(style)))) } func (c *canvas) End() { @@ -274,30 +274,36 @@ func (c *canvas) styleAsSVG(s Style) string { fs := s.FontSize fnc := s.FontColor - strokeWidthText := "stroke-width:0" + var pieces []string + 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() { - strokeText = "stroke:" + sc.String() + pieces = append(pieces, "stroke:"+sc.String()) + } else { + pieces = append(pieces, "stroke:none") } - fillText := "fill:none" if !fc.IsZero() { - fillText = "fill:" + fc.String() + pieces = append(pieces, "fill:"+fc.String()) + } else { + pieces = append(pieces, "fill:none") } - fontSizeText := "" 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() { - fillText = "fill:" + fnc.String() + pieces = append(pieces, "fill:"+fnc.String()) } - fontText := c.getFontFace(s) - return strings.Join([]string{strokeWidthText, strokeText, fillText, fontSizeText, fontText}, ";") + if s.Font != nil { + pieces = append(pieces, c.getFontFace(s)) + } + return strings.Join(pieces, ";") }