Axis.cs 17 KB

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