-
Notifications
You must be signed in to change notification settings - Fork 976
Expand file tree
/
Copy pathPlotModel.Rendering.cs
More file actions
661 lines (574 loc) · 29.1 KB
/
Copy pathPlotModel.Rendering.cs
File metadata and controls
661 lines (574 loc) · 29.1 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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PlotModel.Rendering.cs" company="OxyPlot">
// Copyright (c) 2019 OxyPlot contributors
// </copyright>
// <summary>
// Renders the plot with the specified rendering context.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot
{
using System;
using System.Collections.Generic;
using System.Linq;
using OxyPlot.Annotations;
using OxyPlot.Axes;
using OxyPlot.Series;
using OxyPlot.Legends;
/// <summary>
/// Represents a plot.
/// </summary>
public partial class PlotModel
{
/// <summary>
/// Renders the plot with the specified rendering context within the given rectangle.
/// </summary>
/// <param name="rc">The rendering context.</param>
/// <param name="rect">The plot bounds.</param>
void IPlotModel.Render(IRenderContext rc, OxyRect rect)
{
this.RenderOverride(rc, rect);
}
/// <summary>
/// Renders the plot with the specified rendering context.
/// </summary>
/// <param name="rc">The rendering context.</param>
/// <param name="rect">The plot bounds.</param>
protected virtual void RenderOverride(IRenderContext rc, OxyRect rect)
{
lock (this.SyncRoot)
{
var initialClipCount = rc.ClipCount;
try
{
using var _ = rc.AutoResetClip(rect);
if (this.lastPlotException != null)
{
// There was an exception during plot model update.
// This could happen when OxyPlot is given invalid input data.
// The client application should be responsible for handling this.
// If the client application submitted invalid data, show the exception message and stack trace.
var errorMessage = string.Format(
"An exception of type {0} was thrown when updating the plot model.\r\n{1}",
this.lastPlotException.GetType(),
this.lastPlotException.GetBaseException().StackTrace);
this.RenderErrorMessage(rc, string.Format("OxyPlot exception: {0}", this.lastPlotException.Message), errorMessage);
return;
}
if (this.RenderingDecorator != null)
{
rc = this.RenderingDecorator(rc);
}
this.PlotBounds = rect;
this.ActualPlotMargins =
new OxyThickness(
double.IsNaN(this.PlotMargins.Left) ? 0 : this.PlotMargins.Left,
double.IsNaN(this.PlotMargins.Top) ? 0 : this.PlotMargins.Top,
double.IsNaN(this.PlotMargins.Right) ? 0 : this.PlotMargins.Right,
double.IsNaN(this.PlotMargins.Bottom) ? 0 : this.PlotMargins.Bottom);
foreach (var l in this.Legends)
{
l.EnsureLegendProperties();
}
for (var i = 0; i < 10; i++) // make we sure we don't loop infinitely
{
this.UpdatePlotArea(rc);
this.UpdateAxisTransforms();
this.UpdateIntervals();
if (!this.AdjustPlotMargins(rc))
{
break;
}
}
if (this.PlotType == PlotType.Cartesian)
{
this.EnforceCartesianTransforms();
this.UpdateIntervals();
}
this.RenderBackgrounds(rc);
this.RenderAnnotations(rc, AnnotationLayer.BelowAxes);
this.RenderAxes(rc, AxisLayer.BelowSeries);
this.RenderAnnotations(rc, AnnotationLayer.BelowSeries);
this.RenderSeries(rc);
this.RenderAnnotations(rc, AnnotationLayer.AboveSeries);
this.RenderTitle(rc);
this.RenderBox(rc);
this.RenderAxes(rc, AxisLayer.AboveSeries);
if (this.IsLegendVisible)
{
this.RenderLegends(rc);
}
if (rc.ClipCount != initialClipCount + 1)
{
throw new InvalidOperationException("Unbalanced calls to IRenderContext.PushClip were made during rendering.");
}
}
catch (Exception exception)
{
while (rc.ClipCount > initialClipCount)
{
rc.PopClip();
}
// An exception was raised during rendering. This should not happen...
var errorMessage = string.Format(
"An exception of type {0} was thrown when rendering the plot model.\r\n{1}",
exception.GetType(),
exception.GetBaseException().StackTrace);
this.lastPlotException = exception;
this.RenderErrorMessage(rc, string.Format("OxyPlot exception: {0}", exception.Message), errorMessage);
}
finally
{
// Clean up unused images
rc.CleanUp();
}
}
}
/// <summary>
/// Renders the specified error message.
/// </summary>
/// <param name="rc">The rendering context.</param>
/// <param name="title">The title.</param>
/// <param name="errorMessage">The error message.</param>
/// <param name="fontSize">The font size. The default value is 12.</param>
private void RenderErrorMessage(IRenderContext rc, string title, string errorMessage, double fontSize = 12)
{
var p0 = new ScreenPoint(10, 10);
rc.DrawText(p0, title, this.TextColor, fontWeight: FontWeights.Bold, fontSize: fontSize);
rc.DrawMultilineText(p0 + new ScreenVector(0, fontSize * 1.5), errorMessage, this.TextColor, fontSize: fontSize, dy: fontSize * 1.25);
}
/// <summary>
/// Adjusts the plot margins.
/// </summary>
/// <param name="rc">The render context.</param>
/// <returns><c>true</c> if the margins were adjusted.</returns>
private bool AdjustPlotMargins(IRenderContext rc)
{
var visibleAxes = this.Axes.Where(axis => axis.IsAxisVisible).ToList();
foreach (var axis in visibleAxes)
{
axis.Measure(rc);
}
var desiredMargin = new OxyThickness();
void IncludeInMargin(double size, AxisPosition borderPosition)
{
desiredMargin = borderPosition switch
{
AxisPosition.Bottom => new OxyThickness(desiredMargin.Left, desiredMargin.Top, desiredMargin.Right, Math.Max(desiredMargin.Bottom, size)),
AxisPosition.Left => new OxyThickness(Math.Max(desiredMargin.Left, size), desiredMargin.Top, desiredMargin.Right, desiredMargin.Bottom),
AxisPosition.Right => new OxyThickness(desiredMargin.Left, desiredMargin.Top, Math.Max(desiredMargin.Right, size), desiredMargin.Bottom),
AxisPosition.Top => new OxyThickness(desiredMargin.Left, Math.Max(desiredMargin.Top, size), desiredMargin.Right, desiredMargin.Bottom),
_ => desiredMargin,
};
}
// include the value of the outermost position tier on each side ('normal' axes only)
for (var position = AxisPosition.Left; position <= AxisPosition.Bottom; position++)
{
var axesOfPosition = visibleAxes.Where(a => a.Position == position);
var requiredSize = this.AdjustAxesPositions(axesOfPosition);
IncludeInMargin(requiredSize, position);
}
// include the desired margin of all visible axes (including polar axes)
foreach (var axis in visibleAxes)
{
desiredMargin = desiredMargin.Include(axis.DesiredMargin);
}
var currentMargin = this.PlotMargins;
currentMargin = new OxyThickness(
double.IsNaN(currentMargin.Left) ? desiredMargin.Left : currentMargin.Left,
double.IsNaN(currentMargin.Top) ? desiredMargin.Top : currentMargin.Top,
double.IsNaN(currentMargin.Right) ? desiredMargin.Right : currentMargin.Right,
double.IsNaN(currentMargin.Bottom) ? desiredMargin.Bottom : currentMargin.Bottom);
if (currentMargin.Equals(this.ActualPlotMargins))
{
return false;
}
this.ActualPlotMargins = currentMargin;
return true;
}
/// <summary>
/// Adjust the positions of parallel axes, returns total size
/// </summary>
/// <param name="parallelAxes">The parallel axes.</param>
/// <returns>The maximum value of the position tier??</returns>
private double AdjustAxesPositions(IEnumerable<Axis> parallelAxes)
{
double maxValueOfPositionTier = 0;
static double GetSize(Axis axis)
{
return axis.Position switch
{
AxisPosition.Left => axis.DesiredMargin.Left,
AxisPosition.Right => axis.DesiredMargin.Right,
AxisPosition.Top => axis.DesiredMargin.Top,
AxisPosition.Bottom => axis.DesiredMargin.Bottom,
_ => throw new InvalidOperationException(), // we don't do this for polar axes
};
}
foreach (var tierGroup in parallelAxes.GroupBy(a => a.PositionTier).OrderBy(group => group.Key))
{
var axesOfPositionTier = tierGroup.ToList();
var maxSizeOfPositionTier = axesOfPositionTier.Max(GetSize);
var minValueOfPositionTier = maxValueOfPositionTier;
if (Math.Abs(maxValueOfPositionTier) > 1e-5)
{
maxValueOfPositionTier += this.AxisTierDistance;
}
maxValueOfPositionTier += maxSizeOfPositionTier;
foreach (var axis in axesOfPositionTier)
{
axis.PositionTierSize = maxSizeOfPositionTier;
axis.PositionTierMinShift = minValueOfPositionTier;
axis.PositionTierMaxShift = maxValueOfPositionTier;
}
}
return maxValueOfPositionTier;
}
/// <summary>
/// Measures the size of the title and subtitle.
/// </summary>
/// <param name="rc">The rendering context.</param>
/// <returns>Size of the titles.</returns>
private OxySize MeasureTitles(IRenderContext rc)
{
var titleSize = rc.MeasureText(this.Title, this.ActualTitleFont, this.TitleFontSize, this.TitleFontWeight);
var subtitleSize = rc.MeasureText(this.Subtitle, this.SubtitleFont ?? this.ActualSubtitleFont, this.SubtitleFontSize, this.SubtitleFontWeight);
double height = titleSize.Height + subtitleSize.Height;
double width = Math.Max(titleSize.Width, subtitleSize.Width);
return new OxySize(width, height);
}
/// <summary>
/// Renders the annotations.
/// </summary>
/// <param name="rc">The render context.</param>
/// <param name="layer">The layer.</param>
private void RenderAnnotations(IRenderContext rc, AnnotationLayer layer)
{
this.RenderPlotElements(this.Annotations.Where(a => a.Layer == layer), rc, annotation => annotation.Render(rc));
}
/// <summary>
/// Renders the axes.
/// </summary>
/// <param name="rc">The render context.</param>
/// <param name="layer">The layer.</param>
private void RenderAxes(IRenderContext rc, AxisLayer layer)
{
// render pass 0
foreach (var a in this.Axes.Where(a => a.IsAxisVisible && a.Layer == layer))
{
rc.SetToolTip(a.ToolTip);
a.Render(rc, 0);
}
// render pass 1
foreach (var a in this.Axes.Where(a => a.IsAxisVisible && a.Layer == layer))
{
rc.SetToolTip(a.ToolTip);
a.Render(rc, 1);
}
rc.SetToolTip(null);
}
private void RenderLegends(IRenderContext rc)
{
if (this.IsLegendVisible)
{
foreach (var l in this.Legends.Where(l => l.IsLegendVisible))
{
rc.SetToolTip(l.ToolTip);
l.RenderLegends(rc);
}
}
}
/// <summary>
/// Renders the series backgrounds.
/// </summary>
/// <param name="rc">The render context.</param>
private void RenderBackgrounds(IRenderContext rc)
{
// Render the main background of the plot area (only if there are axes)
if (this.Axes.Count > 0 && this.PlotAreaBackground.IsVisible())
{
rc.DrawRectangle(this.PlotArea, this.PlotAreaBackground, OxyColors.Undefined, 0, this.EdgeRenderingMode);
}
foreach (var s in this.Series.Where(s => s.IsVisible && s is XYAxisSeries && s.Background.IsVisible()).Cast<XYAxisSeries>())
{
rc.DrawRectangle(s.GetScreenRectangle(), s.Background, OxyColors.Undefined, 0, this.EdgeRenderingMode);
}
}
/// <summary>
/// Renders the border around the plot area.
/// </summary>
/// <param name="rc">The render context.</param>
/// <remarks>The border will only by rendered if there are axes in the plot.</remarks>
private void RenderBox(IRenderContext rc)
{
if (this.Axes.Count > 0)
{
rc.DrawRectangle(this.PlotArea, this.PlotAreaBorderColor, this.PlotAreaBorderThickness, this.EdgeRenderingMode.GetActual(EdgeRenderingMode.PreferSharpness));
}
}
/// <summary>
/// Renders the series.
/// </summary>
/// <param name="rc">The render context.</param>
private void RenderSeries(IRenderContext rc)
{
foreach (var barSeriesManager in this.barSeriesManagers)
{
barSeriesManager.InitializeRender();
}
this.RenderPlotElements(this.Series.Where(s => s.IsVisible), rc, series => series.Render(rc));
}
private void RenderPlotElements<T>(IEnumerable<T> plotElements, IRenderContext rc, Action<T> renderAction) where T: PlotElement
{
var previousClippingRect = OxyRect.Everything;
foreach (var plotElement in plotElements)
{
var currentClippingRect = plotElement.GetClippingRect();
if (!currentClippingRect.Equals(previousClippingRect))
{
if (!previousClippingRect.Equals(OxyRect.Everything))
{
rc.PopClip();
previousClippingRect = OxyRect.Everything;
}
if (!currentClippingRect.Equals(OxyRect.Everything))
{
rc.PushClip(currentClippingRect);
previousClippingRect = currentClippingRect;
}
}
rc.SetToolTip(plotElement.ToolTip);
renderAction(plotElement);
}
if (!previousClippingRect.Equals(OxyRect.Everything))
{
rc.PopClip();
}
rc.SetToolTip(null);
}
/// <summary>
/// Renders the title and subtitle.
/// </summary>
/// <param name="rc">The render context.</param>
private void RenderTitle(IRenderContext rc)
{
OxySize? maxSize = null;
if (this.ClipTitle)
{
maxSize = new OxySize(this.TitleArea.Width * this.TitleClippingLength, double.MaxValue);
}
var titleSize = rc.MeasureText(this.Title, this.ActualTitleFont, this.TitleFontSize, this.TitleFontWeight);
double x = (this.TitleArea.Left + this.TitleArea.Right) * 0.5;
double y = this.TitleArea.Top;
if (!string.IsNullOrEmpty(this.Title))
{
rc.SetToolTip(this.TitleToolTip);
rc.DrawMathText(
new ScreenPoint(x, y),
this.Title,
this.TitleColor.GetActualColor(this.TextColor),
this.ActualTitleFont,
this.TitleFontSize,
this.TitleFontWeight,
0,
HorizontalAlignment.Center,
VerticalAlignment.Top,
maxSize);
y += titleSize.Height;
rc.SetToolTip(null);
}
if (!string.IsNullOrEmpty(this.Subtitle))
{
rc.DrawMathText(
new ScreenPoint(x, y),
this.Subtitle,
this.SubtitleColor.GetActualColor(this.TextColor),
this.ActualSubtitleFont,
this.SubtitleFontSize,
this.SubtitleFontWeight,
0,
HorizontalAlignment.Center,
VerticalAlignment.Top,
maxSize);
}
}
/// <summary>
/// Calculates the plot area (subtract padding, title size and outside legends)
/// </summary>
/// <param name="rc">The rendering context.</param>
private void UpdatePlotArea(IRenderContext rc)
{
var plotAndAxisArea = new OxyRect(
this.PlotBounds.Left + this.Padding.Left,
this.PlotBounds.Top + this.Padding.Top,
Math.Max(0, this.Width - this.Padding.Left - this.Padding.Right),
Math.Max(0, this.Height - this.Padding.Top - this.Padding.Bottom));
var titleSize = this.MeasureTitles(rc);
if (titleSize.Height > 0)
{
var titleHeight = titleSize.Height + this.TitlePadding;
plotAndAxisArea = new OxyRect(plotAndAxisArea.Left, plotAndAxisArea.Top + titleHeight, plotAndAxisArea.Width, Math.Max(0, plotAndAxisArea.Height - titleHeight));
}
var plotArea = plotAndAxisArea.Deflate(this.ActualPlotMargins);
if (this.IsLegendVisible)
{
// Make space for legends
OxySize maxLegendSize = new OxySize(0, 0);
double legendMargin = 0;
// first run Outside Left-Side legends
foreach (var legend in this.Legends.Where(l =>
l.LegendPlacement == LegendPlacement.Outside && (l.IsLegendVisible &&
(l.LegendPosition == LegendPosition.LeftTop || l.LegendPosition == LegendPosition.LeftMiddle || l.LegendPosition == LegendPosition.LeftBottom))))
{
// Find the available size for the legend box
var availableLegendWidth = legend.AllowUseFullExtent
? plotAndAxisArea.Width
: plotArea.Width;
var availableLegendHeight = legend.AllowUseFullExtent
? plotAndAxisArea.Height
: plotArea.Height;
availableLegendHeight = double.IsNaN(legend.LegendMaxHeight) ?
availableLegendHeight : Math.Min(availableLegendHeight, legend.LegendMaxHeight);
var lsiz = legend.GetLegendSize(rc, new OxySize(availableLegendWidth, availableLegendHeight));
legend.LegendSize = lsiz;
maxLegendSize = new OxySize(maxLegendSize.Width > lsiz.Width ? maxLegendSize.Width : lsiz.Width, maxLegendSize.Height > lsiz.Height ? maxLegendSize.Height : lsiz.Height);
if (legend.LegendMargin > legendMargin)
legendMargin = legend.LegendMargin;
}
// Adjust the plot area after the size of the legend has been calculated
if (maxLegendSize.Width > 0 || maxLegendSize.Height > 0)
{
plotArea = new OxyRect(plotArea.Left + maxLegendSize.Width + legendMargin, plotArea.Top, Math.Max(0, plotArea.Width - (maxLegendSize.Width + legendMargin)), plotArea.Height);
}
maxLegendSize = new OxySize(0, 0);
legendMargin = 0;
// second run Outside Right-Side legends
foreach (var legend in this.Legends.Where(l =>
l.LegendPlacement == LegendPlacement.Outside && (l.IsLegendVisible &&
(l.LegendPosition == LegendPosition.RightTop || l.LegendPosition == LegendPosition.RightMiddle || l.LegendPosition == LegendPosition.RightBottom))))
{
// Find the available size for the legend box
var availableLegendWidth = legend.AllowUseFullExtent
? plotAndAxisArea.Width
: plotArea.Width;
var availableLegendHeight = legend.AllowUseFullExtent
? plotAndAxisArea.Height
: plotArea.Height;
availableLegendHeight = double.IsNaN(legend.LegendMaxHeight) ?
availableLegendHeight : Math.Min(availableLegendHeight, legend.LegendMaxHeight);
var lsiz = legend.GetLegendSize(rc, new OxySize(availableLegendWidth, availableLegendHeight));
legend.LegendSize = lsiz;
maxLegendSize = new OxySize(maxLegendSize.Width > lsiz.Width ? maxLegendSize.Width : lsiz.Width, maxLegendSize.Height > lsiz.Height ? maxLegendSize.Height : lsiz.Height);
if (legend.LegendMargin > legendMargin)
legendMargin = legend.LegendMargin;
}
// Adjust the plot area after the size of the legend has been calculated
if (maxLegendSize.Width > 0 || maxLegendSize.Height > 0)
{
plotArea = new OxyRect(plotArea.Left, plotArea.Top, Math.Max(0, plotArea.Width - (maxLegendSize.Width + legendMargin)), plotArea.Height);
}
maxLegendSize = new OxySize(0, 0);
legendMargin = 0;
// third run Outside Top legends
foreach (var legend in this.Legends.Where(l =>
l.LegendPlacement == LegendPlacement.Outside && (l.IsLegendVisible &&
(l.LegendPosition == LegendPosition.TopLeft || l.LegendPosition == LegendPosition.TopCenter || l.LegendPosition == LegendPosition.TopRight))))
{
// Find the available size for the legend box
var availableLegendWidth = legend.AllowUseFullExtent
? plotAndAxisArea.Width
: plotArea.Width;
var availableLegendHeight = legend.AllowUseFullExtent
? plotAndAxisArea.Height
: plotArea.Height;
availableLegendHeight = double.IsNaN(legend.LegendMaxHeight) ?
availableLegendHeight : Math.Min(availableLegendHeight, legend.LegendMaxHeight);
var lsiz = legend.GetLegendSize(rc, new OxySize(availableLegendWidth, availableLegendHeight));
legend.LegendSize = lsiz;
maxLegendSize = new OxySize(maxLegendSize.Width > lsiz.Width ? maxLegendSize.Width : lsiz.Width, maxLegendSize.Height > lsiz.Height ? maxLegendSize.Height : lsiz.Height);
if (legend.LegendMargin > legendMargin)
legendMargin = legend.LegendMargin;
}
// Adjust the plot area after the size of the legend has been calculated
if (maxLegendSize.Width > 0 || maxLegendSize.Height > 0)
{
plotArea = new OxyRect(plotArea.Left, plotArea.Top + maxLegendSize.Height + legendMargin, plotArea.Width, Math.Max(0, plotArea.Height - (maxLegendSize.Height + legendMargin)));
}
maxLegendSize = new OxySize(0, 0);
legendMargin = 0;
// fourth run Outside Bottom legends
foreach (var legend in this.Legends.Where(l =>
l.LegendPlacement == LegendPlacement.Outside && (l.IsLegendVisible &&
(l.LegendPosition == LegendPosition.BottomLeft || l.LegendPosition == LegendPosition.BottomCenter || l.LegendPosition == LegendPosition.BottomRight))))
{
// Find the available size for the legend box
var availableLegendWidth = legend.AllowUseFullExtent
? plotAndAxisArea.Width
: plotArea.Width;
var availableLegendHeight = legend.AllowUseFullExtent
? plotAndAxisArea.Height
: plotArea.Height;
availableLegendHeight = double.IsNaN(legend.LegendMaxHeight) ?
availableLegendHeight : Math.Min(availableLegendHeight, legend.LegendMaxHeight);
var lsiz = legend.GetLegendSize(rc, new OxySize(availableLegendWidth, availableLegendHeight));
legend.LegendSize = lsiz;
maxLegendSize = new OxySize(maxLegendSize.Width > lsiz.Width ? maxLegendSize.Width : lsiz.Width, maxLegendSize.Height > lsiz.Height ? maxLegendSize.Height : lsiz.Height);
if (legend.LegendMargin > legendMargin)
legendMargin = legend.LegendMargin;
}
// Adjust the plot area after the size of the legend has been calculated
if (maxLegendSize.Width > 0 || maxLegendSize.Height > 0)
{
plotArea = new OxyRect(plotArea.Left, plotArea.Top, plotArea.Width, Math.Max(0, plotArea.Height - (maxLegendSize.Height + legendMargin)));
}
// Finally calculate size of inside legends
foreach (var legend in this.Legends.Where(l => l.LegendPlacement == LegendPlacement.Inside && l.IsLegendVisible))
{
// Find the available size for the legend box
var availableLegendWidth = plotArea.Width;
var availableLegendHeight = double.IsNaN(legend.LegendMaxHeight) ?
plotArea.Height : Math.Min(plotArea.Height, legend.LegendMaxHeight);
if (legend.LegendPlacement == LegendPlacement.Inside)
{
availableLegendWidth -= legend.LegendMargin * 2;
availableLegendHeight -= legend.LegendMargin * 2;
}
legend.LegendSize = legend.GetLegendSize(rc, new OxySize(availableLegendWidth, availableLegendHeight));
}
}
// Ensure the plot area is valid
if (plotArea.Height < 0)
{
plotArea = new OxyRect(plotArea.Left, plotArea.Top, plotArea.Width, 1);
}
if (plotArea.Width < 0)
{
plotArea = new OxyRect(plotArea.Left, plotArea.Top, 1, plotArea.Height);
}
this.PlotArea = plotArea;
this.PlotAndAxisArea = plotArea.Inflate(this.ActualPlotMargins);
switch (this.TitleHorizontalAlignment)
{
case TitleHorizontalAlignment.CenteredWithinView:
this.TitleArea = new OxyRect(
this.PlotBounds.Left,
this.PlotBounds.Top + this.Padding.Top,
this.Width,
titleSize.Height + (this.TitlePadding * 2));
break;
default:
this.TitleArea = new OxyRect(
this.PlotArea.Left,
this.PlotBounds.Top + this.Padding.Top,
this.PlotArea.Width,
titleSize.Height + (this.TitlePadding * 2));
break;
}
// Calculate the legend area for each legend.
foreach (var l in this.Legends)
{
l.LegendArea = l.GetLegendRectangle(l.LegendSize);
}
}
}
}