GraphViewExample.cs 23 KB

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