PathAnnotation.cs 1.5 KB

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