private static void OnPointSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DataFollowChart chart = (DataFollowChart)d; PointsGraphBase previous = e.OldValue as PointsGraphBase; if (previous != null) { previous.VisiblePointsChanged -= chart.Source_VisiblePointsChanged; } PointsGraphBase current = e.NewValue as PointsGraphBase; if (current != null) { current.ProvideVisiblePoints = true; current.VisiblePointsChanged += chart.Source_VisiblePointsChanged; if (current.VisiblePoints != null) { chart.searcher = new SortedXSearcher1d(current.VisiblePoints); } } chart.UpdateUIRepresentation(); }
private void Source_VisiblePointsChanged(object sender, EventArgs e) { PointsGraphBase source = (PointsGraphBase)sender; if (source.VisiblePoints != null) { searcher = new SortedXSearcher1d(source.VisiblePoints); } UpdateUIRepresentation(); }
void ISupportAttachToViewport.Attach(Viewport2D viewport) { ((INotifyCollectionChanged)viewport.ContentBoundsHosts).CollectionChanged += OnContentBoundsHostsChanged; foreach (var item in viewport.ContentBoundsHosts) { PointsGraphBase chart = item as PointsGraphBase; if (chart != null) { chart.ProvideVisiblePoints = true; } } }
private void UpdateUIRepresentation() { if (Plotter == null) { return; } PointsGraphBase source = this.PointSource; if (source == null || (source != null && source.VisiblePoints == null)) { SetValue(MarkerPositionPropertyKey, new Point(Double.NaN, Double.NaN)); marker.Visibility = Visibility.Hidden; return; } else { Point mousePos = Mouse.GetPosition(Plotter.CentralGrid); var transform = Plotter.Transform; Point viewportPos = mousePos.ScreenToViewport(transform); double x = viewportPos.X; searchResult = searcher.SearchXBetween(x, searchResult); SetValue(ClosestPointIndexPropertyKey, searchResult.Index); if (!searchResult.IsEmpty) { marker.Visibility = Visibility.Visible; IList <Point> points = source.VisiblePoints; Point ptBefore = points[searchResult.Index]; Point ptAfter = points[searchResult.Index + 1]; double ratio = (x - ptBefore.X) / (ptAfter.X - ptBefore.X); double y = ptBefore.Y + (ptAfter.Y - ptBefore.Y) * ratio; Point temp = new Point(x, y); SetX(marker, temp.X); SetY(marker, temp.Y); Point markerPosition = temp; followDataContext.Position = markerPosition; SetValue(MarkerPositionPropertyKey, markerPosition); } else { SetValue(MarkerPositionPropertyKey, new Point(Double.NaN, Double.NaN)); marker.Visibility = Visibility.Hidden; } } }
public static CoordinateTransform GetTransform(PointsGraphBase graph) { if (!(graph.Plotter is Plotter2D)) { return(null); } var transform = ((Plotter2D)graph.Plotter).Viewport.Transform; if (graph.DataTransform != null) { transform = transform.WithDataTransform(graph.DataTransform); } return(transform); }
private void OnContentBoundsHostsChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.NewItems != null) { foreach (var item in e.NewItems) { PointsGraphBase chart = item as PointsGraphBase; if (chart != null) { chart.ProvideVisiblePoints = true; } } } // todo probably set ProvideVisiblePoints to false on OldItems }
/// <summary> /// Initializes a new instance of the <see cref="DataFollowChart"/> class, bound to specified <see cref="PointsGraphBase"/>. /// </summary> /// <param name="pointSource">The point source.</param> public DataFollowChart(PointsGraphBase pointSource) : this() { PointSource = pointSource; }
/// <summary> /// Initializes a new instance of the <see cref="DataFollowChart"/> class, bound to specified <see cref="PointsGraphBase"/>. /// </summary> /// <param name="pointSource">The point source.</param> public DataFollowChart(PointsGraphBase pointSource) : this() { PointSource = pointSource; }
public override DataRect Apply(DataRect oldDataRect, DataRect newDataRect, Viewport2D viewport) { DataRect overallBounds = DataRect.Empty; foreach (var chart in viewport.ContentBoundsHosts) { var plotterElement = chart as IPlotterElement; var visual = viewport.Plotter.VisualBindings[plotterElement]; var points = (ReadOnlyCollection <Point>)PointsGraphBase.GetVisiblePoints(visual); if (points != null) { // searching for indices of chart's visible points which are near left and right borders of newDataRect double startX = newDataRect.XMin; double endX = newDataRect.XMax; if (points[0].X > endX || points[points.Count - 1].X < startX) { continue; } int startIndex = -1; // we assume that points are sorted by x values ascending if (startX <= points[0].X) { startIndex = 0; } else { for (int i = 1; i < points.Count - 1; i++) { if (points[i].X <= startX && startX < points[i + 1].X) { startIndex = i; break; } } } int endIndex = points.Count; if (points[points.Count - 1].X < endX) { endIndex = points.Count; } else { for (int i = points.Count - 1; i >= 1; i--) { if (points[i - 1].X <= endX && endX < points[i].X) { endIndex = i; break; } } } Rect bounds = Rect.Empty; for (int i = startIndex; i < endIndex; i++) { bounds.Union(points[i]); } if (startIndex > 0) { Point pt = GetInterpolatedPoint(startX, points[startIndex], points[startIndex - 1]); bounds.Union(pt); } if (endIndex < points.Count - 1) { Point pt = GetInterpolatedPoint(endX, points[endIndex], points[endIndex + 1]); bounds.Union(pt); } overallBounds.Union(bounds); } } if (!overallBounds.IsEmpty) { double y = overallBounds.YMin; double height = overallBounds.Height; if (height == 0) { height = newDataRect.Height; y -= height / 2; } newDataRect = new DataRect(newDataRect.XMin, y, newDataRect.Width, height); newDataRect = DataRectExtensions.ZoomY(newDataRect, newDataRect.GetCenter(), yEnlargeCoeff); } return(newDataRect); }