-
Notifications
You must be signed in to change notification settings - Fork 4
/
histogram_sets_table.go
161 lines (144 loc) · 5.41 KB
/
histogram_sets_table.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package histogram
import (
"strconv"
"strings"
"github.com/grokify/mogo/strconv/strconvutil"
"github.com/grokify/mogo/type/stringsutil"
"github.com/grokify/gocharts/v2/data/table"
)
func (hsets *HistogramSets) Table(tableName, colNameHSet, colNameHist, colNameBinName, colNameBinCount string) table.Table {
tbl := table.NewTable(tableName)
tbl.Columns = []string{
stringsutil.FirstNonEmpty(colNameHSet, "Histogram Set"),
stringsutil.FirstNonEmpty(colNameHist, "Histogram"),
stringsutil.FirstNonEmpty(colNameBinName, "Bin Name"),
stringsutil.FirstNonEmpty(colNameBinCount, "Bin Count")}
hsets.Visit(func(hsetName, histName, binName string, binCount int) {
tbl.Rows = append(tbl.Rows, []string{
hsetName, histName, binName, strconv.Itoa(binCount)})
})
return tbl
}
type TablePivotOpts struct {
TableName string
ColNameHistogramSet string
ColNameHistogram string
ColNameBinPrefix string
ColNameBinSuffix string
ColNameBinCountsSum string
BinNamesOrder []string
InclBinsUnordered bool
InclBinCounts bool
InclBinCountsSum bool
InclBinPercentages bool
}
func (opts TablePivotOpts) ColNameHistogramSetOrDefault() string {
return stringsutil.FirstNonEmpty(opts.ColNameHistogramSet, "Histogram Set")
}
func (opts TablePivotOpts) ColNameHistogramOrDefault() string {
return stringsutil.FirstNonEmpty(opts.ColNameHistogramSet, "Histogram")
}
func (opts TablePivotOpts) ColNameBinCountsSumOrDefault() string {
return stringsutil.FirstNonEmpty(opts.ColNameHistogramSet, "Total")
}
func (opts TablePivotOpts) InflateBinName(binName string, binNumber int, isPct bool) string {
if strings.TrimSpace(binName) == "" {
binName = "Unnamed Bin"
if binNumber >= 0 {
binName += " " + strconv.Itoa(binNumber)
}
}
return opts.ColNameBinPrefix + binName + opts.ColNameBinSuffix
}
func (opts TablePivotOpts) TableColumns(binNames []string) ([]string, map[int]string) {
cols := []string{
opts.ColNameHistogramSetOrDefault(),
opts.ColNameHistogramOrDefault()}
fmtMap := map[int]string{}
if opts.InclBinCounts {
for i, binName := range binNames {
cols = append(cols, opts.InflateBinName(binName, i+1, false))
}
}
if opts.InclBinCountsSum {
cols = append(cols, opts.ColNameBinCountsSumOrDefault())
}
if opts.InclBinPercentages {
for i, binName := range binNames {
cols = append(cols, opts.InflateBinName(binName, i+1, true))
fmtMap[len(cols)-1] = table.FormatFloat
}
}
return cols, fmtMap
}
// TablePivot returns a `*table.Table` where the first column is the histogram
// set name, the second column is the histogram name and the other columns are
// the bin names.
func (hsets *HistogramSets) TablePivot(opts TablePivotOpts) table.Table {
// func (hsets *HistogramSets) TablePivot(tableName, colNameHSet, colNameHist, colNameBinNamePrefix, colNameBinNameSuffix string, binNamesOrder []string, binsInclUnordered bool, inclPercentages bool) table.Table {
tbl := table.NewTable(opts.TableName)
tbl.FormatMap = map[int]string{
-1: table.FormatInt,
0: table.FormatString,
1: table.FormatString}
binNames := table.Columns(hsets.BinNames())
if len(opts.BinNamesOrder) > 0 {
binNamesOrdered, _ := stringsutil.SliceOrderExplicit(binNames, opts.BinNamesOrder, opts.InclBinsUnordered)
binNames = binNamesOrdered
}
tblCols, fmtMap := opts.TableColumns(binNames)
tbl.Columns = tblCols
for k, v := range fmtMap {
tbl.FormatMap[k] = v
}
for hsetName, hset := range hsets.HistogramSetMap {
for histName, hist := range hset.HistogramMap {
row := []string{hsetName, histName}
histSum := 0
for _, binName := range binNames {
if binCount, ok := hist.Bins[binName]; ok {
if opts.InclBinCounts {
row = append(row, strconv.Itoa(binCount))
histSum += binCount
}
} else {
if opts.InclBinCounts {
row = append(row, "0")
}
}
}
if opts.InclBinCountsSum {
row = append(row, strconv.Itoa(histSum))
}
if opts.InclBinPercentages {
for _, binName := range binNames {
if histSum == 0 {
row = append(row, "0")
} else if binCount, ok := hist.Bins[binName]; ok && binCount != 0 {
row = append(row, strconvutil.Ftoa(float64(binCount)/float64(histSum)))
} else {
row = append(row, "0")
}
}
}
tbl.Rows = append(tbl.Rows, row)
}
}
return tbl
}
func (hsets *HistogramSets) WriteXLSX(filename, sheetname, colNameHSet, colNameHist, colNameBinName, colNameBinCount string) error {
tbl := hsets.Table(sheetname, colNameHSet, colNameHist, colNameBinName, colNameBinCount)
return tbl.WriteXLSX(filename, sheetname)
}
/*
func (hsets *HistogramSets) WriteXLSXPivot(filename, sheetname, colNameHSet, colNameHist, colNameBinNamePrefix, colNameBinNameSuffix string, binNamesOrder []string, binsInclUnordered bool) error {
tbl := hsets.TablePivot(sheetname, colNameHSet, colNameHist, colNameBinNamePrefix, colNameBinNameSuffix, binNamesOrder, binsInclUnordered)
return tbl.WriteXLSX(filename, sheetname)
}
*/
func (hsets *HistogramSets) WriteXLSXPivot(filename string, opts TablePivotOpts) error {
// func (hsets *HistogramSets) WriteXLSXPivot(filename, sheetname, colNameHSet, colNameHist, colNameBinNamePrefix, colNameBinNameSuffix string, binNamesOrder []string, binsInclUnordered bool) error {
//tbl := hsets.TablePivot(sheetname, colNameHSet, colNameHist, colNameBinNamePrefix, colNameBinNameSuffix, binNamesOrder, binsInclUnordered)
tbl := hsets.TablePivot(opts)
return tbl.WriteXLSX(filename, opts.TableName)
}