From 3563447cd5fd91dac97c17f839a4a6bfaa123c25 Mon Sep 17 00:00:00 2001 From: John Wang Date: Mon, 14 Jun 2021 11:25:00 -0700 Subject: [PATCH] enhance: data/statictimeseries: add `Table.FormatMap` to `DataSeriesSet.ToTable()` --- .../data_series_set_report.go | 31 +++++++++++++++---- data/table/table.go | 7 ++++- data/table/write.go | 6 ++-- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/data/statictimeseries/data_series_set_report.go b/data/statictimeseries/data_series_set_report.go index a5e98ac..47ca350 100644 --- a/data/statictimeseries/data_series_set_report.go +++ b/data/statictimeseries/data_series_set_report.go @@ -4,6 +4,7 @@ import ( "fmt" "sort" "strconv" + "strings" "time" "github.com/grokify/gocharts/data/table" @@ -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 { @@ -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 { diff --git a/data/table/table.go b/data/table/table.go index 93386f8..de808a4 100644 --- a/data/table/table.go +++ b/data/table/table.go @@ -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 diff --git a/data/table/write.go b/data/table/write.go index e8eb29f..8b94339 100644 --- a/data/table/write.go +++ b/data/table/write.go @@ -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