private double FindStdDev(IList <object> data, double mean) { double sum = 0; for (var i = 0; i < data.Count; i++) { ZonesData item = (ZonesData)data[i]; var d = item.Score - mean; sum += d * d; } return(System.Math.Sqrt(sum / data.Count)); }
// a method to create a list of zones sample objects of type ChartPoint public static IList <object> getZonesList(int nStudents, int nMaxPoints) { List <object> list = new List <object>(); Random random = new Random(); for (int i = 0; i < nStudents; i++) { ZonesData point = new ZonesData(i, nMaxPoints * 0.5 * (1 + random.NextDouble())); list.Add(point); } return(list); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_zones); var toolbar = FindViewById <Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); SupportActionBar.Title = GetString(Resource.String.zones); SupportActionBar.SetDisplayHomeAsUpEnabled(true); SupportActionBar.SetHomeButtonEnabled(true); // initializing widget mChart = this.FindViewById <FlexChart>(Resource.Id.zonesFlexchart); mChart.BindingX = "Number"; mChart.ChartType = ChartType.Scatter; int nStudents = 20; int nMaxPoints = 100; IList <object> data = ZonesData.getZonesList(nStudents, nMaxPoints); mChart.ItemsSource = data; mChart.Tapped += MChart_Tapped; mChart.AxisX.Title = "student number"; mChart.AxisY.Title = "student accumulated points"; double mean = this.FindMean(data); double stdDev = this.FindStdDev(data, mean); List <double> scores = new List <double>(); foreach (ZonesData item in data) { scores.Add(item.Score); } scores.Sort((x, y) => y.CompareTo(x)); var zones = new double[] { scores[this.GetBoundingIndex(scores, 0.85)], scores[this.GetBoundingIndex(scores, 0.75)], scores[this.GetBoundingIndex(scores, 0.25)], scores[this.GetBoundingIndex(scores, 0.05)] }; Integer[] colors = new Integer[] { new Integer(Color.Argb(255, 255, 192, 192)), new Integer(Color.Argb(255, 55, 228, 228)), new Integer(Color.Argb(255, 255, 228, 128)), new Integer(Color.Argb(255, 128, 255, 128)), new Integer(Color.Argb(255, 128, 128, 255)), }; for (var i = 4; i >= 0; i--) { float y = (float)(i == 4 ? mean + 2 * stdDev : zones[i]); PointF[] sdata = new PointF[data.Count]; for (int j = 0; j < data.Count; j++) { sdata[j] = new PointF(j, y); if (i == 0) { System.Console.WriteLine(j + "=" + y); } } string seriesName = ((char)((short)'A' + 4 - i)).ToString(); var series = new ChartSeries(); series.Chart = mChart; series.SeriesName = seriesName; series.Binding = "Y"; series.ItemsSource = sdata; series.BindingX = "X"; series.ChartType = ChartType.Area; series.Style = new ChartStyle(); series.Style.Fill = new Color(colors[i].IntValue()); mChart.Series.Add(series); } ChartSeries scoreSeries = new ChartSeries(); scoreSeries.Chart = mChart; scoreSeries.SeriesName = "raw score"; scoreSeries.Binding = "Score"; scoreSeries.BindingX = "Number"; mChart.Series.Add(scoreSeries); for (var i = -2; i <= 2; i++) { var y = mean + i * stdDev; string seriesName = string.Empty; if (i > 0) { seriesName = "m+" + i + "s"; } else if (i < 0) { seriesName = "m" + i + "s"; } else { seriesName = "mean"; } PointF[] sdata = new PointF[data.Count]; for (int j = 0; j < data.Count; j++) { sdata[j] = new PointF(j, (float)y); } var series = new ChartSeries(); series.Chart = mChart; series.SeriesName = seriesName; series.Binding = "Y"; series.ItemsSource = sdata; series.BindingX = "X"; series.ChartType = ChartType.Line; series.Style = new ChartStyle(); series.Style.StrokeThickness = 2; series.Style.Stroke = Color.Rgb(0x20, 0x20, 0x20); mChart.Series.Add(series); } }
public override void ViewDidLoad() { base.ViewDidLoad(); // Perform any additional setup after loading the view, typically from a nib. chart = new FlexChart(); chart.BindingX = "Number"; chart.ChartType = ChartType.Scatter; chart.AxisX.Title = "student number"; chart.AxisY.Title = "student accumulated points"; int nStudents = 20; int nMaxPoints = 100; IList <object> data = ZonesData.getZonesList(nStudents, nMaxPoints); chart.ItemsSource = data; this.Add(chart); double mean = this.FindMean(data); double stdDev = this.FindStdDev(data, mean); List <double> scores = new List <double>(); foreach (ZonesData item in data) { scores.Add(item.Score); } scores.Sort((x, y) => y.CompareTo(x)); var zones = new double[] { scores[this.GetBoundingIndex(scores, 0.95)], scores[this.GetBoundingIndex(scores, 0.75)], scores[this.GetBoundingIndex(scores, 0.25)], scores[this.GetBoundingIndex(scores, 0.05)] }; NSArray colors = NSArray.FromObjects(UIColor.FromRGBA(0.99f, 0.75f, 0.75f, 1.00f), UIColor.FromRGBA(0.22f, 0.89f, 0.89f, 1.00f), UIColor.FromRGBA(1.00f, 0.89f, 0.50f, 1.00f), UIColor.FromRGBA(0.50f, 1.00f, 0.50f, 1.00f), UIColor.FromRGBA(0.50f, 0.50f, 1.00f, 1.00f)); for (var i = 4; i >= 0; i--) { float y = (float)(i == 4 ? mean + 2 * stdDev : zones[i]); IList <object> sdata = new List <object>(); for (int j = 0; j < data.Count; j++) { sdata.Add(new ZonesData((double)j, (double)y)); } string seriesName = ((char)((short)'A' + 4 - i)).ToString(); ChartSeries series = new ChartSeries { SeriesName = seriesName, Binding = "Y" }; series.ItemsSource = sdata; series.BindingX = "X"; series.ChartType = ChartType.Area; series.Style = new ChartStyle { Fill = colors.GetItem <UIColor>((nuint)i) }; chart.Series.Add(series); } chart.Series.Add(new ChartSeries { SeriesName = "raw score", Binding = "Score", BindingX = "Number" }); for (var i = -2; i <= 2; i++) { var y = mean + i * stdDev; string seriesName = string.Empty; if (i > 0) { seriesName = "m+" + i + "s"; } else if (i < 0) { seriesName = "m" + i + "s"; } else { seriesName = "mean"; } IList <object> sdata = new List <object>(); for (int j = 0; j < data.Count; j++) { sdata.Add(new ZonesData((double)j, (double)y)); } ChartSeries series = new ChartSeries { SeriesName = seriesName, Binding = "Y" }; series.ItemsSource = sdata; series.BindingX = "X"; series.ChartType = ChartType.Line; series.Style = new ChartStyle { Fill = UIColor.Black, StrokeThickness = 2 }; chart.Series.Add(series); } }