2
0

PathAnnotation.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. namespace Terminal.Gui.Views;
  2. /// <summary>Sequence of lines to connect points e.g. of a <see cref="ScatterSeries"/></summary>
  3. public class PathAnnotation : IAnnotation
  4. {
  5. /// <summary>Color for the line that connects points</summary>
  6. public Attribute? LineColor { get; set; }
  7. /// <summary>The symbol that gets drawn along the line, defaults to '.'</summary>
  8. public Rune LineRune { get; set; } = new ('.');
  9. /// <summary>Points that should be connected. Lines will be drawn between points in the order they appear in the list</summary>
  10. public List<PointF> Points { get; set; } = new ();
  11. /// <summary>True to add line before plotting series. Defaults to false</summary>
  12. public bool BeforeSeries { get; set; }
  13. /// <summary>Draws lines connecting each of the <see cref="Points"/></summary>
  14. /// <param name="graph"></param>
  15. public void Render (GraphView graph)
  16. {
  17. graph.SetAttribute (LineColor ?? graph.GetAttributeForRole (VisualRole.Normal));
  18. foreach (LineF line in PointsToLines ())
  19. {
  20. Point start = graph.GraphSpaceToScreen (line.Start);
  21. Point end = graph.GraphSpaceToScreen (line.End);
  22. graph.DrawLine (start, end, LineRune);
  23. }
  24. }
  25. /// <summary>Generates lines joining <see cref="Points"/></summary>
  26. /// <returns></returns>
  27. private IEnumerable<LineF> PointsToLines ()
  28. {
  29. for (var i = 0; i < Points.Count - 1; i++)
  30. {
  31. yield return new (Points [i], Points [i + 1]);
  32. }
  33. }
  34. }