GraphViewExample.cs 37 KB

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