/// <summary> /// Applies any color overriding /// </summary> /// <param name="graphCellToRender"></param> /// <returns></returns> protected virtual GraphCellToRender AdjustColor(GraphCellToRender graphCellToRender) { if (OverrideBarColor.HasValue) { graphCellToRender.Color = OverrideBarColor; } return(graphCellToRender); }
private void SetupDisco() { graphView.Reset(); about.Text = "This graph shows a graphic equaliser for an imaginary song"; graphView.GraphColor = Application.Driver.MakeAttribute(Color.White, Color.Black); var stiple = new GraphCellToRender('\u2593'); Random r = new Random(); var series = new DiscoBarSeries(); var bars = new List <BarSeries.Bar> (); Func <MainLoop, bool> genSample = (l) => { bars.Clear(); // generate an imaginary sample for (int i = 0; i < 31; i++) { bars.Add( new BarSeries.Bar(null, stiple, r.Next(0, 100)) { //ColorGetter = colorDelegate }); } graphView.SetNeedsDisplay(); // while the equaliser is showing return(graphView.Series.Contains(series)); }; Application.MainLoop.AddTimeout(TimeSpan.FromMilliseconds(250), genSample); series.Bars = bars; graphView.Series.Add(series); // How much graph space each cell of the console depicts graphView.CellSize = new PointF(1, 10); graphView.AxisX.Increment = 0; // No graph ticks graphView.AxisX.ShowLabelsEvery = 0; // no labels graphView.AxisX.Visible = false; graphView.AxisY.Visible = false; graphView.SetNeedsDisplay(); }
private void SetupPopulationPyramid() { /* * Age,M,F * 0-4,2009363,1915127 * 5-9,2108550,2011016 * 10-14,2022370,1933970 * 15-19,1880611,1805522 * 20-24,2072674,2001966 * 25-29,2275138,2208929 * 30-34,2361054,2345774 * 35-39,2279836,2308360 * 40-44,2148253,2159877 * 45-49,2128343,2167778 * 50-54,2281421,2353119 * 55-59,2232388,2306537 * 60-64,1919839,1985177 * 65-69,1647391,1734370 * 70-74,1624635,1763853 * 75-79,1137438,1304709 * 80-84,766956,969611 * 85-89,438663,638892 * 90-94,169952,320625 * 95-99,34524,95559 * 100+,3016,12818*/ about.Text = "This graph shows population of each age divided by gender"; graphView.Reset(); // How much graph space each cell of the console depicts graphView.CellSize = new PointF(100_000, 1); //center the x axis in middle of screen to show both sides graphView.ScrollOffset = new PointF(-3_000_000, 0); graphView.AxisX.Text = "Number Of People"; graphView.AxisX.Increment = 500_000; graphView.AxisX.ShowLabelsEvery = 2; // use Abs to make negative axis labels positive graphView.AxisX.LabelGetter = (v) => Math.Abs(v.Value / 1_000_000).ToString("N2") + "M"; // leave space for axis labels graphView.MarginBottom = 2; graphView.MarginLeft = 1; // do not show axis titles (bars have their own categories) graphView.AxisY.Increment = 0; graphView.AxisY.ShowLabelsEvery = 0; graphView.AxisY.Minimum = 0; var stiple = new GraphCellToRender(Application.Driver.Stipple); // Bars in 2 directions // Males (negative to make the bars go left) var malesSeries = new BarSeries() { Orientation = Orientation.Horizontal, Bars = new List <BarSeries.Bar> () { new BarSeries.Bar("0-4", stiple, -2009363), new BarSeries.Bar("5-9", stiple, -2108550), new BarSeries.Bar("10-14", stiple, -2022370), new BarSeries.Bar("15-19", stiple, -1880611), new BarSeries.Bar("20-24", stiple, -2072674), new BarSeries.Bar("25-29", stiple, -2275138), new BarSeries.Bar("30-34", stiple, -2361054), new BarSeries.Bar("35-39", stiple, -2279836), new BarSeries.Bar("40-44", stiple, -2148253), new BarSeries.Bar("45-49", stiple, -2128343), new BarSeries.Bar("50-54", stiple, -2281421), new BarSeries.Bar("55-59", stiple, -2232388), new BarSeries.Bar("60-64", stiple, -1919839), new BarSeries.Bar("65-69", stiple, -1647391), new BarSeries.Bar("70-74", stiple, -1624635), new BarSeries.Bar("75-79", stiple, -1137438), new BarSeries.Bar("80-84", stiple, -766956), new BarSeries.Bar("85-89", stiple, -438663), new BarSeries.Bar("90-94", stiple, -169952), new BarSeries.Bar("95-99", stiple, -34524), new BarSeries.Bar("100+", stiple, -3016) } }; graphView.Series.Add(malesSeries); // Females var femalesSeries = new BarSeries() { Orientation = Orientation.Horizontal, Bars = new List <BarSeries.Bar> () { new BarSeries.Bar("0-4", stiple, 1915127), new BarSeries.Bar("5-9", stiple, 2011016), new BarSeries.Bar("10-14", stiple, 1933970), new BarSeries.Bar("15-19", stiple, 1805522), new BarSeries.Bar("20-24", stiple, 2001966), new BarSeries.Bar("25-29", stiple, 2208929), new BarSeries.Bar("30-34", stiple, 2345774), new BarSeries.Bar("35-39", stiple, 2308360), new BarSeries.Bar("40-44", stiple, 2159877), new BarSeries.Bar("45-49", stiple, 2167778), new BarSeries.Bar("50-54", stiple, 2353119), new BarSeries.Bar("55-59", stiple, 2306537), new BarSeries.Bar("60-64", stiple, 1985177), new BarSeries.Bar("65-69", stiple, 1734370), new BarSeries.Bar("70-74", stiple, 1763853), new BarSeries.Bar("75-79", stiple, 1304709), new BarSeries.Bar("80-84", stiple, 969611), new BarSeries.Bar("85-89", stiple, 638892), new BarSeries.Bar("90-94", stiple, 320625), new BarSeries.Bar("95-99", stiple, 95559), new BarSeries.Bar("100+", stiple, 12818) } }; var softStiple = new GraphCellToRender('\u2591'); var mediumStiple = new GraphCellToRender('\u2592'); for (int i = 0; i < malesSeries.Bars.Count; i++) { malesSeries.Bars [i].Fill = i % 2 == 0 ? softStiple : mediumStiple; femalesSeries.Bars [i].Fill = i % 2 == 0 ? softStiple : mediumStiple; } graphView.Series.Add(femalesSeries); graphView.Annotations.Add(new TextAnnotation() { Text = "M", ScreenPosition = new Terminal.Gui.Point(0, 10) }); graphView.Annotations.Add(new TextAnnotation() { Text = "F", ScreenPosition = new Terminal.Gui.Point(graphView.Bounds.Width - 1, 10) }); graphView.SetNeedsDisplay(); }
/* * Country,Both,Male,Female * * "Switzerland",83.4,81.8,85.1 * "South Korea",83.3,80.3,86.1 * "Singapore",83.2,81,85.5 * "Spain",83.2,80.7,85.7 * "Cyprus",83.1,81.1,85.1 * "Australia",83,81.3,84.8 * "Italy",83,80.9,84.9 * "Norway",83,81.2,84.7 * "Israel",82.6,80.8,84.4 * "France",82.5,79.8,85.1 * "Luxembourg",82.4,80.6,84.2 * "Sweden",82.4,80.8,84 * "Iceland",82.3,80.8,83.9 * "Canada",82.2,80.4,84.1 * "New Zealand",82,80.4,83.5 * "Malta,81.9",79.9,83.8 * "Ireland",81.8,80.2,83.5 * "Netherlands",81.8,80.4,83.1 * "Germany",81.7,78.7,84.8 * "Austria",81.6,79.4,83.8 * "Finland",81.6,79.2,84 * "Portugal",81.6,78.6,84.4 * "Belgium",81.4,79.3,83.5 * "United Kingdom",81.4,79.8,83 * "Denmark",81.3,79.6,83 * "Slovenia",81.3,78.6,84.1 * "Greece",81.1,78.6,83.6 * "Kuwait",81,79.3,83.9 * "Costa Rica",80.8,78.3,83.4*/ private void SetupLifeExpectancyBarGraph(bool verticalBars) { graphView.Reset(); about.Text = "This graph shows the life expectancy at birth of a range of countries"; var softStiple = new GraphCellToRender('\u2591'); var mediumStiple = new GraphCellToRender('\u2592'); var barSeries = new BarSeries() { Bars = new List <BarSeries.Bar> () { new BarSeries.Bar("Switzerland", softStiple, 83.4f), new BarSeries.Bar("South Korea", !verticalBars?mediumStiple:softStiple, 83.3f), new BarSeries.Bar("Singapore", softStiple, 83.2f), new BarSeries.Bar("Spain", !verticalBars?mediumStiple:softStiple, 83.2f), new BarSeries.Bar("Cyprus", softStiple, 83.1f), new BarSeries.Bar("Australia", !verticalBars?mediumStiple:softStiple, 83), new BarSeries.Bar("Italy", softStiple, 83), new BarSeries.Bar("Norway", !verticalBars?mediumStiple:softStiple, 83), new BarSeries.Bar("Israel", softStiple, 82.6f), new BarSeries.Bar("France", !verticalBars?mediumStiple:softStiple, 82.5f), new BarSeries.Bar("Luxembourg", softStiple, 82.4f), new BarSeries.Bar("Sweden", !verticalBars?mediumStiple:softStiple, 82.4f), new BarSeries.Bar("Iceland", softStiple, 82.3f), new BarSeries.Bar("Canada", !verticalBars?mediumStiple:softStiple, 82.2f), new BarSeries.Bar("New Zealand", softStiple, 82), new BarSeries.Bar("Malta", !verticalBars?mediumStiple:softStiple, 81.9f), new BarSeries.Bar("Ireland", softStiple, 81.8f) } }; graphView.Series.Add(barSeries); if (verticalBars) { barSeries.Orientation = Orientation.Vertical; // How much graph space each cell of the console depicts graphView.CellSize = new PointF(0.1f, 0.25f); // No axis marks since Bar will add it's own categorical marks graphView.AxisX.Increment = 0f; graphView.AxisX.Text = "Country"; graphView.AxisX.Minimum = 0; graphView.AxisY.Increment = 1f; graphView.AxisY.ShowLabelsEvery = 1; graphView.AxisY.LabelGetter = v => v.Value.ToString("N2"); graphView.AxisY.Minimum = 0; graphView.AxisY.Text = "Age"; // leave space for axis labels and title graphView.MarginBottom = 2; graphView.MarginLeft = 6; // Start the graph at 80 years because that is where most of our data is graphView.ScrollOffset = new PointF(0, 80); } else { barSeries.Orientation = Orientation.Horizontal; // How much graph space each cell of the console depicts graphView.CellSize = new PointF(0.1f, 1f); // No axis marks since Bar will add it's own categorical marks graphView.AxisY.Increment = 0f; graphView.AxisY.ShowLabelsEvery = 1; graphView.AxisY.Text = "Country"; graphView.AxisY.Minimum = 0; graphView.AxisX.Increment = 1f; graphView.AxisX.ShowLabelsEvery = 1; graphView.AxisX.LabelGetter = v => v.Value.ToString("N2"); graphView.AxisX.Text = "Age"; graphView.AxisX.Minimum = 0; // leave space for axis labels and title graphView.MarginBottom = 2; graphView.MarginLeft = (uint)barSeries.Bars.Max(b => b.Text.Length) + 2; // Start the graph at 80 years because that is where most of our data is graphView.ScrollOffset = new PointF(80, 0); } graphView.SetNeedsDisplay(); }
/// <summary> /// Creates a new instance of a single bar rendered in the given <paramref name="fill"/> that extends /// out <paramref name="value"/> graph space units in the default <see cref="Orientation"/> /// </summary> /// <param name="text"></param> /// <param name="fill"></param> /// <param name="value"></param> public Bar(string text, GraphCellToRender fill, float value) { Text = text; Fill = fill; Value = value; }
private void SetupBarSeries(DataTable dt, string countColumnName, int boundsWidth, int boundsHeight) { var barSeries = new BarSeries(); var softStiple = new GraphCellToRender('\u2591'); var mediumStiple = new GraphCellToRender('\u2592'); int row = 0; int widestCategory = 0; float min = 0; float max = 1; foreach (DataRow dr in dt.Rows) { var label = dr[0].ToString(); if (string.IsNullOrWhiteSpace(label)) { label = "<Null>"; } // treat nulls as 0 var val = dr[1] == DBNull.Value ? 0 : (float)Convert.ToDouble(dr[1]); min = Math.Min(min, val); max = Math.Max(max, val); widestCategory = Math.Max(widestCategory, label.Length); barSeries.Bars.Add(new BarSeries.Bar(label, row++ % 2 == 0 ? softStiple : mediumStiple, val)); } // show bars alphabetically (graph is rendered y=0 at bottom) barSeries.Bars = barSeries.Bars.OrderByDescending(b => b.Text).ToList(); // Configure Axis, Margins etc // make sure whole graph fits on axis float xIncrement = (max - min) / (boundsWidth); // 1 bar per row of console graphView.CellSize = new PointF(xIncrement, 1); graphView.Series.Add(barSeries); graphView.AxisY.Increment = 0; barSeries.Orientation = Orientation.Horizontal; graphView.MarginBottom = 2; graphView.MarginLeft = (uint)widestCategory + 1; // work out how to space x axis without scrolling graphView.AxisX.Increment = 10 * xIncrement; graphView.AxisX.ShowLabelsEvery = 1; graphView.AxisX.LabelGetter = (v) => FormatValue(v.Value, min, max); graphView.AxisX.Text = countColumnName; graphView.AxisY.Increment = 1; graphView.AxisY.ShowLabelsEvery = 1; // scroll to the top of the bar chart so that the natural scroll direction (down) is preserved graphView.ScrollOffset = new PointF(0, barSeries.Bars.Count - boundsHeight + 4); }
protected override GraphCellToRender AdjustColor(GraphCellToRender graphCellToRender) { return(FinalColor = base.AdjustColor(graphCellToRender)); }