-
Notifications
You must be signed in to change notification settings - Fork 976
Expand file tree
/
Copy pathPlotModel.cs
More file actions
1236 lines (1071 loc) · 41.5 KB
/
Copy pathPlotModel.cs
File metadata and controls
1236 lines (1071 loc) · 41.5 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PlotModel.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Specifies the coordinate system type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using OxyPlot.Annotations;
using OxyPlot.Axes;
using OxyPlot.Legends;
using OxyPlot.Series;
/// <summary>
/// Specifies the coordinate system type.
/// </summary>
public enum PlotType
{
/// <summary>
/// XY coordinate system - two perpendicular axes
/// </summary>
XY,
/// <summary>
/// Cartesian coordinate system - perpendicular axes with the same scaling.
/// </summary>
/// <remarks>See http://en.wikipedia.org/wiki/Cartesian_coordinate_system</remarks>
Cartesian,
/// <summary>
/// Polar coordinate system - with radial and angular axes
/// </summary>
/// <remarks>See http://en.wikipedia.org/wiki/Polar_coordinate_system</remarks>
Polar
}
/// <summary>
/// Specifies the horizontal alignment of the titles.
/// </summary>
public enum TitleHorizontalAlignment
{
/// <summary>
/// Centered within the plot area.
/// </summary>
CenteredWithinPlotArea,
/// <summary>
/// Centered within the client view (excluding padding defined in <see cref="PlotModel.Padding" />).
/// </summary>
CenteredWithinView
}
/// <summary>
/// Represents a plot.
/// </summary>
public partial class PlotModel : Model, IPlotModel
{
/// <summary>
/// The bar series managers.
/// </summary>
private readonly List<BarSeriesManager> barSeriesManagers = new List<BarSeriesManager>();
/// <summary>
/// The plot view that renders this plot.
/// </summary>
private WeakReference plotViewReference;
/// <summary>
/// The current color index.
/// </summary>
private int currentColorIndex;
/// <summary>
/// Flags if the data has been updated.
/// </summary>
private bool isDataUpdated;
/// <summary>
/// The last update exception.
/// </summary>
/// <value>The exception or <c>null</c> if there was no exceptions during the last update.</value>
private Exception lastPlotException;
/// <summary>
/// Initializes a new instance of the <see cref="PlotModel" /> class.
/// </summary>
public PlotModel()
{
this.Axes = new ElementCollection<Axis>(this);
this.Series = new ElementCollection<Series.Series>(this);
this.Annotations = new ElementCollection<Annotation>(this);
this.Legends = new ElementCollection<LegendBase>(this);
this.PlotType = PlotType.XY;
this.PlotMargins = new OxyThickness(double.NaN);
this.Padding = new OxyThickness(8);
this.Background = OxyColors.Undefined;
this.PlotAreaBackground = OxyColors.Undefined;
this.TextColor = OxyColors.Black;
this.TitleColor = OxyColors.Automatic;
this.SubtitleColor = OxyColors.Automatic;
this.DefaultFont = "Segoe UI";
this.DefaultFontSize = 12;
this.TitleToolTip = null;
this.TitleFont = null;
this.TitleFontSize = 18;
this.TitleFontWeight = FontWeights.Bold;
this.SubtitleFont = null;
this.SubtitleFontSize = 14;
this.SubtitleFontWeight = FontWeights.Normal;
this.TitlePadding = 6;
this.ClipTitle = true;
this.TitleClippingLength = 0.9;
this.PlotAreaBorderColor = OxyColors.Black;
this.PlotAreaBorderThickness = new OxyThickness(1);
this.EdgeRenderingMode = EdgeRenderingMode.Automatic;
this.AssignColorsToInvisibleSeries = true;
this.IsLegendVisible = true;
this.DefaultColors = new List<OxyColor>
{
OxyColor.FromRgb(0x4E, 0x9A, 0x06),
OxyColor.FromRgb(0xC8, 0x8D, 0x00),
OxyColor.FromRgb(0xCC, 0x00, 0x00),
OxyColor.FromRgb(0x20, 0x4A, 0x87),
OxyColors.Red,
OxyColors.Orange,
OxyColors.Yellow,
OxyColors.Green,
OxyColors.Blue,
OxyColors.Indigo,
OxyColors.Violet
};
this.AxisTierDistance = 4.0;
}
/// <summary>
/// Occurs when the tracker has been changed.
/// </summary>
[Obsolete("May be removed in v4.0 (#111)")]
public event EventHandler<TrackerEventArgs> TrackerChanged;
/// <summary>
/// Occurs when the plot has been updated.
/// </summary>
[Obsolete("May be removed in v4.0 (#111)")]
public event EventHandler Updated;
/// <summary>
/// Occurs when the plot is about to be updated.
/// </summary>
[Obsolete("May be removed in v4.0 (#111)")]
public event EventHandler Updating;
/// <summary>
/// Gets or sets the default font.
/// </summary>
/// <value>The default font.</value>
/// <remarks>This font is used for text on axes, series, legends and plot titles unless other fonts are specified.</remarks>
public string DefaultFont { get; set; }
/// <summary>
/// Gets or sets the default size of the fonts.
/// </summary>
/// <value>The default size of the font.</value>
public double DefaultFontSize { get; set; }
/// <summary>
/// Gets the actual culture.
/// </summary>
public CultureInfo ActualCulture
{
get
{
return this.Culture ?? CultureInfo.CurrentCulture;
}
}
/// <summary>
/// Gets the actual plot margins.
/// </summary>
/// <value>The actual plot margins.</value>
public OxyThickness ActualPlotMargins { get; private set; }
/// <summary>
/// Gets the plot view that renders this plot.
/// </summary>
/// <value>The plot view.</value>
/// <remarks>Only one view can render the plot at the same time.</remarks>
public IPlotView PlotView
{
get
{
return (this.plotViewReference != null) ? (IPlotView)this.plotViewReference.Target : null;
}
}
/// <summary>
/// Gets the annotations.
/// </summary>
/// <value>The annotations.</value>
public ElementCollection<Annotation> Annotations { get; private set; }
/// <summary>
/// Gets the axes.
/// </summary>
/// <value>The axes.</value>
public ElementCollection<Axis> Axes { get; private set; }
/// <summary>
/// Gets or sets the legends.
/// </summary>
/// <value>The legends.</value>
public ElementCollection<LegendBase> Legends { get; set; }
/// <summary>
/// Gets or sets the color of the background of the plot.
/// </summary>
/// <value>The color. The default is <see cref="OxyColors.Undefined" />.</value>
/// <remarks>If the background color is set to <see cref="OxyColors.Undefined" /> or is otherwise invisible then the background will be determined by the plot view or exporter.</remarks>
public OxyColor Background { get; set; }
/// <summary>
/// Gets or sets the culture.
/// </summary>
/// <value>The culture.</value>
public CultureInfo Culture { get; set; }
/// <summary>
/// Gets or sets the default colors.
/// </summary>
/// <value>The default colors.</value>
public IList<OxyColor> DefaultColors { get; set; }
/// <summary>
/// Gets or sets the edge rendering mode that is used for rendering the plot bounds and backgrounds.
/// </summary>
/// <value>The edge rendering mode. The default is <see cref="OxyPlot.EdgeRenderingMode.Automatic"/>.</value>
public EdgeRenderingMode EdgeRenderingMode { get; set; }
/// <summary>
/// Gets or sets a value indicating whether invisible series should be assigned automatic colors.
/// </summary>
public bool AssignColorsToInvisibleSeries { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the legend is visible. The titles of the series must be set to use the legend.
/// </summary>
public bool IsLegendVisible { get; set; }
/// <summary>
/// Gets or sets the padding around the plot.
/// </summary>
/// <value>The padding.</value>
public OxyThickness Padding { get; set; }
/// <summary>
/// Gets the PlotBounds of the plot (in device units).
/// </summary>
public OxyRect PlotBounds { get; private set; }
/// <summary>
/// Gets the total width of the plot (in device units).
/// </summary>
public double Width => this.PlotBounds.Width;
/// <summary>
/// Gets the total height of the plot (in device units).
/// </summary>
public double Height => this.PlotBounds.Height;
/// <summary>
/// Gets the area including both the plot and the axes. Outside legends are rendered outside this rectangle.
/// </summary>
/// <value>The plot and axis area.</value>
public OxyRect PlotAndAxisArea { get; private set; }
/// <summary>
/// Gets the plot area. This area is used to draw the series (not including axes or legends).
/// </summary>
/// <value>The plot area.</value>
public OxyRect PlotArea { get; private set; }
/// <summary>
/// Gets or sets the distance between two neighborhood tiers of the same AxisPosition.
/// </summary>
public double AxisTierDistance { get; set; }
/// <summary>
/// Gets or sets the color of the background of the plot area.
/// </summary>
public OxyColor PlotAreaBackground { get; set; }
/// <summary>
/// Gets or sets the color of the border around the plot area.
/// </summary>
/// <value>The color of the box.</value>
public OxyColor PlotAreaBorderColor { get; set; }
/// <summary>
/// Gets or sets the thickness of the border around the plot area.
/// </summary>
/// <value>The box thickness.</value>
public OxyThickness PlotAreaBorderThickness { get; set; }
/// <summary>
/// Gets or sets the margins around the plot (this should be large enough to fit the axes).
/// If any of the values is set to <c>double.NaN</c>, the margin is adjusted to the value required by the axes.
/// </summary>
public OxyThickness PlotMargins { get; set; }
/// <summary>
/// Gets or sets the type of the coordinate system.
/// </summary>
/// <value>The type of the plot.</value>
public PlotType PlotType { get; set; }
/// <summary>
/// Gets the series.
/// </summary>
/// <value>The series.</value>
public ElementCollection<Series.Series> Series { get; private set; }
/// <summary>
/// Gets or sets the rendering decorator.
/// </summary>
/// <value>
/// The rendering decorator.
/// </value>
public Func<IRenderContext, IRenderContext> RenderingDecorator { get; set; }
/// <summary>
/// Gets or sets the subtitle.
/// </summary>
/// <value>The subtitle.</value>
public string Subtitle { get; set; }
/// <summary>
/// Gets or sets the subtitle font. If this property is <c>null</c>, the Title font will be used.
/// </summary>
/// <value>The subtitle font.</value>
public string SubtitleFont { get; set; }
/// <summary>
/// Gets or sets the size of the subtitle font.
/// </summary>
/// <value>The size of the subtitle font.</value>
public double SubtitleFontSize { get; set; }
/// <summary>
/// Gets or sets the subtitle font weight.
/// </summary>
/// <value>The subtitle font weight.</value>
public double SubtitleFontWeight { get; set; }
/// <summary>
/// Gets or sets the default color of the text in the plot (titles, legends, annotations, axes).
/// </summary>
/// <value>The color of the text.</value>
public OxyColor TextColor { get; set; }
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>The title.</value>
public string Title { get; set; }
/// <summary>
/// Gets or sets the title tool tip.
/// </summary>
/// <value>The title tool tip.</value>
public string TitleToolTip { get; set; }
/// <summary>
/// Gets or sets the color of the title.
/// </summary>
/// <value>The color of the title.</value>
/// <remarks>If the value is <c>null</c>, the TextColor will be used.</remarks>
public OxyColor TitleColor { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to clip the title. The default value is <c>true</c>.
/// </summary>
public bool ClipTitle { get; set; }
/// <summary>
/// Gets or sets the length of the title clipping rectangle (fraction of the available length of the title area). The default value is <c>0.9</c>.
/// </summary>
public double TitleClippingLength { get; set; }
/// <summary>
/// Gets or sets the color of the subtitle.
/// </summary>
/// <value>The color of the subtitle.</value>
public OxyColor SubtitleColor { get; set; }
/// <summary>
/// Gets or sets the horizontal alignment of the title and subtitle.
/// </summary>
/// <value>
/// The alignment.
/// </value>
public TitleHorizontalAlignment TitleHorizontalAlignment { get; set; }
/// <summary>
/// Gets the title area.
/// </summary>
/// <value>The title area.</value>
public OxyRect TitleArea { get; private set; }
/// <summary>
/// Gets or sets the title font.
/// </summary>
/// <value>The title font.</value>
public string TitleFont { get; set; }
/// <summary>
/// Gets or sets the size of the title font.
/// </summary>
/// <value>The size of the title font.</value>
public double TitleFontSize { get; set; }
/// <summary>
/// Gets or sets the title font weight.
/// </summary>
/// <value>The title font weight.</value>
public double TitleFontWeight { get; set; }
/// <summary>
/// Gets or sets the padding around the title.
/// </summary>
/// <value>The title padding.</value>
public double TitlePadding { get; set; }
/// <summary>
/// Gets the default angle axis.
/// </summary>
/// <value>The default angle axis.</value>
public AngleAxis DefaultAngleAxis { get; private set; }
/// <summary>
/// Gets the default magnitude axis.
/// </summary>
/// <value>The default magnitude axis.</value>
public MagnitudeAxis DefaultMagnitudeAxis { get; private set; }
/// <summary>
/// Gets the default X axis.
/// </summary>
/// <value>The default X axis.</value>
public Axis DefaultXAxis { get; private set; }
/// <summary>
/// Gets the default Y axis.
/// </summary>
/// <value>The default Y axis.</value>
public Axis DefaultYAxis { get; private set; }
/// <summary>
/// Gets the default color axis.
/// </summary>
/// <value>The default color axis.</value>
public IColorAxis DefaultColorAxis { get; private set; }
/// <summary>
/// Gets the actual title font.
/// </summary>
protected string ActualTitleFont
{
get
{
return this.TitleFont ?? this.DefaultFont;
}
}
/// <summary>
/// Gets the actual subtitle font.
/// </summary>
protected string ActualSubtitleFont
{
get
{
return this.SubtitleFont ?? this.DefaultFont;
}
}
/// <summary>
/// Attaches this model to the specified plot view.
/// </summary>
/// <param name="plotView">The plot view.</param>
/// <remarks>Only one plot view can be attached to the plot model.
/// The plot model contains data (e.g. axis scaling) that is only relevant to the current plot view.</remarks>
void IPlotModel.AttachPlotView(IPlotView plotView)
{
var currentPlotView = this.PlotView;
if (!object.ReferenceEquals(currentPlotView, null) &&
!object.ReferenceEquals(plotView, null) &&
!object.ReferenceEquals(currentPlotView, plotView))
{
throw new InvalidOperationException(
"This PlotModel is already in use by some other PlotView control.");
}
this.plotViewReference = (plotView == null) ? null : new WeakReference(plotView);
}
/// <summary>
/// Invalidates the plot.
/// </summary>
/// <param name="updateData">Updates all data sources if set to <c>true</c>.</param>
public void InvalidatePlot(bool updateData)
{
var plotView = this.PlotView;
if (plotView == null)
{
return;
}
plotView.InvalidatePlot(updateData);
}
/// <summary>
/// Gets the first axes that covers the area of the specified point.
/// </summary>
/// <param name="pt">The point.</param>
/// <param name="xaxis">The x-axis.</param>
/// <param name="yaxis">The y-axis.</param>
public void GetAxesFromPoint(ScreenPoint pt, out Axis xaxis, out Axis yaxis)
{
xaxis = yaxis = null;
// Get the axis position of the given point. Using null if the point is inside the plot area.
AxisPosition? position = null;
double plotAreaValue = 0;
if (pt.X < this.PlotArea.Left)
{
position = AxisPosition.Left;
plotAreaValue = this.PlotArea.Left;
}
if (pt.X > this.PlotArea.Right)
{
position = AxisPosition.Right;
plotAreaValue = this.PlotArea.Right;
}
if (pt.Y < this.PlotArea.Top)
{
position = AxisPosition.Top;
plotAreaValue = this.PlotArea.Top;
}
if (pt.Y > this.PlotArea.Bottom)
{
position = AxisPosition.Bottom;
plotAreaValue = this.PlotArea.Bottom;
}
foreach (var axis in this.Axes)
{
if (!axis.IsAxisVisible)
{
continue;
}
if (axis is IColorAxis)
{
continue;
}
if (axis is MagnitudeAxis)
{
xaxis = axis;
continue;
}
if (axis is AngleAxis)
{
yaxis = axis;
continue;
}
double x = double.NaN;
if (axis.IsHorizontal())
{
x = axis.InverseTransform(pt.X);
}
if (axis.IsVertical())
{
x = axis.InverseTransform(pt.Y);
}
if (x >= axis.ClipMinimum && x <= axis.ClipMaximum)
{
if (position == null)
{
if (axis.IsHorizontal())
{
if (xaxis == null)
{
xaxis = axis;
}
}
else if (axis.IsVertical())
{
if (yaxis == null)
{
yaxis = axis;
}
}
}
else if (position == axis.Position)
{
// Choose right tier
double positionTierMinShift = axis.PositionTierMinShift;
double positionTierMaxShift = axis.PositionTierMaxShift;
double posValue = axis.IsHorizontal() ? pt.Y : pt.X;
bool isLeftOrTop = position == AxisPosition.Top || position == AxisPosition.Left;
if ((posValue >= plotAreaValue + positionTierMinShift
&& posValue < plotAreaValue + positionTierMaxShift && !isLeftOrTop)
||
(posValue <= plotAreaValue - positionTierMinShift
&& posValue > plotAreaValue - positionTierMaxShift && isLeftOrTop))
{
if (axis.IsHorizontal())
{
if (xaxis == null)
{
xaxis = axis;
}
}
else if (axis.IsVertical())
{
if (yaxis == null)
{
yaxis = axis;
}
}
}
}
}
}
}
/// <summary>
/// Gets the default color from the DefaultColors palette.
/// </summary>
/// <returns>The next default color.</returns>
public OxyColor GetDefaultColor()
{
return this.DefaultColors[this.currentColorIndex++ % this.DefaultColors.Count];
}
/// <summary>
/// Gets the default line style.
/// </summary>
/// <returns>The next default line style.</returns>
public LineStyle GetDefaultLineStyle()
{
return (LineStyle)((this.currentColorIndex / this.DefaultColors.Count) % (int)LineStyle.None);
}
/// <summary>
/// Gets a series from the specified point.
/// </summary>
/// <param name="point">The point.</param>
/// <param name="limit">The limit.</param>
/// <returns>The nearest series.</returns>
public Series.Series GetSeriesFromPoint(ScreenPoint point, double limit = 100)
{
double mindist = double.MaxValue;
Series.Series nearestSeries = null;
foreach (var series in this.Series.Reverse().Where(s => s.IsVisible))
{
var thr = series.GetNearestPoint(point, true) ?? series.GetNearestPoint(point, false);
if (thr == null)
{
continue;
}
// find distance to this point on the screen
double dist = point.DistanceTo(thr.Position);
if (dist < mindist)
{
nearestSeries = series;
mindist = dist;
}
}
if (mindist < limit)
{
return nearestSeries;
}
return null;
}
/// <summary>
/// Generates C# code of the model.
/// </summary>
/// <returns>C# code.</returns>
public string ToCode()
{
var cg = new CodeGenerator(this);
return cg.ToCode();
}
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
public override string ToString()
{
return this.Title;
}
/// <summary>
/// Gets the legend for the specified key.
/// </summary>
/// <param name="key">The legend key.</param>
/// <returns>The legend that corresponds with the key.</returns>
/// <exception cref="System.InvalidOperationException">Cannot find legend with the specified key.</exception>
public LegendBase GetLegend(string key)
{
if (key == null)
{
throw new ArgumentException("Axis key cannot be null.");
}
var legend = this.Legends.FirstOrDefault(l => l.Key == key);
if (legend == null)
{
throw new InvalidOperationException($"Cannot find legend with Key = \"{key}\"");
}
return legend;
}
/// <summary>
/// Gets any exception thrown during the last <see cref="IPlotModel.Update" /> call.
/// </summary>
/// <returns>The exception or <c>null</c> if there was no exception.</returns>
public Exception GetLastPlotException()
{
return this.lastPlotException;
}
/// <summary>
/// Updates all axes and series.
/// 0. Updates the owner PlotModel of all plot items (axes, series and annotations)
/// 1. Updates the data of each Series (only if updateData==<c>true</c>).
/// 2. Ensure that all series have axes assigned.
/// 3. Updates the max and min of the axes.
/// </summary>
/// <param name="updateData">if set to <c>true</c> , all data collections will be updated.</param>
void IPlotModel.Update(bool updateData)
{
lock (this.SyncRoot)
{
try
{
this.lastPlotException = null;
this.OnUpdating();
// Updates the default axes
this.EnsureDefaultAxes();
var visibleSeries = this.Series.Where(s => s.IsVisible).ToList();
// Update data of the series
if (updateData || !this.isDataUpdated)
{
foreach (var s in visibleSeries)
{
s.UpdateData();
}
this.isDataUpdated = true;
}
// Updates bar series managers and associated category axes
this.UpdateBarSeriesManagers();
// Update the max and min of the axes
this.UpdateMaxMin(updateData);
// Update category axes that are not managed by bar series managers
this.UpdateUnmanagedCategoryAxes();
// Update undefined colors
var automaticColorSeries = this.AssignColorsToInvisibleSeries
? (IEnumerable<Series.Series>)this.Series
: visibleSeries;
this.ResetDefaultColor();
foreach (var s in automaticColorSeries)
{
s.SetDefaultValues();
}
this.OnUpdated();
}
catch (Exception e)
{
this.lastPlotException = e;
}
}
}
/// <summary>
/// Gets the axis for the specified key.
/// </summary>
/// <param name="key">The axis key.</param>
/// <returns>The axis that corresponds with the key.</returns>
/// <exception cref="System.InvalidOperationException">Cannot find axis with the specified key.</exception>
public Axis GetAxis(string key)
{
if (key == null)
{
throw new ArgumentException("Axis key cannot be null.");
}
var axis = this.Axes.FirstOrDefault(a => a.Key == key);
if (axis == null)
{
throw new InvalidOperationException($"Cannot find axis with Key = \"{key}\"");
}
return axis;
}
/// <summary>
/// Gets the axis for the specified key, or returns a default value.
/// </summary>
/// <param name="key">The axis key.</param>
/// <param name="defaultAxis">The default axis.</param>
/// <returns>defaultAxis if key is empty or does not exist; otherwise, the axis that corresponds with the key.</returns>
public Axis GetAxisOrDefault(string key, Axis defaultAxis)
{
if (key != null)
{
var axis = this.Axes.FirstOrDefault(a => a.Key == key);
return axis != null ? axis : defaultAxis;
}
return defaultAxis;
}
/// <summary>
/// Resets all axes in the model.
/// </summary>
public void ResetAllAxes()
{
foreach (var a in this.Axes)
{
a.Reset();
}
}
/// <summary>
/// Pans all axes.
/// </summary>
/// <param name="dx">The horizontal distance to pan (screen coordinates).</param>
/// <param name="dy">The vertical distance to pan (screen coordinates).</param>
public void PanAllAxes(double dx, double dy)
{
foreach (var a in this.Axes)
{
a.Pan(a.IsHorizontal() ? dx : dy);
}
}
/// <summary>
/// Zooms all axes.
/// </summary>
/// <param name="factor">The zoom factor.</param>
public void ZoomAllAxes(double factor)
{
foreach (var a in this.Axes)
{
a.ZoomAtCenter(factor);
}
}
/// <summary>
/// Raises the TrackerChanged event.
/// </summary>
/// <param name="result">The result.</param>
/// <remarks>
/// This method is public so custom implementations of tracker manipulators can invoke this method.
/// </remarks>
public void RaiseTrackerChanged(TrackerHitResult result)
{
var handler = this.TrackerChanged;
if (handler != null)
{
var args = new TrackerEventArgs { HitResult = result };
handler(this, args);
}
}
/// <summary>
/// Raises the TrackerChanged event.
/// </summary>
/// <param name="result">The result.</param>
protected internal virtual void OnTrackerChanged(TrackerHitResult result)
{
this.RaiseTrackerChanged(result);
}
/// <summary>
/// Gets all elements of the model, top-level elements first.
/// </summary>
/// <returns>
/// An enumerator of the elements.
/// </returns>
protected override IEnumerable<PlotElement> GetHitTestElements()
{
foreach (var axis in this.Axes.Reverse().Where(a => a.IsAxisVisible && a.Layer == AxisLayer.AboveSeries))
{
yield return axis;
}
foreach (var annotation in this.Annotations.Reverse().Where(a => a.Layer == AnnotationLayer.AboveSeries))
{
yield return annotation;
}
foreach (var s in this.Series.Reverse().Where(s => s.IsVisible))
{
yield return s;
}
foreach (var annotation in this.Annotations.Reverse().Where(a => a.Layer == AnnotationLayer.BelowSeries))
{
yield return annotation;
}
foreach (var axis in this.Axes.Reverse().Where(a => a.IsAxisVisible && a.Layer == AxisLayer.BelowSeries))
{
yield return axis;
}
foreach (var annotation in this.Annotations.Reverse().Where(a => a.Layer == AnnotationLayer.BelowAxes))
{
yield return annotation;
}
foreach (var legend in this.Legends)
{
yield return legend;
}
}
/// <summary>
/// Raises the Updated event.
/// </summary>
protected virtual void OnUpdated()
{
var handler = this.Updated;
if (handler != null)
{
var args = new EventArgs();
handler(this, args);
}
}
/// <summary>
/// Raises the Updating event.
/// </summary>
protected virtual void OnUpdating()
{
var handler = this.Updating;
if (handler != null)
{
var args = new EventArgs();
handler(this, args);
}
}
/// <summary>
/// Updates the axis transforms.
/// </summary>
private void UpdateAxisTransforms()