GraphViewExample.cs 39 KB

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