This repository was archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathPlotAxis.cs
More file actions
191 lines (171 loc) · 7.44 KB
/
PlotAxis.cs
File metadata and controls
191 lines (171 loc) · 7.44 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright © Microsoft Corporation. All Rights Reserved.
// Licensed under the MIT License.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Data;
namespace InteractiveDataDisplay.WPF
{
/// <summary>Facilitates drawing of vertical or horizontal coordinate axis connected to parent <see cref="Plot"/> element</summary>
[TemplatePart(Name = "PART_Axis", Type = typeof(Axis))]
[Description("Figure's axis")]
public sealed class PlotAxis : ContentControl
{
private PlotBase masterPlot = null;
private Axis axis;
/// <summary>
/// Initializes new instance of <see cref="PlotAxis"/> class
/// </summary>
public PlotAxis()
{
XDataTransform = new IdentityDataTransform();
YDataTransform = new IdentityDataTransform();
Ticks = new double[0];
DefaultStyleKey = typeof(PlotAxis);
Loaded += PlotAxisLoaded;
Unloaded += PlotAxisUnloaded;
}
private void PlotAxisUnloaded(object sender, RoutedEventArgs e)
{
masterPlot = null;
}
private void PlotAxisLoaded(object sender, RoutedEventArgs e)
{
masterPlot = PlotBase.FindMaster(this);
if (masterPlot != null)
InvalidateAxis();
}
/// <summary>
/// Measures the size in layout required for child elements and determines a size for parent.
/// </summary>
/// <param name="availableSize">The available size that this element can give to child elements. Infinity can be specified as a value to indicate that the element will size to whatever content is available.</param>
/// <returns>The size that this element determines it needs during layout, based on its calculations of child element sizes.</returns>
protected override Size MeasureOverride(Size availableSize)
{
InvalidateAxis();
return base.MeasureOverride(availableSize);
}
private void InvalidateAxis()
{
if (axis != null)
{
if (axis.AxisOrientation == AxisOrientation.Left ||
axis.AxisOrientation == AxisOrientation.Right)
{
if (masterPlot != null)
{
axis.Range = new Range(masterPlot.ActualPlotRect.YMin, masterPlot.ActualPlotRect.YMax);
}
axis.DataTransform = YDataTransform;
}
else
{
if (masterPlot != null)
{
axis.Range = new Range(masterPlot.ActualPlotRect.XMin, masterPlot.ActualPlotRect.XMax);
}
axis.DataTransform = XDataTransform;
}
}
}
/// <summary>
/// Invoked whenever application code or internal processes call ApplyTemplate
/// </summary>
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
axis = GetTemplateChild("PART_Axis") as Axis;
if (axis == null)
{
throw new InvalidOperationException("Invalid template!");
}
BindingOperations.SetBinding(this.axis, Axis.TicksProperty, new Binding("Ticks")
{
Source = this,
Mode = BindingMode.TwoWay
});
InvalidateAxis();
}
/// <summary>
/// Gets or sets a set of double values displayed as axis ticks.
/// </summary>
[Browsable(false)]
public IEnumerable<double> Ticks
{
get { return (IEnumerable<double>)GetValue(TicksProperty); }
set { SetValue(TicksProperty, value); }
}
/// <summary>Identify <see cref="Ticks"/> property</summary>
public static readonly DependencyProperty TicksProperty =
DependencyProperty.Register("Ticks", typeof(IEnumerable<double>), typeof(PlotAxis), new PropertyMetadata(new double[0]));
/// <summary>
/// Gets or Sets orientation of axis and location of labels
/// </summary>
[Category("InteractiveDataDisplay")]
[Description("Defines orientation of axis and location of labels")]
public AxisOrientation AxisOrientation
{
get { return (AxisOrientation)GetValue(AxisOrientationProperty); }
set { SetValue(AxisOrientationProperty, value); }
}
/// <summary>Identify <see cref="AxisOrientation"/> property</summary>
public static readonly DependencyProperty AxisOrientationProperty =
DependencyProperty.Register("AxisOrientation", typeof(AxisOrientation), typeof(PlotAxis), new PropertyMetadata(AxisOrientation.Bottom));
/// <summary>
/// Gets or Sets label format of axis
/// </summary>
[Category("InteractiveDataDisplay")]
[Description("Defines label format of axis")]
public ILabelProvider LabelProvider
{
get { return (ILabelProvider)GetValue(LabelProviderProperty); }
set { SetValue(LabelProviderProperty, value); }
}
/// <summary>Identify <see cref="LabelProvider"/> property</summary>
public static readonly DependencyProperty LabelProviderProperty =
DependencyProperty.Register("LabelProvider", typeof(ILabelProvider), typeof(PlotAxis), new PropertyMetadata(new LabelProvider()));
/// <summary>Gets or sets transform from user data to horizontal plot coordinate.
/// By default transform is <see cref="IdentityDataTransform"/>
/// </summary>
[Category("InteractiveDataDisplay")]
public DataTransform XDataTransform
{
get { return (DataTransform)GetValue(XDataTransformProperty); }
set { SetValue(XDataTransformProperty, value); }
}
/// <summary>Gets or sets transform from user data to vertical plot coordinate.
/// By default transform is <see cref="IdentityDataTransform"/>
/// </summary>
[Category("InteractiveDataDisplay")]
public DataTransform YDataTransform
{
get { return (DataTransform)GetValue(YDataTransformProperty); }
set { SetValue(YDataTransformProperty, value); }
}
/// <summary>Identify <see cref="XDataTransform"/> property</summary>
public static readonly DependencyProperty XDataTransformProperty =
DependencyProperty.Register("XDataTransform", typeof(DataTransform), typeof(PlotAxis), new PropertyMetadata(null,
(o, e) =>
{
PlotAxis plot = o as PlotAxis;
if (plot != null)
{
plot.InvalidateAxis();
}
}));
/// <summary>Identify <see cref="YDataTransform"/> property</summary>
public static readonly DependencyProperty YDataTransformProperty =
DependencyProperty.Register("YDataTransform", typeof(DataTransform), typeof(PlotAxis), new PropertyMetadata(null,
(o, e) =>
{
PlotAxis plot = o as PlotAxis;
if (plot != null)
{
plot.InvalidateAxis();
}
}));
}
}