ScatterSeries{T}.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="ScatterSeries{T}.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // Provides a base class for scatter series.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using Avalonia;
  10. namespace OxyPlot.Avalonia
  11. {
  12. using global::Avalonia.Media;
  13. using OxyPlot.Series;
  14. using System;
  15. /// <summary>
  16. /// Provides a base class for scatter series.
  17. /// </summary>
  18. /// <typeparam name="T">The type of the points.</typeparam>
  19. public abstract class ScatterSeries<T> : XYAxisSeries where T : ScatterPoint
  20. {
  21. /// <summary>
  22. /// Identifies the <see cref="BinSize"/> dependency property.
  23. /// </summary>
  24. public static readonly StyledProperty<int> BinSizeProperty = AvaloniaProperty.Register<ScatterSeries<T>, int>(nameof(BinSize), 0);
  25. /// <summary>
  26. /// Identifies the <see cref="DataFieldSize"/> dependency property.
  27. /// </summary>
  28. public static readonly StyledProperty<string> DataFieldSizeProperty = AvaloniaProperty.Register<ScatterSeries<T>, string>(nameof(DataFieldSize), null);
  29. /// <summary>
  30. /// Identifies the <see cref="DataFieldTag"/> dependency property.
  31. /// </summary>
  32. public static readonly StyledProperty<string> DataFieldTagProperty = AvaloniaProperty.Register<ScatterSeries<T>, string>(nameof(DataFieldTag), null);
  33. /// <summary>
  34. /// Identifies the <see cref="DataFieldValue"/> dependency property.
  35. /// </summary>
  36. public static readonly StyledProperty<string> DataFieldValueProperty = AvaloniaProperty.Register<ScatterSeries<T>, string>(nameof(DataFieldValue), null);
  37. /// <summary>
  38. /// Identifies the <see cref="DataFieldX"/> dependency property.
  39. /// </summary>
  40. public static readonly StyledProperty<string> DataFieldXProperty = AvaloniaProperty.Register<ScatterSeries<T>, string>(nameof(DataFieldX), null);
  41. /// <summary>
  42. /// Identifies the <see cref="DataFieldY"/> dependency property.
  43. /// </summary>
  44. public static readonly StyledProperty<string> DataFieldYProperty = AvaloniaProperty.Register<ScatterSeries<T>, string>(nameof(DataFieldY), null);
  45. /// <summary>
  46. /// Identifies the <see cref="Mapping"/> dependency property.
  47. /// </summary>
  48. public static readonly StyledProperty<Func<object, ScatterPoint>> MappingProperty = AvaloniaProperty.Register<ScatterSeries<T>, Func<object, ScatterPoint>>(nameof(Mapping), null);
  49. /// <summary>
  50. /// Identifies the <see cref="MarkerFill"/> dependency property.
  51. /// </summary>
  52. public static readonly StyledProperty<Color> MarkerFillProperty = AvaloniaProperty.Register<ScatterSeries<T>, Color>(nameof(MarkerFill), MoreColors.Automatic);
  53. /// <summary>
  54. /// Identifies the <see cref="MarkerOutline"/> dependency property.
  55. /// </summary>
  56. public static readonly StyledProperty<ScreenPoint[]> MarkerOutlineProperty = AvaloniaProperty.Register<ScatterSeries<T>, ScreenPoint[]>(nameof(MarkerOutline), null);
  57. /// <summary>
  58. /// Identifies the <see cref="MarkerSize"/> dependency property.
  59. /// </summary>
  60. public static readonly StyledProperty<double> MarkerSizeProperty = AvaloniaProperty.Register<ScatterSeries<T>, double>(nameof(MarkerSize), 5.0);
  61. /// <summary>
  62. /// Identifies the <see cref="MarkerStroke"/> dependency property.
  63. /// </summary>
  64. public static readonly StyledProperty<Color> MarkerStrokeProperty = AvaloniaProperty.Register<ScatterSeries<T>, Color>(nameof(MarkerStroke), MoreColors.Automatic);
  65. /// <summary>
  66. /// Identifies the <see cref="MarkerStrokeThickness"/> dependency property.
  67. /// </summary>
  68. public static readonly StyledProperty<double> MarkerStrokeThicknessProperty = AvaloniaProperty.Register<ScatterSeries<T>, double>(nameof(MarkerStrokeThickness), 1.0);
  69. /// <summary>
  70. /// Identifies the <see cref="MarkerType"/> dependency property.
  71. /// </summary>
  72. public static readonly StyledProperty<MarkerType> MarkerTypeProperty = AvaloniaProperty.Register<ScatterSeries<T>, MarkerType>(nameof(MarkerType), MarkerType.Square);
  73. /// <summary>
  74. /// Initializes a new instance of the <see cref = "ScatterSeries{T}" /> class.
  75. /// </summary>
  76. protected ScatterSeries()
  77. {
  78. InternalSeries = new OxyPlot.Series.ScatterSeries();
  79. }
  80. /// <summary>
  81. /// Gets or sets bin size.
  82. /// </summary>
  83. public int BinSize
  84. {
  85. get
  86. {
  87. return GetValue(BinSizeProperty);
  88. }
  89. set
  90. {
  91. SetValue(BinSizeProperty, value);
  92. }
  93. }
  94. /// <summary>
  95. /// Gets or sets size data field.
  96. /// </summary>
  97. public string DataFieldSize
  98. {
  99. get
  100. {
  101. return GetValue(DataFieldSizeProperty);
  102. }
  103. set
  104. {
  105. SetValue(DataFieldSizeProperty, value);
  106. }
  107. }
  108. /// <summary>
  109. /// Gets or sets tag data field.
  110. /// </summary>
  111. public string DataFieldTag
  112. {
  113. get
  114. {
  115. return GetValue(DataFieldTagProperty);
  116. }
  117. set
  118. {
  119. SetValue(DataFieldTagProperty, value);
  120. }
  121. }
  122. /// <summary>
  123. /// Gets or sets value (color) data field.
  124. /// </summary>
  125. public string DataFieldValue
  126. {
  127. get
  128. {
  129. return GetValue(DataFieldValueProperty);
  130. }
  131. set
  132. {
  133. SetValue(DataFieldValueProperty, value);
  134. }
  135. }
  136. /// <summary>
  137. /// Gets or sets X data field.
  138. /// </summary>
  139. public string DataFieldX
  140. {
  141. get
  142. {
  143. return GetValue(DataFieldXProperty);
  144. }
  145. set
  146. {
  147. SetValue(DataFieldXProperty, value);
  148. }
  149. }
  150. /// <summary>
  151. /// Gets or sets Y data field.
  152. /// </summary>
  153. public string DataFieldY
  154. {
  155. get
  156. {
  157. return GetValue(DataFieldYProperty);
  158. }
  159. set
  160. {
  161. SetValue(DataFieldYProperty, value);
  162. }
  163. }
  164. /// <summary>
  165. /// Gets or sets mapping function.
  166. /// </summary>
  167. public Func<object, T> Mapping
  168. {
  169. get
  170. {
  171. return (Func<object, T>)GetValue(MappingProperty);
  172. }
  173. set
  174. {
  175. SetValue(MappingProperty, value);
  176. }
  177. }
  178. /// <summary>
  179. /// Gets or sets fill color of the markers.
  180. /// </summary>
  181. public Color MarkerFill
  182. {
  183. get
  184. {
  185. return GetValue(MarkerFillProperty);
  186. }
  187. set
  188. {
  189. SetValue(MarkerFillProperty, value);
  190. }
  191. }
  192. /// <summary>
  193. /// Gets or sets custom outline of the markers.
  194. /// </summary>
  195. public ScreenPoint[] MarkerOutline
  196. {
  197. get
  198. {
  199. return GetValue(MarkerOutlineProperty);
  200. }
  201. set
  202. {
  203. SetValue(MarkerOutlineProperty, value);
  204. }
  205. }
  206. /// <summary>
  207. /// Gets or sets the size of the markers.
  208. /// </summary>
  209. public double MarkerSize
  210. {
  211. get
  212. {
  213. return GetValue(MarkerSizeProperty);
  214. }
  215. set
  216. {
  217. SetValue(MarkerSizeProperty, value);
  218. }
  219. }
  220. /// <summary>
  221. /// Gets or sets color of the marker strokes.
  222. /// </summary>
  223. public Color MarkerStroke
  224. {
  225. get
  226. {
  227. return GetValue(MarkerStrokeProperty);
  228. }
  229. set
  230. {
  231. SetValue(MarkerStrokeProperty, value);
  232. }
  233. }
  234. /// <summary>
  235. /// Gets or sets thickness of the marker strokes.
  236. /// </summary>
  237. public double MarkerStrokeThickness
  238. {
  239. get
  240. {
  241. return GetValue(MarkerStrokeThicknessProperty);
  242. }
  243. set
  244. {
  245. SetValue(MarkerStrokeThicknessProperty, value);
  246. }
  247. }
  248. /// <summary>
  249. /// Gets or sets type of the markers.
  250. /// </summary>
  251. public MarkerType MarkerType
  252. {
  253. get
  254. {
  255. return GetValue(MarkerTypeProperty);
  256. }
  257. set
  258. {
  259. SetValue(MarkerTypeProperty, value);
  260. }
  261. }
  262. /// <summary>
  263. /// Gets or sets the color axis key.
  264. /// </summary>
  265. /// <value>The color axis key.</value>
  266. public string ColorAxisKey { get; set; }
  267. /// <summary>
  268. /// Creates the internal series.
  269. /// </summary>
  270. /// <returns>The series.</returns>
  271. public override OxyPlot.Series.Series CreateModel()
  272. {
  273. SynchronizeProperties(InternalSeries);
  274. return InternalSeries;
  275. }
  276. /// <summary>
  277. /// Synchronizes the properties.
  278. /// </summary>
  279. /// <param name="series">The series.</param>
  280. protected override void SynchronizeProperties(OxyPlot.Series.Series series)
  281. {
  282. base.SynchronizeProperties(series);
  283. var s = (OxyPlot.Series.ScatterSeries<T>)series;
  284. s.MarkerFill = MarkerFill.ToOxyColor();
  285. s.MarkerStroke = MarkerStroke.ToOxyColor();
  286. s.MarkerStrokeThickness = MarkerStrokeThickness;
  287. s.MarkerType = MarkerType;
  288. s.MarkerSize = MarkerSize;
  289. s.DataFieldX = DataFieldX;
  290. s.DataFieldY = DataFieldY;
  291. s.DataFieldSize = DataFieldSize;
  292. s.DataFieldValue = DataFieldValue;
  293. s.DataFieldTag = DataFieldTag;
  294. s.ItemsSource = Items;
  295. s.BinSize = BinSize;
  296. s.Mapping = Mapping;
  297. s.MarkerOutline = MarkerOutline;
  298. s.ColorAxisKey = ColorAxisKey;
  299. }
  300. static ScatterSeries()
  301. {
  302. BinSizeProperty.Changed.AddClassHandler<ScatterSeries<T>>(AppearanceChanged);
  303. DataFieldSizeProperty.Changed.AddClassHandler<ScatterSeries<T>>(DataChanged);
  304. DataFieldTagProperty.Changed.AddClassHandler<ScatterSeries<T>>(DataChanged);
  305. DataFieldValueProperty.Changed.AddClassHandler<ScatterSeries<T>>(DataChanged);
  306. DataFieldXProperty.Changed.AddClassHandler<ScatterSeries<T>>(DataChanged);
  307. DataFieldYProperty.Changed.AddClassHandler<ScatterSeries<T>>(DataChanged);
  308. MappingProperty.Changed.AddClassHandler<ScatterSeries<T>>(DataChanged);
  309. MarkerFillProperty.Changed.AddClassHandler<ScatterSeries<T>>(AppearanceChanged);
  310. MarkerOutlineProperty.Changed.AddClassHandler<ScatterSeries<T>>(AppearanceChanged);
  311. MarkerSizeProperty.Changed.AddClassHandler<ScatterSeries<T>>(AppearanceChanged);
  312. MarkerStrokeProperty.Changed.AddClassHandler<ScatterSeries<T>>(AppearanceChanged);
  313. MarkerStrokeThicknessProperty.Changed.AddClassHandler<ScatterSeries<T>>(AppearanceChanged);
  314. MarkerTypeProperty.Changed.AddClassHandler<ScatterSeries<T>>(AppearanceChanged);
  315. }
  316. }
  317. }