Beispiel #1
0
        /// <summary>
        /// Returns a string representation of the given matrix with axis as well as rows and columns labeled.
        /// Pass <i>null</i> to one or more parameters to indicate that the corresponding decoration element shall not appear in the string converted matrix.
        /// </summary>
        /// <param name="matrix">The matrix to format.</param>
        /// <param name="rowNames">The headers of all rows (to be put to the left of the matrix).</param>
        /// <param name="columnNames">The headers of all columns (to be put to above the matrix).</param>
        /// <param name="rowAxisName">The label of the y-axis.</param>
        /// <param name="columnAxisName">The label of the x-axis.</param>
        /// <param name="title">The overall title of the matrix to be formatted.</param>
        /// <returns>the matrix converted to a string.</returns>
        public String ToTitleString(ObjectMatrix2D matrix, String[] rowNames, String[] columnNames, String rowAxisName, String columnAxisName, String title)
        {
            if (matrix.Size == 0)
            {
                return("Empty matrix");
            }
            String oldFormat = this.formatString;

            this.formatString = LEFT;

            int rows    = matrix.Rows;
            int columns = matrix.Columns;

            // determine how many rows and columns are needed
            int r = 0;
            int c = 0;

            r += (columnNames == null ? 0 : 1);
            c += (rowNames == null ? 0 : 1);
            c += (rowAxisName == null ? 0 : 1);
            c += (rowNames != null || rowAxisName != null ? 1 : 0);

            int height = r + System.Math.Max(rows, rowAxisName == null ? 0 : rowAxisName.Length);
            int width  = c + columns;

            // make larger matrix holding original matrix and naming strings
            ObjectMatrix2D titleMatrix = matrix.Like(height, width);

            // insert original matrix into larger matrix
            titleMatrix.ViewPart(r, c, rows, columns).Assign(matrix);

            // insert column axis name in leading row
            if (r > 0)
            {
                titleMatrix.ViewRow(0).ViewPart(c, columns).Assign(columnNames);
            }

            // insert row axis name in leading column
            if (rowAxisName != null)
            {
                String[] rowAxisStrings = new String[rowAxisName.Length];
                for (int i = rowAxisName.Length; --i >= 0;)
                {
                    rowAxisStrings[i] = rowAxisName.Substring(i, i + 1);
                }
                titleMatrix.ViewColumn(0).ViewPart(r, rowAxisName.Length).Assign(rowAxisStrings);
            }
            // insert row names in next leading columns
            if (rowNames != null)
            {
                titleMatrix.ViewColumn(c - 2).ViewPart(r, rows).Assign(rowNames);
            }

            // insert vertical "---------" separator line in next leading column
            if (c > 0)
            {
                titleMatrix.ViewColumn(c - 2 + 1).ViewPart(0, rows + r).Assign("|");
            }

            // convert the large matrix to a string
            Boolean oldPrintShape = this.printShape;

            this.printShape = false;
            String str = ToString(titleMatrix);

            this.printShape = oldPrintShape;

            // insert horizontal "--------------" separator line
            StringBuilder total = new StringBuilder(str);

            if (columnNames != null)
            {
                int i = str.IndexOf(rowSeparator);
                total.Insert(i + 1, Repeat('-', i) + rowSeparator);
            }
            else if (columnAxisName != null)
            {
                int i = str.IndexOf(rowSeparator);
                total.Insert(0, Repeat('-', i) + rowSeparator);
            }

            // insert line for column axis name
            if (columnAxisName != null)
            {
                int j = 0;
                if (c > 0)
                {
                    j = str.IndexOf('|');
                }
                String s = Blanks(j);
                if (c > 0)
                {
                    s = s + "| ";
                }
                s = s + columnAxisName + "\n";
                total.Insert(0, s);
            }

            // insert title
            if (title != null)
            {
                total.Insert(0, title + "\n");
            }

            this.formatString = oldFormat;

            return(total.ToString());
        }