public static void ShowDecomposeByColumnContentDialog(this DataTable srcTable, IAscendingIntegerCollection selectedDataRows, IAscendingIntegerCollection selectedDataColumns)
        {
            DataTableMultipleColumnProxy    proxy   = null;
            DecomposeByColumnContentOptions options = null;

            try
            {
                proxy = new DataTableMultipleColumnProxy(DecomposeByColumnContentDataAndOptions.ColumnsParticipatingIdentifier, srcTable, selectedDataRows, selectedDataColumns);
                proxy.EnsureExistenceOfIdentifier(DecomposeByColumnContentDataAndOptions.ColumnWithCyclingVariableIdentifier, 1);

                options = new DecomposeByColumnContentOptions();
            }
            catch (Exception ex)
            {
                Current.Gui.ErrorMessageBox(string.Format("{0}\r\nDetails:\r\n{1}", ex.Message, ex.ToString()), "Error in preparation of 'Decompose by column content'");
                return;
            }

            var dataAndOptions = new DecomposeByColumnContentDataAndOptions(proxy, options);

            // in order to show the column names etc in the dialog, it is neccessary to set the source
            if (true == Current.Gui.ShowDialog(ref dataAndOptions, "Choose options", false))
            {
                var destTable = new DataTable();
                proxy   = dataAndOptions.Data;
                options = dataAndOptions.Options;

                string error = null;
                try
                {
                    error = DecomposeByColumnContent(dataAndOptions.Data, dataAndOptions.Options, destTable);
                }
                catch (Exception ex)
                {
                    error = ex.ToString();
                }
                if (null != error)
                {
                    Current.Gui.ErrorMessageBox(error);
                }

                destTable.Name = srcTable.Name + "_Decomposed";

                // Create a DataSource
                var dataSource = new DecomposeByColumnContentDataSource(proxy, options, new Altaxo.Data.DataSourceImportOptions());
                destTable.DataSource = dataSource;

                Current.Project.DataTableCollection.Add(destTable);
                Current.IProjectService.ShowDocumentView(destTable);
            }
        }
