-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
77 lines (68 loc) · 2.06 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
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"os"
"path/filepath"
"github.com/go-analyze/charts"
)
/*
Example of building a painter to write multiple charts on the same image.
*/
func writeFile(buf []byte) error {
tmpPath := "./tmp"
if err := os.MkdirAll(tmpPath, 0700); err != nil {
return err
}
file := filepath.Join(tmpPath, "multiple-charts-1.png")
return os.WriteFile(file, buf, 0600)
}
func main() {
p := charts.NewPainter(charts.PainterOptions{
OutputFormat: charts.ChartOutputPNG,
Width: 800,
Height: 600,
})
p.FilledRect(0, 0, 800, 600, charts.ColorWhite, charts.ColorWhite, 0.0)
// set the space and theme for each chart
topCenterPainter := p.Child(charts.PainterBoxOption(charts.NewBox(0, 300, 0, 800)),
charts.PainterThemeOption(charts.GetTheme(charts.ThemeVividLight)))
bottomLeftPainter := p.Child(charts.PainterBoxOption(charts.NewBox(300, 600, 0, 400)),
charts.PainterThemeOption(charts.GetTheme(charts.ThemeAnt)))
bottomRightPainter := p.Child(charts.PainterBoxOption(charts.NewBox(300, 600, 400, 800)),
charts.PainterThemeOption(charts.GetTheme(charts.ThemeLight)))
lineOpt := charts.LineChartOption{
Padding: charts.NewBoxEqual(10),
XAxis: charts.XAxisOption{
Data: []string{
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
},
LabelCount: 7,
},
Legend: charts.LegendOption{
Data: []string{
"Email", "Union Ads", "Video Ads", "Direct", "Search Engine",
},
},
SeriesList: charts.NewSeriesListLine([][]float64{
{120, 132, 101, 134, 90, 230, 210},
{220, 182, 191, 234, 290, 330, 310},
{150, 232, 201, 154, 190, 330, 410},
{320, 332, 301, 334, 390, 330, 320},
{820, 932, 901, 934, 1290, 1330, 1320},
}),
}
// render the same chart in each spot for the demo
if err := bottomLeftPainter.LineChart(lineOpt); err != nil {
panic(err)
}
if err := bottomRightPainter.LineChart(lineOpt); err != nil {
panic(err)
}
if err := topCenterPainter.LineChart(lineOpt); err != nil {
panic(err)
}
if buf, err := p.Bytes(); err != nil {
panic(err)
} else if err = writeFile(buf); err != nil {
panic(err)
}
}