CategoryAxis.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="CategoryAxis.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // This is a Avalonia wrapper of OxyPlot.CategoryAxis.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using Avalonia;
  10. namespace OxyPlot.Avalonia
  11. {
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. /// <summary>
  15. /// This is a Avalonia wrapper of OxyPlot.CategoryAxis.
  16. /// </summary>
  17. public class CategoryAxis : LinearAxis
  18. {
  19. /// <summary>
  20. /// Identifies the <see cref="GapWidth"/> dependency property.
  21. /// </summary>
  22. public static readonly StyledProperty<double> GapWidthProperty = AvaloniaProperty.Register<CategoryAxis, double>(nameof(GapWidth), 1.0);
  23. /// <summary>
  24. /// Identifies the <see cref="IsTickCentered"/> dependency property.
  25. /// </summary>
  26. public static readonly StyledProperty<bool> IsTickCenteredProperty = AvaloniaProperty.Register<CategoryAxis, bool>(nameof(IsTickCentered), false);
  27. /// <summary>
  28. /// Identifies the <see cref="Items"/> dependency property.
  29. /// </summary>
  30. public static readonly StyledProperty<IEnumerable> ItemsProperty = AvaloniaProperty.Register<CategoryAxis, IEnumerable>(nameof(Items), null);
  31. /// <summary>
  32. /// Identifies the <see cref="LabelField"/> dependency property.
  33. /// </summary>
  34. public static readonly StyledProperty<string> LabelFieldProperty = AvaloniaProperty.Register<CategoryAxis, string>(nameof(LabelField), null);
  35. /// <summary>
  36. /// Identifies the <see cref="Labels"/> dependency property.
  37. /// </summary>
  38. public static readonly StyledProperty<IList<string>> LabelsProperty = AvaloniaProperty.Register<CategoryAxis, IList<string>>(nameof(Labels), new List<string>());
  39. /// <summary>
  40. /// Initializes static members of the <see cref="CategoryAxis" /> class.
  41. /// </summary>
  42. static CategoryAxis()
  43. {
  44. PositionProperty.OverrideMetadata(typeof(CategoryAxis), new StyledPropertyMetadata<Axes.AxisPosition>(Axes.AxisPosition.Bottom));
  45. MinimumPaddingProperty.OverrideMetadata(typeof(CategoryAxis), new StyledPropertyMetadata<double>(0.0));
  46. MaximumPaddingProperty.OverrideMetadata(typeof(CategoryAxis), new StyledPropertyMetadata<double>(0.0));
  47. MajorStepProperty.OverrideMetadata(typeof(CategoryAxis), new StyledPropertyMetadata<double>(1.0));
  48. IsTickCenteredProperty.Changed.AddClassHandler<CategoryAxis>(DataChanged);
  49. ItemsProperty.Changed.AddClassHandler<CategoryAxis>(DataChanged);
  50. LabelFieldProperty.Changed.AddClassHandler<CategoryAxis>(DataChanged);
  51. LabelsProperty.Changed.AddClassHandler<CategoryAxis>(DataChanged);
  52. PositionProperty.Changed.AddClassHandler<CategoryAxis>(DataChanged);
  53. MinimumPaddingProperty.Changed.AddClassHandler<CategoryAxis>(DataChanged);
  54. MaximumPaddingProperty.Changed.AddClassHandler<CategoryAxis>(DataChanged);
  55. MajorStepProperty.Changed.AddClassHandler<CategoryAxis>(DataChanged);
  56. }
  57. /// <summary>
  58. /// Initializes a new instance of the <see cref="CategoryAxis" /> class.
  59. /// </summary>
  60. public CategoryAxis()
  61. {
  62. InternalAxis = new Axes.CategoryAxis();
  63. }
  64. /// <summary>
  65. /// Gets or sets the gap width.
  66. /// </summary>
  67. /// <value>The width of the gap.</value>
  68. public double GapWidth
  69. {
  70. get
  71. {
  72. return GetValue(GapWidthProperty);
  73. }
  74. set
  75. {
  76. SetValue(GapWidthProperty, value);
  77. }
  78. }
  79. /// <summary>
  80. /// Gets or sets a value indicating whether IsTickCentered.
  81. /// </summary>
  82. public bool IsTickCentered
  83. {
  84. get
  85. {
  86. return GetValue(IsTickCenteredProperty);
  87. }
  88. set
  89. {
  90. SetValue(IsTickCenteredProperty, value);
  91. }
  92. }
  93. /// <summary>
  94. /// Gets or sets ItemsSource.
  95. /// </summary>
  96. public IEnumerable Items
  97. {
  98. get
  99. {
  100. return GetValue(ItemsProperty);
  101. }
  102. set
  103. {
  104. SetValue(ItemsProperty, value);
  105. }
  106. }
  107. /// <summary>
  108. /// Gets or sets LabelField.
  109. /// </summary>
  110. public string LabelField
  111. {
  112. get
  113. {
  114. return GetValue(LabelFieldProperty);
  115. }
  116. set
  117. {
  118. SetValue(LabelFieldProperty, value);
  119. }
  120. }
  121. /// <summary>
  122. /// Gets or sets Labels.
  123. /// </summary>
  124. public IList<string> Labels
  125. {
  126. get
  127. {
  128. return GetValue(LabelsProperty);
  129. }
  130. set
  131. {
  132. SetValue(LabelsProperty, value);
  133. }
  134. }
  135. /// <summary>
  136. /// Creates the internal axis.
  137. /// </summary>
  138. /// <returns>The internal axis.</returns>
  139. public override Axes.Axis CreateModel()
  140. {
  141. SynchronizeProperties();
  142. return InternalAxis;
  143. }
  144. /// <summary>
  145. /// Synchronizes the properties.
  146. /// </summary>
  147. protected override void SynchronizeProperties()
  148. {
  149. base.SynchronizeProperties();
  150. var a = (Axes.CategoryAxis)InternalAxis;
  151. a.IsTickCentered = IsTickCentered;
  152. a.ItemsSource = Items;
  153. a.LabelField = LabelField;
  154. a.GapWidth = GapWidth;
  155. if (Labels != null && Items == null)
  156. {
  157. a.Labels.Clear();
  158. a.Labels.AddRange(Labels);
  159. }
  160. }
  161. }
  162. }