Series.cs 9.6 KB

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