Files
DynamicDataDisplay/PointMarkers/CenteredTextMarker.cs
2024-02-23 00:46:06 -05:00

85 lines
3.1 KiB
C#

using System.Threading;
using System.Windows;
using System.Windows.Media;
using System;
namespace Microsoft.Research.DynamicDataDisplay.PointMarkers
{
/// <summary>Renders specified text near the point</summary>
public class CenteredTextMarker : PointMarker
{
public static double DEFAULT_VERTICAL_SHIFT=-20;
public static double DEFAULT_HORIZONTAL_SHIFT=0;
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(
"Text",
typeof(string),
typeof(CenteredTextMarker),
new FrameworkPropertyMetadata(""));
public static readonly DependencyProperty VerticalShiftProperty =
DependencyProperty.Register(
"VerticalShift",
typeof(string),
typeof(CenteredTextMarker),
new FrameworkPropertyMetadata(""));
public static readonly DependencyProperty HorizontalShiftProperty =
DependencyProperty.Register(
"HorizontalShift",
typeof(string),
typeof(CenteredTextMarker),
new FrameworkPropertyMetadata(""));
public string VerticalShift
{
get { return (string)GetValue(VerticalShiftProperty); }
set { SetValue(VerticalShiftProperty, value); }
}
public string HorizontalShift
{
get { return (string)GetValue(HorizontalShiftProperty); }
set { SetValue(HorizontalShiftProperty, value); }
}
public override void Render(DrawingContext dc, Point screenPoint)
{
FormattedText textToDraw = new FormattedText(Text, Thread.CurrentThread.CurrentCulture,
FlowDirection.LeftToRight, new Typeface("Arial"), 12, Brushes.Black,1.0);
double width = textToDraw.Width;
double height = textToDraw.Height;
double verticalShift = DEFAULT_VERTICAL_SHIFT; // px
if (null != VerticalShift&&!"".Equals(VerticalShift))verticalShift = double.Parse(VerticalShift);
double horizontalShift=DEFAULT_HORIZONTAL_SHIFT;
if (null != HorizontalShift&&!"".Equals(HorizontalShift)) horizontalShift = double.Parse(HorizontalShift);
// Rect bounds = RectExtensions.FromCenterSize(new Point(screenPoint.X+horizontalShift, screenPoint.Y + verticalShift - height / 2),new Size(width, height));
double xPoint=0.00;
double yPoint=0.00;
if(0.00==horizontalShift)xPoint=screenPoint.X+horizontalShift;
else xPoint=screenPoint.X+horizontalShift - width/2;
yPoint=screenPoint.Y + verticalShift - height / 2;
Rect bounds = RectExtensions.FromCenterSize(new Point(xPoint, yPoint),new Size(width, height));
// forcing the X bound is incorrect as this will always force the text into the visible viewport even when it should be out of view
// if(bounds.X<0)bounds.X=Math.Abs(horizontalShift);
Point loc = bounds.Location;
bounds = CoordinateUtilities.RectZoom(bounds, 1.05, 1.15);
dc.DrawLine(new Pen(Brushes.Black, 1), Point.Add(screenPoint, new Vector(0, verticalShift)), screenPoint);
dc.DrawRectangle(Brushes.White, new Pen(Brushes.Black, 1), bounds);
dc.DrawText(textToDraw, loc);
}
}
}