Series.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. namespace Terminal.Gui {
  6. /// <summary>
  7. /// Describes a series of data that can be rendered into a <see cref="GraphView"/>>
  8. /// </summary>
  9. public interface ISeries {
  10. /// <summary>
  11. /// Draws the <paramref name="graphBounds"/> section of a series into the
  12. /// <paramref name="graph"/> view <paramref name="drawBounds"/>
  13. /// </summary>
  14. /// <param name="graph">Graph series is to be drawn onto</param>
  15. /// <param name="drawBounds">Visible area of the graph in Console Screen units (excluding margins)</param>
  16. /// <param name="graphBounds">Visible area of the graph in Graph space units</param>
  17. void DrawSeries (GraphView graph, Rect drawBounds, RectangleF graphBounds);
  18. }
  19. /// <summary>
  20. /// Series composed of any number of discrete data points
  21. /// </summary>
  22. public class ScatterSeries : ISeries {
  23. /// <summary>
  24. /// Collection of each discrete point in the series
  25. /// </summary>
  26. /// <returns></returns>
  27. public List<PointF> Points { get; set; } = new List<PointF> ();
  28. /// <summary>
  29. /// The color and character that will be rendered in the console
  30. /// when there are point(s) in the corresponding graph space.
  31. /// Defaults to uncolored 'x'
  32. /// </summary>
  33. public GraphCellToRender Fill { get; set; } = new GraphCellToRender ('x');
  34. /// <summary>
  35. /// Draws all points directly onto the graph
  36. /// </summary>
  37. public void DrawSeries (GraphView graph, Rect drawBounds, RectangleF graphBounds)
  38. {
  39. if (Fill.Color.HasValue) {
  40. Application.Driver.SetAttribute (Fill.Color.Value);
  41. }
  42. foreach (var p in Points.Where (p => graphBounds.Contains (p))) {
  43. var screenPoint = graph.GraphSpaceToScreen (p);
  44. graph.AddRune (screenPoint.X, screenPoint.Y, Fill.Rune);
  45. }
  46. }
  47. }
  48. /// <summary>
  49. /// Collection of <see cref="BarSeries"/> in which bars are clustered by category
  50. /// </summary>
  51. public class MultiBarSeries : ISeries {
  52. BarSeries [] subSeries;
  53. /// <summary>
  54. /// Sub collections. Each series contains the bars for a different category. Thus
  55. /// SubSeries[0].Bars[0] is the first bar on the axis and SubSeries[1].Bars[0] is the
  56. /// second etc
  57. /// </summary>
  58. public IReadOnlyCollection<BarSeries> SubSeries { get => new ReadOnlyCollection<BarSeries> (subSeries); }
  59. /// <summary>
  60. /// The number of units of graph space between bars. Should be
  61. /// less than <see cref="BarSeries.BarEvery"/>
  62. /// </summary>
  63. public float Spacing { get; }
  64. /// <summary>
  65. /// Creates a new series of clustered bars.
  66. /// </summary>
  67. /// <param name="numberOfBarsPerCategory">Each category has this many bars</param>
  68. /// <param name="barsEvery">How far appart to put each category (in graph space)</param>
  69. /// <param name="spacing">How much spacing between bars in a category (should be less than <paramref name="barsEvery"/>/<paramref name="numberOfBarsPerCategory"/>)</param>
  70. /// <param name="colors">Array of colors that define bar color in each category. Length must match <paramref name="numberOfBarsPerCategory"/></param>
  71. public MultiBarSeries (int numberOfBarsPerCategory, float barsEvery, float spacing, Attribute [] colors = null)
  72. {
  73. subSeries = new BarSeries [numberOfBarsPerCategory];
  74. if (colors != null && colors.Length != numberOfBarsPerCategory) {
  75. throw new ArgumentException ("Number of colors must match the number of bars", nameof (numberOfBarsPerCategory));
  76. }
  77. for (int i = 0; i < numberOfBarsPerCategory; i++) {
  78. subSeries [i] = new BarSeries ();
  79. subSeries [i].BarEvery = barsEvery;
  80. subSeries [i].Offset = i * spacing;
  81. // Only draw labels for the first bar in each category
  82. subSeries [i].DrawLabels = i == 0;
  83. if (colors != null) {
  84. subSeries [i].OverrideBarColor = colors [i];
  85. }
  86. }
  87. Spacing = spacing;
  88. }
  89. /// <summary>
  90. /// Adds a new cluster of bars
  91. /// </summary>
  92. /// <param name="label"></param>
  93. /// <param name="fill"></param>
  94. /// <param name="values">Values for each bar in category, must match the number of bars per category</param>
  95. public void AddBars (string label, Rune fill, params float [] values)
  96. {
  97. if (values.Length != subSeries.Length) {
  98. throw new ArgumentException ("Number of values must match the number of bars per category", nameof (values));
  99. }
  100. for (int i = 0; i < values.Length; i++) {
  101. subSeries [i].Bars.Add (new BarSeries.Bar (label,
  102. new GraphCellToRender (fill), values [i]));
  103. }
  104. }
  105. /// <summary>
  106. /// Draws all <see cref="SubSeries"/>
  107. /// </summary>
  108. /// <param name="graph"></param>
  109. /// <param name="drawBounds"></param>
  110. /// <param name="graphBounds"></param>
  111. public void DrawSeries (GraphView graph, Rect drawBounds, RectangleF graphBounds)
  112. {
  113. foreach (var bar in subSeries) {
  114. bar.DrawSeries (graph, drawBounds, graphBounds);
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// Series of bars positioned at regular intervals
  120. /// </summary>
  121. public class BarSeries : ISeries {
  122. /// <summary>
  123. /// Ordered collection of graph bars to position along axis
  124. /// </summary>
  125. public List<Bar> Bars { get; set; } = new List<Bar> ();
  126. /// <summary>
  127. /// Determines the spacing of bars along the axis. Defaults to 1 i.e.
  128. /// every 1 unit of graph space a bar is rendered. Note that you should
  129. /// also consider <see cref="GraphView.CellSize"/> when changing this.
  130. /// </summary>
  131. public float BarEvery { get; set; } = 1;
  132. /// <summary>
  133. /// Direction bars protrude from the corresponding axis.
  134. /// Defaults to vertical
  135. /// </summary>
  136. public Orientation Orientation { get; set; } = Orientation.Vertical;
  137. /// <summary>
  138. /// The number of units of graph space along the axis before rendering the first bar
  139. /// (and subsequent bars - see <see cref="BarEvery"/>). Defaults to 0
  140. /// </summary>
  141. public float Offset { get; set; } = 0;
  142. /// <summary>
  143. /// Overrides the <see cref="Bar.Fill"/> with a fixed color
  144. /// </summary>
  145. public Attribute? OverrideBarColor { get; set; }
  146. /// <summary>
  147. /// True to draw <see cref="Bar.Text"/> along the axis under the bar. Defaults
  148. /// to true.
  149. /// </summary>
  150. public bool DrawLabels { get; set; } = true;
  151. /// <summary>
  152. /// Applies any color overriding
  153. /// </summary>
  154. /// <param name="graphCellToRender"></param>
  155. /// <returns></returns>
  156. protected virtual GraphCellToRender AdjustColor (GraphCellToRender graphCellToRender)
  157. {
  158. if (OverrideBarColor.HasValue) {
  159. graphCellToRender.Color = OverrideBarColor;
  160. }
  161. return graphCellToRender;
  162. }
  163. /// <summary>
  164. /// Draws bars that are currently in the <paramref name="drawBounds"/>
  165. /// </summary>
  166. /// <param name="graph"></param>
  167. /// <param name="drawBounds">Screen area of the graph excluding margins</param>
  168. /// <param name="graphBounds">Graph space area that should be drawn into <paramref name="drawBounds"/></param>
  169. public virtual void DrawSeries (GraphView graph, Rect drawBounds, RectangleF graphBounds)
  170. {
  171. for (int i = 0; i < Bars.Count; i++) {
  172. float xStart = Orientation == Orientation.Horizontal ? 0 : Offset + ((i + 1) * BarEvery);
  173. float yStart = Orientation == Orientation.Horizontal ? Offset + ((i + 1) * BarEvery) : 0;
  174. float endX = Orientation == Orientation.Horizontal ? Bars [i].Value : xStart;
  175. float endY = Orientation == Orientation.Horizontal ? yStart : Bars [i].Value;
  176. // translate to screen positions
  177. var screenStart = graph.GraphSpaceToScreen (new PointF (xStart, yStart));
  178. var screenEnd = graph.GraphSpaceToScreen (new PointF (endX, endY));
  179. // Start the bar from wherever the axis is
  180. if (Orientation == Orientation.Horizontal) {
  181. screenStart.X = graph.AxisY.GetAxisXPosition (graph);
  182. // dont draw bar off the right of the control
  183. screenEnd.X = Math.Min (graph.Bounds.Width - 1, screenEnd.X);
  184. // if bar is off the screen
  185. if (screenStart.Y < 0 || screenStart.Y > drawBounds.Height - graph.MarginBottom) {
  186. continue;
  187. }
  188. } else {
  189. // Start the axis
  190. screenStart.Y = graph.AxisX.GetAxisYPosition (graph);
  191. // dont draw bar up above top of control
  192. screenEnd.Y = Math.Max (0, screenEnd.Y);
  193. // if bar is off the screen
  194. if (screenStart.X < graph.MarginLeft || screenStart.X > graph.MarginLeft + drawBounds.Width - 1) {
  195. continue;
  196. }
  197. }
  198. // draw the bar unless it has no height
  199. if (Bars [i].Value != 0) {
  200. DrawBarLine (graph, screenStart, screenEnd, Bars [i]);
  201. }
  202. // If we are drawing labels and the bar has one
  203. if (DrawLabels && !string.IsNullOrWhiteSpace (Bars [i].Text)) {
  204. // Add the label to the relevant axis
  205. if (Orientation == Orientation.Horizontal) {
  206. graph.AxisY.DrawAxisLabel (graph, screenStart.Y, Bars [i].Text);
  207. } else if (Orientation == Orientation.Vertical) {
  208. graph.AxisX.DrawAxisLabel (graph, screenStart.X, Bars [i].Text);
  209. }
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// Override to do custom drawing of the bar e.g. to apply varying color or changing the fill
  215. /// symbol mid bar.
  216. /// </summary>
  217. /// <param name="graph"></param>
  218. /// <param name="start">Screen position of the start of the bar</param>
  219. /// <param name="end">Screen position of the end of the bar</param>
  220. /// <param name="beingDrawn">The Bar that occupies this space and is being drawn</param>
  221. protected virtual void DrawBarLine (GraphView graph, Point start, Point end, Bar beingDrawn)
  222. {
  223. var adjusted = AdjustColor (beingDrawn.Fill);
  224. if (adjusted.Color.HasValue) {
  225. Application.Driver.SetAttribute (adjusted.Color.Value);
  226. }
  227. graph.DrawLine (start, end, adjusted.Rune);
  228. graph.SetDriverColorToGraphColor ();
  229. }
  230. /// <summary>
  231. /// A single bar in a <see cref="BarSeries"/>
  232. /// </summary>
  233. public class Bar {
  234. /// <summary>
  235. /// Optional text that describes the bar. This will be rendered on the corresponding
  236. /// <see cref="Axis"/> unless <see cref="DrawLabels"/> is false
  237. /// </summary>
  238. public string Text { get; set; }
  239. /// <summary>
  240. /// The color and character that will be rendered in the console
  241. /// when the bar extends over it
  242. /// </summary>
  243. public GraphCellToRender Fill { get; set; }
  244. /// <summary>
  245. /// The value in graph space X/Y (depending on <see cref="Orientation"/>) to which the bar extends.
  246. /// </summary>
  247. public float Value { get; }
  248. /// <summary>
  249. /// Creates a new instance of a single bar rendered in the given <paramref name="fill"/> that extends
  250. /// out <paramref name="value"/> graph space units in the default <see cref="Orientation"/>
  251. /// </summary>
  252. /// <param name="text"></param>
  253. /// <param name="fill"></param>
  254. /// <param name="value"></param>
  255. public Bar (string text, GraphCellToRender fill, float value)
  256. {
  257. Text = text;
  258. Fill = fill;
  259. Value = value;
  260. }
  261. }
  262. }
  263. }