PlotBase.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PlotBase.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // Represents a control that displays a <see cref="PlotModel" />.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using Avalonia.Reactive;
  10. namespace OxyPlot.Avalonia
  11. {
  12. using global::Avalonia;
  13. using global::Avalonia.Controls;
  14. using global::Avalonia.Controls.Presenters;
  15. using global::Avalonia.Controls.Primitives;
  16. using global::Avalonia.Input;
  17. using global::Avalonia.Threading;
  18. using global::Avalonia.VisualTree;
  19. using System;
  20. using System.Collections.ObjectModel;
  21. using System.Linq;
  22. using System.Threading;
  23. /// <summary>
  24. /// Represents a control that displays a <see cref="PlotModel" />.
  25. /// </summary>
  26. public abstract partial class PlotBase : TemplatedControl, IPlotView
  27. {
  28. /// <summary>
  29. /// The Grid PART constant.
  30. /// </summary>
  31. protected const string PartPanel = "PART_Panel";
  32. /// <summary>
  33. /// The tracker definitions.
  34. /// </summary>
  35. private readonly ObservableCollection<TrackerDefinition> trackerDefinitions;
  36. /// <summary>
  37. /// The render context
  38. /// </summary>
  39. private CanvasRenderContext renderContext;
  40. /// <summary>
  41. /// The canvas.
  42. /// </summary>
  43. private Canvas canvas;
  44. /// <summary>
  45. /// The current tracker.
  46. /// </summary>
  47. private Control currentTracker;
  48. /// <summary>
  49. /// The grid.
  50. /// </summary>
  51. private Panel panel;
  52. /// <summary>
  53. /// Invalidation flag (0: no update, 1: update, 2: update date).
  54. /// </summary>
  55. private int isUpdateRequired;
  56. /// <summary>
  57. /// Invalidation flag (0: no update, 1: update visual elements).
  58. /// </summary>
  59. private int isPlotInvalidated;
  60. /// <summary>
  61. /// The mouse down point.
  62. /// </summary>
  63. private ScreenPoint mouseDownPoint;
  64. /// <summary>
  65. /// The overlays.
  66. /// </summary>
  67. private Canvas overlays;
  68. /// <summary>
  69. /// The zoom control.
  70. /// </summary>
  71. private ContentControl zoomControl;
  72. /// <summary>
  73. /// The is visible to user cache.
  74. /// </summary>
  75. private bool isVisibleToUserCache;
  76. /// <summary>
  77. /// The cached parent.
  78. /// </summary>
  79. private Control containerCache;
  80. /// <summary>
  81. /// Initializes a new instance of the <see cref="PlotBase" /> class.
  82. /// </summary>
  83. protected PlotBase()
  84. {
  85. DisconnectCanvasWhileUpdating = true;
  86. trackerDefinitions = new ObservableCollection<TrackerDefinition>();
  87. this.GetObservable(BoundsProperty).Subscribe(new AnonymousObserver<Rect>(OnSizeChanged));
  88. }
  89. /// <summary>
  90. /// Gets or sets a value indicating whether to disconnect the canvas while updating.
  91. /// </summary>
  92. /// <value><c>true</c> if canvas should be disconnected while updating; otherwise, <c>false</c>.</value>
  93. public bool DisconnectCanvasWhileUpdating { get; set; }
  94. /// <summary>
  95. /// Gets the actual model in the view.
  96. /// </summary>
  97. /// <value>
  98. /// The actual model.
  99. /// </value>
  100. Model IView.ActualModel
  101. {
  102. get
  103. {
  104. return ActualModel;
  105. }
  106. }
  107. /// <summary>
  108. /// Gets the actual model.
  109. /// </summary>
  110. /// <value>The actual model.</value>
  111. public abstract PlotModel ActualModel { get; }
  112. /// <summary>
  113. /// Gets the actual controller.
  114. /// </summary>
  115. /// <value>
  116. /// The actual <see cref="IController" />.
  117. /// </value>
  118. IController IView.ActualController
  119. {
  120. get
  121. {
  122. return ActualController;
  123. }
  124. }
  125. /// <summary>
  126. /// Gets the actual PlotView controller.
  127. /// </summary>
  128. /// <value>The actual PlotView controller.</value>
  129. public abstract IPlotController ActualController { get; }
  130. /// <summary>
  131. /// Gets the coordinates of the client area of the view.
  132. /// </summary>
  133. public OxyRect ClientArea
  134. {
  135. get
  136. {
  137. return new OxyRect(0, 0, Bounds.Width, Bounds.Height);
  138. }
  139. }
  140. /// <summary>
  141. /// Gets the tracker definitions.
  142. /// </summary>
  143. /// <value>The tracker definitions.</value>
  144. public ObservableCollection<TrackerDefinition> TrackerDefinitions
  145. {
  146. get
  147. {
  148. return trackerDefinitions;
  149. }
  150. }
  151. /// <summary>
  152. /// Hides the tracker.
  153. /// </summary>
  154. public void HideTracker()
  155. {
  156. if (currentTracker != null)
  157. {
  158. overlays.Children.Remove(currentTracker);
  159. currentTracker = null;
  160. }
  161. }
  162. /// <summary>
  163. /// Hides the zoom rectangle.
  164. /// </summary>
  165. public void HideZoomRectangle()
  166. {
  167. zoomControl.IsVisible = false;
  168. }
  169. /// <summary>
  170. /// Pans all axes.
  171. /// </summary>
  172. /// <param name="delta">The delta.</param>
  173. public void PanAllAxes(Vector delta)
  174. {
  175. ActualModel?.PanAllAxes(delta.X, delta.Y);
  176. InvalidatePlot(false);
  177. }
  178. /// <summary>
  179. /// Zooms all axes.
  180. /// </summary>
  181. /// <param name="factor">The zoom factor.</param>
  182. public void ZoomAllAxes(double factor)
  183. {
  184. ActualModel?.ZoomAllAxes(factor);
  185. InvalidatePlot(false);
  186. }
  187. /// <summary>
  188. /// Resets all axes.
  189. /// </summary>
  190. public void ResetAllAxes()
  191. {
  192. ActualModel?.ResetAllAxes();
  193. InvalidatePlot(false);
  194. }
  195. /// <summary>
  196. /// Invalidate the PlotView (not blocking the UI thread)
  197. /// </summary>
  198. /// <param name="updateData">The update Data.</param>
  199. public void InvalidatePlot(bool updateData = true)
  200. {
  201. // perform update on UI thread
  202. var updateState = updateData ? 2 : 1;
  203. int currentState = isUpdateRequired;
  204. while (currentState < updateState)
  205. {
  206. if (Interlocked.CompareExchange(ref isUpdateRequired, updateState, currentState) == currentState)
  207. {
  208. BeginInvoke(() => UpdateModel(updateData));
  209. break;
  210. }
  211. else
  212. {
  213. currentState = isUpdateRequired;
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// When overridden in a derived class, is invoked whenever application code or internal processes (such as a rebuilding layout pass)
  219. /// call <see cref="M:System.Windows.Controls.Control.ApplyTemplate" /> . In simplest terms, this means the method is called
  220. /// just before a UI element displays in an application. For more information, see Remarks.
  221. /// </summary>
  222. /// <param name="e">Event data for applying the template.</param>
  223. protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
  224. {
  225. base.OnApplyTemplate(e);
  226. panel = e.NameScope.Find(PartPanel) as Panel;
  227. if (panel == null)
  228. {
  229. return;
  230. }
  231. canvas = new Canvas();
  232. panel.Children.Add(canvas);
  233. renderContext = new CanvasRenderContext(canvas);
  234. overlays = new Canvas { Name = "Overlays" };
  235. panel.Children.Add(overlays);
  236. zoomControl = new ContentControl();
  237. overlays.Children.Add(zoomControl);
  238. }
  239. /// <summary>
  240. /// Sets the cursor type.
  241. /// </summary>
  242. /// <param name="cursorType">The cursor type.</param>
  243. public void SetCursorType(CursorType cursorType)
  244. {
  245. switch (cursorType)
  246. {
  247. case CursorType.Pan:
  248. Cursor = PanCursor;
  249. break;
  250. case CursorType.ZoomRectangle:
  251. Cursor = ZoomRectangleCursor;
  252. break;
  253. case CursorType.ZoomHorizontal:
  254. Cursor = ZoomHorizontalCursor;
  255. break;
  256. case CursorType.ZoomVertical:
  257. Cursor = ZoomVerticalCursor;
  258. break;
  259. default:
  260. Cursor = Cursor.Default;
  261. break;
  262. }
  263. }
  264. /// <summary>
  265. /// Shows the tracker.
  266. /// </summary>
  267. /// <param name="trackerHitResult">The tracker data.</param>
  268. public void ShowTracker(TrackerHitResult trackerHitResult)
  269. {
  270. if (trackerHitResult == null)
  271. {
  272. HideTracker();
  273. return;
  274. }
  275. var trackerTemplate = DefaultTrackerTemplate;
  276. if (trackerHitResult.Series != null && !string.IsNullOrEmpty(trackerHitResult.Series.TrackerKey))
  277. {
  278. var match = TrackerDefinitions.FirstOrDefault(t => t.TrackerKey == trackerHitResult.Series.TrackerKey);
  279. if (match != null)
  280. {
  281. trackerTemplate = match.TrackerTemplate;
  282. }
  283. }
  284. if (trackerTemplate == null)
  285. {
  286. HideTracker();
  287. return;
  288. }
  289. var tracker = trackerTemplate.Build(new ContentControl());
  290. // ReSharper disable once RedundantNameQualifier
  291. if (!object.ReferenceEquals(tracker, currentTracker))
  292. {
  293. HideTracker();
  294. overlays.Children.Add(tracker.Result);
  295. currentTracker = tracker.Result;
  296. }
  297. if (currentTracker != null)
  298. {
  299. currentTracker.DataContext = trackerHitResult;
  300. }
  301. }
  302. /// <summary>
  303. /// Shows the zoom rectangle.
  304. /// </summary>
  305. /// <param name="r">The rectangle.</param>
  306. public void ShowZoomRectangle(OxyRect r)
  307. {
  308. zoomControl.Width = r.Width;
  309. zoomControl.Height = r.Height;
  310. Canvas.SetLeft(zoomControl, r.Left);
  311. Canvas.SetTop(zoomControl, r.Top);
  312. zoomControl.Template = ZoomRectangleTemplate;
  313. zoomControl.IsVisible = true;
  314. }
  315. /// <summary>
  316. /// Stores text on the clipboard.
  317. /// </summary>
  318. /// <param name="text">The text.</param>
  319. public async void SetClipboardText(string text)
  320. {
  321. if (TopLevel.GetTopLevel(this) is { Clipboard: { } clipboard })
  322. {
  323. await clipboard.SetTextAsync(text).ConfigureAwait(true);
  324. }
  325. }
  326. /// <summary>
  327. /// Provides the behavior for the Arrange pass of Silverlight layout. Classes can override this method to define their own Arrange pass behavior.
  328. /// </summary>
  329. /// <param name="finalSize">The final area within the parent that this object should use to arrange itself and its children.</param>
  330. /// <returns>The actual size that is used after the element is arranged in layout.</returns>
  331. protected override Size ArrangeOverride(Size finalSize)
  332. {
  333. var actualSize = base.ArrangeOverride(finalSize);
  334. if (actualSize.Width > 0 && actualSize.Height > 0)
  335. {
  336. if (Interlocked.CompareExchange(ref isPlotInvalidated, 0, 1) == 1)
  337. {
  338. UpdateVisuals();
  339. }
  340. }
  341. return actualSize;
  342. }
  343. /// <summary>
  344. /// Updates the model.
  345. /// </summary>
  346. /// <param name="updateData">The update Data.</param>
  347. protected void UpdateModel(bool updateData = true)
  348. {
  349. if (Width <= 0 || Height <= 0 || ActualModel == null)
  350. {
  351. isUpdateRequired = 0;
  352. return;
  353. }
  354. lock (this.ActualModel.SyncRoot)
  355. {
  356. var updateState = (Interlocked.Exchange(ref isUpdateRequired, 0));
  357. if (updateState > 0)
  358. {
  359. ((IPlotModel)ActualModel).Update(updateState == 2 || updateData);
  360. }
  361. }
  362. if (Interlocked.CompareExchange(ref isPlotInvalidated, 1, 0) == 0)
  363. {
  364. // Invalidate the arrange state for the element.
  365. // After the invalidation, the element will have its layout updated,
  366. // which will occur asynchronously unless subsequently forced by UpdateLayout.
  367. BeginInvoke(InvalidateArrange);
  368. }
  369. }
  370. /// <summary>
  371. /// Determines whether the plot is currently visible to the user.
  372. /// </summary>
  373. /// <returns><c>true</c> if the plot is currently visible to the user; otherwise, <c>false</c>.</returns>
  374. protected bool IsVisibleToUser()
  375. {
  376. return IsUserVisible(this);
  377. }
  378. /// <summary>
  379. /// Determines whether the specified element is currently visible to the user.
  380. /// </summary>
  381. /// <param name="element">The element.</param>
  382. /// <returns><c>true</c> if the specified element is currently visible to the user; otherwise, <c>false</c>.</returns>
  383. private static bool IsUserVisible(Control element)
  384. {
  385. return element.IsEffectivelyVisible;
  386. }
  387. /// <summary>
  388. /// Called when the size of the control is changed.
  389. /// </summary>
  390. /// <param name="sender">The sender.</param>
  391. /// <param name="size">The new size</param>
  392. private void OnSizeChanged(Rect size)
  393. {
  394. if (size.Height > 0 && size.Width > 0)
  395. {
  396. InvalidatePlot(false);
  397. }
  398. }
  399. /// <summary>
  400. /// Gets the relevant parent.
  401. /// </summary>
  402. /// <typeparam name="T">Type of the relevant parent</typeparam>
  403. /// <param name="obj">The object.</param>
  404. /// <returns>The relevant parent.</returns>
  405. private Control GetRelevantParent<T>(Visual obj)
  406. where T : Control
  407. {
  408. var container = obj.GetVisualParent();
  409. if (container is ContentPresenter contentPresenter)
  410. {
  411. container = GetRelevantParent<T>(contentPresenter);
  412. }
  413. if (container is Panel panel)
  414. {
  415. container = GetRelevantParent<ScrollViewer>(panel);
  416. }
  417. if (!(container is T) && (container != null))
  418. {
  419. container = GetRelevantParent<T>(container);
  420. }
  421. return (Control)container;
  422. }
  423. /// <summary>
  424. /// Updates the visuals.
  425. /// </summary>
  426. private void UpdateVisuals()
  427. {
  428. if (canvas == null || renderContext == null)
  429. {
  430. return;
  431. }
  432. if (!IsVisibleToUser())
  433. {
  434. return;
  435. }
  436. // Clear the canvas
  437. canvas.Children.Clear();
  438. if (ActualModel?.Background.IsVisible() == true)
  439. {
  440. canvas.Background = ActualModel.Background.ToBrush();
  441. }
  442. else
  443. {
  444. canvas.Background = null;
  445. }
  446. if (ActualModel != null)
  447. {
  448. lock (this.ActualModel.SyncRoot)
  449. {
  450. var updateState = (Interlocked.Exchange(ref isUpdateRequired, 0));
  451. if (updateState > 0)
  452. {
  453. ((IPlotModel)ActualModel).Update(updateState == 2);
  454. }
  455. if (DisconnectCanvasWhileUpdating)
  456. {
  457. // TODO: profile... not sure if this makes any difference
  458. var idx = panel.Children.IndexOf(canvas);
  459. if (idx != -1)
  460. {
  461. panel.Children.RemoveAt(idx);
  462. }
  463. ((IPlotModel)ActualModel).Render(renderContext, new OxyRect(0, 0, canvas.Bounds.Width, canvas.Bounds.Height));
  464. // reinsert the canvas again
  465. if (idx != -1)
  466. {
  467. panel.Children.Insert(idx, canvas);
  468. }
  469. }
  470. else
  471. {
  472. ((IPlotModel)ActualModel).Render(renderContext, new OxyRect(0, 0, canvas.Bounds.Width, canvas.Bounds.Height));
  473. }
  474. }
  475. }
  476. }
  477. /// <summary>
  478. /// Invokes the specified action on the dispatcher, if necessary.
  479. /// </summary>
  480. /// <param name="action">The action.</param>
  481. private static void BeginInvoke(Action action)
  482. {
  483. if (Dispatcher.UIThread.CheckAccess())
  484. {
  485. action?.Invoke();
  486. }
  487. else
  488. {
  489. Dispatcher.UIThread.InvokeAsync(action, DispatcherPriority.Loaded);
  490. }
  491. }
  492. }
  493. }