forked from oxyplot/oxyplot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPolygonSeries.cs
More file actions
395 lines (342 loc) · 14 KB
/
PolygonSeries.cs
File metadata and controls
395 lines (342 loc) · 14 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
namespace OxyPlot.Series
{
using System;
using System.Collections.Generic;
using System.Linq;
using Axes;
/// <summary>
/// Represents a series that can be bound to a collection of <see cref="PolygonItem"/>.
/// </summary>
public class PolygonSeries : XYAxisSeries
{
/// <summary>
/// The items originating from the items source.
/// </summary>
private List<PolygonItem> actualItems;
/// <summary>
/// Specifies if the <see cref="actualItems" /> list can be modified.
/// </summary>
private bool ownsActualItems;
/// <summary>
/// The default tracker format string
/// </summary>
public new const string DefaultTrackerFormatString = "{0}\n{1}: {2}\n{3}: {4}\n{5}: {6}";
/// <summary>
/// The default color-axis title
/// </summary>
private const string DefaultColorAxisTitle = "Value";
/// <summary>
/// Initializes a new instance of the <see cref="HeatMapSeries" /> class.
/// </summary>
public PolygonSeries()
{
this.TrackerFormatString = DefaultTrackerFormatString;
this.LabelFormatString = "0.00";
this.LabelFontSize = 0;
this.Stroke = OxyColors.Undefined;
this.Fill = OxyColors.Undefined;
this.StrokeThickness = 2;
}
/// <summary>
/// Gets the minimum value of the dataset.
/// </summary>
public double MinValue { get; private set; }
/// <summary>
/// Gets the maximum value of the dataset.
/// </summary>
public double MaxValue { get; private set; }
/// <summary>
/// Gets or sets the color axis.
/// </summary>
/// <value>The color axis.</value>
public IColorAxis ColorAxis { get; protected set; }
/// <summary>
/// Gets or sets the color axis key.
/// </summary>
/// <value>The color axis key.</value>
public string ColorAxisKey { get; set; }
/// <summary>
/// Gets or sets the format string for the cell labels. The default value is <c>0.00</c>.
/// </summary>
/// <value>The format string.</value>
/// <remarks>The label format string is only used when <see cref="LabelFontSize" /> is greater than 0.</remarks>
public string LabelFormatString { get; set; }
/// <summary>
/// Gets or sets the font size of the labels. The default value is <c>0</c> (labels not visible).
/// </summary>
/// <value>The font size relative to the cell height.</value>
public double LabelFontSize { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the tracker can interpolate points.
/// </summary>
public bool CanTrackerInterpolatePoints { get; set; }
/// <summary>
/// Gets or sets the delegate used to map from <see cref="ItemsSeries.ItemsSource" /> to <see cref="PolygonItem" />. The default is <c>null</c>.
/// </summary>
/// <value>The mapping.</value>
public Func<object, PolygonItem> Mapping { get; set; }
/// <summary>
/// Gets the list of Polygons.
/// </summary>
/// <value>A list of <see cref="PolygonItem" />. This list is used if <see cref="ItemsSeries.ItemsSource" /> is not set.</value>
public List<PolygonItem> Items { get; } = new List<PolygonItem>();
/// <summary>
/// Gets or sets the stroke. The default is <c>OxyColors.Undefined</c>.
/// </summary>
/// <value>The stroke.</value>
public OxyColor Stroke { get; set; }
/// <summary>
/// Gets or sets the fill color of the polygon.
/// </summary>
/// <value>The fill color.</value>
public OxyColor Fill { get; set; }
/// <summary>
/// Gets or sets the stroke thickness. The default is <c>2</c>.
/// </summary>
/// <value>The stroke thickness.</value>
public double StrokeThickness { get; set; }
/// <summary>
/// Gets the list of Polygons that should be rendered.
/// </summary>
/// <value>A list of <see cref="PolygonItem" />.</value>
protected List<PolygonItem> ActualItems => this.ItemsSource != null ? this.actualItems : this.Items;
/// <summary>
/// Renders the series on the specified rendering context.
/// </summary>
/// <param name="rc">The rendering context.</param>
public override void Render(IRenderContext rc)
{
this.VerifyAxes();
this.RenderPolygons(rc, this.ActualItems);
}
/// <summary>
/// Updates the data.
/// </summary>
protected internal override void UpdateData()
{
if (this.ItemsSource == null)
{
return;
}
this.UpdateActualItems();
}
/// <summary>
/// Gets the item at the specified index.
/// </summary>
/// <param name="i">The index of the item.</param>
/// <returns>The item of the index.</returns>
protected override object GetItem(int i)
{
var items = this.ActualItems;
if (this.ItemsSource == null && items != null && i < items.Count)
{
return items[i];
}
return base.GetItem(i);
}
/// <summary>
/// Clears or creates the <see cref="actualItems"/> list.
/// </summary>
private void ClearActualItems()
{
if (!this.ownsActualItems || this.actualItems == null)
{
this.actualItems = new List<PolygonItem>();
}
else
{
this.actualItems.Clear();
}
this.ownsActualItems = true;
}
/// <summary>
/// Updates the points from the <see cref="ItemsSeries.ItemsSource" />.
/// </summary>
private void UpdateActualItems()
{
// Use the Mapping property to generate the points
if (this.Mapping != null)
{
this.ClearActualItems();
foreach (var item in this.ItemsSource)
{
this.actualItems.Add(this.Mapping(item));
}
return;
}
var sourceAsListOfDataPolys = this.ItemsSource as List<PolygonItem>;
if (sourceAsListOfDataPolys != null)
{
this.actualItems = sourceAsListOfDataPolys;
this.ownsActualItems = false;
return;
}
this.ClearActualItems();
var sourceAsEnumerableDataRects = this.ItemsSource as IEnumerable<PolygonItem>;
if (sourceAsEnumerableDataRects != null)
{
this.actualItems.AddRange(sourceAsEnumerableDataRects);
}
}
/// <summary>
/// Renders the points as line, broken line and markers.
/// </summary>
/// <param name="rc">The rendering context.</param>
/// <param name="items">The Items to render.</param>
protected void RenderPolygons(IRenderContext rc, ICollection<PolygonItem> items)
{
var screenPointOutline = new List<ScreenPoint>();
foreach (var item in items)
{
var polyColor = this.Fill.IsUndefined() ?
this.ColorAxis?.GetColor(item.Value) ?? this.PlotModel.GetDefaultColor() :
this.Fill.IsAutomatic() ?
this.PlotModel.GetDefaultColor() :
this.Fill;
foreach (var outline in item.Outlines)
{
screenPointOutline.Clear();
screenPointOutline.AddRange(outline.Select(this.Transform));
rc.DrawPolygon(
screenPointOutline,
polyColor,
this.Stroke,
this.StrokeThickness,
this.EdgeRenderingMode.GetActual(EdgeRenderingMode.PreferSharpness));
if (this.LabelFontSize > 0)
{
rc.DrawText(
ScreenPointHelper.GetCentroid(screenPointOutline),
item.Value.ToString(this.LabelFormatString),
this.ActualTextColor,
this.ActualFont,
this.LabelFontSize,
this.ActualFontWeight,
0,
HorizontalAlignment.Center,
VerticalAlignment.Middle);
}
}
}
}
/// <summary>
/// Gets the point on the series that is nearest the specified point.
/// </summary>
/// <param name="point">The point.</param>
/// <param name="interpolate">Interpolate the series if this flag is set to <c>true</c>.</param>
/// <returns>A TrackerHitResult for the current hit.</returns>
public override TrackerHitResult GetNearestPoint(ScreenPoint point, bool interpolate)
{
var p = this.InverseTransform(point);
if (!this.IsPointInRange(p))
{
return null;
}
var colorAxis = this.ColorAxis as Axis;
var colorAxisTitle = colorAxis?.Title ?? DefaultColorAxisTitle;
var screenPointOutline = new List<ScreenPoint>();
if (this.ActualItems != null)
{
// iterate through the DataRects and return the first one that contains the point
foreach (var item in this.ActualItems)
{
for (int i = 0; i < item.Outlines.Count; i++)
{
// perform cheap bounds check before we transform etc.
var bounds = item.Bounds[i];
if (!bounds.Contains(p.X, p.Y))
{
continue;
}
// check against polygon
screenPointOutline.Clear();
screenPointOutline.AddRange(item.Outlines[i].Select(this.Transform));
if (ScreenPointHelper.IsPointInPolygon(point, screenPointOutline))
{
return new TrackerHitResult
{
Series = this,
DataPoint = p,
Position = point,
Item = null,
Index = -1,
Text = StringHelper.Format(
this.ActualCulture,
this.TrackerFormatString,
item,
this.Title,
this.XAxis.Title ?? DefaultXAxisTitle,
this.XAxis.GetValue(p.X),
this.YAxis.Title ?? DefaultYAxisTitle,
this.YAxis.GetValue(p.Y),
colorAxisTitle,
item.Value)
};
}
}
}
}
// if no polys contain the point, return null
return null;
}
/// <summary>
/// Ensures that the axes of the series is defined.
/// </summary>
protected internal override void EnsureAxes()
{
base.EnsureAxes();
this.ColorAxis = this.ColorAxisKey != null ?
this.PlotModel.GetAxis(this.ColorAxisKey) as IColorAxis :
this.PlotModel.DefaultColorAxis as IColorAxis;
}
/// <summary>
/// Updates the maximum and minimum values of the series for the x and y dimensions only.
/// </summary>
protected internal void UpdateMaxMinXY()
{
if (this.ActualItems != null && this.ActualItems.Count > 0)
{
this.MinX = this.ActualItems.SelectMany(p => p.Bounds).Min(b => b.Left);
this.MaxX = this.ActualItems.SelectMany(p => p.Bounds).Max(b => b.Right);
this.MinY = this.ActualItems.SelectMany(p => p.Bounds).Min(b => b.Top);
this.MaxY = this.ActualItems.SelectMany(p => p.Bounds).Max(b => b.Bottom);
}
}
/// <summary>
/// Updates the maximum and minimum values of the series.
/// </summary>
protected internal override void UpdateMaxMin()
{
base.UpdateMaxMin();
this.UpdateMaxMinXY();
if (this.ActualItems != null && this.ActualItems.Count > 0)
{
this.MinValue = this.ActualItems.Min(r => r.Value);
this.MaxValue = this.ActualItems.Max(r => r.Value);
}
}
/// <summary>
/// Updates the axes to include the max and min of this series.
/// </summary>
protected internal override void UpdateAxisMaxMin()
{
base.UpdateAxisMaxMin();
var colorAxis = this.ColorAxis as Axis;
if (colorAxis != null)
{
colorAxis.Include(this.MinValue);
colorAxis.Include(this.MaxValue);
}
}
/// <summary>
/// Tests if a <see cref="DataPoint" /> is inside the heat map
/// </summary>
/// <param name="p">The <see cref="DataPoint" /> to test.</param>
/// <returns><c>True</c> if the point is inside the heat map.</returns>
private bool IsPointInRange(DataPoint p)
{
this.UpdateMaxMinXY();
return p.X >= this.MinX && p.X <= this.MaxX && p.Y >= this.MinY && p.Y <= this.MaxY;
}
}
}