PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Charts/Axes/TimeSpan/TimeTicksProviderBase.cs

#
C# | 159 lines | 119 code | 28 blank | 12 comment | 13 complexity | 1f624e7ff6fc76c2895be95e94ba22c9 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
  6. namespace Microsoft.Research.DynamicDataDisplay.Charts
  7. {
  8. public abstract class TimeTicksProviderBase<T> : ITicksProvider<T>
  9. {
  10. public event EventHandler Changed;
  11. protected void RaiseChanged()
  12. {
  13. if (Changed != null)
  14. {
  15. Changed(this, EventArgs.Empty);
  16. }
  17. }
  18. private static readonly Dictionary<DifferenceIn, ITicksProvider<T>> providers =
  19. new Dictionary<DifferenceIn, ITicksProvider<T>>();
  20. protected static Dictionary<DifferenceIn, ITicksProvider<T>> Providers
  21. {
  22. get { return TimeTicksProviderBase<T>.providers; }
  23. }
  24. private static readonly Dictionary<DifferenceIn, ITicksProvider<T>> minorProviders =
  25. new Dictionary<DifferenceIn, ITicksProvider<T>>();
  26. protected static Dictionary<DifferenceIn, ITicksProvider<T>> MinorProviders
  27. {
  28. get { return TimeTicksProviderBase<T>.minorProviders; }
  29. }
  30. protected abstract TimeSpan GetDifference(T start, T end);
  31. #region ITicksProvider<T> Members
  32. private IDateTimeTicksStrategy strategy = new DefaultDateTimeTicksStrategy();
  33. public IDateTimeTicksStrategy Strategy
  34. {
  35. get { return strategy; }
  36. set
  37. {
  38. if (strategy != value)
  39. {
  40. strategy = value;
  41. RaiseChanged();
  42. }
  43. }
  44. }
  45. private ITicksInfo<T> result;
  46. private DifferenceIn diff;
  47. public ITicksInfo<T> GetTicks(Range<T> range, int ticksCount)
  48. {
  49. Verify.IsTrue(ticksCount > 0);
  50. T start = range.Min;
  51. T end = range.Max;
  52. TimeSpan length = GetDifference(start, end);
  53. diff = strategy.GetDifference(length);
  54. TicksInfo<T> result = new TicksInfo<T> { Info = diff };
  55. if (providers.ContainsKey(diff))
  56. {
  57. ITicksInfo<T> innerResult = providers[diff].GetTicks(range, ticksCount);
  58. T[] ticks = ModifyTicksGuard(innerResult.Ticks, diff);
  59. result.Ticks = ticks;
  60. this.result = result;
  61. return result;
  62. }
  63. throw new InvalidOperationException(Strings.Exceptions.UnsupportedRangeInAxis);
  64. }
  65. private T[] ModifyTicksGuard(T[] ticks, object info)
  66. {
  67. var result = ModifyTicks(ticks, info);
  68. if (result == null)
  69. throw new ArgumentNullException("ticks");
  70. return result;
  71. }
  72. protected virtual T[] ModifyTicks(T[] ticks, object info)
  73. {
  74. return ticks;
  75. }
  76. /// <summary>
  77. /// Decreases the tick count.
  78. /// </summary>
  79. /// <param name="tickCount">The tick count.</param>
  80. /// <returns></returns>
  81. public int DecreaseTickCount(int ticksCount)
  82. {
  83. if (providers.ContainsKey(diff))
  84. return providers[diff].DecreaseTickCount(ticksCount);
  85. int res = ticksCount / 2;
  86. if (res < 2) res = 2;
  87. return res;
  88. }
  89. /// <summary>
  90. /// Increases the tick count.
  91. /// </summary>
  92. /// <param name="ticksCount">The tick count.</param>
  93. /// <returns></returns>
  94. public int IncreaseTickCount(int ticksCount)
  95. {
  96. DebugVerify.Is(ticksCount < 2000);
  97. if (providers.ContainsKey(diff))
  98. return providers[diff].IncreaseTickCount(ticksCount);
  99. return ticksCount * 2;
  100. }
  101. public ITicksProvider<T> MinorProvider
  102. {
  103. get
  104. {
  105. DifferenceIn smallerDiff = DifferenceIn.Smallest;
  106. if (strategy.TryGetLowerDiff(diff, out smallerDiff) && minorProviders.ContainsKey(smallerDiff))
  107. {
  108. var minorProvider = (MinorTimeProviderBase<T>)minorProviders[smallerDiff];
  109. minorProvider.SetTicks(result.Ticks);
  110. return minorProvider;
  111. }
  112. return null;
  113. // todo What to do if this already is the smallest provider?
  114. }
  115. }
  116. public ITicksProvider<T> MajorProvider
  117. {
  118. get
  119. {
  120. DifferenceIn biggerDiff = DifferenceIn.Smallest;
  121. if (strategy.TryGetBiggerDiff(diff, out biggerDiff))
  122. {
  123. return providers[biggerDiff];
  124. }
  125. return null;
  126. // todo What to do if this already is the biggest provider?
  127. }
  128. }
  129. #endregion
  130. }
  131. }