Ejemplo n.º 1
0
        private void UpdateUIRepresentation()
        {
            if (isUpdating)
            {
                return;
            }

            if (plotter == null)
            {
                return;
            }

            IPointDataSource dataSource = DataSource;

            if (dataSource == null)
            {
                return;
            }

            visibleWhileUpdate = plotter.Viewport.Visible;

            isUpdating = true;

            panel.Children.Clear();

            DependencyObject dependencyDataSource = dataSource as DependencyObject;

            if (dependencyDataSource != null)
            {
                DataSource2dContext.SetVisibleRect(dependencyDataSource, plotter.Viewport.Visible);
                DataSource2dContext.SetScreenRect(dependencyDataSource, plotter.Viewport.Output);
            }

            IEnumerable <Point> viewportPoints = dataSource;

            var transform = plotter.Viewport.Transform;

            if (!(transform.DataTransform is IdentityTransform))
            {
                viewportPoints = dataSource.DataToViewport(transform.DataTransform);
            }

            var screenPoints   = viewportPoints.ViewportToScreen(transform);
            var filteredPoints = filters.Filter(screenPoints, plotter.Viewport);

            DataRect bounds = DataRect.Empty;

            double strokeThickness = 3;

            bool  first = true;
            Point lastPointOfPrevLine = new Point();

            int overallCount = 0;

            panel.BeginBatchAdd();

            const int ptsInPolyline = 500;

            foreach (var pointGroup in filteredPoints.Split(ptsInPolyline))
            {
                int ptsCount = ptsInPolyline;

                if (!first)
                {
                    ptsCount++;
                }

                PointCollection pointCollection = new PointCollection(ptsCount);

                if (!first)
                {
                    pointCollection.Add(lastPointOfPrevLine);
                }
                else
                {
                    first = false;
                }

                pointCollection.AddMany(pointGroup);

                overallCount += pointCollection.Count - 1;

                if (pointCollection.Count == 0)
                {
                    break;
                }

                lastPointOfPrevLine = pointCollection[pointCollection.Count - 1];

                pointCollection.Freeze();

                DataRect ithBounds = BoundsHelper.GetViewportBounds(pointCollection.ScreenToViewport(transform));
#if geom
                UIElement line = null;

                StreamGeometry geometry = new StreamGeometry();
                using (var dc = geometry.Open())
                {
                    dc.BeginFigure(pointCollection[0], false, false);
                    dc.PolyLineTo(pointCollection, true, false);
                }
                geometry.Freeze();
                GeometryDrawing drawing = new GeometryDrawing {
                    Geometry = geometry, Pen = new Pen(Brushes.Blue, 1)
                };
                drawing.Freeze();
                DrawingBrush brush = new DrawingBrush {
                    Drawing = drawing
                };
                brush.Freeze();

                var rectangle = new Rectangle {
                    Fill = brush, IsHitTestVisible = false
                };
                Rect ithScreenBounds = ithBounds.ViewportToScreen(transform);
                if (true || ithScreenBounds.Width > 2000 || ithScreenBounds.Height > 2000 || ithScreenBounds.Width < 1 || ithScreenBounds.Height < 1)
                {
                    line = rectangle;
                }
                else
                {
                    Size intSize = new Size((int)ithScreenBounds.Width, (int)ithScreenBounds.Height);
                    rectangle.Measure(intSize);
                    rectangle.Arrange(new Rect(intSize));

                    RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)ithScreenBounds.Width, (int)ithScreenBounds.Height, 96, 96, PixelFormats.Pbgra32);
                    renderBitmap.Render(rectangle);
                    renderBitmap.Freeze();
                    line = new Image {
                        Source = renderBitmap
                    };
                }
#else
                var line = CreateLine();
                line.Points = pointCollection;
#endif

                bounds.Union(ithBounds);

                ViewportRectPanel.SetViewportBounds(line, ithBounds);
#if !geom
                ViewportMarginPanel.SetScreenMargin(line, new Size(strokeThickness / 2, strokeThickness / 2));
#endif
                panel.Children.Add(line);

                isUpdating = false;
            }

            panel.EndBatchAdd();

            Debug.WriteLine("OverallCount = " + (overallCount + 1));

            Viewport2D.SetContentBounds(this, bounds);

            isUpdating = false;
        }