Axis.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. namespace Terminal.Gui;
  2. /// <summary>Renders a continuous line with grid line ticks and labels</summary>
  3. public abstract class Axis
  4. {
  5. /// <summary>Default value for <see cref="ShowLabelsEvery"/></summary>
  6. private const uint DefaultShowLabelsEvery = 5;
  7. /// <summary>
  8. /// Allows you to control what label text is rendered for a given <see cref="Increment"/> when
  9. /// <see cref="ShowLabelsEvery"/> is above 0
  10. /// </summary>
  11. public LabelGetterDelegate LabelGetter;
  12. /// <summary>Populates base properties and sets the read only <see cref="Orientation"/></summary>
  13. /// <param name="orientation"></param>
  14. protected Axis (Orientation orientation)
  15. {
  16. Orientation = orientation;
  17. LabelGetter = DefaultLabelGetter;
  18. }
  19. /// <summary>Number of units of graph space between ticks on axis. 0 for no ticks</summary>
  20. /// <value></value>
  21. public float Increment { get; set; } = 1;
  22. /// <summary>The minimum axis point to show. Defaults to null (no minimum)</summary>
  23. public float? Minimum { get; set; }
  24. /// <summary>Direction of the axis</summary>
  25. /// <value></value>
  26. public Orientation Orientation { get; }
  27. /// <summary>The number of <see cref="Increment"/> before an label is added. 0 = never show labels</summary>
  28. public uint ShowLabelsEvery { get; set; } = DefaultShowLabelsEvery;
  29. /// <summary>
  30. /// Displayed below/to left of labels (see <see cref="Orientation"/>). If text is not visible, check
  31. /// <see cref="GraphView.MarginBottom"/> / <see cref="GraphView.MarginLeft"/>
  32. /// </summary>
  33. public string Text { get; set; }
  34. /// <summary>True to render axis. Defaults to true</summary>
  35. public bool Visible { get; set; } = true;
  36. /// <summary>
  37. /// Draws a custom label <paramref name="text"/> at <paramref name="screenPosition"/> units along the axis (X or Y
  38. /// depending on <see cref="Orientation"/>)
  39. /// </summary>
  40. /// <param name="graph"></param>
  41. /// <param name="screenPosition"></param>
  42. /// <param name="text"></param>
  43. public abstract void DrawAxisLabel (GraphView graph, int screenPosition, string text);
  44. /// <summary>Draws labels and axis <see cref="Increment"/> ticks</summary>
  45. /// <param name="graph"></param>
  46. public abstract void DrawAxisLabels (GraphView graph);
  47. /// <summary>Draws the solid line of the axis</summary>
  48. /// <param name="graph"></param>
  49. public abstract void DrawAxisLine (GraphView graph);
  50. /// <summary>Resets all configurable properties of the axis to default values</summary>
  51. public virtual void Reset ()
  52. {
  53. Increment = 1;
  54. ShowLabelsEvery = DefaultShowLabelsEvery;
  55. Visible = true;
  56. Text = "";
  57. LabelGetter = DefaultLabelGetter;
  58. Minimum = null;
  59. }
  60. /// <summary>Draws a single cell of the solid line of the axis</summary>
  61. /// <param name="graph"></param>
  62. /// <param name="x"></param>
  63. /// <param name="y"></param>
  64. protected abstract void DrawAxisLine (GraphView graph, int x, int y);
  65. private string DefaultLabelGetter (AxisIncrementToRender toRender) { return toRender.Value.ToString ("N0"); }
  66. }
  67. /// <summary>The horizontal (x axis) of a <see cref="GraphView"/></summary>
  68. public class HorizontalAxis : Axis
  69. {
  70. /// <summary>
  71. /// Creates a new instance of axis with an <see cref="Orientation"/> of <see cref="Orientation.Horizontal"/>
  72. /// </summary>
  73. public HorizontalAxis () : base (Orientation.Horizontal) { }
  74. /// <summary>
  75. /// Draws the given <paramref name="text"/> on the axis at x <paramref name="screenPosition"/>. For the screen y
  76. /// position use <see cref="GetAxisYPosition(GraphView)"/>
  77. /// </summary>
  78. /// <param name="graph">Graph being drawn onto</param>
  79. /// <param name="screenPosition">Number of screen columns along the axis to take before rendering</param>
  80. /// <param name="text">Text to render under the axis tick</param>
  81. public override void DrawAxisLabel (GraphView graph, int screenPosition, string text)
  82. {
  83. ConsoleDriver driver = Application.Driver;
  84. int y = GetAxisYPosition (graph);
  85. graph.Move (screenPosition, y);
  86. // draw the tick on the axis
  87. Application.Driver.AddRune (Glyphs.TopTee);
  88. // and the label text
  89. if (!string.IsNullOrWhiteSpace (text))
  90. {
  91. // center the label but don't draw it outside bounds of the graph
  92. int drawAtX = Math.Max (0, screenPosition - text.Length / 2);
  93. string toRender = text;
  94. // this is how much space is left
  95. int xSpaceAvailable = graph.Viewport.Width - drawAtX;
  96. // There is no space for the label at all!
  97. if (xSpaceAvailable <= 0)
  98. {
  99. return;
  100. }
  101. // if we are close to right side of graph, don't overspill
  102. if (toRender.Length > xSpaceAvailable)
  103. {
  104. toRender = toRender.Substring (0, xSpaceAvailable);
  105. }
  106. graph.Move (drawAtX, Math.Min (y + 1, graph.Viewport.Height - 1));
  107. driver.AddStr (toRender);
  108. }
  109. }
  110. /// <summary>Draws the horizontal x axis labels and <see cref="Axis.Increment"/> ticks</summary>
  111. public override void DrawAxisLabels (GraphView graph)
  112. {
  113. if (!Visible || Increment == 0)
  114. {
  115. return;
  116. }
  117. Rectangle bounds = graph.Viewport;
  118. IEnumerable<AxisIncrementToRender> labels = GetLabels (graph, bounds);
  119. foreach (AxisIncrementToRender label in labels)
  120. {
  121. DrawAxisLabel (graph, label.ScreenLocation, label.Text);
  122. }
  123. // if there is a title
  124. if (!string.IsNullOrWhiteSpace (Text))
  125. {
  126. string toRender = Text;
  127. // if label is too long
  128. if (toRender.Length > graph.Viewport.Width)
  129. {
  130. toRender = toRender.Substring (0, graph.Viewport.Width);
  131. }
  132. graph.Move (graph.Viewport.Width / 2 - toRender.Length / 2, graph.Viewport.Height - 1);
  133. Application.Driver.AddStr (toRender);
  134. }
  135. }
  136. /// <summary>Draws the horizontal axis line</summary>
  137. /// <param name="graph"></param>
  138. public override void DrawAxisLine (GraphView graph)
  139. {
  140. if (!Visible)
  141. {
  142. return;
  143. }
  144. Rectangle bounds = graph.Viewport;
  145. graph.Move (0, 0);
  146. int y = GetAxisYPosition (graph);
  147. // start the x axis at left of screen (either 0 or margin)
  148. var xStart = (int)graph.MarginLeft;
  149. // but if the x axis has a minmum (minimum is in graph space units)
  150. if (Minimum.HasValue)
  151. {
  152. // start at the screen location of the minimum
  153. int minimumScreenX = graph.GraphSpaceToScreen (new PointF (Minimum.Value, y)).X;
  154. // unless that is off the screen to the left
  155. xStart = Math.Max (xStart, minimumScreenX);
  156. }
  157. for (int i = xStart; i < bounds.Width; i++)
  158. {
  159. DrawAxisLine (graph, i, y);
  160. }
  161. }
  162. /// <summary>
  163. /// Returns the Y screen position of the origin (typically 0,0) of graph space. Return value is bounded by the
  164. /// screen i.e. the axis is always rendered even if the origin is offscreen.
  165. /// </summary>
  166. /// <param name="graph"></param>
  167. public int GetAxisYPosition (GraphView graph)
  168. {
  169. // find the origin of the graph in screen space (this allows for 'crosshair' style
  170. // graphs where positive and negative numbers visible
  171. Point origin = graph.GraphSpaceToScreen (new PointF (0, 0));
  172. // float the X axis so that it accurately represents the origin of the graph
  173. // but anchor it to top/bottom if the origin is offscreen
  174. return Math.Min (Math.Max (0, origin.Y), graph.Viewport.Height - ((int)graph.MarginBottom + 1));
  175. }
  176. /// <summary>Draws a horizontal axis line at the given <paramref name="x"/>, <paramref name="y"/> screen coordinates</summary>
  177. /// <param name="graph"></param>
  178. /// <param name="x"></param>
  179. /// <param name="y"></param>
  180. protected override void DrawAxisLine (GraphView graph, int x, int y)
  181. {
  182. graph.Move (x, y);
  183. Application.Driver.AddRune (Glyphs.HLine);
  184. }
  185. private IEnumerable<AxisIncrementToRender> GetLabels (GraphView graph, Rectangle bounds)
  186. {
  187. // if no labels
  188. if (Increment == 0)
  189. {
  190. yield break;
  191. }
  192. var labels = 0;
  193. int y = GetAxisYPosition (graph);
  194. RectangleF start = graph.ScreenToGraphSpace ((int)graph.MarginLeft, y);
  195. RectangleF end = graph.ScreenToGraphSpace (bounds.Width, y);
  196. // don't draw labels below the minimum
  197. if (Minimum.HasValue)
  198. {
  199. start.X = Math.Max (start.X, Minimum.Value);
  200. }
  201. RectangleF current = start;
  202. while (current.X < end.X)
  203. {
  204. int screenX = graph.GraphSpaceToScreen (new PointF (current.X, current.Y)).X;
  205. // The increment we will render (normally a top T unicode symbol)
  206. var toRender = new AxisIncrementToRender (Orientation, screenX, current.X);
  207. // Not every increment has to have a label
  208. if (ShowLabelsEvery != 0)
  209. {
  210. // if this increment does also needs a label
  211. if (labels++ % ShowLabelsEvery == 0)
  212. {
  213. toRender.Text = LabelGetter (toRender);
  214. }
  215. ;
  216. }
  217. // Label or no label definetly render it
  218. yield return toRender;
  219. current.X += Increment;
  220. }
  221. }
  222. }
  223. /// <summary>The vertical (i.e. Y axis) of a <see cref="GraphView"/></summary>
  224. public class VerticalAxis : Axis
  225. {
  226. /// <summary>Creates a new <see cref="Orientation.Vertical"/> axis</summary>
  227. public VerticalAxis () : base (Orientation.Vertical) { }
  228. /// <summary>
  229. /// Draws the given <paramref name="text"/> on the axis at y <paramref name="screenPosition"/>. For the screen x
  230. /// position use <see cref="GetAxisXPosition(GraphView)"/>
  231. /// </summary>
  232. /// <param name="graph">Graph being drawn onto</param>
  233. /// <param name="screenPosition">Number of rows from the top of the screen (i.e. down the axis) before rendering</param>
  234. /// <param name="text">
  235. /// Text to render to the left of the axis tick. Ensure to set <see cref="GraphView.MarginLeft"/> or
  236. /// <see cref="GraphView.ScrollOffset"/> sufficient that it is visible
  237. /// </param>
  238. public override void DrawAxisLabel (GraphView graph, int screenPosition, string text)
  239. {
  240. int x = GetAxisXPosition (graph);
  241. int labelThickness = text.Length;
  242. graph.Move (x, screenPosition);
  243. // draw the tick on the axis
  244. Application.Driver.AddRune (Glyphs.RightTee);
  245. // and the label text
  246. if (!string.IsNullOrWhiteSpace (text))
  247. {
  248. graph.Move (Math.Max (0, x - labelThickness), screenPosition);
  249. Application.Driver.AddStr (text);
  250. }
  251. }
  252. /// <summary>Draws axis <see cref="Axis.Increment"/> markers and labels</summary>
  253. /// <param name="graph"></param>
  254. public override void DrawAxisLabels (GraphView graph)
  255. {
  256. if (!Visible || Increment == 0)
  257. {
  258. return;
  259. }
  260. Rectangle bounds = graph.Viewport;
  261. IEnumerable<AxisIncrementToRender> labels = GetLabels (graph, bounds);
  262. foreach (AxisIncrementToRender label in labels)
  263. {
  264. DrawAxisLabel (graph, label.ScreenLocation, label.Text);
  265. }
  266. // if there is a title
  267. if (!string.IsNullOrWhiteSpace (Text))
  268. {
  269. string toRender = Text;
  270. // if label is too long
  271. if (toRender.Length > graph.Viewport.Height)
  272. {
  273. toRender = toRender.Substring (0, graph.Viewport.Height);
  274. }
  275. // Draw it 1 letter at a time vertically down row 0 of the control
  276. int startDrawingAtY = graph.Viewport.Height / 2 - toRender.Length / 2;
  277. for (var i = 0; i < toRender.Length; i++)
  278. {
  279. graph.Move (0, startDrawingAtY + i);
  280. Application.Driver.AddRune ((Rune)toRender [i]);
  281. }
  282. }
  283. }
  284. /// <summary>Draws the vertical axis line</summary>
  285. /// <param name="graph"></param>
  286. public override void DrawAxisLine (GraphView graph)
  287. {
  288. if (!Visible)
  289. {
  290. return;
  291. }
  292. Rectangle bounds = graph.Viewport;
  293. int x = GetAxisXPosition (graph);
  294. int yEnd = GetAxisYEnd (graph);
  295. // don't draw down further than the control bounds
  296. yEnd = Math.Min (yEnd, bounds.Height - (int)graph.MarginBottom);
  297. // Draw solid line
  298. for (var i = 0; i < yEnd; i++)
  299. {
  300. DrawAxisLine (graph, x, i);
  301. }
  302. }
  303. /// <summary>
  304. /// Returns the X screen position of the origin (typically 0,0) of graph space. Return value is bounded by the
  305. /// screen i.e. the axis is always rendered even if the origin is offscreen.
  306. /// </summary>
  307. /// <param name="graph"></param>
  308. public int GetAxisXPosition (GraphView graph)
  309. {
  310. // find the origin of the graph in screen space (this allows for 'crosshair' style
  311. // graphs where positive and negative numbers visible
  312. Point origin = graph.GraphSpaceToScreen (new PointF (0, 0));
  313. // float the Y axis so that it accurately represents the origin of the graph
  314. // but anchor it to left/right if the origin is offscreen
  315. return Math.Min (Math.Max ((int)graph.MarginLeft, origin.X), graph.Viewport.Width - 1);
  316. }
  317. /// <summary>Draws a vertical axis line at the given <paramref name="x"/>, <paramref name="y"/> screen coordinates</summary>
  318. /// <param name="graph"></param>
  319. /// <param name="x"></param>
  320. /// <param name="y"></param>
  321. protected override void DrawAxisLine (GraphView graph, int x, int y)
  322. {
  323. graph.Move (x, y);
  324. Application.Driver.AddRune (Glyphs.VLine);
  325. }
  326. private int GetAxisYEnd (GraphView graph)
  327. {
  328. // draw down the screen (0 is top of screen)
  329. // end at the bottom of the screen
  330. //unless there is a minimum
  331. if (Minimum.HasValue)
  332. {
  333. return graph.GraphSpaceToScreen (new PointF (0, Minimum.Value)).Y;
  334. }
  335. return graph.Viewport.Height;
  336. }
  337. private IEnumerable<AxisIncrementToRender> GetLabels (GraphView graph, Rectangle bounds)
  338. {
  339. // if no labels
  340. if (Increment == 0)
  341. {
  342. yield break;
  343. }
  344. var labels = 0;
  345. int x = GetAxisXPosition (graph);
  346. // remember screen space is top down so the lowest graph
  347. // space value is at the bottom of the screen
  348. RectangleF start = graph.ScreenToGraphSpace (x, bounds.Height - (1 + (int)graph.MarginBottom));
  349. RectangleF end = graph.ScreenToGraphSpace (x, 0);
  350. // don't draw labels below the minimum
  351. if (Minimum.HasValue)
  352. {
  353. start.Y = Math.Max (start.Y, Minimum.Value);
  354. }
  355. RectangleF current = start;
  356. while (current.Y < end.Y)
  357. {
  358. int screenY = graph.GraphSpaceToScreen (new PointF (current.X, current.Y)).Y;
  359. // Create the axis symbol
  360. var toRender = new AxisIncrementToRender (Orientation, screenY, current.Y);
  361. // and the label (if we are due one)
  362. if (ShowLabelsEvery != 0)
  363. {
  364. // if this increment also needs a label
  365. if (labels++ % ShowLabelsEvery == 0)
  366. {
  367. toRender.Text = LabelGetter (toRender);
  368. }
  369. ;
  370. }
  371. // draw the axis symbol (and label if it has one)
  372. yield return toRender;
  373. current.Y += Increment;
  374. }
  375. }
  376. }
  377. /// <summary>A location on an axis of a <see cref="GraphView"/> that may or may not have a label associated with it</summary>
  378. public class AxisIncrementToRender
  379. {
  380. private string _text = "";
  381. /// <summary>Describe a new section of an axis that requires an axis increment symbol and/or label</summary>
  382. /// <param name="orientation"></param>
  383. /// <param name="screen"></param>
  384. /// <param name="value"></param>
  385. public AxisIncrementToRender (Orientation orientation, int screen, float value)
  386. {
  387. Orientation = orientation;
  388. ScreenLocation = screen;
  389. Value = value;
  390. }
  391. /// <summary>Direction of the parent axis</summary>
  392. public Orientation Orientation { get; }
  393. /// <summary>The screen location (X or Y depending on <see cref="Orientation"/>) that the increment will be rendered at</summary>
  394. public int ScreenLocation { get; }
  395. /// <summary>The value at this position on the axis in graph space</summary>
  396. public float Value { get; }
  397. /// <summary>The text (if any) that should be displayed at this axis increment</summary>
  398. /// <value></value>
  399. internal string Text
  400. {
  401. get => _text;
  402. set => _text = value ?? "";
  403. }
  404. }
  405. /// <summary>Delegate for custom formatting of axis labels. Determines what should be displayed at a given label</summary>
  406. /// <param name="toRender">The axis increment to which the label is attached</param>
  407. /// <returns></returns>
  408. public delegate string LabelGetterDelegate (AxisIncrementToRender toRender);