protected override void OnRender(DrawingContext drawingContext) { if (Fill == null && SpriteData != null) { Fill = ToWPF.Convert(SpriteData.GetFrame(Animation, Orientation, AnimationProgress)); } drawingContext.DrawRectangle(Fill, null, _Rectangle); }
/// <summary> /// Refresh the graph contents /// </summary> public void Refresh() { Children.Clear(); double width = ActualWidth; double height = ActualHeight; if (Data != null && Data.Count > 0 && height > 0 && width > 0) { // Calculate transform: Interval xRange, yRange; if (FlipXY) { xRange = Data.ValueRange.Include(0); yRange = Data.KeyRange.Include(0); } else { yRange = Data.ValueRange.Include(0); xRange = Data.KeyRange.Include(0); } if (xRange.IsValid && yRange.IsValid) { //Extend range to include any axis label presets: if (YAxisLabels != null) { yRange = yRange.Union(YAxisLabels.KeyRange()); } if (XAxisLabels != null) { xRange = xRange.Union(XAxisLabels.KeyRange()); } // Draw grid: var xAxisLabels = XAxisLabels; var yAxisLabels = YAxisLabels; if (xAxisLabels == null) { xAxisLabels = GenerateAxisLabels(xRange, XAxisLabelSuffix); } if (yAxisLabels == null) { yAxisLabels = GenerateAxisLabels(yRange, YAxisLabelSuffix); } // Y-axis labels: if (xAxisLabels != null) { foreach (KeyValuePair <double, string> kvp in xAxisLabels) { double x = xRange.ParameterOf(kvp.Key) * width; Line gridLine = new Line(); gridLine.X1 = x; gridLine.X2 = x; gridLine.Y1 = 0; gridLine.Y2 = height; gridLine.Stroke = Brushes.Black; gridLine.Opacity = 0.25; Children.Add(gridLine); TextBlock tB = new TextBlock(); tB.Text = kvp.Value; tB.TextAlignment = TextAlignment.Right; FormattedText fT = new FormattedText(kvp.Value, CultureInfo.CurrentCulture, tB.FlowDirection, new Typeface(tB.FontFamily, tB.FontStyle, tB.FontWeight, tB.FontStretch), tB.FontSize, tB.Foreground); Children.Add(tB); Canvas.SetLeft(tB, x - fT.Width / 2); Canvas.SetTop(tB, (1 - yRange.ParameterOf(0)) * height + 5); } } // Y-axis labels: if (yAxisLabels != null) { foreach (KeyValuePair <double, string> kvp in yAxisLabels) { double y = (1 - yRange.ParameterOf(kvp.Key)) * height; Line gridLine = new Line(); gridLine.X1 = 0; gridLine.X2 = width; gridLine.Y1 = y; gridLine.Y2 = y; gridLine.Stroke = Brushes.Black; gridLine.Opacity = 0.25; Children.Add(gridLine); TextBlock tB = new TextBlock(); tB.Text = kvp.Value; tB.TextAlignment = TextAlignment.Right; Children.Add(tB); Canvas.SetRight(tB, (1 - xRange.ParameterOf(0)) * width + 5); Canvas.SetTop(tB, y - tB.FontSize / 2); } } // X-axis: Line xAxis = new Line(); xAxis.X1 = 0; xAxis.X2 = width; xAxis.Y1 = (1 - yRange.ParameterOf(0)) * height; xAxis.Y2 = xAxis.Y1; xAxis.Stroke = Brushes.Black; xAxis.StrokeThickness = 2.0; Children.Add(xAxis); // Y-axis: Line yAxis = new Line(); yAxis.X1 = xRange.ParameterOf(0) * width; yAxis.X2 = yAxis.X1; yAxis.Y1 = 0; yAxis.Y2 = height; yAxis.Stroke = Brushes.Black; yAxis.StrokeThickness = 2.0; Children.Add(yAxis); // Draw graph lines: for (int i = 0; i < Data.Count; i++) { GraphLineData data = Data[i]; Colour col = Colour.FromHSV(i * 360.0 / Data.Count, 255, 255); Polyline pLine = new Polyline(); pLine.Stroke = new SolidColorBrush(ToWPF.Convert(col)); //pLine.StrokeThickness = 1.0; pLine.ToolTip = data.Name; ToolTipService.SetInitialShowDelay(pLine, 0); ToolTipService.SetShowDuration(pLine, 60000); //Generate points: PointCollection points = new PointCollection(); var graph = data.Data; foreach (KeyValuePair <double, Interval> kvp in graph) { points.Add(ToPoint(kvp.Key, kvp.Value.End, xRange, yRange)); } if (graph.IsEnvelope) { foreach (KeyValuePair <double, Interval> kvp in graph.Reverse()) { points.Add(ToPoint(kvp.Key, kvp.Value.Start, xRange, yRange)); } } pLine.Points = points; Children.Add(pLine); } } } }
/// <summary> /// Add geometric contents to the canvas, automatically converting from /// Nucleus geometry to the WPF equivalent /// </summary> /// <param name="shape"></param> public void AddContents(VertexGeometry shape) { if (shape is Curve) { Curve crv = (Curve)shape; PathGeometry pathGeo = new PathGeometry(); pathGeo.Figures.Add(ToWPF.Convert(crv)); Path path = new Path(); path.DataContext = crv; //if (crv.Attributes == null || crv.Attributes.Brush == null) //{ // path.Stroke = DefaultBrush; //} //else path.Stroke = FBtoWPF.Convert(crv.Attributes.Brush); var strokeBinding = new Binding("Attributes.Brush"); strokeBinding.Converter = new Converters.BrushConverter(); strokeBinding.FallbackValue = DefaultBrush; path.SetBinding(Path.StrokeProperty, strokeBinding); path.StrokeStartLineCap = PenLineCap.Round; path.StrokeEndLineCap = PenLineCap.Round; //path.StrokeThickness = CurveThickness; //var thicknessBinding = new Binding("CurveThickness"); //thicknessBinding.Source = this; if (crv.Attributes == null) { //path.SetBinding(Path.StrokeThicknessProperty, thicknessBinding); path.StrokeThickness = CurveThickness; } else { /*var mBinding = new MultiBinding(); * mBinding.Converter = new Converters.MultiplicationConverter(); * mBinding.Bindings.Add(thicknessBinding); * mBinding.Bindings.Add(new Binding("Attributes.Weight")); * * path.SetBinding(Path.StrokeThicknessProperty, mBinding);*/ path.StrokeThickness = CurveThickness * crv.Attributes.Weight; } // * scaleFactor; path.Data = pathGeo; path.StrokeLineJoin = PenLineJoin.Round; if (crv.Closed && FillBrush != null) { path.Fill = FillBrush; } //fillBrush = null; path.Tag = shape; Children.Add(path); } else if (shape is PlanarRegion) { PlanarRegion reg = (PlanarRegion)shape; Curve perimeter = reg.Perimeter; CurveCollection voids = reg.Voids; PathGeometry perimeterPath = new PathGeometry(); perimeterPath.Figures.Add(ToWPF.Convert(perimeter)); CombinedGeometry cg = new CombinedGeometry(); cg.GeometryCombineMode = GeometryCombineMode.Exclude; cg.Geometry1 = perimeterPath; if (voids != null && voids.Count > 0) { PathGeometry inside = new PathGeometry(); foreach (Curve vCrv in voids) { inside.Figures.Add(ToWPF.Convert(vCrv)); } cg.Geometry2 = inside; } Path path = new Path(); path.DataContext = shape; //if (reg.Attributes == null || reg.Attributes.Brush == null) //{ // path.Fill = DefaultBrush; //} //else path.Fill = FBtoWPF.Convert(reg.Attributes.Brush, 192); var fillBinding = new Binding("Attributes.Brush"); fillBinding.Converter = new Converters.BrushConverter(); fillBinding.FallbackValue = DefaultBrush; fillBinding.ConverterParameter = 192; path.SetBinding(Path.FillProperty, fillBinding); //path.Stroke = Brushes.Black; //path.StrokeThickness = 0.01; path.Data = cg; path.Tag = shape; Children.Add(path); } else if (shape is FB.Label) { FB.Label label = (FB.Label)shape; TextBlock tB = new TextBlock(); tB.DataContext = label; tB.Padding = new Thickness(0); if (label.TextBinding != null) { tB.SetBinding(TextBlock.TextProperty, ToWPF.Convert(label.TextBinding)); } else { tB.SetBinding(TextBlock.TextProperty, new Binding("Text")); } tB.FontSize = 1; //label.TextSize; tB.RenderTransform = new ScaleTransform(label.TextSize, label.TextSize); //tB.RenderTransformOrigin = new System.Windows.Point(0, 1); var fillBinding = new Binding("Attributes.Brush"); fillBinding.Converter = new Converters.BrushConverter(); fillBinding.FallbackValue = DefaultBrush; tB.SetBinding(TextBlock.ForegroundProperty, fillBinding); FormattedText fT = new FormattedText(label.Text, CultureInfo.CurrentCulture, tB.FlowDirection, new Typeface(tB.FontFamily, tB.FontStyle, tB.FontWeight, tB.FontStretch), 1, tB.Foreground); double xOffset; if (label.HorizontalSetOut == HorizontalSetOut.Left) { xOffset = 0; } else if (label.HorizontalSetOut == HorizontalSetOut.Right) { xOffset = fT.Width * label.TextSize; } else { xOffset = label.TextSize * fT.Width / 2; } double yOffset; if (label.VerticalSetOut == VerticalSetOut.Top) { yOffset = 0; } else if (label.VerticalSetOut == VerticalSetOut.Bottom) { yOffset = fT.Height * label.TextSize; } else { yOffset = label.TextSize * fT.Height / 2; } SetLeft(tB, label.Position.X - xOffset); SetTop(tB, -label.Position.Y - yOffset); tB.Tag = shape; Children.Add(tB); } else if (shape is Cloud || shape is FB.Point) { double diameter = PointDiameter; if (shape.Attributes != null) { diameter *= shape.Attributes.Weight; } foreach (Vertex v in shape.Vertices) { Ellipse ellipse = new Ellipse(); ellipse.Width = diameter; ellipse.Height = diameter; ellipse.DataContext = shape; var fillBinding = new Binding("Attributes.Brush"); fillBinding.Converter = new Converters.BrushConverter(); fillBinding.FallbackValue = DefaultBrush; ellipse.SetBinding(Ellipse.FillProperty, fillBinding); SetLeft(ellipse, v.X - diameter / 2.0); SetTop(ellipse, -v.Y - diameter / 2.0); ellipse.Tag = shape; if (shape.Attributes != null && shape.Attributes.Interactive) { //TODO: CHANGE THIS! TEMP ONLY! ellipse.Cursor = Cursors.Hand; } Children.Add(ellipse); } } }
/// <summary> /// Recreate the displayed geometry inside this canvas /// </summary> public void RefreshContents() { BoundingBox bBox = new BoundingBox(0, 0, 0); Children.Clear(); var steelGrad = new GradientStopCollection(); steelGrad.Add(new GradientStop(Colors.LightGray, 0)); steelGrad.Add(new GradientStop(Colors.White, 0.5)); steelGrad.Add(new GradientStop(Colors.LightGray, 1)); var steelBrush = new LinearGradientBrush(steelGrad, 45); var topLayer = new List <UIElement>(); if (Profiles != null) { foreach (SectionProfile sp in Profiles) { Curve perimeter = sp.Perimeter; if (perimeter != null) { CurveCollection voids = sp.Voids; bBox.Include(perimeter.BoundingBox); PathGeometry perimeterPath = new PathGeometry(); perimeterPath.Figures.Add(ToWPF.Convert(perimeter)); CombinedGeometry cg = new CombinedGeometry(); cg.GeometryCombineMode = GeometryCombineMode.Exclude; cg.Geometry1 = perimeterPath; if (voids != null && voids.Count > 0) { PathGeometry inside = new PathGeometry(); foreach (Curve vCrv in voids) { inside.Figures.Add(ToWPF.Convert(vCrv)); } cg.Geometry2 = inside; } Path path = new Path(); path.Fill = steelBrush; path.Stroke = Brushes.Black; path.StrokeThickness = 0.01; path.Data = cg; topLayer.Add(path); } } bBox.Expand(Math.Max(bBox.SizeX, bBox.SizeY) * 0.1); double maxSize = Math.Max(bBox.SizeX, bBox.SizeY); //Draw grid: /*var axisBrush = new RadialGradientBrush(Colors.Black, Colors.Transparent); * * var hAxis = new W.Line(); * hAxis.X1 = -maxSize; * hAxis.X2 = maxSize; * hAxis.Stroke = axisBrush; * hAxis.StrokeThickness = maxSize*0.005; * Children.Add(hAxis);*/ //Adjust stroke: foreach (UIElement el in topLayer) { if (el is Path) { ((Path)el).StrokeThickness = maxSize * 0.005; } } foreach (UIElement el in topLayer) { Children.Add(el); } //Draw set-out point Ellipse setOutPt = new Ellipse(); setOutPt.Width = maxSize * 0.03; setOutPt.Height = setOutPt.Width; SetLeft(setOutPt, -setOutPt.Width / 2); SetTop(setOutPt, -setOutPt.Height / 2); setOutPt.Fill = Brushes.OrangeRed; Children.Add(setOutPt); } Width = bBox.SizeX; Height = bBox.SizeY; RenderTransform = new TranslateTransform(bBox.SizeX / 2 - bBox.Mid.X, bBox.SizeY / 2 + bBox.Mid.Y); }
/// <summary> /// Refresh and redraw the diagram /// </summary> public void Refresh() { MainCanvas.Children.Clear(); //Clear canvas if (SourceData != null) { // Size of the diagram (in canvas units): double radius = 100; NamedDataSet axisRanges = AxisRanges; if (axisRanges == null) { axisRanges = SourceData.GetValueRanges(); } if (MinimumAxisRange.IsValid) { axisRanges.UnionAll(MinimumAxisRange); } //Draw axes: List <string> axes = SourceData.GetAllKeys().ToList(); axes.Sort(); for (int i = 0; i < axes.Count; i++) { string axisName = axes[i]; double angle = ((double)i) * (Math.PI * 2 / axes.Count); Line axis = new Line(); axis.X2 = Math.Sin(angle) * radius; axis.Y2 = -Math.Cos(angle) * radius; axis.Stroke = Brushes.Black; axis.StrokeThickness = 1; axis.Opacity = 0.6; axis.ToolTip = axisName; MainCanvas.Children.Add(axis); TextBlock tB = new TextBlock(); tB.Text = axisName; tB.Width = radius - 3; tB.FontSize = LabelFontSize; if (angle <= Math.PI) { tB.TextAlignment = TextAlignment.Right; var rTrans = new RotateTransform(angle * 180 / Math.PI - 90); rTrans.CenterX = 0; rTrans.CenterY = 0; tB.RenderTransform = rTrans; } else { Canvas.SetRight(tB, 0); tB.TextAlignment = TextAlignment.Left; var rTrans = new RotateTransform(angle * 180 / Math.PI + 90); rTrans.CenterX = radius - 3; rTrans.CenterY = 0; tB.RenderTransform = rTrans; } tB.Opacity = 0.8; tB.ToolTip = axisName; MainCanvas.Children.Add(tB); } // Draw map: foreach (NamedDataSet dataSet in SourceData) { Polygon pgon = new Polygon(); Color color = ToWPF.Convert(dataSet.Colour.CapBrightness(ColourBrightnessCap)); pgon.Stroke = new SolidColorBrush(color); pgon.StrokeThickness = 2; pgon.StrokeLineJoin = PenLineJoin.Bevel; Color outColor = color; Color inColor = color; outColor.A = 100; inColor.A = 100; var fill = new RadialGradientBrush(inColor, outColor); fill.Opacity = FillOpacity; pgon.Fill = fill; pgon.Opacity = 0.95; pgon.ToolTip = dataSet.Name; PointCollection outerPts = new PointCollection(); PointCollection innerPts = new PointCollection(); bool hollow = false; for (int i = 0; i < axes.Count; i++) { string axisName = axes[i]; double angle = ((double)i) * (Math.PI * 2 / axes.Count); double value = 0; double value2 = 0; if (dataSet.Data.ContainsKey(axisName)) { Interval range; if (axisRanges.Data.ContainsKey(axisName)) { range = axisRanges.Data[axisName]; } else { range = new Interval(0, 1.0); } if (range.IsSingularity) { range = new Interval(range.Start - 0.1, range.End); } Interval data = dataSet.Data[axisName]; value = range.ParameterOf(data.End) * radius; value2 = range.ParameterOf(data.Start) * radius; if (value2 > 0.001) { hollow = true; } } Point point = new Point(Math.Sin(angle) * value, -Math.Cos(angle) * value); outerPts.Add(point); Point point2 = new Point(Math.Sin(angle) * value2, -Math.Cos(angle) * value2); innerPts.Add(point2); } pgon.Points = outerPts; //TODO: Include 'hollow' polygons that have minimum values MainCanvas.Children.Add(pgon); } } }
public VertexGeometry GeometryOver(FB.Vector point) { _hTR = null; VisualTreeHelper.HitTest(ItemsControl, new HitTestFilterCallback(HitTestFilterCallback), new HitTestResultCallback(HitTestResultCallback), new PointHitTestParameters(ToWPF.Convert(point))); if (_hTR != null) { FrameworkElement fE = _hTR.VisualHit as FrameworkElement; if (fE != null && fE.Tag != null && fE.Tag is VertexGeometry) { return((VertexGeometry)fE.Tag); } } return(null); }