PlotView.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PlotView.cs" company="OxyPlot">
  3. // Copyright (c) 2020 OxyPlot contributors
  4. // </copyright>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace OxyPlot.SkiaSharp.Wpf
  7. {
  8. using global::SkiaSharp;
  9. using global::SkiaSharp.Views.Desktop;
  10. using OxyPlot;
  11. using OxyPlot.SkiaSharp;
  12. using OxyPlot.Wpf;
  13. using System.Windows;
  14. /// <summary>
  15. /// Represents a control that displays a <see cref="PlotModel" />. This <see cref="IPlotView"/> is based on <see cref="SkiaSharp.SkiaRenderContext"/>.
  16. /// </summary>
  17. public partial class PlotView : PlotViewBase
  18. {
  19. /// <summary>
  20. /// Gets the SkiaRenderContext.
  21. /// </summary>
  22. private SkiaRenderContext SkiaRenderContext => (SkiaRenderContext)this.renderContext;
  23. /// <summary>
  24. /// Gets the OxySKElement.
  25. /// </summary>
  26. private OxySKElement OxySKElement => (OxySKElement)this.plotPresenter;
  27. /// <inheritdoc/>
  28. protected override void ClearBackground()
  29. {
  30. var color = this.ActualModel?.Background.IsVisible() == true
  31. ? this.ActualModel.Background.ToSKColor()
  32. : SKColors.Empty;
  33. this.SkiaRenderContext.SkCanvas.Clear(color);
  34. }
  35. /// <inheritdoc/>
  36. protected override FrameworkElement CreatePlotPresenter()
  37. {
  38. var skElement = new OxySKElement();
  39. skElement.PaintSurface += this.SkElement_PaintSurface;
  40. return skElement;
  41. }
  42. /// <inheritdoc/>
  43. protected override IRenderContext CreateRenderContext()
  44. {
  45. return new SkiaRenderContext();
  46. }
  47. /// <inheritdoc/>
  48. protected override void RenderOverride()
  49. {
  50. // Instead of rendering directly, invalidate the plot presenter.
  51. // Actual rendering is done in SkElement_PaintSurface.
  52. this.plotPresenter.InvalidateVisual();
  53. }
  54. /// <inheritdoc/>
  55. protected override double UpdateDpi()
  56. {
  57. var dpiScale = base.UpdateDpi();
  58. var renderScale = this.OxySKElement.GetRenderScale();
  59. var skiaScale = (float)(dpiScale * renderScale);
  60. this.SkiaRenderContext.DpiScale = skiaScale;
  61. return dpiScale;
  62. }
  63. /// <summary>
  64. /// This is called when the SKElement paints its surface.
  65. /// </summary>
  66. /// <param name="sender">The sender.</param>
  67. /// <param name="e">The surface paint event args.</param>
  68. private void SkElement_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
  69. {
  70. if (this.plotPresenter == null || this.renderContext == null)
  71. {
  72. return;
  73. }
  74. this.SkiaRenderContext.SkCanvas = e.Surface.Canvas;
  75. base.RenderOverride();
  76. this.SkiaRenderContext.SkCanvas = null;
  77. }
  78. }
  79. }