private void DoRender() { if (PrimitiveTransform == null || PrimitiveList == null) { return; } _drawingGroup.Children.Clear(); DrawingContext dc = _drawingGroup.Append(); // Need to draw something on the whole rectangle, otherwise it doesn't align properly dc.DrawGeometry(new SolidColorBrush(Color.FromArgb(1, 255, 255, 255)), null, _drawingGroup.ClipGeometry); if (BackgroundImage != null) { Rect rect = PrimitiveTransform.TransformBounds(PrimitiveDataRange); dc.DrawImage(BackgroundImage, rect); } foreach (IChartRendererWPF primitive in PrimitiveList) { primitive.RenderFilledElements(dc, ChartDataRange, PrimitiveTransform); } foreach (IChartRendererWPF primitive in PrimitiveList) { primitive.RenderUnfilledElements(dc, ChartDataRange, PrimitiveTransform); } // Now render the points Rect clipBounds = _drawingGroup.ClipGeometry.Bounds; Rect bounds = new Rect(-3, -3, clipBounds.Width + 6, clipBounds.Height + 6); foreach (ChartPrimitive primitive in PrimitiveList) { if (primitive.ShowPoints) { Brush pointBrush = new SolidColorBrush(primitive.PointColor); Pen pointPen = new Pen(pointBrush, 1); foreach (Point point in primitive.Points) { Point transformedPoint = PrimitiveTransform.Transform(point); if (bounds.Contains(transformedPoint)) { dc.DrawEllipse(null, pointPen, transformedPoint, 2, 2); // How to draw text next to each point //FormattedText formattedLabel = new FormattedText(point.ToString(), System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Arial"), 10, pointBrush); //var invertY = new ScaleTransform(1, -1); //dc.PushTransform(invertY); //dc.DrawText(formattedLabel, invertY.Transform(transformedPoint)); //dc.Pop(); } } } } dc.Close(); }
private void DoRender() { if (PrimitiveTransform == null || PrimitiveList == null) { return; } _drawingGroup.Children.Clear(); DrawingContext dc = _drawingGroup.Append(); // Need to draw something on the whole rectangle, otherwise it doesn't align properly dc.DrawGeometry(new SolidColorBrush(Color.FromArgb(1, 255, 255, 255)), null, _drawingGroup.ClipGeometry); foreach (IChartRendererWPF primitive in PrimitiveList) { primitive.RenderFilledElements(dc, ChartDataRange, PrimitiveTransform); } foreach (IChartRendererWPF primitive in PrimitiveList) { primitive.RenderUnfilledElements(dc, ChartDataRange, PrimitiveTransform); } // Now render the points Rect clipBounds = _drawingGroup.ClipGeometry.Bounds; Rect bounds = new Rect(-3, -3, clipBounds.Width + 6, clipBounds.Height + 6); foreach (ChartPrimitive primitive in PrimitiveList) { if (primitive.ShowPoints) { Brush pointBrush = new SolidColorBrush(primitive.PointColor); Pen pointPen = new Pen(pointBrush, 1); foreach (Point point in primitive.Points) { Point transformedPoint = PrimitiveTransform.Transform(point); if (bounds.Contains(transformedPoint)) { dc.DrawEllipse(null, pointPen, transformedPoint, 2, 2); } } } } dc.Close(); }
private void DoRender(IntPtr pIDXGISurface, bool dunnoWhatThisIsFor) { if (pIDXGISurface != m_pIDXGISurfacePreviousNoRef) { m_pIDXGISurfacePreviousNoRef = pIDXGISurface; // Create the render target Surface dxgiSurface = new Surface(pIDXGISurface); SurfaceDescription sd = dxgiSurface.Description; RenderTargetProperties rtp = new RenderTargetProperties( RenderTargetType.Default, new PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied), 96, 96, // Force bitmap rendering if you want it to work on remote desktop connections RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); try { _renderTarget = new RenderTarget(_d2dFactory, dxgiSurface, rtp); } catch (Exception) { return; } // Clear the surface to transparent //_renderTarget.BeginDraw(); //_renderTarget.Clear(new ColorF(1, 1, 1, 0)); //_renderTarget.EndDraw(); _isValid = true; } if (PrimitiveTransform == null || PrimitiveList == null) { return; } _renderTarget.BeginDraw(); _renderTarget.Clear(new SharpDX.Mathematics.Interop.RawColor4(1, 1, 1, 0)); foreach (IChartRendererD2D primitive in PrimitiveList) { primitive.RenderFilledElements(this, ChartDataRange, PrimitiveTransform); } foreach (IChartRendererD2D primitive in PrimitiveList) { primitive.RenderUnfilledElements(this, ChartDataRange, PrimitiveTransform); } // Now render the points System.Windows.Rect bounds = new System.Windows.Rect(-3, -3, _canvasRect.Width + 6, _canvasRect.Height + 6); foreach (ChartPrimitive primitive in PrimitiveList) { if (primitive.ShowPoints) { var brush = new SolidColorBrush(RenderTarget, primitive.PointColor.ToD2D()); Ellipse ellipse = new Ellipse(new SharpDX.Mathematics.Interop.RawVector2(0, 0), 2, 2); foreach (Point point in primitive.Points) { Point transformedPoint = PrimitiveTransform.Transform(point); if (bounds.Contains(transformedPoint)) { ellipse.Point = transformedPoint.ToD2D(); RenderTarget.DrawEllipse(ellipse, brush, 1); } } } } _renderTarget.EndDraw(); }
/// <summary> /// Draws a mouse cursor on the adorened element /// </summary> /// <param name="drawingContext"></param> protected override void OnRender(DrawingContext drawingContext) { GeneralTransform inverse = PrimitiveTransform.Inverse; if (inverse == null) { return; } Func <double, string> DefaultLabelGenerator = x => x.ToString(); // Make sure these functions are assigned XAxisLabelGenerator = XAxisLabelGenerator ?? DefaultLabelGenerator; YAxisLabelGenerator = YAxisLabelGenerator ?? DefaultLabelGenerator; //_closestPointPicker.MouseMoved(MousePoint, inverse); Brush blackBrush = new SolidColorBrush(Colors.Black); Pen thinBlackPen = new Pen(blackBrush, 0.5); float radius = 15; if (_locked) { // Draw the little circle around the lock point Pen blackPen = new Pen(blackBrush, 3); Point point = PrimitiveTransform.Transform(_lockPoint); drawingContext.DrawEllipse(null, blackPen, point, 2.5, 2.5); drawingContext.DrawEllipse(null, new Pen(new SolidColorBrush(Colors.White), 2), point, 2.5, 2.5); // Draw the big yellow circle Pen yellowPen = new Pen(new SolidColorBrush(Colors.Yellow), 2); drawingContext.DrawEllipse(null, blackPen, _mousePoint, radius, radius); drawingContext.DrawEllipse(null, yellowPen, _mousePoint, radius, radius); if (_drawWholePlotCursor) { drawingContext.DrawLine(thinBlackPen, new Point(point.X, 0), new Point(point.X, point.Y - 4)); drawingContext.DrawLine(thinBlackPen, new Point(point.X, point.Y + 4), new Point(point.X, this.ActualHeight)); drawingContext.DrawLine(thinBlackPen, new Point(0, point.Y), new Point(point.X - 4, point.Y)); drawingContext.DrawLine(thinBlackPen, new Point(point.X + 4, point.Y), new Point(this.ActualWidth, point.Y)); } } else { // Draw the target symbol drawingContext.DrawEllipse(null, thinBlackPen, _mousePoint, radius, radius); if (_drawWholePlotCursor) { drawingContext.DrawLine(thinBlackPen, new Point(_mousePoint.X, 0), new Point(_mousePoint.X, _mousePoint.Y - 2)); drawingContext.DrawLine(thinBlackPen, new Point(_mousePoint.X, _mousePoint.Y + 2), new Point(_mousePoint.X, this.ActualHeight)); drawingContext.DrawLine(thinBlackPen, new Point(0, _mousePoint.Y), new Point(_mousePoint.X - 2, _mousePoint.Y)); drawingContext.DrawLine(thinBlackPen, new Point(_mousePoint.X + 2, _mousePoint.Y), new Point(this.ActualWidth, _mousePoint.Y)); } else { drawingContext.DrawLine(thinBlackPen, new Point(_mousePoint.X - radius * 1.6, _mousePoint.Y), new Point(_mousePoint.X - 2, _mousePoint.Y)); drawingContext.DrawLine(thinBlackPen, new Point(_mousePoint.X + radius * 1.6, _mousePoint.Y), new Point(_mousePoint.X + 2, _mousePoint.Y)); drawingContext.DrawLine(thinBlackPen, new Point(_mousePoint.X, _mousePoint.Y - radius * 1.6), new Point(_mousePoint.X, _mousePoint.Y - 2)); drawingContext.DrawLine(thinBlackPen, new Point(_mousePoint.X, _mousePoint.Y + radius * 1.6), new Point(_mousePoint.X, _mousePoint.Y + 2)); } } // Draw the coordinate text // Works out the number of decimal places required to show the difference between // 2 pixels. E.g if pixels are .1 apart then use 2 places etc Rect rect = inverse.TransformBounds(new Rect(0, 0, 1, 1)); int xFigures = Math.Max(1, (int)(Math.Ceiling(-Math.Log10(rect.Width)) + .1)); int yFigures = Math.Max(1, (int)(Math.Ceiling(-Math.Log10(rect.Height)) + .1)); // Number of significant figures for the x coordinate string xFormat = "#0." + new string('#', xFigures); /// Number of significant figures for the y coordinate string yFormat = "#0." + new string('#', yFigures); string coordinateText; if (_locked && _pointToText.ContainsKey(_lockPoint)) { coordinateText = _pointToText[_lockPoint]; } else { Point coordinate = _locked ? _lockPoint : inverse.Transform(_mousePoint); coordinateText = XAxisLabelGenerator(Math.Round(coordinate.X, xFigures)) + " , " + YAxisLabelGenerator(Math.Round(coordinate.Y, yFigures)); } drawingContext.PushTransform(new ScaleTransform(1, -1)); FormattedText formattedText = new FormattedText(coordinateText, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Arial"), 10, blackBrush); Rect textBoxRect = new Rect(new Point(_mousePoint.X + radius * .7, -_mousePoint.Y + radius * .7), new Size(formattedText.Width, formattedText.Height)); double diff = textBoxRect.Right + 3 - ActualWidth; if (diff > 0) { textBoxRect.Location = new Point(textBoxRect.Left - diff, textBoxRect.Top); } drawingContext.DrawRectangle(textBoxPen.Brush, textBoxPen, textBoxRect); drawingContext.DrawText(formattedText, textBoxRect.Location); drawingContext.Pop(); }