PageRenderTime 46ms CodeModel.GetById 33ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Charts/Isolines/AdditionalLinesDisplay.cs

#
C# | 88 lines | 72 code | 15 blank | 1 comment | 10 complexity | 97b67b0d8b1600c4660275144a69fbf7 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using System.Windows.Media;
 6using System.Windows;
 7using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
 8using Microsoft.Research.DynamicDataDisplay.Charts.NewLine;
 9
10namespace Microsoft.Research.DynamicDataDisplay.Charts.Isolines
11{
12	public sealed class AdditionalLinesDisplay : IsolineGraphBase
13	{
14		protected override void OnPlotterAttached()
15		{
16			base.OnPlotterAttached();
17
18			Plotter2D.Viewport.PropertyChanged += Viewport_PropertyChanged;
19		}
20
21		protected override void OnPlotterDetaching()
22		{
23			Plotter2D.Viewport.PropertyChanged -= Viewport_PropertyChanged;
24
25			base.OnPlotterDetaching();
26		}
27
28		void Viewport_PropertyChanged(object sender, ExtendedPropertyChangedEventArgs e)
29		{
30			InvalidateVisual();
31		}
32
33		protected override void OnRender(DrawingContext drawingContext)
34		{
35			if (Plotter2D == null) return;
36			if (DataSource == null) return;
37			if (Collection == null) return;
38			if (Collection.Lines.Count == 0)
39			{
40				IsolineBuilder.DataSource = DataSource;
41			}
42
43			var dc = drawingContext;
44			var dataSource = DataSource;
45			var localMinMax = dataSource.GetMinMax();
46			var globalMinMax = dataSource.Range.Value;
47			double lengthsRatio = globalMinMax.GetLength() / localMinMax.GetLength();
48
49			if (lengthsRatio > 16)
50			{
51				double log = Math.Round(Math.Log(lengthsRatio, 2));
52				double number = 2 * Math.Pow(2, log);
53				double delta = globalMinMax.GetLength() / number;
54
55				double start = Math.Floor((localMinMax.Min - globalMinMax.Min) / delta) * delta + globalMinMax.Min;
56				double end = localMinMax.Max;
57
58				var transform = Plotter2D.Transform;
59				var strokeThickness = StrokeThickness;
60
61				double x = start;
62				while (x < end)
63				{
64					var collection = IsolineBuilder.BuildIsoline(x);
65
66					foreach (LevelLine line in collection)
67					{
68						StreamGeometry lineGeometry = new StreamGeometry();
69						using (var context = lineGeometry.Open())
70						{
71							context.BeginFigure(line.StartPoint.ViewportToScreen(transform), false, false);
72							context.PolyLineTo(line.OtherPoints.ViewportToScreen(transform).ToArray(), true, true);
73						}
74						lineGeometry.Freeze();
75
76						var paletteRatio = (line.RealValue - globalMinMax.Min) / globalMinMax.GetLength();
77						Pen pen = new Pen(new SolidColorBrush(Palette.GetColor(paletteRatio)), strokeThickness);
78
79						dc.DrawGeometry(null, pen, lineGeometry);
80					}
81
82					x += delta;
83				}
84			}
85			//dc.DrawRectangle(Brushes.Green.MakeTransparent(0.3), null, new Rect(RenderSize));
86		}
87	}
88}