Skip to content

Commit

Permalink
enhance: statictimeseries: add DataSeries.ToMonth(inflate)
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Jun 14, 2021
1 parent 7ced065 commit 904639e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion charts/wchart/data_series_set_simple_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func DSSToChart(data statictimeseries.DataSeriesSet, opts ChartOptions) chart.Ch
}
times := timeutil.TimeSeriesSlice(
timeutil.Month,
statictimeseries.DataSeriesItemTimes(&series))
series.ItemTimes())
ts.XValues = times
yvalues := []float64{}
for _, t := range times {
Expand Down
33 changes: 24 additions & 9 deletions data/statictimeseries/data_series.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (ds *DataSeries) DeleteByTime(dt time.Time) {
delete(ds.ItemMap, dt.Format(time.RFC3339))
}

func (ds *DataSeries) ToMonth() DataSeries {
func (ds *DataSeries) ToMonth(inflate bool) DataSeries {
newDataSeries := DataSeries{
SeriesName: ds.SeriesName,
ItemMap: map[string]DataItem{},
Expand All @@ -225,16 +225,29 @@ func (ds *DataSeries) ToMonth() DataSeries {
Value: item.Value,
ValueFloat: item.ValueFloat})
}
if inflate {
timeSeries := timeutil.TimeSeriesSlice(
timeutil.Month,
newDataSeries.ItemTimes())
for _, dt := range timeSeries {
newDataSeries.AddItem(DataItem{
SeriesName: newDataSeries.SeriesName,
Time: dt,
IsFloat: newDataSeries.IsFloat,
Value: 0,
ValueFloat: 0.0})
}
}
return newDataSeries
}

func (ds *DataSeries) ToMonthCumulative(timesInput ...time.Time) (DataSeries, error) {
func (ds *DataSeries) ToMonthCumulative(inflate bool, timesInput ...time.Time) (DataSeries, error) {
newDataSeries := DataSeries{
SeriesName: ds.SeriesName,
ItemMap: map[string]DataItem{},
IsFloat: ds.IsFloat,
Interval: timeutil.Month}
dsMonth := ds.ToMonth()
dsMonth := ds.ToMonth(inflate)
var min time.Time
var max time.Time
var err error
Expand Down Expand Up @@ -335,9 +348,9 @@ func (ds *DataSeries) WriteXLSX(filename, sheetname, col1, col2 string) error {
Rows: rows})
}

func AggregateSeries(s1 DataSeries) DataSeries {
func AggregateSeries(series DataSeries) DataSeries {
aggregate := NewDataSeries()
sortedItems := s1.ItemsSorted()
sortedItems := series.ItemsSorted()
sum := int64(0)
for _, atomicItem := range sortedItems {
aggregateItem := DataItem{
Expand All @@ -351,24 +364,26 @@ func AggregateSeries(s1 DataSeries) DataSeries {
return aggregate
}

func DataSeriesTimeSeries(series *DataSeries, interval timeutil.Interval) []time.Time {
return timeutil.TimeSeriesSlice(interval, DataSeriesItemTimes(series))
func (series *DataSeries) TimeSeries(interval timeutil.Interval) []time.Time {
return timeutil.TimeSeriesSlice(interval, series.ItemTimes())
}

func DataSeriesItemTimes(series *DataSeries) []time.Time {
func (series *DataSeries) ItemTimes() []time.Time {
times := []time.Time{}
for _, item := range series.ItemMap {
times = append(times, item.Time)
}
return times
}

/*
func DataSeriesMinMaxTimes(series *DataSeries) (time.Time, time.Time) {
return timeutil.SliceMinMax(DataSeriesItemTimes(series))
}
*/

func (series *DataSeries) MinMaxTimes() (time.Time, time.Time) {
return DataSeriesMinMaxTimes(series)
return timeutil.SliceMinMax(series.ItemTimes())
}

func (ds *DataSeries) Stats() point.PointSet {
Expand Down
8 changes: 4 additions & 4 deletions data/statictimeseries/data_series_set_mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ import (
"github.com/grokify/simplego/time/timeutil"
)

func (set *DataSeriesSet) ToMonth() DataSeriesSet {
func (set *DataSeriesSet) ToMonth(inflate bool) DataSeriesSet {
newDss := DataSeriesSet{
Name: set.Name,
Series: map[string]DataSeries{},
Times: set.Times,
Interval: timeutil.Month,
Order: set.Order}
for name, ds := range set.Series {
newDss.Series[name] = ds.ToMonth()
newDss.Series[name] = ds.ToMonth(inflate)
}
newDss.Times = newDss.GetTimeSlice(true)
return newDss
}

func (set *DataSeriesSet) ToMonthCumulative(popLast bool) (DataSeriesSet, error) {
func (set *DataSeriesSet) ToMonthCumulative(popLast, inflate bool) (DataSeriesSet, error) {
newDss := DataSeriesSet{
Name: set.Name,
Series: map[string]DataSeries{},
Times: set.Times,
Interval: timeutil.Month,
Order: set.Order}
for name, ds := range set.Series {
newDs, err := ds.ToMonthCumulative(newDss.Times...)
newDs, err := ds.ToMonthCumulative(inflate, newDss.Times...)
if err != nil {
return newDss, err
}
Expand Down

0 comments on commit 904639e

Please sign in to comment.