namespace Terminal.Gui.Views;
/// Displays text at a given position (in screen space or graph space)
public class TextAnnotation : IAnnotation
{
///
/// The location in graph space to draw the . This annotation will only show if the point is in
/// the current viewable area of the graph presented in the
///
public PointF GraphPosition { get; set; }
///
/// The location on screen to draw the regardless of scroll/zoom settings. This overrides
/// if specified.
///
public Point? ScreenPosition { get; set; }
/// Text to display on the graph
public string Text { get; set; }
/// True to add text before plotting series. Defaults to false
public bool BeforeSeries { get; set; }
/// Draws the annotation
///
public void Render (GraphView graph)
{
if (ScreenPosition.HasValue)
{
DrawText (graph, ScreenPosition.Value.X, ScreenPosition.Value.Y);
return;
}
Point screenPos = graph.GraphSpaceToScreen (GraphPosition);
DrawText (graph, screenPos.X, screenPos.Y);
}
///
/// Draws the at the given coordinates with truncation to avoid spilling over
/// of the
///
///
/// Screen x position to start drawing string
/// Screen y position to start drawing string
protected void DrawText (GraphView graph, int x, int y)
{
// the draw point is out of control bounds
if (!graph.Viewport.Contains (new Point (x, y)))
{
return;
}
// There is no text to draw
if (string.IsNullOrWhiteSpace (Text))
{
return;
}
graph.Move (x, y);
int availableWidth = graph.Viewport.Width - x;
if (availableWidth <= 0)
{
return;
}
if (Text.Length < availableWidth)
{
graph.AddStr (Text);
}
else
{
graph.AddStr (Text.Substring (0, availableWidth));
}
}
}