Skip to content

Commit

Permalink
enhance: data/statictimeseries: add Table.FormatMap to `DataSeriesS…
Browse files Browse the repository at this point in the history
…et.ToTable()`
  • Loading branch information
grokify committed Jun 14, 2021
1 parent 8dfa439 commit 3563447
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
31 changes: 25 additions & 6 deletions data/statictimeseries/data_series_set_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"sort"
"strconv"
"strings"
"time"

"github.com/grokify/gocharts/data/table"
Expand Down Expand Up @@ -125,11 +126,12 @@ func ReportGrowthPct(rows []RowInt64) []RowFloat64 {
}

type DssTableOpts struct {
FuncFormatTime func(time.Time) string
TotalInclude bool
TotalTitle string
PercentInclude bool
PercentSuffix string
TimeColumnTitle string
FuncFormatTime func(time.Time) string
TotalInclude bool
TotalTitle string
PercentInclude bool
PercentSuffix string
}

func (opts *DssTableOpts) TotalTitleOrDefault() string {
Expand All @@ -154,10 +156,27 @@ func (dss *DataSeriesSet) ToTable(opts *DssTableOpts) (table.Table, error) {
}
tbl := table.NewTable()
seriesNames := dss.SeriesNames()
tbl.Columns = []string{"Time"}
timeColumnTitle := strings.TrimSpace(opts.TimeColumnTitle)
if len(timeColumnTitle) == 0 {
timeColumnTitle = "Time"
}
tbl.Columns = []string{timeColumnTitle}
tbl.Columns = append(tbl.Columns, seriesNames...)
tbl.FormatMap = map[int]string{0: table.FormatTime}
for i := range seriesNames {
if dss.IsFloat {
tbl.FormatMap[i+1] = table.FormatFloat
} else {
tbl.FormatMap[i+1] = table.FormatInt
}
}
if opts.TotalInclude {
tbl.Columns = append(tbl.Columns, opts.TotalTitleOrDefault())
if dss.IsFloat {
tbl.FormatMap[len(tbl.Columns)-1] = table.FormatFloat
} else {
tbl.FormatMap[len(tbl.Columns)-1] = table.FormatInt
}
}
if opts.PercentInclude {
for _, seriesName := range seriesNames {
Expand Down
7 changes: 6 additions & 1 deletion data/table/table.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// table provides a struct to handle tabular data.
package table

const StyleSimple = "border:1px solid #000;border-collapse:collapse"
const (
FormatFloat = "float"
FormatInt = "int"
FormatTime = "time"
StyleSimple = "border:1px solid #000;border-collapse:collapse"
)

// Table is useful for working on CSV data. It stores
// records as `[]string` with typed formatting information
Expand Down
6 changes: 3 additions & 3 deletions data/table/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,19 @@ func (tbl *Table) FormatterFunc() func(val string, colIdx uint) (interface{}, er
fmtType = strings.ToLower(strings.TrimSpace(fmtType))
if len(fmtType) > 0 {
switch fmtType {
case "float":
case FormatFloat:
floatVal, err := strconv.ParseFloat(val, 64)
if err != nil {
return val, err
}
return floatVal, nil
case "int":
case FormatInt:
intVal, err := strconv.Atoi(val)
if err != nil {
return val, err
}
return intVal, nil
case "time":
case FormatTime:
dtVal, err := time.Parse(time.RFC3339, val)
if err != nil {
return val, err
Expand Down

0 comments on commit 3563447

Please sign in to comment.