-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.go
More file actions
62 lines (54 loc) · 1.38 KB
/
Copy pathmain.go
File metadata and controls
62 lines (54 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"os"
"path/filepath"
"github.com/go-analyze/charts"
)
/*
Example pie chart with a variety of basic configuration options shown using the Painter API.
*/
func writeFile(buf []byte) error {
tmpPath := "./tmp"
if err := os.MkdirAll(tmpPath, 0700); err != nil {
return err
}
file := filepath.Join(tmpPath, "pie-chart-1-basic.png")
return os.WriteFile(file, buf, 0600)
}
func main() {
values := []float64{
1048, 735, 580, 484, 300,
}
opt := charts.NewPieChartOptionWithData(values)
opt.Title = charts.TitleOption{
Text: "Pie Chart",
Subtext: "(Fake Data)",
Offset: charts.OffsetCenter,
FontStyle: charts.NewFontStyleWithSize(16),
SubtextFontStyle: charts.NewFontStyleWithSize(10),
}
opt.Padding = charts.NewBoxEqual(20)
opt.Legend = charts.LegendOption{
SeriesNames: []string{
"Search Engine", "Direct", "Email", "Union Ads", "Video Ads",
},
Vertical: charts.Ptr(true),
Offset: charts.OffsetStr{
Left: "80%",
Top: charts.PositionBottom,
},
FontStyle: charts.NewFontStyleWithSize(10),
}
p := charts.NewPainter(charts.PainterOptions{
OutputFormat: charts.ChartOutputPNG,
Width: 600,
Height: 400,
})
if err := p.PieChart(opt); err != nil {
panic(err)
} else if buf, err := p.Bytes(); err != nil {
panic(err)
} else if err = writeFile(buf); err != nil {
panic(err)
}
}