BoxPlotSeries.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="BoxPlotSeries.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // This is a Avalonia wrapper of OxyPlot.BoxPlotSeries
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using Avalonia;
  10. namespace OxyPlot.Avalonia
  11. {
  12. using global::Avalonia.Media;
  13. using System.Linq;
  14. /// <summary>
  15. /// This is a Avalonia wrapper of OxyPlot.BoxPlotSeries
  16. /// </summary>
  17. public class BoxPlotSeries : XYAxisSeries
  18. {
  19. /// <summary>
  20. /// Identifies this <see cref="BoxWidthProperty"/> dependency property.
  21. /// </summary>
  22. public static readonly StyledProperty<double> BoxWidthProperty = AvaloniaProperty.Register<BoxPlotSeries, double>(nameof(BoxWidth), 0.3);
  23. /// <summary>
  24. /// Identifies this <see cref="FillProperty"/> dependency property.
  25. /// </summary>
  26. public static readonly StyledProperty<Color> FillProperty = AvaloniaProperty.Register<BoxPlotSeries, Color>(nameof(Fill), MoreColors.Automatic);
  27. /// <summary>
  28. /// Identifies this <see cref="LineStyleProperty"/> dependency property.
  29. /// </summary>
  30. public static readonly StyledProperty<LineStyle> LineStyleProperty = AvaloniaProperty.Register<BoxPlotSeries, LineStyle>(nameof(LineStyle), LineStyle.Solid);
  31. /// <summary>
  32. /// Identifies this <see cref="MedianPointSizeProperty"/> dependency property.
  33. /// </summary>
  34. public static readonly StyledProperty<double> MedianPointSizeProperty = AvaloniaProperty.Register<BoxPlotSeries, double>(nameof(MedianPointSize), 2.0);
  35. /// <summary>
  36. /// Identifies this <see cref="MedianThicknessProperty"/> dependency property.
  37. /// </summary>
  38. public static readonly StyledProperty<double> MedianThicknessProperty = AvaloniaProperty.Register<BoxPlotSeries, double>(nameof(MedianThickness), 2.0);
  39. /// <summary>
  40. /// Identifies this <see cref="OutlierSizeProperty"/> dependency property.
  41. /// </summary>
  42. public static readonly StyledProperty<double> OutlierSizeProperty = AvaloniaProperty.Register<BoxPlotSeries, double>(nameof(OutlierSize), 2.0);
  43. /// <summary>
  44. /// Identifies this <see cref="OutlierTypeProperty"/> dependency property.
  45. /// </summary>
  46. public static readonly StyledProperty<MarkerType> OutlierTypeProperty = AvaloniaProperty.Register<BoxPlotSeries, MarkerType>(nameof(OutlierType), MarkerType.Circle);
  47. /// <summary>
  48. /// Identifies this <see cref="OutlierOutlineProperty"/> dependency property.
  49. /// </summary>
  50. public static readonly StyledProperty<Point[]> OutlierOutlineProperty = AvaloniaProperty.Register<BoxPlotSeries, Point[]>(nameof(OutlierOutline), null);
  51. /// <summary>
  52. /// Identifies this <see cref="ShowBoxProperty"/> dependency property.
  53. /// </summary>
  54. public static readonly StyledProperty<bool> ShowBoxProperty = AvaloniaProperty.Register<BoxPlotSeries, bool>(nameof(ShowBox), true);
  55. /// <summary>
  56. /// Identifies this <see cref="ShowMedianAsDotProperty"/> dependency property.
  57. /// </summary>
  58. public static readonly StyledProperty<bool> ShowMedianAsDotProperty = AvaloniaProperty.Register<BoxPlotSeries, bool>(nameof(ShowMedianAsDot), false);
  59. /// <summary>
  60. /// Identifies this <see cref="StrokeProperty"/> dependency property.
  61. /// </summary>
  62. public static readonly StyledProperty<Color> StrokeProperty = AvaloniaProperty.Register<BoxPlotSeries, Color>(nameof(Stroke), Colors.Black);
  63. /// <summary>
  64. /// Identifies this <see cref="StrokeThicknessProperty"/> dependency property.
  65. /// </summary>
  66. public static readonly StyledProperty<double> StrokeThicknessProperty = AvaloniaProperty.Register<BoxPlotSeries, double>(nameof(StrokeThickness), 1.0);
  67. /// <summary>
  68. /// Identifies this <see cref="WhiskerWidthProperty"/> dependency property.
  69. /// </summary>
  70. public static readonly StyledProperty<double> WhiskerWidthProperty = AvaloniaProperty.Register<BoxPlotSeries, double>(nameof(WhiskerWidth), 0.5);
  71. /// <summary>
  72. /// Initializes static members of the <see cref="BoxPlotSeries"/> class.
  73. /// </summary>
  74. static BoxPlotSeries()
  75. {
  76. TrackerFormatStringProperty.OverrideMetadata(typeof(BoxPlotSeries), new StyledPropertyMetadata<string>(OxyPlot.Series.BoxPlotSeries.DefaultTrackerFormatString));
  77. BoxWidthProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  78. FillProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  79. LineStyleProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  80. MedianPointSizeProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  81. MedianThicknessProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  82. OutlierSizeProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  83. OutlierTypeProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  84. OutlierOutlineProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  85. ShowBoxProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  86. ShowMedianAsDotProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  87. StrokeProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  88. StrokeThicknessProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  89. WhiskerWidthProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  90. TrackerFormatStringProperty.Changed.AddClassHandler<BoxPlotSeries>(AppearanceChanged);
  91. }
  92. /// <summary>
  93. /// Initializes a new instance of the <see cref="BoxPlotSeries" /> class.
  94. /// </summary>
  95. public BoxPlotSeries()
  96. {
  97. InternalSeries = new OxyPlot.Series.BoxPlotSeries();
  98. }
  99. /// <summary>
  100. /// Gets or sets the width of the boxes (specified in x-axis units).
  101. /// </summary>
  102. /// <value>The width of the boxes.</value>
  103. public double BoxWidth
  104. {
  105. get { return GetValue(BoxWidthProperty); }
  106. set { SetValue(BoxWidthProperty, value); }
  107. }
  108. /// <summary>
  109. /// Gets or sets the fill color. If <c>null</c>, this color will be automatically set.
  110. /// </summary>
  111. /// <value>The fill color.</value>
  112. public Color Fill
  113. {
  114. get { return GetValue(FillProperty); }
  115. set { SetValue(FillProperty, value); }
  116. }
  117. /// <summary>
  118. /// Gets or sets the line style.
  119. /// </summary>
  120. /// <value>The line style.</value>
  121. public LineStyle LineStyle
  122. {
  123. get { return GetValue(LineStyleProperty); }
  124. set { SetValue(LineStyleProperty, value); }
  125. }
  126. /// <summary>
  127. /// Gets or sets the size of the median point.
  128. /// </summary>
  129. /// <remarks>This property is only used when MedianStyle = Dot.</remarks>
  130. public double MedianPointSize
  131. {
  132. get { return GetValue(MedianPointSizeProperty); }
  133. set { SetValue(MedianPointSizeProperty, value); }
  134. }
  135. /// <summary>
  136. /// Gets or sets the median thickness, relative to the StrokeThickness.
  137. /// </summary>
  138. /// <value>The median thickness.</value>
  139. public double MedianThickness
  140. {
  141. get { return GetValue(MedianThicknessProperty); }
  142. set { SetValue(MedianThicknessProperty, value); }
  143. }
  144. /// <summary>
  145. /// Gets or sets the diameter of the outlier circles (specified in points).
  146. /// </summary>
  147. /// <value>The size of the outlier.</value>
  148. public double OutlierSize
  149. {
  150. get { return GetValue(OutlierSizeProperty); }
  151. set { SetValue(OutlierSizeProperty, value); }
  152. }
  153. /// <summary>
  154. /// Gets or sets the type of the outliers.
  155. /// </summary>
  156. /// <value>The type of the outliers.</value>
  157. /// <remarks>MarkerType.Custom is currently not supported.</remarks>
  158. public MarkerType OutlierType
  159. {
  160. get { return GetValue(OutlierTypeProperty); }
  161. set { SetValue(OutlierTypeProperty, value); }
  162. }
  163. /// <summary>
  164. /// Gets or sets the a custom polygon outline for the outlier markers. Set <see cref="OutlierType" /> to <see cref="OxyPlot.MarkerType.Custom" /> to use this property.
  165. /// </summary>
  166. /// <value>A polyline. The default is <c>null</c>.</value>
  167. public Point[] OutlierOutline
  168. {
  169. get { return GetValue(OutlierOutlineProperty); }
  170. set { SetValue(OutlierOutlineProperty, value); }
  171. }
  172. /// <summary>
  173. /// Gets or sets a value indicating whether to show the boxes.
  174. /// </summary>
  175. public bool ShowBox
  176. {
  177. get { return GetValue(ShowBoxProperty); }
  178. set { SetValue(ShowBoxProperty, value); }
  179. }
  180. /// <summary>
  181. /// Gets or sets a value indicating whether to show the median as a dot.
  182. /// </summary>
  183. public bool ShowMedianAsDot
  184. {
  185. get { return GetValue(ShowMedianAsDotProperty); }
  186. set { SetValue(ShowMedianAsDotProperty, value); }
  187. }
  188. /// <summary>
  189. /// Gets or sets the stroke color.
  190. /// </summary>
  191. /// <value>The stroke color.</value>
  192. public Color Stroke
  193. {
  194. get { return GetValue(StrokeProperty); }
  195. set { SetValue(StrokeProperty, value); }
  196. }
  197. /// <summary>
  198. /// Gets or sets the stroke thickness.
  199. /// </summary>
  200. /// <value>The stroke thickness.</value>
  201. public double StrokeThickness
  202. {
  203. get { return GetValue(StrokeThicknessProperty); }
  204. set { SetValue(StrokeThicknessProperty, value); }
  205. }
  206. /// <summary>
  207. /// Gets or sets the width of the whiskers (relative to the BoxWidth).
  208. /// </summary>
  209. /// <value>The width of the whiskers.</value>
  210. public double WhiskerWidth
  211. {
  212. get { return GetValue(WhiskerWidthProperty); }
  213. set { SetValue(WhiskerWidthProperty, value); }
  214. }
  215. /// <summary>
  216. /// Creates the internal series.
  217. /// </summary>
  218. /// <returns>The internal series.</returns>
  219. public override OxyPlot.Series.Series CreateModel()
  220. {
  221. SynchronizeProperties(InternalSeries);
  222. return InternalSeries;
  223. }
  224. /// <summary>
  225. /// Synchronizes the properties.
  226. /// </summary>
  227. /// <param name="series">The series.</param>
  228. protected override void SynchronizeProperties(OxyPlot.Series.Series series)
  229. {
  230. base.SynchronizeProperties(series);
  231. var s = (OxyPlot.Series.BoxPlotSeries)series;
  232. s.Fill = Fill.ToOxyColor();
  233. s.LineStyle = LineStyle;
  234. s.MedianPointSize = MedianPointSize;
  235. s.OutlierSize = OutlierSize;
  236. s.OutlierType = OutlierType;
  237. s.OutlierOutline = (OutlierOutline ?? Enumerable.Empty<Point>()).Select(point => point.ToScreenPoint()).ToArray();
  238. s.ShowBox = ShowBox;
  239. s.ShowMedianAsDot = ShowMedianAsDot;
  240. s.Stroke = Stroke.ToOxyColor();
  241. s.StrokeThickness = StrokeThickness;
  242. s.WhiskerWidth = WhiskerWidth;
  243. if (Items != null)
  244. {
  245. s.Items.Clear();
  246. foreach (var item in Items)
  247. {
  248. s.Items.Add((OxyPlot.Series.BoxPlotItem)item);
  249. }
  250. }
  251. }
  252. }
  253. }