/// <summary> /// Converts a data item to a string. /// </summary> /// <param name="col">The data column.</param> /// <param name="index">Index of the item in the data column, which should be converted.</param> /// <returns>The converted item as string.</returns> public string DataItemToString(Altaxo.Data.DataColumn col, int index) { if (col.IsElementEmpty(index)) { return(string.Empty); } string result; if (_typeConverters.TryGetValue(col.GetType(), out var func)) { result = func(col[index]); } else { result = DefaultTextConverter(col[index]); } return(result.Replace(SeparatorChar, SubstituteForSeparatorChar)); }
/// <summary> /// Creates a matrix from three selected columns. This must be a x-column, a y-column, and a value column. /// </summary> /// <param name="xcol">Column of x-values.</param> /// <param name="ycol">Column of y-values.</param> /// <param name="vcol">Column of v-values.</param> /// <param name="originalTable">The source table. This is only needed to get the names of the columns.</param> /// <param name="newtable">On return, contains the newly created table matrix.</param> /// <returns>Null if no error occurs, or an error message.</returns> public static string XYVToMatrix(DataColumn xcol, DataColumn ycol, DataColumn vcol, DataTable originalTable, out DataTable newtable) { newtable = null; System.Collections.SortedList xx = new System.Collections.SortedList(); System.Collections.SortedList yy = new System.Collections.SortedList(); int len = xcol.Count; len = Math.Min(len, ycol.Count); len = Math.Min(len, vcol.Count); // Fill the xx and yy lists for (int i = 0; i < len; ++i) { if (!xx.Contains(xcol[i])) xx.Add(xcol[i], null); if (!yy.Contains(ycol[i])) yy.Add(ycol[i], null); } DataColumn xnew = (DataColumn)Activator.CreateInstance(xcol.GetType()); DataColumn ynew = (DataColumn)Activator.CreateInstance(ycol.GetType()); xnew.Clear(); ynew.Clear(); for (int i = xx.Count - 1; i >= 0; --i) { xnew[i] = (AltaxoVariant)xx.GetKey(i); xx[xx.GetKey(i)] = i; } for (int i = yy.Count - 1; i >= 0; --i) { ynew[1 + i] = (AltaxoVariant)yy.GetKey(i); // 1 + is because the table will get an additional x-column yy[yy.GetKey(i)] = i; } DataColumn vtemplate = (DataColumn)Activator.CreateInstance(vcol.GetType()); // make a new table with yy.Count number of columns DataColumn[] vcols = new DataColumn[yy.Count]; for (int i = yy.Count - 1; i >= 0; --i) { vcols[i] = (DataColumn)vtemplate.Clone(); } // now fill the columns for (int i = 0; i < len; ++i) { int xidx = (int)xx[xcol[i]]; int yidx = (int)yy[ycol[i]]; vcols[yidx][xidx] = vcol[i]; } // assemble all columns together in a table newtable = new DataTable(); // add the x-column to the data collection string xname = null; if (null != originalTable) xname = originalTable.DataColumns.GetNameOfChildObject(xcol); if (string.IsNullOrEmpty(xname)) xname = "X"; newtable.DataColumns.Add(xnew, xname, ColumnKind.X, 0); // add the y-column to the property collection string yname = null; if (null != originalTable) yname = originalTable.DataColumns.GetNameOfChildObject(ycol); if (string.IsNullOrEmpty(yname)) yname = "Y"; newtable.PropertyColumns.Add(ynew, yname, ColumnKind.Y, 0); // add the v-columns to the data collection string vname = null; if (null != originalTable) vname = originalTable.DataColumns.GetNameOfChildObject(vcol); if (string.IsNullOrEmpty(vname)) vname = "V"; for (int i = 0; i < vcols.Length; ++i) newtable.DataColumns.Add(vcols[i], vname + i.ToString(), ColumnKind.V, 0); return null; }
/// <summary> /// Gets the fractional index for merging of two tables. /// </summary> /// <param name="masterColumn">X-column of the master table.</param> /// <param name="slaveColumn">X-column of the slave table.</param> /// <returns>Array of fractional indices. Each item points into the slaveTable to the value that should be included in the master column at the item's index.</returns> public static DoubleColumn GetFractionalIndex(DataColumn masterColumn, DataColumn slaveColumn) { if (masterColumn is DateTimeColumn && slaveColumn is DateTimeColumn) return GetFractionalIndex((DateTimeColumn)masterColumn, (DateTimeColumn)slaveColumn); if (masterColumn is INumericColumn && slaveColumn is INumericColumn) return GetFractionalIndex((INumericColumn)masterColumn, (INumericColumn)slaveColumn); throw new ArgumentException(string.Format("Unable to create fractional index from columns of type {0} and {1}", masterColumn.GetType(), slaveColumn.GetType())); }
/// <summary> /// Copies the data of the column (columns have same type, index is inside bounds), or replaces /// the column (columns of different types, index inside bounds), or adds the column (index outside bounds). /// </summary> /// <param name="index">The column position where to replace or add.</param> /// <param name="datac">The column from which the data should be copied or which should replace the existing column or which should be added.</param> /// <param name="name">The name under which the column should be stored.</param> public void CopyOrReplaceOrAdd(int index, DataColumn datac, string name) { if(index<ColumnCount) { if(this[index].GetType().Equals(datac.GetType())) { this[index].CopyDataFrom(datac); } else { // if the column to add has a parent, we can not add the column directly (we are then not the owner), so we clone it Replace(index,datac.ParentObject==null ? datac : (DataColumn)datac.Clone()); } } else { // if the column to add has a parent, we can not add the column directly (we are then not the owner), so we clone it Add(datac.ParentObject==null ? datac : (DataColumn)datac.Clone(), name); } }