| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"net/http" |
| 7 |
|
| 8 |
"github.com/wcharczuk/go-chart/v2" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 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 |
|
| 22 |
|
| 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 |
} |