/// <summary> /// Changes the zoomlevel of a timespanseries /// </summary> /// <param name="Data"></param> /// <param name="NewZoomLevel"></param> /// <param name="Accumulate"></param> /// <returns></returns> public static IEnumerable <TimeSpanValue> ChangeZoomLevel(TimeSpanSeries Data, TimeStepUnit NewZoomLevel, bool Accumulate) { if (Data.TimeStepSize <= NewZoomLevel) { return(null); } List <TimeSpanValue> ToReturn = new List <TimeSpanValue>(); DateTime start = Data.StartTime; DateTime end = Data.EndTime; TimeSpanValue CurrentValue = new TimeSpanValue(GetTimeOfFirstTimeStep(start, NewZoomLevel), GetTimeOfFirstTimeStep(start, NewZoomLevel).AddTimeStepUnit(NewZoomLevel), 0); double currentcount = 0; foreach (var v in Data.Items.Where(dv => dv.Value != Data.DeleteValue)) { if (CurrentValue.StartTime <= v.StartTime & v.StartTime <= CurrentValue.EndTime) { if (CurrentValue.EndTime >= v.EndTime) //We are still within the timespan { CurrentValue.Value += v.Value; currentcount++; } else //We exceed the timespan { double outsidefraction = (v.EndTime.Subtract(CurrentValue.EndTime).TotalDays / v.EndTime.Subtract(v.StartTime).TotalDays); CurrentValue.Value += v.Value * (1 - outsidefraction); currentcount += 1 - outsidefraction; if (!Accumulate & currentcount != 0) { CurrentValue.Value /= currentcount; } ToReturn.Add(CurrentValue); CurrentValue = new TimeSpanValue(CurrentValue.EndTime, CurrentValue.EndTime.AddTimeStepUnit(NewZoomLevel), v.Value * outsidefraction); currentcount = outsidefraction; } } else { if (!Accumulate & currentcount != 0) { CurrentValue.Value /= currentcount; } ToReturn.Add(CurrentValue); CurrentValue = new TimeSpanValue(GetTimeOfFirstTimeStep(v.StartTime, NewZoomLevel), GetTimeOfFirstTimeStep(v.StartTime, NewZoomLevel).AddTimeStepUnit(NewZoomLevel), v.Value); currentcount = 1; } } if (!Accumulate & currentcount != 0) { CurrentValue.Value /= currentcount; } ToReturn.Add(CurrentValue); return(ToReturn); }
/// <summary> /// Checks if there are any holes in the data series. Returns the active periods. /// </summary> /// <param name="data"></param> /// <param name="Interval"></param> /// <returns></returns> public static TimeSpanSeries GetActivePeriods(TimeStampSeries data, TimeSpan Interval) { TimeSpanSeries toreturn = new TimeSpanSeries(); if (data.Count > 0) { TimeSpanValue currentvalue = new TimeSpanValue(data.StartTime, data.StartTime, 1); for (int i = 0; i < data.Count; i++) { if (data.Items[i].Time <= currentvalue.EndTime.Add(Interval)) { currentvalue.EndTime = data.Items[i].Time; } else { toreturn.Items.Add(currentvalue); currentvalue = new TimeSpanValue(data.Items[i].Time, data.Items[i].Time, 1); } } toreturn.Items.Add(currentvalue); } return(toreturn); }
/// <summary> /// Returns the appropriate time step for a given time period /// </summary> /// <param name="Start"></param> /// <param name="End"></param> /// <returns></returns> public static TimeStepUnit GetTimeStep(TimeSpanValue value) { return(GetTimeStep(value.StartTime, value.EndTime)); }