Debian Codegraph @

examples/css_classes/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "fmt"
5 "log"
6 "net/http"
7
8 "github.com/wcharczuk/go-chart/v2"
9 )
10
11 // Note: Additional examples on how to add Stylesheets are in the custom_stylesheets example
12
13 func inlineSVGWithClasses(res http.ResponseWriter, req *http.Request) {
14 res.Write([]byte(
15 "<!DOCTYPE html><html><head>" +
16 "<link rel=\"stylesheet\" type=\"text/css\" href=\"/main.css\">" +
17 "</head>" +
18 "<body>"))
19
20 pie := chart.PieChart{
21 // Notes: * Setting ClassName will cause all other inline styles to be dropped!
22 // * The following type classes may be added additionally: stroke, fill, text
23 Background: chart.Style{ClassName: "background"},
24 Canvas: chart.Style{
25 ClassName: "canvas",
26 },
27 Width: 512,
28 Height: 512,
29 Values: []chart.Value{
30 {Value: 5, Label: "Blue", Style: chart.Style{ClassName: "blue"}},
31 {Value: 5, Label: "Green", Style: chart.Style{ClassName: "green"}},
32 {Value: 4, Label: "Gray", Style: chart.Style{ClassName: "gray"}},
33 },
34 }
35
36 err := pie.Render(chart.SVG, res)
37 if err != nil {
38 fmt.Printf("Error rendering pie chart: %v\n", err)
39 }
40 res.Write([]byte("</body>"))
41 }
42
43 func css(res http.ResponseWriter, req *http.Request) {
44 res.Header().Set("Content-Type", "text/css")
45 res.Write([]byte("svg .background { fill: white; }" +
46 "svg .canvas { fill: white; }" +
47 "svg .blue.fill.stroke { fill: blue; stroke: lightblue; }" +
48 "svg .green.fill.stroke { fill: green; stroke: lightgreen; }" +
49 "svg .gray.fill.stroke { fill: gray; stroke: lightgray; }" +
50 "svg .blue.text { fill: white; }" +
51 "svg .green.text { fill: white; }" +
52 "svg .gray.text { fill: white; }"))
53 }
54
55 func main() {
56 http.HandleFunc("/", inlineSVGWithClasses)
57 http.HandleFunc("/main.css", css)
58 log.Fatal(http.ListenAndServe(":8080", nil))
59 }