Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/ViewportConstraints/DataHeightConstraint.cs @ 13376

Last change on this file since 13376 was 12503, checked in by aballeit, 9 years ago

#2283 added GUI and charts; fixed MCTS

File size: 4.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows;
6using System.Collections.ObjectModel;
7using System.Collections.Specialized;
8
9namespace Microsoft.Research.DynamicDataDisplay.ViewportConstraints
10{
11  public class DataHeightConstraint : ViewportConstraint, ISupportAttachToViewport
12  {
13    private double yEnlargeCoeff = 1.1;
14    public double YEnlargeCoeff
15    {
16      get { return yEnlargeCoeff; }
17      set
18      {
19        if (yEnlargeCoeff != value)
20        {
21          yEnlargeCoeff = value;
22          RaiseChanged();
23        }
24      }
25    }
26
27    public override DataRect Apply(DataRect oldDataRect, DataRect newDataRect, Viewport2D viewport)
28    {
29      DataRect overallBounds = DataRect.Empty;
30
31      foreach (var chart in viewport.ContentBoundsHosts)
32      {
33        var plotterElement = chart as IPlotterElement;
34        //var visual = viewport.Plotter.VisualBindings[plotterElement];
35        var visual = plotterElement as DependencyObject;
36        if (visual == null)
37          continue;
38
39        var points = (ReadOnlyCollection<Point>)PointsGraphBase.GetVisiblePoints(visual);
40        if (points != null)
41        {
42
43          // searching for indices of chart's visible points which are near left and right borders of newDataRect
44          double startX = newDataRect.XMin;
45          double endX = newDataRect.XMax;
46
47          if (points[0].X > endX || points[points.Count - 1].X < startX)
48          {
49            continue;
50          }
51
52          int startIndex = -1;
53
54          // we assume that points are sorted by x values ascending
55          if (startX <= points[0].X)
56          {
57            startIndex = 0;
58          }
59          else
60          {
61            for (int i = 1; i < points.Count - 1; i++)
62            {
63              if (points[i].X <= startX && startX < points[i + 1].X)
64              {
65                startIndex = i;
66                break;
67              }
68            }
69          }
70
71          int endIndex = points.Count;
72
73          if (points[points.Count - 1].X < endX)
74          {
75            endIndex = points.Count;
76          }
77          else
78          {
79            for (int i = points.Count - 1; i >= 1; i--)
80            {
81              if (points[i - 1].X <= endX && endX < points[i].X)
82              {
83                endIndex = i;
84                break;
85              }
86            }
87          }
88
89          Rect bounds = Rect.Empty;
90          for (int i = startIndex; i < endIndex; i++)
91          {
92            bounds.Union(points[i]);
93          }
94          if (startIndex > 0)
95          {
96            Point pt = GetInterpolatedPoint(startX, points[startIndex], points[startIndex - 1]);
97            bounds.Union(pt);
98          }
99          if (endIndex < points.Count - 1)
100          {
101            Point pt = GetInterpolatedPoint(endX, points[endIndex], points[endIndex + 1]);
102            bounds.Union(pt);
103          }
104
105          overallBounds.Union(bounds);
106        }
107      }
108
109      if (!overallBounds.IsEmpty)
110      {
111        double y = overallBounds.YMin;
112        double height = overallBounds.Height;
113
114        if (height == 0)
115        {
116          height = newDataRect.Height;
117          y -= height / 2;
118        }
119
120        newDataRect = new DataRect(newDataRect.XMin, y, newDataRect.Width, height);
121        newDataRect = DataRectExtensions.ZoomY(newDataRect, newDataRect.GetCenter(), yEnlargeCoeff);
122      }
123
124      return newDataRect;
125    }
126
127    private static Point GetInterpolatedPoint(double x, Point p1, Point p2)
128    {
129      double xRatio = (x - p1.X) / (p2.X - p1.X);
130      double y = (1 - xRatio) * p1.Y + xRatio * p2.Y;
131
132      return new Point(x, y);
133    }
134
135    #region ISupportAttach Members
136
137    void ISupportAttachToViewport.Attach(Viewport2D viewport)
138    {
139      ((INotifyCollectionChanged)viewport.ContentBoundsHosts).CollectionChanged += OnContentBoundsHostsChanged;
140
141      foreach (var item in viewport.ContentBoundsHosts)
142      {
143        PointsGraphBase chart = item as PointsGraphBase;
144        if (chart != null)
145        {
146          chart.ProvideVisiblePoints = true;
147        }
148      }
149    }
150
151    private void OnContentBoundsHostsChanged(object sender, NotifyCollectionChangedEventArgs e)
152    {
153      if (e.NewItems != null)
154      {
155        foreach (var item in e.NewItems)
156        {
157          PointsGraphBase chart = item as PointsGraphBase;
158          if (chart != null)
159          {
160            chart.ProvideVisiblePoints = true;
161          }
162        }
163      }
164
165      // todo probably set ProvideVisiblePoints to false on OldItems
166    }
167
168    void ISupportAttachToViewport.Detach(Viewport2D viewport)
169    {
170      ((INotifyCollectionChanged)viewport.ContentBoundsHosts).CollectionChanged -= OnContentBoundsHostsChanged;
171    }
172
173    #endregion
174  }
175}
Note: See TracBrowser for help on using the repository browser.