-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
65 lines (57 loc) · 1.42 KB
/
main.go
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
63
64
65
package main
import (
"os"
"path/filepath"
"github.com/go-analyze/charts"
)
/*
Example of a "Stacked" horizontal bar chart. Stacked charts are a good way to represent data where the sum is important,
and you want to show what components produce that sum.
*/
func writeFile(buf []byte) error {
tmpPath := "./tmp"
if err := os.MkdirAll(tmpPath, 0700); err != nil {
return err
}
file := filepath.Join(tmpPath, "horizontal-bar-chart-4-stacked.png")
return os.WriteFile(file, buf, 0600)
}
func main() {
values := [][]float64{
{10, 30, 50, 70, 90, 110, 130},
{20, 40, 60, 80, 100, 120, 140},
}
opt := charts.NewHorizontalBarChartOptionWithData(values)
opt.Title.Text = "Some Numbers"
opt.Padding = charts.Box{
Top: 20,
Right: 20,
Bottom: 0,
Left: 20,
}
opt.StackSeries = charts.Ptr(true)
for i := range opt.SeriesList {
opt.SeriesList[i].Label.Show = charts.Ptr(true)
}
opt.Legend.SeriesNames = []string{
"2011", "2012",
}
opt.XAxis.Show = charts.Ptr(false)
opt.YAxis = charts.YAxisOption{
Labels: []string{
"UN", "Brazil", "Indonesia", "USA", "India", "China", "World",
},
}
p := charts.NewPainter(charts.PainterOptions{
OutputFormat: charts.ChartOutputPNG,
Width: 600,
Height: 400,
})
if err := p.HorizontalBarChart(opt); err != nil {
panic(err)
} else if buf, err := p.Bytes(); err != nil {
panic(err)
} else if err = writeFile(buf); err != nil {
panic(err)
}
}