GraphViewExample.cs 23 KB

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