protected override void CreateUIRepresentation() { if (Plotter2D == null) { return; } content.Children.Clear(); linePaths.Clear(); if (Collection != null) { DataRect bounds = DataRect.Empty; foreach (var line in Collection.Lines) { foreach (var point in line.AllPoints) { bounds.Union(point); } Path path = new Path { Stroke = new SolidColorBrush(Palette.GetColor(line.Value01)), StrokeThickness = StrokeThickness, Data = CreateGeometry(line), Tag = line }; content.Children.Add(path); linePaths.Add(path); } Viewport2D.SetContentBounds(this, bounds); if (DrawLabels) { var transform = Plotter2D.Viewport.Transform; double wayBeforeText = new Rect(new Size(2000, 2000)).ScreenToData(transform).Width; Annotater.WayBeforeText = wayBeforeText; var textLabels = Annotater.Annotate(Collection, Plotter2D.Viewport.Visible); foreach (var textLabel in textLabels) { var text = CreateTextLabel(textLabel); content.Children.Add(text); textBlocks.Add(text); } } } }
private void UpdateUIRepresentation() { foreach (var path in linePaths) { LevelLine line = (LevelLine)path.Tag; path.Data = CreateGeometry(line); } var transform = Plotter2D.Viewport.Transform; Rect output = Plotter2D.Viewport.Output; DataRect visible = Plotter2D.Viewport.Visible; if (rebuildText) { foreach (var text in textBlocks) { if (text.Visibility == Visibility.Visible) { content.Children.Remove(text); } } textBlocks.Clear(); double wayBeforeText = new Rect(new Size(100, 100)).ScreenToData(transform).Width; Annotater.WayBeforeText = wayBeforeText; var textLabels = Annotater.Annotate(Collection, Plotter2D.Viewport.Visible); foreach (var textLabel in textLabels) { TextBlock text = CreateTextLabel(textLabel); textBlocks.Add(text); if (visible.Contains(textLabel.Position)) { content.Children.Add(text); } else { text.Visibility = Visibility.Hidden; } } } else { foreach (var text in textBlocks) { IsolineTextLabel label = (IsolineTextLabel)text.Tag; Point screenPos = label.Position.DataToScreen(transform); Size textSize = text.DesiredSize; Point position = new Point(screenPos.X - textSize.Width / 2, screenPos.Y - textSize.Height / 2); if (output.Contains(position)) { Canvas.SetLeft(text, position.X); Canvas.SetTop(text, position.Y); if (text.Visibility == Visibility.Hidden) { text.Visibility = Visibility.Visible; content.Children.Add(text); } } else if (text.Visibility == Visibility.Visible) { text.Visibility = Visibility.Hidden; content.Children.Remove(text); } } } }
private void RenderLabels(DrawingContext dc, IsolineCollection collection) { if (Plotter2D == null) { return; } if (collection == null) { return; } if (!DrawLabels) { return; } var viewportBounds = ViewportPanel.GetViewportBounds(this); if (viewportBounds.IsEmpty) { return; } var strokeThickness = StrokeThickness; var visible = Plotter2D.Visible; var output = Plotter2D.Viewport.Output; var transform = Plotter2D.Transform.WithRects(viewportBounds, new Rect(RenderSize)); var labelStringFormat = LabelStringFormat; // drawing constants var labelRectangleFill = Brushes.White; var biggerViewport = viewportBounds.ZoomOutFromCenter(1.1); // getting and filtering annotations to draw only visible ones Annotater.WayBeforeText = Math.Sqrt(visible.Width * visible.Width + visible.Height * visible.Height) / 8 * WayBeforeTextMultiplier; var annotations = Annotater.Annotate(collection, visible) .Where(annotation => { Point viewportPosition = annotation.Position.DataToViewport(transform); return(biggerViewport.Contains(viewportPosition)); }); var labelsScale = LabelsScaling; // drawing annotations foreach (var annotation in annotations) { FormattedText text = CreateFormattedText(annotation.Value.ToString(LabelStringFormat)); Point position = annotation.Position.DataToScreen(transform); var labelTransform = CreateTransform(annotation, text, position); // creating rectange stroke double colorRatio = (annotation.Value - collection.Min) / (collection.Max - collection.Min); colorRatio = MathHelper.Clamp(colorRatio); Color rectangleStrokeColor = Palette.GetColor(colorRatio); SolidColorBrush rectangleStroke = new SolidColorBrush(rectangleStrokeColor); Pen labelRectangleStrokePen = new Pen(rectangleStroke, 2); dc.PushTransform(new ScaleTransform(1, labelsScale)); dc.PushTransform(labelTransform); { var bounds = RectExtensions.FromCenterSize(position, new Size(text.Width, text.Height)); bounds = bounds.ZoomOutFromCenter(1.3); dc.DrawRoundedRectangle(labelRectangleFill, labelRectangleStrokePen, bounds, 8, 8); DrawTextInPosition(dc, text, position); } dc.Pop(); dc.Pop(); } }