/// <summary> /// Creates an xy-plot form a table /// </summary> /// <param name="Tab"></param> /// <param name="ColName_ForXValues"> /// Column name, where the values for the x-axis are taken. /// </param> /// <param name="ColName_ForYValues"></param> /// <param name="ColName_GroupSelection"> /// Selects, which table row will end up in which graph, resp. data group (<see cref="Plot2Ddata.dataGroups"/>). /// </param> /// <returns></returns> public static Plot2Ddata ToPlot(this DataTable Tab, string ColName_ForXValues, string ColName_ForYValues, params string[] ColName_GroupSelection) { Plot2Ddata ret = new Plot2Ddata(); // loop over table rows // ==================== string[] ColNames = Tab.GetColumnNames(); int L = Tab.Rows.Count; int J = Tab.Columns.Count; for (int i = 0; i < L; i++) { DataRow orgRow = Tab.Rows[i]; Dictionary <string, object> orgRowAsDict = new Dictionary <string, object>(); foreach (string ColName in ColNames) { object obj_ColName = orgRow[ColName]; if (obj_ColName == DBNull.Value) { orgRowAsDict.Add(ColName, null); } else { orgRowAsDict.Add(ColName, obj_ColName); } } string groupName = ""; try { //groupName = GroupSelector(i, orgRowAsDict); for (int iS = 0; iS < ColName_GroupSelection.Length; iS++) { groupName += ColName_GroupSelection[iS] + orgRow[ColName_GroupSelection[iS]].ToString(); if (iS < ColName_GroupSelection.Length - 1) { groupName += "--"; } } } catch (Exception e) { Console.WriteLine("Exception in the selection test of row {0}: {1}, Message: {2}.", i, e.GetType().Name, e.Message); groupName = null; } if (groupName != null) { double xValue = Convert.ToDouble(orgRowAsDict[ColName_ForXValues]); double yValue = Convert.ToDouble(orgRowAsDict[ColName_ForYValues]); Plot2Ddata.XYvalues xyGroup = Array.Find(ret.dataGroups, xyG => xyG.Name.Equals(groupName)); if (xyGroup == null) { xyGroup = new Plot2Ddata.XYvalues(groupName); ArrayTools.AddToArray(xyGroup, ref ret.dataGroups); } ArrayTools.AddToArray(xValue, ref xyGroup.Abscissas); ArrayTools.AddToArray(yValue, ref xyGroup.Values); } } // sort data // ========= foreach (var xyGroup in ret.dataGroups) { Array.Sort(xyGroup.Abscissas, xyGroup.Values); } // return // ====== return(ret); }
/// <summary> /// Creates an xy-plot form a table /// </summary> /// <param name="Tab"></param> /// <param name="RowSelector"> /// Selects, which table row will end up in which graph, resp. data group (<see cref="Plot2Ddata.dataGroups"/>). /// If the returned name is null, or if an exception is thrown, the respective data row will not be included in any graph. /// </param> /// <param name="NoOfSweeps"> /// To sweep over the table multiple times, e.g. for selecting more than one value per row. /// </param> /// <returns></returns> public static Plot2Ddata ToPlot(this DataTable Tab, PlotRowSelectorEx RowSelector, int NoOfSweeps) { Plot2Ddata ret = new Plot2Ddata(); // loop over table rows // ==================== for (int iSweep = 0; iSweep < NoOfSweeps; iSweep++) { string[] ColNames = Tab.GetColumnNames(); int L = Tab.Rows.Count; int J = Tab.Columns.Count; for (int i = 0; i < L; i++) { DataRow orgRow = Tab.Rows[i]; Dictionary <string, object> orgRowAsDict = new Dictionary <string, object>(); foreach (string ColName in ColNames) { object obj_ColName = orgRow[ColName]; if (obj_ColName == DBNull.Value) { orgRowAsDict.Add(ColName, null); } else { orgRowAsDict.Add(ColName, obj_ColName); } } string groupName; Solution.Gnuplot.PlotFormat graphFormat; double xValue; double yValue; try { RowSelector(iSweep, i, orgRowAsDict, out groupName, out graphFormat, out xValue, out yValue); } catch (Exception e) { Console.WriteLine("Exception in the selection test of row {0}: {1}, Message: {2}.", i, e.GetType().Name, e.Message); groupName = null; graphFormat = null; xValue = 0.0; yValue = 0.0; } if (groupName != null) { //double xValue = Convert.ToDouble(orgRowAsDict[ColName_ForXValues]); //double yValue = Convert.ToDouble(orgRowAsDict[ColName_ForYValues]); Plot2Ddata.XYvalues xyGroup = Array.Find(ret.dataGroups, xyG => xyG.Name.Equals(groupName)); if (xyGroup == null) { xyGroup = new Plot2Ddata.XYvalues(groupName); ArrayTools.AddToArray(xyGroup, ref ret.dataGroups); } ArrayTools.AddToArray(xValue, ref xyGroup.Abscissas); ArrayTools.AddToArray(yValue, ref xyGroup.Values); xyGroup.Format = graphFormat; } } } // sort data // ========= foreach (var xyGroup in ret.dataGroups) { Array.Sort(xyGroup.Abscissas, xyGroup.Values); } // return // ====== return(ret); }
/// <summary> /// Maybe useful for multi-plots, when just one plot should show the legend for all plots: /// It loops over all plots, collects all names and formats and adds a dummy graph to a specific /// plot if the name/format pair is not already shown in this plot. /// </summary> /// <param name="multiplots"></param> /// <param name="I">destination plot (where the dummys are added) row</param> /// <param name="J">destination plot (where the dummys are added) column</param> /// <param name="byName"> /// - if true: go by <see cref="Plot2Ddata.XYvalues.Name"/>. /// - if false: go by <see cref="Plot2Ddata.XYvalues.Format"/>. /// </param> /// <param name="DummyX">x-value of the dummy plot to add, should be outside visible range, <see cref="Plot2Ddata.XrangeMax"/>.</param> /// <param name="DummyY">y-value of the dummy plot to add, should be outside visible range, <see cref="Plot2Ddata.YrangeMax"/>.</param> public static void AddDummyPlotsForLegend(this Plot2Ddata[,] multiplots, int I, int J, bool byName = true, double DummyX = 1e55, double DummyY = 1e56) { // collect all names & formats // --------------------------- var names = new List <string>(); var fomts = new List <PlotFormat>(); for (int i = 0; i < multiplots.GetLength(0); i++) { for (int j = 0; j < multiplots.GetLength(1); j++) { if (multiplots[i, j] != null) { foreach (var p in multiplots[i, j].dataGroups) { bool isthere; if (byName) { isthere = names.Contains(p.Name); } else { isthere = fomts.Contains(p.Format); } if (!isthere) { names.Add(p.Name); fomts.Add(p.Format); } } } } } // see what we have to add // ----------------------- for (int iGraph = 0; iGraph < names.Count; iGraph++) { //foreach (var p in multiplots[I, J].dataGroups) { // //} string nmn = names[iGraph]; PlotFormat fmt = fomts[iGraph]; bool isThere; if (byName) { isThere = multiplots[I, J].dataGroups.Where(gr => gr.Name.Equals(nmn)).Count() > 0; } else { isThere = multiplots[I, J].dataGroups.Where(gr => gr.Format.Equals(fmt)).Count() > 0; } if (!isThere) { Plot2Ddata.XYvalues dummy = new Plot2Ddata.XYvalues(nmn) { Format = fmt, Abscissas = new double[] { DummyX }, Values = new double[] { DummyY } }; ArrayTools.AddToArray(dummy, ref multiplots[I, J].dataGroups); } } }