GraphViewExample.cs 23 KB

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