using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RealTimeGraphX.DataPoints { /// /// Represents a graph value data point. /// /// public class TimeSpanDataPoint : GraphDataTypeBase { /// /// Initializes a new instance of the class. /// public TimeSpanDataPoint() : base() { } /// /// Initializes a new instance of the class. /// /// The value. public TimeSpanDataPoint(TimeSpan value) : base(value) { } /// /// Performs an implicit conversion from to . /// /// The value. /// /// The result of the conversion. /// public static implicit operator TimeSpanDataPoint(TimeSpan value) { return new TimeSpanDataPoint(value); } /// /// Implements the operator -. /// /// a. /// The b. /// /// The result of the operator. /// public static TimeSpanDataPoint operator -(TimeSpanDataPoint a, TimeSpanDataPoint b) { return new TimeSpanDataPoint(a.Value - b.Value); } /// /// Implements the operator +. /// /// a. /// The b. /// /// The result of the operator. /// public static TimeSpanDataPoint operator +(TimeSpanDataPoint a, TimeSpanDataPoint b) { return new TimeSpanDataPoint(a.Value + b.Value); } /// /// Sums the value of this instance with another instance value and returns the result. /// /// The other instance. /// public override IGraphDataPoint Add(IGraphDataPoint other) { return new TimeSpanDataPoint(this.Value + (other as TimeSpanDataPoint).Value); } /// /// Subtract the value of another instance from this instance and returns the result. /// /// The other instance. /// public override IGraphDataPoint Subtract(IGraphDataPoint other) { return new TimeSpanDataPoint(this.Value - (other as TimeSpanDataPoint).Value); } /// /// Multiplies the value of this instance with another instance value and returns the result. /// /// The other instance. /// public override IGraphDataPoint Multiply(IGraphDataPoint other) { return new TimeSpanDataPoint(TimeSpan.FromMilliseconds(this.Value.TotalMilliseconds * (other as TimeSpanDataPoint).Value.TotalMilliseconds)); } /// /// Divides the value of this instance with another instance value and returns the result. /// /// The other instance. /// public override IGraphDataPoint Divide(IGraphDataPoint other) { return new TimeSpanDataPoint(TimeSpan.FromMilliseconds(this.Value.TotalMilliseconds / (other as TimeSpanDataPoint).Value.TotalMilliseconds)); } /// /// Returns the percentage value of this instance between the specified minimum and maximum values. /// /// The minimum. /// The maximum. /// public override double ComputeRelativePosition(IGraphDataPoint min, IGraphDataPoint max) { TimeSpan dMin = min as TimeSpanDataPoint; TimeSpan dMax = max as TimeSpanDataPoint; var result = ((Value.TotalMilliseconds - dMin.TotalMilliseconds) * 100) / (dMax.TotalMilliseconds - dMin.TotalMilliseconds); return double.IsNaN(result) ? dMin.TotalMilliseconds : result; } /// /// Returns the absolute value of the specified percentage value between the specified minimum and maximum values. /// /// The minimum. /// The maximum. /// The percentage. /// public override IGraphDataPoint ComputeAbsolutePosition(IGraphDataPoint min, IGraphDataPoint max, double percentage) { double minimum = ((TimeSpan)min.GetValue()).TotalMilliseconds; double maximum = ((TimeSpan)max.GetValue()).TotalMilliseconds; return new TimeSpanDataPoint(TimeSpan.FromMilliseconds(minimum + (maximum - minimum) * percentage)); } /// /// Creates a range of values from the specified minimum and maximum. /// /// The minimum. /// The maximum. /// The count. /// public override IEnumerable CreateRange(IGraphDataPoint min, IGraphDataPoint max, int count) { double minimum = ((TimeSpan)min.GetValue()).TotalMilliseconds; double maximum = ((TimeSpan)max.GetValue()).TotalMilliseconds; return Enumerable.Range(0, count). Select(i => minimum + (maximum - minimum) * ((double)i / (count - 1))). Select(x => new TimeSpanDataPoint(TimeSpan.FromMilliseconds(x))); } /// /// Returns a formated string of this data point. /// /// The format. /// public override string ToString(string format) { return Value.ToString(format); } } }