-
Notifications
You must be signed in to change notification settings - Fork 948
/
PlotView.cs
163 lines (144 loc) · 5.8 KB
/
PlotView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PlotView.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Represents a control that displays a <see cref="PlotModel" />.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot.Wpf
{
using OxyPlot;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
/// <summary>
/// Represents a control that displays a <see cref="PlotModel" />. This <see cref="IPlotView"/> is based on <see cref="CanvasRenderContext"/>.
/// </summary>
public partial class PlotView : PlotViewBase
{
/// <summary>
/// Identifies the <see cref="TextMeasurementMethod"/> dependency property.
/// </summary>
public static readonly DependencyProperty TextMeasurementMethodProperty =
DependencyProperty.Register(
nameof(TextMeasurementMethod), typeof(TextMeasurementMethod), typeof(PlotViewBase), new PropertyMetadata(TextMeasurementMethod.TextBlock));
/// <summary>
/// Initializes a new instance of the <see cref="PlotView" /> class.
/// </summary>
public PlotView()
{
this.DisconnectCanvasWhileUpdating = true;
this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, this.DoCopy));
}
/// <summary>
/// Gets or sets a value indicating whether to disconnect the canvas while updating.
/// </summary>
/// <value><c>true</c> if canvas should be disconnected while updating; otherwise, <c>false</c>.</value>
public bool DisconnectCanvasWhileUpdating { get; set; }
/// <summary>
/// Gets or sets the vertical zoom cursor.
/// </summary>
/// <value>The zoom vertical cursor.</value>
public TextMeasurementMethod TextMeasurementMethod
{
get => (TextMeasurementMethod)this.GetValue(TextMeasurementMethodProperty);
set => this.SetValue(TextMeasurementMethodProperty, value);
}
/// <summary>
/// Gets the Canvas.
/// </summary>
protected Canvas Canvas => (Canvas)this.plotPresenter;
/// <summary>
/// Gets the CanvasRenderContext.
/// </summary>
private CanvasRenderContext RenderContext => (CanvasRenderContext)this.renderContext;
/// <inheritdoc/>
protected override void ClearBackground()
{
this.Canvas.Children.Clear();
if (this.ActualModel != null && this.ActualModel.Background.IsVisible())
{
this.Canvas.Background = this.ActualModel.Background.ToBrush();
}
else
{
this.Canvas.Background = null;
}
}
/// <inheritdoc/>
protected override FrameworkElement CreatePlotPresenter()
{
return new Canvas();
}
/// <inheritdoc/>
protected override IRenderContext CreateRenderContext()
{
return new CanvasRenderContext(this.Canvas);
}
/// <inheritdoc/>
protected override void OnRender(DrawingContext drawingContext)
{
this.Render();
base.OnRender(drawingContext);
}
/// <inheritdoc/>
protected override void RenderOverride()
{
this.RenderContext.TextMeasurementMethod = this.TextMeasurementMethod;
if (this.DisconnectCanvasWhileUpdating)
{
// TODO: profile... not sure if this makes any difference
var idx = this.grid.Children.IndexOf(this.plotPresenter);
if (idx != -1)
{
this.grid.Children.RemoveAt(idx);
}
base.RenderOverride();
if (idx != -1)
{
// reinsert the canvas again
this.grid.Children.Insert(idx, this.plotPresenter);
}
}
else
{
base.RenderOverride();
}
}
/// <inheritdoc/>
protected override double UpdateDpi()
{
var scale = base.UpdateDpi();
this.RenderContext.DpiScale = scale;
this.RenderContext.VisualOffset = this.TransformToAncestor(this.GetAncestorWindowFromVisualTree(this)).Transform(default);
return scale;
}
/// <summary>
/// Performs the copy operation.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs" /> instance containing the event data.</param>
private void DoCopy(object sender, ExecutedRoutedEventArgs e)
{
var exporter = new PngExporter() { Width = (int)this.ActualWidth, Height = (int)this.ActualHeight };
var bitmap = exporter.ExportToBitmap(this.ActualModel);
Clipboard.SetImage(bitmap);
}
/// <summary>
/// Returns a reference to the window object that hosts the dependency object in the visual tree.
/// </summary>
/// <returns> The host window from the visual tree.</returns>
private Window GetAncestorWindowFromVisualTree(DependencyObject startElement)
{
DependencyObject parent = startElement;
while (!(parent is Window))
{
if (parent == null) { break; }
parent = VisualTreeHelper.GetParent(parent);
}
return parent as Window ?? Window.GetWindow(this);
}
}
}