GraphViewExample.cs 23 KB

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