GraphViewExample.cs 38 KB

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