LineSeries.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="LineSeries.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // This is a Avalonia wrapper of OxyPlot.LineSeries
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using Avalonia;
  10. namespace OxyPlot.Avalonia
  11. {
  12. using global::Avalonia.Media;
  13. using OxyPlot.Series;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. /// <summary>
  18. /// This is a Avalonia wrapper of OxyPlot.LineSeries
  19. /// </summary>
  20. public class LineSeries : DataPointSeries
  21. {
  22. /// <summary>
  23. /// Identifies the <see cref="BrokenLineColor"/> dependency property.
  24. /// </summary>
  25. public static readonly StyledProperty<Color> BrokenLineColorProperty = AvaloniaProperty.Register<LineSeries, Color>(nameof(BrokenLineColor), MoreColors.Undefined);
  26. /// <summary>
  27. /// Identifies the <see cref="BrokenLineStyle"/> dependency property.
  28. /// </summary>
  29. public static readonly StyledProperty<LineStyle> BrokenLineStyleProperty = AvaloniaProperty.Register<LineSeries, LineStyle>(nameof(BrokenLineStyle), LineStyle.Solid);
  30. /// <summary>
  31. /// Identifies the <see cref="BrokenLineThickness"/> dependency property.
  32. /// </summary>
  33. public static readonly StyledProperty<double> BrokenLineThicknessProperty = AvaloniaProperty.Register<LineSeries, double>(nameof(BrokenLineThickness), 0d);
  34. /// <summary>
  35. /// Identifies the <see cref="Dashes"/> dependency property.
  36. /// </summary>
  37. public static readonly StyledProperty<double[]> DashesProperty = AvaloniaProperty.Register<LineSeries, double[]>(nameof(Dashes), null);
  38. /// <summary>
  39. /// Identifies the <see cref="Decimator"/> dependency property.
  40. /// </summary>
  41. public static readonly StyledProperty<Action<List<ScreenPoint>, List<ScreenPoint>>> DecimatorProperty = AvaloniaProperty.Register<LineSeries, Action<List<ScreenPoint>, List<ScreenPoint>>>(nameof(Decimator), null);
  42. /// <summary>
  43. /// Identifies the <see cref="LabelFormatString"/> dependency property.
  44. /// </summary>
  45. public static readonly StyledProperty<string> LabelFormatStringProperty = AvaloniaProperty.Register<LineSeries, string>(nameof(LabelFormatString));
  46. /// <summary>
  47. /// Identifies the <see cref="LabelMargin"/> dependency property.
  48. /// </summary>
  49. public static readonly StyledProperty<double> LabelMarginProperty = AvaloniaProperty.Register<LineSeries, double>(nameof(LabelMargin), 6.0);
  50. /// <summary>
  51. /// Identifies the <see cref="LineJoin"/> dependency property.
  52. /// </summary>
  53. public static readonly StyledProperty<LineJoin> LineJoinProperty = AvaloniaProperty.Register<LineSeries, LineJoin>(nameof(LineJoin), LineJoin.Bevel);
  54. /// <summary>
  55. /// Identifies the <see cref="LineLegendPosition"/> dependency property.
  56. /// </summary>
  57. public static readonly StyledProperty<LineLegendPosition> LineLegendPositionProperty = AvaloniaProperty.Register<LineSeries, LineLegendPosition>(nameof(LineLegendPosition), LineLegendPosition.None);
  58. /// <summary>
  59. /// Identifies the <see cref="LineStyle"/> dependency property.
  60. /// </summary>
  61. public static readonly StyledProperty<LineStyle> LineStyleProperty = AvaloniaProperty.Register<LineSeries, LineStyle>(nameof(LineStyle), LineStyle.Automatic);
  62. /// <summary>
  63. /// Identifies the <see cref="MarkerFill"/> dependency property.
  64. /// </summary>
  65. public static readonly StyledProperty<Color> MarkerFillProperty = AvaloniaProperty.Register<LineSeries, Color>(nameof(MarkerFill), MoreColors.Automatic);
  66. /// <summary>
  67. /// Identifies the <see cref="MarkerOutline"/> dependency property.
  68. /// </summary>
  69. public static readonly StyledProperty<Point[]> MarkerOutlineProperty = AvaloniaProperty.Register<LineSeries, Point[]>(nameof(MarkerOutline), null);
  70. /// <summary>
  71. /// Identifies the <see cref="MarkerResolution"/> dependency property.
  72. /// </summary>
  73. public static readonly StyledProperty<int> MarkerResolutionProperty = AvaloniaProperty.Register<LineSeries, int>(nameof(MarkerResolution), 0);
  74. /// <summary>
  75. /// Identifies the <see cref="MarkerSize"/> dependency property.
  76. /// </summary>
  77. public static readonly StyledProperty<double> MarkerSizeProperty = AvaloniaProperty.Register<LineSeries, double>(nameof(MarkerSize), 3.0);
  78. /// <summary>
  79. /// Identifies the <see cref="MarkerStroke"/> dependency property.
  80. /// </summary>
  81. public static readonly StyledProperty<Color> MarkerStrokeProperty = AvaloniaProperty.Register<LineSeries, Color>(nameof(MarkerStroke), MoreColors.Automatic);
  82. /// <summary>
  83. /// Identifies the <see cref="MarkerStrokeThickness"/> dependency property.
  84. /// </summary>
  85. public static readonly StyledProperty<double> MarkerStrokeThicknessProperty = AvaloniaProperty.Register<LineSeries, double>(nameof(MarkerStrokeThickness), 1.0);
  86. /// <summary>
  87. /// Identifies the <see cref="MarkerType"/> dependency property.
  88. /// </summary>
  89. public static readonly StyledProperty<MarkerType> MarkerTypeProperty = AvaloniaProperty.Register<LineSeries, MarkerType>(nameof(MarkerType), MarkerType.None);
  90. /// <summary>
  91. /// Identifies the <see cref="MinimumSegmentLength"/> dependency property.
  92. /// </summary>
  93. public static readonly StyledProperty<double> MinimumSegmentLengthProperty = AvaloniaProperty.Register<LineSeries, double>(nameof(MinimumSegmentLength), 2.0);
  94. /// <summary>
  95. /// Identifies the <see cref="InterpolationAlgorithm"/> dependency property.
  96. /// </summary>
  97. public static readonly StyledProperty<IInterpolationAlgorithm> InterpolationAlgorithmProperty = AvaloniaProperty.Register<LineSeries, IInterpolationAlgorithm>(nameof(InterpolationAlgorithm), null);
  98. /// <summary>
  99. /// Identifies the <see cref="StrokeThickness"/> dependency property.
  100. /// </summary>
  101. public static readonly StyledProperty<double> StrokeThicknessProperty = AvaloniaProperty.Register<LineSeries, double>(nameof(StrokeThickness), 2.0);
  102. /// <summary>
  103. /// Initializes static members of the <see cref="LineSeries" /> class.
  104. /// </summary>
  105. static LineSeries()
  106. {
  107. CanTrackerInterpolatePointsProperty.OverrideMetadata(typeof(LineSeries), new StyledPropertyMetadata<bool>(true));
  108. BrokenLineColorProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  109. BrokenLineStyleProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  110. BrokenLineThicknessProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  111. DashesProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  112. DecimatorProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  113. LineJoinProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  114. LineStyleProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  115. MarkerFillProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  116. MarkerOutlineProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  117. MarkerResolutionProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  118. MarkerSizeProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  119. MarkerStrokeProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  120. MarkerStrokeThicknessProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  121. MarkerTypeProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  122. MinimumSegmentLengthProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  123. StrokeThicknessProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  124. CanTrackerInterpolatePointsProperty.Changed.AddClassHandler<LineSeries>(AppearanceChanged);
  125. }
  126. /// <summary>
  127. /// Initializes a new instance of the <see cref="LineSeries" /> class.
  128. /// </summary>
  129. public LineSeries()
  130. {
  131. this.InternalSeries = new OxyPlot.Series.LineSeries();
  132. }
  133. /// <summary>
  134. /// Gets or sets the broken line color.
  135. /// </summary>
  136. public Color BrokenLineColor
  137. {
  138. get
  139. {
  140. return GetValue(BrokenLineColorProperty);
  141. }
  142. set
  143. {
  144. SetValue(BrokenLineColorProperty, value);
  145. }
  146. }
  147. /// <summary>
  148. /// Gets or sets the broken line style.
  149. /// </summary>
  150. public LineStyle BrokenLineStyle
  151. {
  152. get
  153. {
  154. return GetValue(BrokenLineStyleProperty);
  155. }
  156. set
  157. {
  158. SetValue(BrokenLineStyleProperty, value);
  159. }
  160. }
  161. /// <summary>
  162. /// Gets or sets the broken line thickness.
  163. /// </summary>
  164. public double BrokenLineThickness
  165. {
  166. get
  167. {
  168. return GetValue(BrokenLineThicknessProperty);
  169. }
  170. set
  171. {
  172. SetValue(BrokenLineThicknessProperty, value);
  173. }
  174. }
  175. /// <summary>
  176. /// Gets or sets Dashes.
  177. /// </summary>
  178. public double[] Dashes
  179. {
  180. get
  181. {
  182. return GetValue(DashesProperty);
  183. }
  184. set
  185. {
  186. SetValue(DashesProperty, value);
  187. }
  188. }
  189. /// <summary>
  190. /// Gets or sets the decimator.
  191. /// </summary>
  192. /// <value>
  193. /// The decimator.
  194. /// </value>
  195. public Action<List<ScreenPoint>, List<ScreenPoint>> Decimator
  196. {
  197. get { return GetValue(DecimatorProperty); }
  198. set { SetValue(DecimatorProperty, value); }
  199. }
  200. /// <summary>
  201. /// Gets or sets the label format string.
  202. /// </summary>
  203. /// <value>The label format string.</value>
  204. public string LabelFormatString
  205. {
  206. get
  207. {
  208. return GetValue(LabelFormatStringProperty);
  209. }
  210. set
  211. {
  212. SetValue(LabelFormatStringProperty, value);
  213. }
  214. }
  215. /// <summary>
  216. /// Gets or sets the label margin.
  217. /// </summary>
  218. /// <value>The label margin.</value>
  219. public double LabelMargin
  220. {
  221. get
  222. {
  223. return GetValue(LabelMarginProperty);
  224. }
  225. set
  226. {
  227. SetValue(LabelMarginProperty, value);
  228. }
  229. }
  230. /// <summary>
  231. /// Gets or sets LineJoin.
  232. /// </summary>
  233. public LineJoin LineJoin
  234. {
  235. get
  236. {
  237. return GetValue(LineJoinProperty);
  238. }
  239. set
  240. {
  241. SetValue(LineJoinProperty, value);
  242. }
  243. }
  244. /// <summary>
  245. /// Gets or sets LineLegendPosition.
  246. /// </summary>
  247. public LineLegendPosition LineLegendPosition
  248. {
  249. get
  250. {
  251. return GetValue(LineLegendPositionProperty);
  252. }
  253. set
  254. {
  255. SetValue(LineLegendPositionProperty, value);
  256. }
  257. }
  258. /// <summary>
  259. /// Gets or sets LineStyle.
  260. /// </summary>
  261. public LineStyle LineStyle
  262. {
  263. get
  264. {
  265. return GetValue(LineStyleProperty);
  266. }
  267. set
  268. {
  269. SetValue(LineStyleProperty, value);
  270. }
  271. }
  272. /// <summary>
  273. /// Gets or sets MarkerFill.
  274. /// </summary>
  275. public Color MarkerFill
  276. {
  277. get
  278. {
  279. return GetValue(MarkerFillProperty);
  280. }
  281. set
  282. {
  283. SetValue(MarkerFillProperty, value);
  284. }
  285. }
  286. /// <summary>
  287. /// Gets or sets MarkerOutline.
  288. /// </summary>
  289. public Point[] MarkerOutline
  290. {
  291. get
  292. {
  293. return GetValue(MarkerOutlineProperty);
  294. }
  295. set
  296. {
  297. SetValue(MarkerOutlineProperty, value);
  298. }
  299. }
  300. /// <summary>
  301. /// Gets or sets the marker resolution.
  302. /// </summary>
  303. /// <value>The marker resolution.</value>
  304. public int MarkerResolution
  305. {
  306. get { return GetValue(MarkerResolutionProperty); }
  307. set { SetValue(MarkerResolutionProperty, value); }
  308. }
  309. /// <summary>
  310. /// Gets or sets the marker size.
  311. /// </summary>
  312. public double MarkerSize
  313. {
  314. get
  315. {
  316. return GetValue(MarkerSizeProperty);
  317. }
  318. set
  319. {
  320. SetValue(MarkerSizeProperty, value);
  321. }
  322. }
  323. /// <summary>
  324. /// Gets or sets MarkerStroke.
  325. /// </summary>
  326. public Color MarkerStroke
  327. {
  328. get
  329. {
  330. return GetValue(MarkerStrokeProperty);
  331. }
  332. set
  333. {
  334. SetValue(MarkerStrokeProperty, value);
  335. }
  336. }
  337. /// <summary>
  338. /// Gets or sets MarkerStrokeThickness.
  339. /// </summary>
  340. public double MarkerStrokeThickness
  341. {
  342. get
  343. {
  344. return GetValue(MarkerStrokeThicknessProperty);
  345. }
  346. set
  347. {
  348. SetValue(MarkerStrokeThicknessProperty, value);
  349. }
  350. }
  351. /// <summary>
  352. /// Gets or sets MarkerType.
  353. /// </summary>
  354. public MarkerType MarkerType
  355. {
  356. get
  357. {
  358. return GetValue(MarkerTypeProperty);
  359. }
  360. set
  361. {
  362. SetValue(MarkerTypeProperty, value);
  363. }
  364. }
  365. /// <summary>
  366. /// Gets or sets MinimumSegmentLength.
  367. /// </summary>
  368. public double MinimumSegmentLength
  369. {
  370. get
  371. {
  372. return GetValue(MinimumSegmentLengthProperty);
  373. }
  374. set
  375. {
  376. SetValue(MinimumSegmentLengthProperty, value);
  377. }
  378. }
  379. /// <summary>
  380. /// Gets or sets a value the interpolation algorithm.
  381. /// </summary>
  382. /// <value>Interpolation algorithm.</value>
  383. public IInterpolationAlgorithm InterpolationAlgorithm
  384. {
  385. get
  386. {
  387. return this.GetValue(InterpolationAlgorithmProperty);
  388. }
  389. set
  390. {
  391. this.SetValue(InterpolationAlgorithmProperty, value);
  392. }
  393. }
  394. /// <summary>
  395. /// Gets or sets StrokeThickness.
  396. /// </summary>
  397. public double StrokeThickness
  398. {
  399. get
  400. {
  401. return GetValue(StrokeThicknessProperty);
  402. }
  403. set
  404. {
  405. SetValue(StrokeThicknessProperty, value);
  406. }
  407. }
  408. /// <summary>
  409. /// Creates the internal series.
  410. /// </summary>
  411. /// <returns>The internal series.</returns>
  412. public override OxyPlot.Series.Series CreateModel()
  413. {
  414. SynchronizeProperties(InternalSeries);
  415. return InternalSeries;
  416. }
  417. /// <summary>
  418. /// Synchronizes the properties.
  419. /// </summary>
  420. /// <param name="series">The series.</param>
  421. protected override void SynchronizeProperties(OxyPlot.Series.Series series)
  422. {
  423. base.SynchronizeProperties(series);
  424. var s = (OxyPlot.Series.LineSeries)series;
  425. s.Color = Color.ToOxyColor();
  426. s.StrokeThickness = StrokeThickness;
  427. s.LineStyle = LineStyle;
  428. s.MarkerResolution = MarkerResolution;
  429. s.MarkerSize = MarkerSize;
  430. s.MarkerStroke = MarkerStroke.ToOxyColor();
  431. s.MarkerType = MarkerType;
  432. s.MarkerStrokeThickness = MarkerStrokeThickness;
  433. s.Dashes = Dashes;
  434. s.LineJoin = LineJoin;
  435. s.MarkerFill = MarkerFill.ToOxyColor();
  436. s.MarkerOutline = (MarkerOutline ?? Enumerable.Empty<Point>()).Select(point => point.ToScreenPoint()).ToArray();
  437. s.MinimumSegmentLength = MinimumSegmentLength;
  438. s.LabelFormatString = LabelFormatString;
  439. s.LabelMargin = LabelMargin;
  440. s.LineLegendPosition = LineLegendPosition;
  441. s.BrokenLineColor = BrokenLineColor.ToOxyColor();
  442. s.BrokenLineStyle = BrokenLineStyle;
  443. s.BrokenLineThickness = BrokenLineThickness;
  444. s.Decimator = Decimator;
  445. s.InterpolationAlgorithm = this.InterpolationAlgorithm;
  446. }
  447. }
  448. }