public List <DataSubset> CreateSubsets(DataTable dt, List <string> varnames, List <string> groupingvarnames) { bool boo1 = (groupingvarnames[0] == "none") ? false : true; bool boo2 = (groupingvarnames.Count >= 1) ? true : false; bool hasgroupingvars = (groupingvarnames[0] != "none" && groupingvarnames.Count >= 1) ? true : false; _groupingvars = new List <Groupingvar>(); List <DataSubset> mysubsets = new List <DataSubset>(); if (hasgroupingvars) { try { //Create the list of GroupingVars for (int g = 0; g < groupingvarnames.Count; g++) { string vtype = dt.Columns[groupingvarnames[g]].DataType.ToString().ToLower(); if (!vtype.Contains("string")) { dt.ConvertColumnType(groupingvarnames[g], typeof(string)); } List <string> unsorted_levels = dt.AsEnumerable().Select(f => f.Field <string>(groupingvarnames[g].ToString())).Distinct().ToList(); List <string> levels = unsorted_levels.OrderBy(x => x).ToList(); Groupingvar gv = new Groupingvar(groupingvarnames[g], levels); _groupingvars.Add(gv); } // from: http://www.scriptscoop.net/t/7516b362c821/c-c-linq-how-to-build-group-by-clause-dynamically.html IEnumerable <string> columnsToGroupBy = groupingvarnames; var groups = dt.AsEnumerable() .GroupBy(r => new NTuple <object>(from column in columnsToGroupBy select r[column])) .OrderBy(p => p.Key); //var groups = dt.AsEnumerable() // .GroupBy(r => new NTuple<object>(from column in columnsToGroupBy select r[column])); foreach (var group in groups) { DataTable dtSub = group.CopyToDataTable(); List <string> keyvalues = new List <string>(); foreach (var s in group.Key.Values) { keyvalues.Add(s.ToString()); } DataSubset subset = new DataSubset(dtSub, groupingvarnames, keyvalues); mysubsets.Add(subset); } } catch (Exception ex) { throw new Exception("ERROR! instantiating DataSubsets Msg:" + ex.Message); // + "............." + ex.StackTrace.ToString()); } } return(mysubsets); }
//Given a list of subsets are passed in, these are displayed in a panes public void CreateHistogram_withPanes(List <DataSubset> subsets, string mysubtitle, string var) //, Color mycolor) { List <HistBinsData> list_o_histdata = new List <HistBinsData>(); List <string> subtitle1 = new List <string>(); string panelvar = ""; //Create a series for each subset for (int i = 0; i < subsets.Count; i++) { DataSubset subset = subsets[i]; HistBinsData histdata = new HistBinsData(subset.dt, var, _settings.numbins); if (histdata.N > 0) { if (panelvar == "") { panelvar = subset.Cols_ToString(); } list_o_histdata.Add(histdata); subtitle1.Add(subset.Vals_ToString()); } } //Panes this.AddPanes((list_o_histdata.Count - 1), _settings.panesLayoutDirection); List <string> subtitles = new List <string>(); //Create a series for each subset for (int i = 0; i < list_o_histdata.Count; i++) { HistBinsData histdata = list_o_histdata[i]; if (histdata.N > 0) { Color mypanelcolor = _settings.color(i); Series s = CreateHistogramSeries(histdata.bins, mypanelcolor, false); AreaSeriesView vw = (AreaSeriesView)s.View; if (i >= 1) { vw.Pane = this.xydiagram.Panes[(i - 1)]; //Panes collection starts at index 0, but first series goes into default pane } chart.Series.Add(s); //Normal Curve if (_settings.shownormalcurve & histdata.normalX != null) { Series s_nc = CreateNormalCurveSeries(histdata.normalX, histdata.normalY, mypanelcolor, false); PointSeriesView vw_nc = (PointSeriesView)s_nc.View; if (i >= 1) { vw_nc.Pane = this.xydiagram.Panes[(i - 1)]; } chart.Series.Add(s_nc); } //M SD if (_settings.histMSD) { Series s_msd = CreateMSDSeries(histdata.mean, histdata.sd, 0, mypanelcolor, false); PointSeriesView vw_msd = (PointSeriesView)s_msd.View; if (i >= 1) { vw_msd.Pane = this.xydiagram.Panes[(i - 1)]; } chart.Series.Add(s_msd); } string newline = Environment.NewLine; string subtitle_text = String.Format("<color={0}><b>{1}</b> N={2} M={3} SD={4}</color>{5}", ColorTranslator.ToHtml(mypanelcolor) , subtitle1[i] , histdata.N, Math.Round(histdata.mean, 2), Math.Round(histdata.sd, 2), newline); subtitles.Add(subtitle_text); } } string subtitles_merged = String.Join(" ", subtitles); //this.AddTitles(String.Format("Histogram <b>{0}{1}* by {2} *</b>" // , var // , Environment.NewLine // , panelvar), subtitles_merged, "", "Count"); string maintitle = String.Format("Histogram <b>{0}{1}* by {2} *</b>" , var , Environment.NewLine , panelvar); this.AddTitles(maintitle); XYDiagramDefaultPane defpane = xydiagram.DefaultPane; defpane.Title.Text = subtitles[0]; defpane.Title.Visibility = DefaultBoolean.True; for (int p = 0; p < xydiagram.Panes.Count; p++) { XYDiagramPane pane = xydiagram.Panes[p]; pane.Title.Text = subtitles[p + 1]; } chart.Width = _settings.W; chart.Height = _settings.H; chart.BorderOptions.Color = Color.White; chart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.True; }