Series.cs 9.6 KB

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