-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
125 lines (99 loc) · 2.63 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"context"
"fmt"
"image/color"
"os"
"time"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
"github.com/shal/mono"
)
type day struct {
Expense float64
Revenue float64
}
func transactions(ctx context.Context, token string) []mono.Transaction {
personal := mono.NewPersonal(token)
// Get information about current user.
user, err := personal.User(ctx)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
// Find UAH account.
account := mono.Account{}
for _, acc := range user.Accounts {
ccy, _ := mono.CurrencyFromISO4217(acc.CurrencyCode)
if ccy.Code == "UAH" {
account = acc
}
}
// List all transactions for last month.
transactions, err := personal.Transactions(ctx, account.ID, time.Now().Add(-730*time.Hour), time.Now())
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
return transactions
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
transactions := transactions(ctx, "token")
p, err := plot.New()
if err != nil {
panic(err)
}
p.X.Label.Text = "Time"
p.Y.Label.Text = "UAH"
days := make([]day, 35)
for _, t := range transactions {
if t.Amount < 0 {
days[t.Time.Day()].Expense += float64(-t.Amount/100) + float64(-t.Amount%100)
} else {
days[t.Time.Day()].Revenue += float64(t.Amount/100) + float64(t.Amount%100)
}
}
expenses := make(plotter.XYs, len(days))
revenues := make(plotter.XYs, len(days))
x := 0
for _, v := range days {
expenses[x].X = float64(x)
revenues[x].X = float64(x)
expenses[x].Y = v.Expense
revenues[x].Y = v.Revenue
x += 1
}
expensesPlot, err := plotter.NewLine(expenses)
if err != nil {
panic(err)
}
revenuesPlot, err := plotter.NewLine(revenues)
if err != nil {
panic(err)
}
expensesPlot.LineStyle.Color = color.RGBA{R: 255, A: 255}
revenuesPlot.LineStyle.Color = color.RGBA{G: 100, A: 255}
expensesCircles, err := plotter.NewScatter(expenses)
if err != nil {
panic(err)
}
revenuesCircles, err := plotter.NewScatter(revenues)
if err != nil {
panic(err)
}
expensesPlot.LineStyle.Color = color.RGBA{R: 255, A: 255}
revenuesPlot.LineStyle.Color = color.RGBA{G: 100, A: 255}
expensesCircles.GlyphStyle.Color = color.RGBA{R: 255, A: 255}
revenuesCircles.GlyphStyle.Color = color.RGBA{G: 100, A: 255}
expensesCircles.GlyphStyle.Shape = draw.CircleGlyph{}
revenuesCircles.GlyphStyle.Shape = draw.CircleGlyph{}
p.Add(expensesPlot, revenuesPlot, expensesCircles, revenuesCircles)
// Save the plot to a PNG file.
if err := p.Save(10*vg.Inch, 5*vg.Inch, "report.png"); err != nil {
panic(err)
}
}