GraphViewExample.cs 23 KB

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