DateTimeAxis.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="DateTimeAxis.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // This is a Avalonia wrapper of OxyPlot.DateTimeAxis.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using Avalonia;
  10. namespace OxyPlot.Avalonia
  11. {
  12. using OxyPlot.Axes;
  13. using System;
  14. using System.Globalization;
  15. /// <summary>
  16. /// This is a Avalonia wrapper of OxyPlot.DateTimeAxis.
  17. /// </summary>
  18. public class DateTimeAxis : Axis
  19. {
  20. /// <summary>
  21. /// Identifies the <see cref="CalendarWeekRule"/> dependency property.
  22. /// </summary>
  23. public static readonly StyledProperty<CalendarWeekRule> CalendarWeekRuleProperty = AvaloniaProperty.Register<DateTimeAxis, CalendarWeekRule>(nameof(CalendarWeekRule), CalendarWeekRule.FirstFourDayWeek);
  24. /// <summary>
  25. /// Identifies the <see cref="FirstDateTime"/> dependency property.
  26. /// </summary>
  27. public static readonly StyledProperty<DateTime> FirstDateTimeProperty = AvaloniaProperty.Register<DateTimeAxis, DateTime>(nameof(FirstDateTime), DateTime.MinValue);
  28. /// <summary>
  29. /// Identifies the <see cref="FirstDayOfWeek"/> dependency property.
  30. /// </summary>
  31. public static readonly StyledProperty<DayOfWeek> FirstDayOfWeekProperty = AvaloniaProperty.Register<DateTimeAxis, DayOfWeek>(nameof(FirstDayOfWeek), DayOfWeek.Monday);
  32. /// <summary>
  33. /// Identifies the <see cref="IntervalType"/> dependency property.
  34. /// </summary>
  35. public static readonly StyledProperty<DateTimeIntervalType> IntervalTypeProperty = AvaloniaProperty.Register<DateTimeAxis, DateTimeIntervalType>(nameof(IntervalType), DateTimeIntervalType.Auto);
  36. /// <summary>
  37. /// Identifies the <see cref="LastDateTime"/> dependency property.
  38. /// </summary>
  39. public static readonly StyledProperty<DateTime> LastDateTimeProperty = AvaloniaProperty.Register<DateTimeAxis, DateTime>(nameof(LastDateTime), DateTime.MaxValue);
  40. /// <summary>
  41. /// Identifies the <see cref="MinorIntervalType"/> dependency property.
  42. /// </summary>
  43. public static readonly StyledProperty<DateTimeIntervalType> MinorIntervalTypeProperty = AvaloniaProperty.Register<DateTimeAxis, DateTimeIntervalType>(nameof(MinorIntervalType), DateTimeIntervalType.Auto);
  44. /// <summary>
  45. /// Initializes static members of the <see cref="DateTimeAxis" /> class.
  46. /// </summary>
  47. static DateTimeAxis()
  48. {
  49. PositionProperty.OverrideDefaultValue<DateTimeAxis>(AxisPosition.Bottom);
  50. PositionProperty.Changed.AddClassHandler<DateTimeAxis>(AppearanceChanged);
  51. CalendarWeekRuleProperty.Changed.AddClassHandler<DateTimeAxis>(DataChanged);
  52. FirstDayOfWeekProperty.Changed.AddClassHandler<DateTimeAxis>(DataChanged);
  53. MinorIntervalTypeProperty.Changed.AddClassHandler<DateTimeAxis>(DataChanged);
  54. }
  55. /// <summary>
  56. /// Initializes a new instance of the <see cref = "DateTimeAxis" /> class.
  57. /// </summary>
  58. public DateTimeAxis()
  59. {
  60. InternalAxis = new Axes.DateTimeAxis();
  61. }
  62. /// <summary>
  63. /// Gets or sets CalendarWeekRule.
  64. /// </summary>
  65. public CalendarWeekRule CalendarWeekRule
  66. {
  67. get
  68. {
  69. return GetValue(CalendarWeekRuleProperty);
  70. }
  71. set
  72. {
  73. SetValue(CalendarWeekRuleProperty, value);
  74. }
  75. }
  76. /// <summary>
  77. /// Gets or sets FirstDateTime.
  78. /// </summary>
  79. public DateTime FirstDateTime
  80. {
  81. get
  82. {
  83. return GetValue(FirstDateTimeProperty);
  84. }
  85. set
  86. {
  87. SetValue(FirstDateTimeProperty, value);
  88. }
  89. }
  90. /// <summary>
  91. /// Gets or sets FirstDayOfWeek.
  92. /// </summary>
  93. public DayOfWeek FirstDayOfWeek
  94. {
  95. get
  96. {
  97. return GetValue(FirstDayOfWeekProperty);
  98. }
  99. set
  100. {
  101. SetValue(FirstDayOfWeekProperty, value);
  102. }
  103. }
  104. /// <summary>
  105. /// Gets or sets IntervalType.
  106. /// </summary>
  107. public DateTimeIntervalType IntervalType
  108. {
  109. get
  110. {
  111. return GetValue(IntervalTypeProperty);
  112. }
  113. set
  114. {
  115. SetValue(IntervalTypeProperty, value);
  116. }
  117. }
  118. /// <summary>
  119. /// Gets or sets LastDateTime.
  120. /// </summary>
  121. public DateTime LastDateTime
  122. {
  123. get
  124. {
  125. return GetValue(LastDateTimeProperty);
  126. }
  127. set
  128. {
  129. SetValue(LastDateTimeProperty, value);
  130. }
  131. }
  132. /// <summary>
  133. /// Gets or sets MinorIntervalType.
  134. /// </summary>
  135. public DateTimeIntervalType MinorIntervalType
  136. {
  137. get
  138. {
  139. return GetValue(MinorIntervalTypeProperty);
  140. }
  141. set
  142. {
  143. SetValue(MinorIntervalTypeProperty, value);
  144. }
  145. }
  146. /// <summary>
  147. /// Creates the internal model.
  148. /// </summary>
  149. /// <returns>The internal axis.</returns>
  150. public override Axes.Axis CreateModel()
  151. {
  152. SynchronizeProperties();
  153. return InternalAxis;
  154. }
  155. /// <summary>
  156. /// Synchronizes the properties.
  157. /// </summary>
  158. protected override void SynchronizeProperties()
  159. {
  160. base.SynchronizeProperties();
  161. var a = (Axes.DateTimeAxis)InternalAxis;
  162. a.IntervalType = IntervalType;
  163. a.MinorIntervalType = MinorIntervalType;
  164. a.FirstDayOfWeek = FirstDayOfWeek;
  165. a.CalendarWeekRule = CalendarWeekRule;
  166. if (FirstDateTime > DateTime.MinValue)
  167. {
  168. a.Minimum = Axes.DateTimeAxis.ToDouble(FirstDateTime);
  169. }
  170. if (LastDateTime < DateTime.MaxValue)
  171. {
  172. a.Maximum = Axes.DateTimeAxis.ToDouble(LastDateTime);
  173. }
  174. }
  175. }
  176. }