-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwrite_data.go
64 lines (58 loc) · 1.65 KB
/
write_data.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
package collections
import (
"strings"
"github.com/grokify/gocharts/v2/charts/wchart/sts2wchart"
"github.com/grokify/gocharts/v2/data/timeseries"
"github.com/grokify/gocharts/v2/data/yahoohistorical"
"github.com/grokify/mogo/fmt/fmtutil"
"github.com/grokify/mogo/math/accounting"
"github.com/grokify/mogo/time/timeutil"
)
func WriteFilesHistoricalData(filePrefix string, hd *yahoohistorical.HistoricalData, verbose bool) error {
if len(strings.TrimSpace(filePrefix)) == 0 {
filePrefix = "_data"
}
tbl := hd.Table
if verbose {
fmtutil.MustPrintJSON(tbl.Rows)
fmtutil.MustPrintJSON(tbl.Columns)
}
ts, err := hd.CloseTimeSeries(timeutil.IntervalMonth)
if err != nil {
return err
}
if verbose {
fmtutil.MustPrintJSON(ts)
}
return WriteFilesTimeSeries(filePrefix, ts, verbose)
}
func WriteFilesTimeSeries(filePrefix string, ts timeseries.TimeSeries, verbose bool) error {
if len(strings.TrimSpace(filePrefix)) == 0 {
filePrefix = "_data"
}
tblXox := ts.TableMonthXOX("Jan 2006", "", "", "", "", "",
×eries.TableMonthXOXOpts{
AddMOMGrowth: true,
MOMGrowthPct: accounting.AnnualToMonthly(.3),
MOMBaseMonth: timeutil.MustParse(timeutil.RFC3339FullDate, "2021-12-01"),
})
if verbose {
fmtutil.MustPrintJSON(tblXox.Rows)
fmtutil.MustPrintJSON(tblXox.Columns)
}
err := tblXox.WriteXLSX(filePrefix+".xlsx", "data")
if err != nil {
return err
}
err = tblXox.WriteCSV(filePrefix + ".csv")
if err != nil {
return err
}
opts := sts2wchart.DefaultLineChartOpts()
opts.XAxisTickInterval = timeutil.IntervalYear
err = sts2wchart.WriteLineChartTimeSeries(filePrefix+".png", ts, opts)
if err != nil {
return err
}
return nil
}