Exemplo n.º 2
0
		public static void ShowDecomposeByColumnContentDialog(this DataTable srcTable, IAscendingIntegerCollection selectedDataRows, IAscendingIntegerCollection selectedDataColumns)
		{
			DataTableMultipleColumnProxy proxy = null;
			DecomposeByColumnContentOptions options = null;

			try
			{
				proxy = new DataTableMultipleColumnProxy(DecomposeByColumnContentDataAndOptions.ColumnsParticipatingIdentifier, srcTable, selectedDataRows, selectedDataColumns);
				proxy.EnsureExistenceOfIdentifier(DecomposeByColumnContentDataAndOptions.ColumnWithCyclingVariableIdentifier, 1);

				options = new DecomposeByColumnContentOptions();
			}
			catch (Exception ex)
			{
				Current.Gui.ErrorMessageBox(string.Format("{0}\r\nDetails:\r\n{1}", ex.Message, ex.ToString()), "Error in preparation of 'Decompose by column content'");
				return;
			}

			var dataAndOptions = new DecomposeByColumnContentDataAndOptions(proxy, options);

			// in order to show the column names etc in the dialog, it is neccessary to set the source
			if (true == Current.Gui.ShowDialog(ref dataAndOptions, "Choose options", false))
			{
				var destTable = new DataTable();
				proxy = dataAndOptions.Data;
				options = dataAndOptions.Options;

				string error = null;
				try
				{
					error = DecomposeByColumnContent(dataAndOptions.Data, dataAndOptions.Options, destTable);
				}
				catch (Exception ex)
				{
					error = ex.ToString();
				}
				if (null != error)
					Current.Gui.ErrorMessageBox(error);

				destTable.Name = srcTable.Name + "_Decomposed";

				// Create a DataSource
				var dataSource = new DecomposeByColumnContentDataSource(proxy, options, new Altaxo.Data.DataSourceImportOptions());
				destTable.DataSource = dataSource;

				Current.Project.DataTableCollection.Add(destTable);
				Current.ProjectService.ShowDocumentView(destTable);
			}
		}
        /// <summary>
        /// Decompose the source columns according to the provided options. The source table and the settings are provided in the <paramref name="options"/> variable.
        /// The provided destination table is cleared from all data and property values before.
        /// </summary>
        /// <param name="inputData">The data containing the source table, the participating columns and the column with the cycling variable.</param>
        /// <param name="options">The settings for decomposing.</param>
        /// <param name="destTable">The destination table. Any data will be removed before filling with the new data.</param>
        /// <returns>Null if the method finishes successfully, or an error information.</returns>
        public static string DecomposeByColumnContent(DataTableMultipleColumnProxy inputData, DecomposeByColumnContentOptions options, DataTable destTable)
        {
            var srcTable = inputData.DataTable;

            try
            {
                DecomposeByColumnContentDataAndOptions.EnsureCoherence(inputData, true);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }

            destTable.DataColumns.RemoveColumnsAll();
            destTable.PropCols.RemoveColumnsAll();

            DataColumn srcCycCol        = inputData.GetDataColumnOrNull(DecomposeByColumnContentDataAndOptions.ColumnWithCyclingVariableIdentifier);
            var        decomposedValues = Decompose(srcCycCol);

            // the decomposedValues are not sorted yes

            if (options.DestinationColumnSorting == DecomposeByColumnContentOptions.OutputSorting.Ascending)
            {
                decomposedValues.Sort();
            }
            else if (options.DestinationColumnSorting == DecomposeByColumnContentOptions.OutputSorting.Descending)
            {
                decomposedValues.Sort();
                decomposedValues.Reverse();
            }
            // get the other columns to process

            var srcColumnsToProcess = new List <DataColumn>(inputData.GetDataColumns(DecomposeByColumnContentDataAndOptions.ColumnsParticipatingIdentifier));

            // subtract the column containing the decompose values
            srcColumnsToProcess.Remove(srcCycCol);

            // the only property column that is now usefull is that with the repeated values
            var destPropCol = destTable.PropCols.EnsureExistence(srcTable.DataColumns.GetColumnName(srcCycCol), srcCycCol.GetType(), srcTable.DataColumns.GetColumnKind(srcCycCol), srcTable.DataColumns.GetColumnGroup(srcCycCol));

            if (options.DestinationOutput == DecomposeByColumnContentOptions.OutputFormat.GroupOneColumn)
            {
                // columns originating from the same column but with different property are grouped together, but they will get different group numbers
                foreach (var srcCol in srcColumnsToProcess)
                {
                    int nCreatedCol  = -1;
                    int nCreatedProp = 0;
                    foreach (var prop in decomposedValues)
                    {
                        ++nCreatedCol;
                        ++nCreatedProp;
                        var destCol  = destTable.DataColumns.EnsureExistence(srcTable.DataColumns.GetColumnName(srcCol) + "." + nCreatedCol.ToString(), srcCol.GetType(), srcTable.DataColumns.GetColumnKind(srcCol), nCreatedProp);
                        var nDestCol = destTable.DataColumns.GetColumnNumber(destCol);
                        for (int i = 0, j = 0; i < srcCycCol.Count; ++i)
                        {
                            if (prop == srcCycCol[i])
                            {
                                destCol[j] = srcCol[i];
                                ++j;
                            }
                        }
                        // fill also property column
                        destPropCol[nDestCol] = prop;
                    }
                }
            }
            else if (options.DestinationOutput == DecomposeByColumnContentOptions.OutputFormat.GroupAllColumns)
            {
                // all columns with the same property are grouped together, and those columns will share the same group number
                int nCreatedCol  = -1; // running number of processed range for column creation (Naming)
                int nCreatedProp = -1;
                foreach (var prop in decomposedValues)
                {
                    ++nCreatedProp;
                    ++nCreatedCol;

                    foreach (var srcCol in srcColumnsToProcess)
                    {
                        var destCol  = destTable.DataColumns.EnsureExistence(srcTable.DataColumns.GetColumnName(srcCol) + "." + nCreatedCol.ToString(), srcCol.GetType(), srcTable.DataColumns.GetColumnKind(srcCol), nCreatedProp);
                        var nDestCol = destTable.DataColumns.GetColumnNumber(destCol);
                        for (int i = 0, j = 0; i < srcCycCol.Count; ++i)
                        {
                            if (prop == srcCycCol[i])
                            {
                                destCol[j] = srcCol[i];
                                ++j;
                            }
                        }
                        // fill also property column
                        destPropCol[nDestCol] = prop;
                    }
                }
            }
            else
            {
                throw new NotImplementedException("The option for destination output is unknown: " + options.DestinationOutput.ToString());
            }

            return(null);
        }