| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"net/http" |
| 7 |
|
| 8 |
"github.com/wcharczuk/go-chart/v2" |
| 9 |
) |
| 10 |
|
| 11 |
const style = "svg .background { fill: white; }" + |
| 12 |
"svg .canvas { fill: white; }" + |
| 13 |
"svg .blue.fill.stroke { fill: blue; stroke: lightblue; }" + |
| 14 |
"svg .green.fill.stroke { fill: green; stroke: lightgreen; }" + |
| 15 |
"svg .gray.fill.stroke { fill: gray; stroke: lightgray; }" + |
| 16 |
"svg .blue.text { fill: white; }" + |
| 17 |
"svg .green.text { fill: white; }" + |
| 18 |
"svg .gray.text { fill: white; }" |
| 19 |
|
| 20 |
func svgWithCustomInlineCSS(res http.ResponseWriter, _ *http.Request) { |
| 21 |
res.Header().Set("Content-Type", chart.ContentTypeSVG) |
| 22 |
|
| 23 |
|
| 24 |
err := pieChart().Render(chart.SVGWithCSS(style, ""), res) |
| 25 |
if err != nil { |
| 26 |
fmt.Printf("Error rendering pie chart: %v\n", err) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func svgWithCustomInlineCSSNonce(res http.ResponseWriter, _ *http.Request) { |
| 31 |
|
| 32 |
|
| 33 |
const nonce = "RAND0MBASE64" |
| 34 |
|
| 35 |
res.Header().Set("Content-Security-Policy", fmt.Sprintf("style-src 'nonce-%s'", nonce)) |
| 36 |
res.Header().Set("Content-Type", chart.ContentTypeSVG) |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
err := pieChart().Render(chart.SVGWithCSS(style, nonce), res) |
| 41 |
if err != nil { |
| 42 |
fmt.Printf("Error rendering pie chart: %v\n", err) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func svgWithCustomExternalCSS(res http.ResponseWriter, _ *http.Request) { |
| 47 |
|
| 48 |
res.Write([]byte( |
| 49 |
`<?xml version="1.0" standalone="no"?>` + |
| 50 |
`<?xml-stylesheet href="/main.css" type="text/css"?>` + |
| 51 |
`<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">`)) |
| 52 |
|
| 53 |
res.Header().Set("Content-Type", chart.ContentTypeSVG) |
| 54 |
err := pieChart().Render(chart.SVG, res) |
| 55 |
if err != nil { |
| 56 |
fmt.Printf("Error rendering pie chart: %v\n", err) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func pieChart() chart.PieChart { |
| 61 |
return chart.PieChart{ |
| 62 |
|
| 63 |
Background: chart.Style{ClassName: "background"}, |
| 64 |
Canvas: chart.Style{ |
| 65 |
ClassName: "canvas", |
| 66 |
}, |
| 67 |
Width: 512, |
| 68 |
Height: 512, |
| 69 |
Values: []chart.Value{ |
| 70 |
{Value: 5, Label: "Blue", Style: chart.Style{ClassName: "blue"}}, |
| 71 |
{Value: 5, Label: "Green", Style: chart.Style{ClassName: "green"}}, |
| 72 |
{Value: 4, Label: "Gray", Style: chart.Style{ClassName: "gray"}}, |
| 73 |
}, |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func css(res http.ResponseWriter, req *http.Request) { |
| 78 |
res.Header().Set("Content-Type", "text/css") |
| 79 |
res.Write([]byte(style)) |
| 80 |
} |
| 81 |
|
| 82 |
func main() { |
| 83 |
http.HandleFunc("/", svgWithCustomInlineCSS) |
| 84 |
http.HandleFunc("/nonce", svgWithCustomInlineCSSNonce) |
| 85 |
http.HandleFunc("/external", svgWithCustomExternalCSS) |
| 86 |
http.HandleFunc("/main.css", css) |
| 87 |
log.Fatal(http.ListenAndServe(":8080", nil)) |
| 88 |
} |