GraphViewExample.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Terminal.Gui;
  6. namespace UICatalog.Scenarios;
  7. [ScenarioMetadata ("Graph View", "Demos the GraphView control.")]
  8. [ScenarioCategory ("Controls")]
  9. [ScenarioCategory ("Drawing")]
  10. public class GraphViewExample : Scenario {
  11. TextView _about;
  12. int _currentGraph;
  13. Action [] _graphs;
  14. GraphView _graphView;
  15. MenuItem _miDiags;
  16. MenuItem _miShowBorder;
  17. public override void Setup ()
  18. {
  19. Win.Title = GetName ();
  20. Win.Y = 1; // menu
  21. Win.Height = Dim.Fill (1); // status bar
  22. _graphs = new [] {
  23. () => SetupPeriodicTableScatterPlot (), //0
  24. () => SetupLifeExpectancyBarGraph (true), //1
  25. () => SetupLifeExpectancyBarGraph (false), //2
  26. () => SetupPopulationPyramid (), //3
  27. () => SetupLineGraph (), //4
  28. () => SetupSineWave (), //5
  29. () => SetupDisco (), //6
  30. () => MultiBarGraph () //7
  31. };
  32. var menu = new MenuBar (new MenuBarItem [] {
  33. new ("_File", new MenuItem [] {
  34. new ("Scatter _Plot", "", () => _graphs [_currentGraph = 0] ()),
  35. new ("_V Bar Graph", "", () => _graphs [_currentGraph = 1] ()),
  36. new ("_H Bar Graph", "", () => _graphs [_currentGraph = 2] ()),
  37. new ("P_opulation Pyramid", "", () => _graphs [_currentGraph = 3] ()),
  38. new ("_Line Graph", "", () => _graphs [_currentGraph = 4] ()),
  39. new ("Sine _Wave", "", () => _graphs [_currentGraph = 5] ()),
  40. new ("Silent _Disco", "", () => _graphs [_currentGraph = 6] ()),
  41. new ("_Multi Bar Graph", "", () => _graphs [_currentGraph = 7] ()),
  42. new ("_Quit", "", () => Quit ())
  43. }),
  44. new ("_View", new [] {
  45. new ("Zoom _In", "", () => Zoom (0.5f)),
  46. new ("Zoom _Out", "", () => Zoom (2f)),
  47. new ("MarginLeft++", "", () => Margin (true, true)),
  48. new ("MarginLeft--", "", () => Margin (true, false)),
  49. new ("MarginBottom++", "", () => Margin (false, true)),
  50. new ("MarginBottom--", "", () => Margin (false, false)),
  51. _miShowBorder = new MenuItem ("_Enable Margin, Border, and Padding", "", () => ShowBorder ()) {
  52. Checked = true,
  53. CheckType = MenuItemCheckStyle.Checked
  54. },
  55. _miDiags = new MenuItem ("Dri_ver Diagnostics", "", () => EnableDiagnostics ()) {
  56. Checked = false,
  57. CheckType = MenuItemCheckStyle.Checked
  58. }
  59. })
  60. });
  61. Application.Top.Add (menu);
  62. _graphView = new GraphView {
  63. X = 0,
  64. Y = 0,
  65. Width = 60,
  66. Height = Dim.Fill (),
  67. BorderStyle = LineStyle.Single
  68. };
  69. Win.Add (_graphView);
  70. var frameRight = new FrameView ("About") {
  71. X = Pos.Right (_graphView) + 1,
  72. Y = 0,
  73. Width = Dim.Fill (),
  74. Height = Dim.Fill ()
  75. };
  76. frameRight.Add (_about = new TextView {
  77. Width = Dim.Fill (),
  78. Height = Dim.Fill ()
  79. });
  80. Win.Add (frameRight);
  81. var statusBar = new StatusBar (new StatusItem [] {
  82. new (Application.QuitKey, $"{Application.QuitKey} to Quit", () => Quit ()),
  83. new (KeyCode.CtrlMask | KeyCode.G, "~^G~ Next", () => _graphs [_currentGraph++ % _graphs.Length] ())
  84. });
  85. Application.Top.Add (statusBar);
  86. }
  87. void ShowBorder ()
  88. {
  89. _miShowBorder.Checked = !_miShowBorder.Checked;
  90. if (_miShowBorder.Checked == true) {
  91. _graphView.BorderStyle = LineStyle.Single;
  92. _graphView.Border.Thickness = new Thickness (1, 3, 1, 1);
  93. _graphView.Margin.Thickness = new Thickness (2, 2, 2, 2);
  94. _graphView.Padding.Thickness = new Thickness (2, 2, 2, 2);
  95. } else {
  96. _graphView.BorderStyle = LineStyle.None;
  97. _graphView.Margin.Thickness = Thickness.Empty;
  98. _graphView.Padding.Thickness = Thickness.Empty;
  99. }
  100. }
  101. void EnableDiagnostics ()
  102. {
  103. _miDiags.Checked = !_miDiags.Checked;
  104. ConsoleDriver.Diagnostics = _miDiags.Checked == true ? ConsoleDriver.DiagnosticFlags.FramePadding | ConsoleDriver.DiagnosticFlags.FrameRuler : ConsoleDriver.DiagnosticFlags.Off;
  105. Application.Refresh ();
  106. }
  107. void MultiBarGraph ()
  108. {
  109. _graphView.Reset ();
  110. _graphView.Title = "Multi Bar";
  111. _about.Text = "Housing Expenditures by income thirds 1996-2003";
  112. var fore = _graphView.ColorScheme.Normal.Foreground == new Color (ColorName.Black) ? new Color (ColorName.White) : _graphView.ColorScheme.Normal.Foreground;
  113. var black = new Attribute (fore, Color.Black);
  114. var cyan = new Attribute (Color.BrightCyan, Color.Black);
  115. var magenta = new Attribute (Color.BrightMagenta, Color.Black);
  116. var red = new Attribute (Color.BrightRed, Color.Black);
  117. _graphView.GraphColor = black;
  118. var series = new MultiBarSeries (3, 1, 0.25f, new [] { magenta, cyan, red });
  119. var stiple = CM.Glyphs.Stipple;
  120. series.AddBars ("'96", stiple, 5900, 9000, 14000);
  121. series.AddBars ("'97", stiple, 6100, 9200, 14800);
  122. series.AddBars ("'98", stiple, 6000, 9300, 14600);
  123. series.AddBars ("'99", stiple, 6100, 9400, 14950);
  124. series.AddBars ("'00", stiple, 6200, 9500, 15200);
  125. series.AddBars ("'01", stiple, 6250, 9900, 16000);
  126. series.AddBars ("'02", stiple, 6600, 11000, 16700);
  127. series.AddBars ("'03", stiple, 7000, 12000, 17000);
  128. _graphView.CellSize = new PointF (0.25f, 1000);
  129. _graphView.Series.Add (series);
  130. _graphView.SetNeedsDisplay ();
  131. _graphView.MarginLeft = 3;
  132. _graphView.MarginBottom = 1;
  133. _graphView.AxisY.LabelGetter = v => '$' + (v.Value / 1000f).ToString ("N0") + 'k';
  134. // Do not show x axis labels (bars draw their own labels)
  135. _graphView.AxisX.Increment = 0;
  136. _graphView.AxisX.ShowLabelsEvery = 0;
  137. _graphView.AxisX.Minimum = 0;
  138. _graphView.AxisY.Minimum = 0;
  139. var legend = new LegendAnnotation (new Rect (_graphView.Bounds.Width - 20, 0, 20, 5));
  140. legend.AddEntry (new GraphCellToRender (stiple, series.SubSeries.ElementAt (0).OverrideBarColor), "Lower Third");
  141. legend.AddEntry (new GraphCellToRender (stiple, series.SubSeries.ElementAt (1).OverrideBarColor), "Middle Third");
  142. legend.AddEntry (new GraphCellToRender (stiple, series.SubSeries.ElementAt (2).OverrideBarColor), "Upper Third");
  143. _graphView.Annotations.Add (legend);
  144. }
  145. void SetupLineGraph ()
  146. {
  147. _graphView.Reset ();
  148. _graphView.Title = "Line";
  149. _about.Text = "This graph shows random points";
  150. var black = new Attribute (_graphView.ColorScheme.Normal.Foreground, Color.Black);
  151. var cyan = new Attribute (Color.BrightCyan, Color.Black);
  152. var magenta = new Attribute (Color.BrightMagenta, Color.Black);
  153. var red = new Attribute (Color.BrightRed, Color.Black);
  154. _graphView.GraphColor = black;
  155. var randomPoints = new List<PointF> ();
  156. var r = new Random ();
  157. for (var i = 0; i < 10; i++) {
  158. randomPoints.Add (new PointF (r.Next (100), r.Next (100)));
  159. }
  160. var points = new ScatterSeries {
  161. Points = randomPoints
  162. };
  163. var line = new PathAnnotation {
  164. LineColor = cyan,
  165. Points = randomPoints.OrderBy (p => p.X).ToList (),
  166. BeforeSeries = true
  167. };
  168. _graphView.Series.Add (points);
  169. _graphView.Annotations.Add (line);
  170. randomPoints = new List<PointF> ();
  171. for (var i = 0; i < 10; i++) {
  172. randomPoints.Add (new PointF (r.Next (100), r.Next (100)));
  173. }
  174. var points2 = new ScatterSeries {
  175. Points = randomPoints,
  176. Fill = new GraphCellToRender ((Rune)'x', red)
  177. };
  178. var line2 = new PathAnnotation {
  179. LineColor = magenta,
  180. Points = randomPoints.OrderBy (p => p.X).ToList (),
  181. BeforeSeries = true
  182. };
  183. _graphView.Series.Add (points2);
  184. _graphView.Annotations.Add (line2);
  185. // How much graph space each cell of the console depicts
  186. _graphView.CellSize = new PointF (2, 5);
  187. // leave space for axis labels
  188. _graphView.MarginBottom = 2;
  189. _graphView.MarginLeft = 3;
  190. // One axis tick/label per
  191. _graphView.AxisX.Increment = 20;
  192. _graphView.AxisX.ShowLabelsEvery = 1;
  193. _graphView.AxisX.Text = "X →";
  194. _graphView.AxisY.Increment = 20;
  195. _graphView.AxisY.ShowLabelsEvery = 1;
  196. _graphView.AxisY.Text = "↑Y";
  197. var max = line.Points.Union (line2.Points).OrderByDescending (p => p.Y).First ();
  198. _graphView.Annotations.Add (new TextAnnotation { Text = "(Max)", GraphPosition = new PointF (max.X + 2 * _graphView.CellSize.X, max.Y) });
  199. _graphView.SetNeedsDisplay ();
  200. }
  201. void SetupSineWave ()
  202. {
  203. _graphView.Reset ();
  204. _graphView.Title = "Sine Wave";
  205. _about.Text = "This graph shows a sine wave";
  206. var points = new ScatterSeries ();
  207. var line = new PathAnnotation ();
  208. // Draw line first so it does not draw over top of points or axis labels
  209. line.BeforeSeries = true;
  210. // Generate line graph with 2,000 points
  211. for (float x = -500; x < 500; x += 0.5f) {
  212. points.Points.Add (new PointF (x, (float)Math.Sin (x)));
  213. line.Points.Add (new PointF (x, (float)Math.Sin (x)));
  214. }
  215. _graphView.Series.Add (points);
  216. _graphView.Annotations.Add (line);
  217. // How much graph space each cell of the console depicts
  218. _graphView.CellSize = new PointF (0.1f, 0.1f);
  219. // leave space for axis labels
  220. _graphView.MarginBottom = 2;
  221. _graphView.MarginLeft = 3;
  222. // One axis tick/label per
  223. _graphView.AxisX.Increment = 0.5f;
  224. _graphView.AxisX.ShowLabelsEvery = 2;
  225. _graphView.AxisX.Text = "X →";
  226. _graphView.AxisX.LabelGetter = v => v.Value.ToString ("N2");
  227. _graphView.AxisY.Increment = 0.2f;
  228. _graphView.AxisY.ShowLabelsEvery = 2;
  229. _graphView.AxisY.Text = "↑Y";
  230. _graphView.AxisY.LabelGetter = v => v.Value.ToString ("N2");
  231. _graphView.ScrollOffset = new PointF (-2.5f, -1);
  232. _graphView.SetNeedsDisplay ();
  233. }
  234. /*
  235. Country,Both,Male,Female
  236. "Switzerland",83.4,81.8,85.1
  237. "South Korea",83.3,80.3,86.1
  238. "Singapore",83.2,81,85.5
  239. "Spain",83.2,80.7,85.7
  240. "Cyprus",83.1,81.1,85.1
  241. "Australia",83,81.3,84.8
  242. "Italy",83,80.9,84.9
  243. "Norway",83,81.2,84.7
  244. "Israel",82.6,80.8,84.4
  245. "France",82.5,79.8,85.1
  246. "Luxembourg",82.4,80.6,84.2
  247. "Sweden",82.4,80.8,84
  248. "Iceland",82.3,80.8,83.9
  249. "Canada",82.2,80.4,84.1
  250. "New Zealand",82,80.4,83.5
  251. "Malta,81.9",79.9,83.8
  252. "Ireland",81.8,80.2,83.5
  253. "Netherlands",81.8,80.4,83.1
  254. "Germany",81.7,78.7,84.8
  255. "Austria",81.6,79.4,83.8
  256. "Finland",81.6,79.2,84
  257. "Portugal",81.6,78.6,84.4
  258. "Belgium",81.4,79.3,83.5
  259. "United Kingdom",81.4,79.8,83
  260. "Denmark",81.3,79.6,83
  261. "Slovenia",81.3,78.6,84.1
  262. "Greece",81.1,78.6,83.6
  263. "Kuwait",81,79.3,83.9
  264. "Costa Rica",80.8,78.3,83.4*/
  265. void SetupLifeExpectancyBarGraph (bool verticalBars)
  266. {
  267. _graphView.Reset ();
  268. _graphView.Title = $"Life Expectancy - {(verticalBars ? "Vertical" : "Horizontal")}";
  269. _about.Text = "This graph shows the life expectancy at birth of a range of countries";
  270. var softStiple = new GraphCellToRender ((Rune)'\u2591');
  271. var mediumStiple = new GraphCellToRender ((Rune)'\u2592');
  272. var barSeries = new BarSeries {
  273. Bars = new List<BarSeriesBar> {
  274. new ("Switzerland", softStiple, 83.4f),
  275. new ("South Korea", !verticalBars ? mediumStiple : softStiple, 83.3f),
  276. new ("Singapore", softStiple, 83.2f),
  277. new ("Spain", !verticalBars ? mediumStiple : softStiple, 83.2f),
  278. new ("Cyprus", softStiple, 83.1f),
  279. new ("Australia", !verticalBars ? mediumStiple : softStiple, 83),
  280. new ("Italy", softStiple, 83),
  281. new ("Norway", !verticalBars ? mediumStiple : softStiple, 83),
  282. new ("Israel", softStiple, 82.6f),
  283. new ("France", !verticalBars ? mediumStiple : softStiple, 82.5f),
  284. new ("Luxembourg", softStiple, 82.4f),
  285. new ("Sweden", !verticalBars ? mediumStiple : softStiple, 82.4f),
  286. new ("Iceland", softStiple, 82.3f),
  287. new ("Canada", !verticalBars ? mediumStiple : softStiple, 82.2f),
  288. new ("New Zealand", softStiple, 82),
  289. new ("Malta", !verticalBars ? mediumStiple : softStiple, 81.9f),
  290. new ("Ireland", softStiple, 81.8f)
  291. }
  292. };
  293. _graphView.Series.Add (barSeries);
  294. if (verticalBars) {
  295. barSeries.Orientation = Orientation.Vertical;
  296. // How much graph space each cell of the console depicts
  297. _graphView.CellSize = new PointF (0.1f, 0.25f);
  298. // No axis marks since Bar will add it's own categorical marks
  299. _graphView.AxisX.Increment = 0f;
  300. _graphView.AxisX.Text = "Country";
  301. _graphView.AxisX.Minimum = 0;
  302. _graphView.AxisY.Increment = 1f;
  303. _graphView.AxisY.ShowLabelsEvery = 1;
  304. _graphView.AxisY.LabelGetter = v => v.Value.ToString ("N2");
  305. _graphView.AxisY.Minimum = 0;
  306. _graphView.AxisY.Text = "Age";
  307. // leave space for axis labels and title
  308. _graphView.MarginBottom = 2;
  309. _graphView.MarginLeft = 6;
  310. // Start the graph at 80 years because that is where most of our data is
  311. _graphView.ScrollOffset = new PointF (0, 80);
  312. } else {
  313. barSeries.Orientation = Orientation.Horizontal;
  314. // How much graph space each cell of the console depicts
  315. _graphView.CellSize = new PointF (0.1f, 1f);
  316. // No axis marks since Bar will add it's own categorical marks
  317. _graphView.AxisY.Increment = 0f;
  318. _graphView.AxisY.ShowLabelsEvery = 1;
  319. _graphView.AxisY.Text = "Country";
  320. _graphView.AxisY.Minimum = 0;
  321. _graphView.AxisX.Increment = 1f;
  322. _graphView.AxisX.ShowLabelsEvery = 1;
  323. _graphView.AxisX.LabelGetter = v => v.Value.ToString ("N2");
  324. _graphView.AxisX.Text = "Age";
  325. _graphView.AxisX.Minimum = 0;
  326. // leave space for axis labels and title
  327. _graphView.MarginBottom = 2;
  328. _graphView.MarginLeft = (uint)barSeries.Bars.Max (b => b.Text.Length) + 2;
  329. // Start the graph at 80 years because that is where most of our data is
  330. _graphView.ScrollOffset = new PointF (80, 0);
  331. }
  332. _graphView.SetNeedsDisplay ();
  333. }
  334. void SetupPopulationPyramid ()
  335. {
  336. /*
  337. Age,M,F
  338. 0-4,2009363,1915127
  339. 5-9,2108550,2011016
  340. 10-14,2022370,1933970
  341. 15-19,1880611,1805522
  342. 20-24,2072674,2001966
  343. 25-29,2275138,2208929
  344. 30-34,2361054,2345774
  345. 35-39,2279836,2308360
  346. 40-44,2148253,2159877
  347. 45-49,2128343,2167778
  348. 50-54,2281421,2353119
  349. 55-59,2232388,2306537
  350. 60-64,1919839,1985177
  351. 65-69,1647391,1734370
  352. 70-74,1624635,1763853
  353. 75-79,1137438,1304709
  354. 80-84,766956,969611
  355. 85-89,438663,638892
  356. 90-94,169952,320625
  357. 95-99,34524,95559
  358. 100+,3016,12818*/
  359. _about.Text = "This graph shows population of each age divided by gender";
  360. _graphView.Title = "Population Pyramid";
  361. _graphView.Reset ();
  362. // How much graph space each cell of the console depicts
  363. _graphView.CellSize = new PointF (100_000, 1);
  364. //center the x axis in middle of screen to show both sides
  365. _graphView.ScrollOffset = new PointF (-3_000_000, 0);
  366. _graphView.AxisX.Text = "Number Of People";
  367. _graphView.AxisX.Increment = 500_000;
  368. _graphView.AxisX.ShowLabelsEvery = 2;
  369. // use Abs to make negative axis labels positive
  370. _graphView.AxisX.LabelGetter = v => Math.Abs (v.Value / 1_000_000).ToString ("N2") + "M";
  371. // leave space for axis labels
  372. _graphView.MarginBottom = 2;
  373. _graphView.MarginLeft = 1;
  374. // do not show axis titles (bars have their own categories)
  375. _graphView.AxisY.Increment = 0;
  376. _graphView.AxisY.ShowLabelsEvery = 0;
  377. _graphView.AxisY.Minimum = 0;
  378. var stiple = new GraphCellToRender (CM.Glyphs.Stipple);
  379. // Bars in 2 directions
  380. // Males (negative to make the bars go left)
  381. var malesSeries = new BarSeries {
  382. Orientation = Orientation.Horizontal,
  383. Bars = new List<BarSeriesBar> {
  384. new ("0-4", stiple, -2009363),
  385. new ("5-9", stiple, -2108550),
  386. new ("10-14", stiple, -2022370),
  387. new ("15-19", stiple, -1880611),
  388. new ("20-24", stiple, -2072674),
  389. new ("25-29", stiple, -2275138),
  390. new ("30-34", stiple, -2361054),
  391. new ("35-39", stiple, -2279836),
  392. new ("40-44", stiple, -2148253),
  393. new ("45-49", stiple, -2128343),
  394. new ("50-54", stiple, -2281421),
  395. new ("55-59", stiple, -2232388),
  396. new ("60-64", stiple, -1919839),
  397. new ("65-69", stiple, -1647391),
  398. new ("70-74", stiple, -1624635),
  399. new ("75-79", stiple, -1137438),
  400. new ("80-84", stiple, -766956),
  401. new ("85-89", stiple, -438663),
  402. new ("90-94", stiple, -169952),
  403. new ("95-99", stiple, -34524),
  404. new ("100+", stiple, -3016)
  405. }
  406. };
  407. _graphView.Series.Add (malesSeries);
  408. // Females
  409. var femalesSeries = new BarSeries {
  410. Orientation = Orientation.Horizontal,
  411. Bars = new List<BarSeriesBar> {
  412. new ("0-4", stiple, 1915127),
  413. new ("5-9", stiple, 2011016),
  414. new ("10-14", stiple, 1933970),
  415. new ("15-19", stiple, 1805522),
  416. new ("20-24", stiple, 2001966),
  417. new ("25-29", stiple, 2208929),
  418. new ("30-34", stiple, 2345774),
  419. new ("35-39", stiple, 2308360),
  420. new ("40-44", stiple, 2159877),
  421. new ("45-49", stiple, 2167778),
  422. new ("50-54", stiple, 2353119),
  423. new ("55-59", stiple, 2306537),
  424. new ("60-64", stiple, 1985177),
  425. new ("65-69", stiple, 1734370),
  426. new ("70-74", stiple, 1763853),
  427. new ("75-79", stiple, 1304709),
  428. new ("80-84", stiple, 969611),
  429. new ("85-89", stiple, 638892),
  430. new ("90-94", stiple, 320625),
  431. new ("95-99", stiple, 95559),
  432. new ("100+", stiple, 12818)
  433. }
  434. };
  435. var softStiple = new GraphCellToRender ((Rune)'\u2591');
  436. var mediumStiple = new GraphCellToRender ((Rune)'\u2592');
  437. for (var i = 0; i < malesSeries.Bars.Count; i++) {
  438. malesSeries.Bars [i].Fill = i % 2 == 0 ? softStiple : mediumStiple;
  439. femalesSeries.Bars [i].Fill = i % 2 == 0 ? softStiple : mediumStiple;
  440. }
  441. _graphView.Series.Add (femalesSeries);
  442. _graphView.Annotations.Add (new TextAnnotation { Text = "M", ScreenPosition = new Point (0, 10) });
  443. _graphView.Annotations.Add (new TextAnnotation { Text = "F", ScreenPosition = new Point (_graphView.Bounds.Width - 1, 10) });
  444. _graphView.SetNeedsDisplay ();
  445. }
  446. void SetupDisco ()
  447. {
  448. _graphView.Reset ();
  449. _graphView.Title = "Graphic Equalizer";
  450. _about.Text = "This graph shows a graphic equalizer for an imaginary song";
  451. _graphView.GraphColor = new Attribute (Color.White, Color.Black);
  452. var stiple = new GraphCellToRender ((Rune)'\u2593');
  453. var r = new Random ();
  454. var series = new DiscoBarSeries ();
  455. var bars = new List<BarSeriesBar> ();
  456. var genSample = () => {
  457. bars.Clear ();
  458. // generate an imaginary sample
  459. for (var i = 0; i < 31; i++) {
  460. bars.Add (
  461. new BarSeriesBar (null, stiple, r.Next (0, 100)) {
  462. //ColorGetter = colorDelegate
  463. });
  464. }
  465. _graphView.SetNeedsDisplay ();
  466. // while the equaliser is showing
  467. return _graphView.Series.Contains (series);
  468. };
  469. Application.AddTimeout (TimeSpan.FromMilliseconds (250), genSample);
  470. series.Bars = bars;
  471. _graphView.Series.Add (series);
  472. // How much graph space each cell of the console depicts
  473. _graphView.CellSize = new PointF (1, 10);
  474. _graphView.AxisX.Increment = 0; // No graph ticks
  475. _graphView.AxisX.ShowLabelsEvery = 0; // no labels
  476. _graphView.AxisX.Visible = false;
  477. _graphView.AxisY.Visible = false;
  478. _graphView.SetNeedsDisplay ();
  479. }
  480. void SetupPeriodicTableScatterPlot ()
  481. {
  482. _graphView.Reset ();
  483. _graphView.Title = "Scatter Plot";
  484. _about.Text = "This graph shows the atomic weight of each element in the periodic table.\nStarting with Hydrogen (atomic Number 1 with a weight of 1.007)";
  485. //AtomicNumber and AtomicMass of all elements in the periodic table
  486. _graphView.Series.Add (
  487. new ScatterSeries {
  488. Points = new List<PointF> {
  489. new (1, 1.007f), new (2, 4.002f), new (3, 6.941f), new (4, 9.012f), new (5, 10.811f), new (6, 12.011f),
  490. new (7, 14.007f), new (8, 15.999f), new (9, 18.998f), new (10, 20.18f), new (11, 22.99f), new (12, 24.305f),
  491. new (13, 26.982f), new (14, 28.086f), new (15, 30.974f), new (16, 32.065f), new (17, 35.453f), new (18, 39.948f),
  492. new (19, 39.098f), new (20, 40.078f), new (21, 44.956f), new (22, 47.867f), new (23, 50.942f), new (24, 51.996f),
  493. new (25, 54.938f), new (26, 55.845f), new (27, 58.933f), new (28, 58.693f), new (29, 63.546f), new (30, 65.38f),
  494. new (31, 69.723f), new (32, 72.64f), new (33, 74.922f), new (34, 78.96f), new (35, 79.904f), new (36, 83.798f),
  495. new (37, 85.468f), new (38, 87.62f), new (39, 88.906f), new (40, 91.224f), new (41, 92.906f), new (42, 95.96f),
  496. new (43, 98f), new (44, 101.07f), new (45, 102.906f), new (46, 106.42f), new (47, 107.868f), new (48, 112.411f),
  497. new (49, 114.818f), new (50, 118.71f), new (51, 121.76f), new (52, 127.6f), new (53, 126.904f), new (54, 131.293f),
  498. new (55, 132.905f), new (56, 137.327f), new (57, 138.905f), new (58, 140.116f), new (59, 140.908f), new (60, 144.242f),
  499. new (61, 145), new (62, 150.36f), new (63, 151.964f), new (64, 157.25f), new (65, 158.925f), new (66, 162.5f),
  500. new (67, 164.93f), new (68, 167.259f), new (69, 168.934f), new (70, 173.054f), new (71, 174.967f), new (72, 178.49f),
  501. new (73, 180.948f), new (74, 183.84f), new (75, 186.207f), new (76, 190.23f), new (77, 192.217f), new (78, 195.084f),
  502. new (79, 196.967f), new (80, 200.59f), new (81, 204.383f), new (82, 207.2f), new (83, 208.98f), new (84, 210),
  503. new (85, 210), new (86, 222), new (87, 223), new (88, 226), new (89, 227), new (90, 232.038f), new (91, 231.036f),
  504. new (92, 238.029f), new (93, 237), new (94, 244), new (95, 243), new (96, 247), new (97, 247), new (98, 251),
  505. new (99, 252), new (100, 257), new (101, 258), new (102, 259), new (103, 262), new (104, 261), new (105, 262),
  506. new (106, 266), new (107, 264), new (108, 267), new (109, 268), new (113, 284), new (114, 289), new (115, 288),
  507. new (116, 292), new (117, 295), new (118, 294)
  508. }
  509. });
  510. // How much graph space each cell of the console depicts
  511. _graphView.CellSize = new PointF (1, 5);
  512. // leave space for axis labels
  513. _graphView.MarginBottom = 2;
  514. _graphView.MarginLeft = 3;
  515. // One axis tick/label per 5 atomic numbers
  516. _graphView.AxisX.Increment = 5;
  517. _graphView.AxisX.ShowLabelsEvery = 1;
  518. _graphView.AxisX.Text = "Atomic Number";
  519. _graphView.AxisX.Minimum = 0;
  520. // One label every 5 atomic weight
  521. _graphView.AxisY.Increment = 5;
  522. _graphView.AxisY.ShowLabelsEvery = 1;
  523. _graphView.AxisY.Minimum = 0;
  524. _graphView.SetNeedsDisplay ();
  525. }
  526. void Zoom (float factor)
  527. {
  528. _graphView.CellSize = new PointF (
  529. _graphView.CellSize.X * factor,
  530. _graphView.CellSize.Y * factor
  531. );
  532. _graphView.AxisX.Increment *= factor;
  533. _graphView.AxisY.Increment *= factor;
  534. _graphView.SetNeedsDisplay ();
  535. }
  536. void Margin (bool left, bool increase)
  537. {
  538. if (left) {
  539. _graphView.MarginLeft = (uint)Math.Max (0, _graphView.MarginLeft + (increase ? 1 : -1));
  540. } else {
  541. _graphView.MarginBottom = (uint)Math.Max (0, _graphView.MarginBottom + (increase ? 1 : -1));
  542. }
  543. _graphView.SetNeedsDisplay ();
  544. }
  545. void Quit () => Application.RequestStop ();
  546. class DiscoBarSeries : BarSeries {
  547. readonly Attribute brightgreen;
  548. readonly Attribute brightred;
  549. readonly Attribute brightyellow;
  550. readonly Attribute green;
  551. readonly Attribute red;
  552. public DiscoBarSeries ()
  553. {
  554. green = new Attribute (Color.BrightGreen, Color.Black);
  555. brightgreen = new Attribute (Color.Green, Color.Black);
  556. brightyellow = new Attribute (Color.BrightYellow, Color.Black);
  557. red = new Attribute (Color.Red, Color.Black);
  558. brightred = new Attribute (Color.BrightRed, Color.Black);
  559. }
  560. protected override void DrawBarLine (GraphView graph, Point start, Point end, BarSeriesBar beingDrawn)
  561. {
  562. var driver = Application.Driver;
  563. var x = start.X;
  564. for (var y = end.Y; y <= start.Y; y++) {
  565. var height = graph.ScreenToGraphSpace (x, y).Y;
  566. if (height >= 85) {
  567. driver.SetAttribute (red);
  568. } else if (height >= 66) {
  569. driver.SetAttribute (brightred);
  570. } else if (height >= 45) {
  571. driver.SetAttribute (brightyellow);
  572. } else if (height >= 25) {
  573. driver.SetAttribute (brightgreen);
  574. } else {
  575. driver.SetAttribute (green);
  576. }
  577. graph.AddRune (x, y, beingDrawn.Fill.Rune);
  578. }
  579. }
  580. }
  581. }