Annotations.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Terminal.Gui {
  5. /// <summary>
  6. /// <para>Describes an overlay element that is rendered either before or
  7. /// after a series.</para>
  8. ///
  9. /// <para>Annotations can be positioned either in screen space (e.g.
  10. /// a legend) or in graph space (e.g. a line showing high point)
  11. /// </para>
  12. /// <para>Unlike <see cref="ISeries"/>, annotations are allowed to
  13. /// draw into graph margins
  14. /// </para>
  15. /// </summary>
  16. public interface IAnnotation {
  17. /// <summary>
  18. /// True if annotation should be drawn before <see cref="ISeries"/>. This
  19. /// allows Series and later annotations to potentially draw over the top
  20. /// of this annotation.
  21. /// </summary>
  22. bool BeforeSeries { get; }
  23. /// <summary>
  24. /// Called once after series have been rendered (or before if <see cref="BeforeSeries"/> is true).
  25. /// Use <see cref="View.Driver"/> to draw and <see cref="View.Bounds"/> to avoid drawing outside of
  26. /// graph
  27. /// </summary>
  28. /// <param name="graph"></param>
  29. void Render (GraphView graph);
  30. }
  31. /// <summary>
  32. /// Displays text at a given position (in screen space or graph space)
  33. /// </summary>
  34. public class TextAnnotation : IAnnotation {
  35. /// <summary>
  36. /// The location on screen to draw the <see cref="Text"/> regardless
  37. /// of scroll/zoom settings. This overrides <see cref="GraphPosition"/>
  38. /// if specified.
  39. /// </summary>
  40. public Point? ScreenPosition { get; set; }
  41. /// <summary>
  42. /// The location in graph space to draw the <see cref="Text"/>. This
  43. /// annotation will only show if the point is in the current viewable
  44. /// area of the graph presented in the <see cref="GraphView"/>
  45. /// </summary>
  46. public PointF GraphPosition { get; set; }
  47. /// <summary>
  48. /// Text to display on the graph
  49. /// </summary>
  50. public string Text { get; set; }
  51. /// <summary>
  52. /// True to add text before plotting series. Defaults to false
  53. /// </summary>
  54. public bool BeforeSeries { get; set; }
  55. /// <summary>
  56. /// Draws the annotation
  57. /// </summary>
  58. /// <param name="graph"></param>
  59. public void Render (GraphView graph)
  60. {
  61. if (ScreenPosition.HasValue) {
  62. DrawText (graph, ScreenPosition.Value.X, ScreenPosition.Value.Y);
  63. return;
  64. }
  65. var screenPos = graph.GraphSpaceToScreen (GraphPosition);
  66. DrawText (graph, screenPos.X, screenPos.Y);
  67. }
  68. /// <summary>
  69. /// Draws the <see cref="Text"/> at the given coordinates with truncation to avoid
  70. /// spilling over <see name="View.Bounds"/> of the <paramref name="graph"/>
  71. /// </summary>
  72. /// <param name="graph"></param>
  73. /// <param name="x">Screen x position to start drawing string</param>
  74. /// <param name="y">Screen y position to start drawing string</param>
  75. protected void DrawText (GraphView graph, int x, int y)
  76. {
  77. // the draw point is out of control bounds
  78. if (!graph.Bounds.Contains (new Point (x, y))) {
  79. return;
  80. }
  81. // There is no text to draw
  82. if (string.IsNullOrWhiteSpace (Text)) {
  83. return;
  84. }
  85. graph.Move (x, y);
  86. int availableWidth = graph.Bounds.Width - x;
  87. if (availableWidth <= 0) {
  88. return;
  89. }
  90. if (Text.Length < availableWidth) {
  91. View.Driver.AddStr (Text);
  92. } else {
  93. View.Driver.AddStr (Text.Substring (0, availableWidth));
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// A box containing symbol definitions e.g. meanings for colors in a graph.
  99. /// The 'Key' to the graph
  100. /// </summary>
  101. public class LegendAnnotation : View, IAnnotation {
  102. /// <summary>
  103. /// Returns false i.e. Legends render after series
  104. /// </summary>
  105. public bool BeforeSeries => false;
  106. /// <summary>
  107. /// Ordered collection of entries that are rendered in the legend.
  108. /// </summary>
  109. List<Tuple<GraphCellToRender, string>> _entries = new List<Tuple<GraphCellToRender, string>> ();
  110. /// <summary>
  111. /// Creates a new empty legend at the empty screen coordinates.
  112. /// </summary>
  113. public LegendAnnotation () : this (Rect.Empty) { }
  114. /// <summary>
  115. /// Creates a new empty legend at the given screen coordinates.
  116. /// </summary>
  117. /// <param name="legendBounds">Defines the area available for the legend to render in
  118. /// (within the graph). This is in screen units (i.e. not graph space)</param>
  119. public LegendAnnotation (Rect legendBounds)
  120. {
  121. X = legendBounds.X;
  122. Y = legendBounds.Y;
  123. Width = legendBounds.Width;
  124. Height = legendBounds.Height;
  125. BorderStyle = LineStyle.Single;
  126. }
  127. /// <summary>
  128. /// Draws the Legend and all entries into the area within <see cref="View.Bounds"/>
  129. /// </summary>
  130. /// <param name="graph"></param>
  131. public void Render (GraphView graph)
  132. {
  133. if (!IsInitialized) {
  134. ColorScheme = new ColorScheme () { Normal = Application.Driver.GetAttribute () };
  135. graph.Add (this);
  136. }
  137. if (BorderStyle != LineStyle.None) {
  138. OnDrawAdornments ();
  139. OnRenderLineCanvas ();
  140. }
  141. int linesDrawn = 0;
  142. foreach (var entry in _entries) {
  143. if (entry.Item1.Color.HasValue) {
  144. Application.Driver.SetAttribute (entry.Item1.Color.Value);
  145. } else {
  146. graph.SetDriverColorToGraphColor ();
  147. }
  148. // add the symbol
  149. AddRune (0, linesDrawn, entry.Item1.Rune);
  150. // switch to normal coloring (for the text)
  151. graph.SetDriverColorToGraphColor ();
  152. // add the text
  153. Move (1, linesDrawn);
  154. string str = TextFormatter.ClipOrPad (entry.Item2, Bounds.Width - 1);
  155. Application.Driver.AddStr (str);
  156. linesDrawn++;
  157. // Legend has run out of space
  158. if (linesDrawn >= Bounds.Height) {
  159. break;
  160. }
  161. }
  162. }
  163. /// <summary>
  164. /// Adds an entry into the legend. Duplicate entries are permissible
  165. /// </summary>
  166. /// <param name="graphCellToRender">The symbol appearing on the graph that should appear in the legend</param>
  167. /// <param name="text">Text to render on this line of the legend. Will be truncated
  168. /// if outside of Legend <see cref="View.Bounds"/></param>
  169. public void AddEntry (GraphCellToRender graphCellToRender, string text)
  170. {
  171. _entries.Add (Tuple.Create (graphCellToRender, text));
  172. }
  173. }
  174. /// <summary>
  175. /// Sequence of lines to connect points e.g. of a <see cref="ScatterSeries"/>
  176. /// </summary>
  177. public class PathAnnotation : IAnnotation {
  178. /// <summary>
  179. /// Points that should be connected. Lines will be drawn between points in the order
  180. /// they appear in the list
  181. /// </summary>
  182. public List<PointF> Points { get; set; } = new List<PointF> ();
  183. /// <summary>
  184. /// Color for the line that connects points
  185. /// </summary>
  186. public Attribute? LineColor { get; set; }
  187. /// <summary>
  188. /// The symbol that gets drawn along the line, defaults to '.'
  189. /// </summary>
  190. public Rune LineRune { get; set; } = new Rune ('.');
  191. /// <summary>
  192. /// True to add line before plotting series. Defaults to false
  193. /// </summary>
  194. public bool BeforeSeries { get; set; }
  195. /// <summary>
  196. /// Draws lines connecting each of the <see cref="Points"/>
  197. /// </summary>
  198. /// <param name="graph"></param>
  199. public void Render (GraphView graph)
  200. {
  201. View.Driver.SetAttribute (LineColor ?? graph.ColorScheme.Normal);
  202. foreach (var line in PointsToLines ()) {
  203. var start = graph.GraphSpaceToScreen (line.Start);
  204. var end = graph.GraphSpaceToScreen (line.End);
  205. graph.DrawLine (start, end, LineRune);
  206. }
  207. }
  208. /// <summary>
  209. /// Generates lines joining <see cref="Points"/>
  210. /// </summary>
  211. /// <returns></returns>
  212. private IEnumerable<LineF> PointsToLines ()
  213. {
  214. for (int i = 0; i < Points.Count - 1; i++) {
  215. yield return new LineF (Points [i], Points [i + 1]);
  216. }
  217. }
  218. }
  219. }