Example #1
0
        protected override void RenderChart(System.Drawing.Graphics graphics, Palette palette, Bamboo.Css.StyleStack styleStack, System.Drawing.RectangleF rectangle, string title, Bamboo.DataStructures.Table table)
        {
            styleStack.PushTag("Table");



            string backColor = (string)styleStack["BackColor"];

            Bamboo.Css.Font font      = (Bamboo.Css.Font)styleStack["Font"];
            string          foreColor = (string)styleStack["ForeColor"];
            int             padding   = (int)styleStack["Padding"];

            //TODO put in stylesheet
            System.Drawing.Font titleFont = palette.Font(font.Name, font.Size * 1.2f, System.Drawing.FontStyle.Bold);

            System.Drawing.Pen   backColorPen   = palette.Pen(backColor);
            System.Drawing.Brush backColorBrush = palette.Brush(backColor);
            System.Drawing.Brush foreColorBrush = palette.Brush(foreColor);



            // Background
            graphics.DrawRectangle(backColorPen, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
            graphics.FillRectangle(backColorBrush, rectangle);
            rectangle = new System.Drawing.RectangleF(rectangle.Left + padding, rectangle.Top + padding, rectangle.Width - padding - padding, rectangle.Height - padding - padding);

            // Title
            if (title != null)
            {
                rectangle = DrawTitle(title, titleFont, foreColorBrush, graphics, rectangle);
            }



            int width  = table.Columns.Count;
            int height = table.Rows.Count;

            List <Cell[]> cellsList = new List <Cell[]>(height + 1);

            Cell[] cells;

            styleStack.PushTag("Header");
            backColor = (string)styleStack["BackColor"];
            foreColor = (string)styleStack["ForeColor"];
            System.Drawing.ContentAlignment textAlign = (System.Drawing.ContentAlignment)styleStack["TextAlign"];
            cells = new Cell[width];
            for (int x = 0; x < width; x++)
            {
                cells[x].HasValue        = true;
                cells[x].Type            = CellType.Header;
                cells[x].Colspan         = 1;
                cells[x].Rowspan         = 1;
                cells[x].Value           = table.Columns[x];
                cells[x].BackgroundColor = backColor;
                cells[x].ForegroundColor = foreColor;
                cells[x].TextAlign       = textAlign;
            }
            cellsList.Add(cells);
            styleStack.PopTag();


            styleStack.PushTag("Cell");
            backColor = (string)styleStack["BackColor"];
            foreColor = (string)styleStack["ForeColor"];
            textAlign = (System.Drawing.ContentAlignment)styleStack["TextAlign"];
            for (int y = 0; y < height; y++)
            {
                Bamboo.DataStructures.Tuple row = table.Rows[y];
                cells = new Cell[width];
                for (int x = 0; x < width; x++)
                {
                    //TODO push cell.
                    cells[x].HasValue        = true;
                    cells[x].Type            = CellType.Cell;
                    cells[x].Colspan         = 1;
                    cells[x].Rowspan         = 1;
                    cells[x].Value           = row[x];
                    cells[x].BackgroundColor = backColor;
                    cells[x].ForegroundColor = foreColor;
                    cells[x].TextAlign       = textAlign;
                }
                cellsList.Add(cells);
            }
            styleStack.PopTag();


            Render(graphics, palette, styleStack, cellsList, rectangle);



            styleStack.PopTag();
        }
Example #2
0
        protected override void RenderChart(System.Drawing.Graphics graphics, Palette palette, Bamboo.Css.StyleStack styleStack, System.Drawing.RectangleF rectangle, string title, Bamboo.DataStructures.Table table)
        {
            styleStack.PushTag("LineGraph");



            string axisForeColor = (string)styleStack["AxisForeColor"];
            string axisLineColor = (string)styleStack["AxisLineColor"];
            string backColor     = (string)styleStack["BackColor"];

            Bamboo.Css.Font font          = (Bamboo.Css.Font)styleStack["Font"];
            string          foreColor     = (string)styleStack["ForeColor"];
            string          gridLineColor = (string)styleStack["GridLineColor"];
            float           lineWidth     = System.Convert.ToSingle(styleStack["LineWidth"]);
            int             padding       = (int)styleStack["Padding"];
            bool            showPoints    = (bool)styleStack["ShowPoints"];
            float           tickSize      = System.Convert.ToSingle(styleStack["TickSize"]);

            string[] colors = (string[])styleStack["Colors"];

            //TODO put in stylesheet
            System.Drawing.Font titleFont = palette.Font(font.Name, font.Size * 1.2f, System.Drawing.FontStyle.Bold);

            //TODO put in stylesheet
            System.Drawing.Font axisTickFont  = palette.Font(font.Name, font.Size * .66f);            //TODO put in stylesheet.
            System.Drawing.Font axisTitleFont = palette.Font(font.Name, font.Size, System.Drawing.FontStyle.Bold);

            System.Drawing.Brush axisForeColorBrush = palette.Brush(axisForeColor);
            System.Drawing.Pen   axisLineColorPen   = palette.Pen(axisLineColor);
            System.Drawing.Pen   backColorPen       = palette.Pen(backColor);
            System.Drawing.Brush backColorBrush     = palette.Brush(backColor);
            System.Drawing.Brush foreColorBrush     = palette.Brush(foreColor);
            System.Drawing.Pen   gridLineColorPen   = palette.Pen(gridLineColor);



            // Background
            graphics.DrawRectangle(backColorPen, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
            graphics.FillRectangle(backColorBrush, rectangle);
            rectangle = new System.Drawing.RectangleF(rectangle.Left + padding, rectangle.Top + padding, rectangle.Width - padding - padding, rectangle.Height - padding - padding);

            // Title
            if (title != null)
            {
                rectangle = DrawTitle(title, titleFont, foreColorBrush, graphics, rectangle);
            }

            //TODO
            // Legend
            rectangle = new System.Drawing.RectangleF(rectangle.Left, rectangle.Top, rectangle.Width - padding - padding, rectangle.Height);



            string  xAxisTitle      = table.Columns[0].ToString();
            decimal x_min           = 0;
            decimal x_max           = table.Rows.Count - 1;
            Range   xRange          = new Range(x_min, x_max);
            bool    isXRangeNumeric = IsNumeric(table.Rows[0][0]);
            int     xAxisScale      = CalculateScale(xRange);
            int     xAxisPrecision  = Math.Max(0, 0 - xAxisScale);


            string  yAxisTitle = table.Columns[1].ToString();
            decimal y0         = System.Convert.ToDecimal(table.Rows[0][1]);
            decimal y_min      = y0;
            decimal y_max      = y0;

            for (int j = 1; j < table.Rows.Count; j++)
            {
                for (int i = 1; i < table.Columns.Count; i++)
                {
                    decimal y = System.Convert.ToDecimal(table.Rows[j][i]);
                    if (y > y_max)
                    {
                        y_max = y;
                    }
                    else if (y < y_min)
                    {
                        y_min = y;
                    }
                }
            }
            Range yRange             = new Range(y_min, y_max);
            bool  isYRangeNumeric    = true;
            int   yAxisScale         = CalculateScale(yRange);
            int   yAxisPrecision     = Math.Max(0, 0 - yAxisScale);
            float maxYTickLabelWidth = Math.Max(
                MeasureString(graphics, y_min.ToString("N" + yAxisPrecision), axisTickFont).Width,
                MeasureString(graphics, y_max.ToString("N" + yAxisPrecision), axisTickFont).Width);



            System.Drawing.SizeF yAxisTitleSize = MeasureString(graphics, yAxisTitle, axisTitleFont);
            System.Drawing.SizeF xAxisTitleSize = MeasureString(graphics, xAxisTitle, axisTitleFont);
            float maxXTickLabelHeight           = MeasureString(graphics, "A", axisTickFont).Height;

            // Y-Axis Title
            rectangle = DrawYAxisTitle(graphics, rectangle, yAxisTitle, axisTitleFont, yAxisTitleSize, xAxisTitleSize.Height + maxXTickLabelHeight + tickSize, axisForeColorBrush);

            // X-Axis Title
            rectangle = DrawXAxisTitle(graphics, rectangle, xAxisTitle, axisTitleFont, xAxisTitleSize, yAxisTitleSize.Height + maxYTickLabelWidth + tickSize, axisForeColorBrush);

            rectangle = new System.Drawing.RectangleF(rectangle.Left + maxYTickLabelWidth + tickSize, rectangle.Top, rectangle.Width - (maxYTickLabelWidth + tickSize), rectangle.Height - (maxXTickLabelHeight + tickSize));

            DrawYAxis(graphics, rectangle, YAxisTicks(rectangle, graphics, axisTickFont, yRange, isYRangeNumeric, yAxisScale, yAxisPrecision, table), tickSize, axisTickFont, gridLineColorPen, axisLineColorPen, axisForeColorBrush, true);
            DrawXAxis(graphics, rectangle, XAxisTicks(rectangle, graphics, axisTickFont, xRange, isXRangeNumeric, xAxisScale, xAxisPrecision, table), tickSize, axisTickFont, gridLineColorPen, axisLineColorPen, axisForeColorBrush, true);

            rectangle = new System.Drawing.RectangleF(rectangle.Left + 1, rectangle.Top, rectangle.Width - 1, rectangle.Height - 1);



            // Plot
            float pointRadius = lineWidth * 2f;             //TODO put in stylesheet.

            Line[] lines = new Line[table.Columns.Count - 1];
            for (int i = 1; i < table.Columns.Count; i++)
            {
                lines[i - 1].Pen   = palette.Pen(colors[(i - 1) % colors.Length], lineWidth);
                lines[i - 1].Brush = palette.Brush(colors[(i - 1) % colors.Length]);

                lines[i - 1].Points = new Point[table.Rows.Count];
                for (int j = 0; j < table.Rows.Count; j++)
                {
                    float value = System.Convert.ToSingle(table.Rows[j][i]);
                    System.Drawing.PointF point = Coordinate(j, System.Convert.ToSingle(xRange.Min), System.Convert.ToSingle(xRange.Max), rectangle.Left, rectangle.Width, value, System.Convert.ToSingle(yRange.Min), System.Convert.ToSingle(yRange.Max), rectangle.Top, rectangle.Height);
                    lines[i - 1].Points[j].X = point.X;
                    lines[i - 1].Points[j].Y = point.Y;
                }
            }
            DrawLines(graphics, lines);
            if (showPoints)
            {
                for (int i = 0; i < lines.Length; i++)
                {
                    System.Drawing.PointF[] points = new System.Drawing.PointF[lines[i].Points.Length - 1];
                    for (int j = 1; j < points.Length; j++)
                    {
                        points[j - 1] = new System.Drawing.PointF(lines[i].Points[j].X, lines[i].Points[j].Y);
                    }
                    DrawPoints(graphics, lines[i].Brush, points, pointRadius);
                }
            }



            styleStack.PopTag();
        }
Example #3
0
        protected override void RenderChart(System.Drawing.Graphics graphics, Palette palette, Bamboo.Css.StyleStack styleStack, System.Drawing.RectangleF rectangle, string title, Bamboo.DataStructures.Table table)
        {
            styleStack.PushTag("OHLCGraph");



            string axisForeColor = (string)styleStack["AxisForeColor"];
            string axisLineColor = (string)styleStack["AxisLineColor"];
            string backColor     = (string)styleStack["BackColor"];

            Bamboo.Css.Font font          = (Bamboo.Css.Font)styleStack["Font"];
            string          foreColor     = (string)styleStack["ForeColor"];
            string          gridLineColor = (string)styleStack["GridLineColor"];
            int             padding       = (int)styleStack["Padding"];
            float           tickSize      = System.Convert.ToSingle(styleStack["TickSize"]);

            //TODO put in stylesheet
            System.Drawing.Font titleFont = palette.Font(font.Name, font.Size * 1.2f, System.Drawing.FontStyle.Bold);

            //TODO put in stylesheet
            System.Drawing.Font axisTickFont  = palette.Font(font.Name, font.Size * .66f);            //TODO put in stylesheet.
            System.Drawing.Font axisTitleFont = palette.Font(font.Name, font.Size, System.Drawing.FontStyle.Bold);

            System.Drawing.Brush axisForeColorBrush = palette.Brush(axisForeColor);
            System.Drawing.Pen   axisLineColorPen   = palette.Pen(axisLineColor);
            System.Drawing.Pen   backColorPen       = palette.Pen(backColor);
            System.Drawing.Brush backColorBrush     = palette.Brush(backColor);
            System.Drawing.Brush foreColorBrush     = palette.Brush(foreColor);
            System.Drawing.Pen   gridLineColorPen   = palette.Pen(gridLineColor);



            // Background
            graphics.DrawRectangle(backColorPen, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
            graphics.FillRectangle(backColorBrush, rectangle);
            rectangle = new System.Drawing.RectangleF(rectangle.Left + padding, rectangle.Top + padding, rectangle.Width - padding - padding, rectangle.Height - padding - padding);

            // Title
            if (title != null)
            {
                rectangle = DrawTitle(title, titleFont, foreColorBrush, graphics, rectangle);
            }

            //TODO
            // Legend
            rectangle = new System.Drawing.RectangleF(rectangle.Left, rectangle.Top, rectangle.Width - padding - padding, rectangle.Height);



            string  xAxisTitle      = table.Columns[0].ToString();
            decimal x_min           = 0;
            decimal x_max           = table.Rows.Count - 1;
            Range   xRange          = new Range(x_min, x_max);
            bool    isXRangeNumeric = true;
            int     xAxisScale      = 0;
            int     xAxisPrecision  = 0;


            string  yAxisTitle = table.Columns[1].ToString();
            decimal y0         = System.Convert.ToDecimal(table.Rows[0][1]);
            decimal y_min      = y0;
            decimal y_max      = y0;

            for (int j = 1; j < table.Rows.Count; j++)
            {
                for (int i = 1; i < table.Columns.Count; i++)
                {
                    decimal y = System.Convert.ToDecimal(table.Rows[j][i]);
                    if (y > y_max)
                    {
                        y_max = y;
                    }
                    else if (y < y_min)
                    {
                        y_min = y;
                    }
                }
            }
            Range yRange             = new Range(y_min, y_max);
            bool  isYRangeNumeric    = true;
            int   yAxisScale         = CalculateScale(yRange);
            int   yAxisPrecision     = Math.Max(0, 0 - yAxisScale);
            float maxYTickLabelWidth = Math.Max(
                MeasureString(graphics, y_min.ToString("N" + yAxisPrecision), axisTickFont).Width,
                MeasureString(graphics, y_max.ToString("N" + yAxisPrecision), axisTickFont).Width);



            System.Drawing.SizeF yAxisTitleSize = MeasureString(graphics, yAxisTitle, axisTitleFont);
            System.Drawing.SizeF xAxisTitleSize = MeasureString(graphics, xAxisTitle, axisTitleFont);
            float maxXTickLabelHeight           = MeasureString(graphics, "A", axisTickFont).Height;

            // Y-Axis Title
            rectangle = DrawYAxisTitle(graphics, rectangle, yAxisTitle, axisTitleFont, yAxisTitleSize, xAxisTitleSize.Height + maxXTickLabelHeight + tickSize, axisForeColorBrush);

            // X-Axis Title
            rectangle = DrawXAxisTitle(graphics, rectangle, xAxisTitle, axisTitleFont, xAxisTitleSize, yAxisTitleSize.Height + maxYTickLabelWidth + tickSize, axisForeColorBrush);

            rectangle = new System.Drawing.RectangleF(rectangle.Left + maxYTickLabelWidth + tickSize, rectangle.Top, rectangle.Width - (maxYTickLabelWidth + tickSize), rectangle.Height - (maxXTickLabelHeight + tickSize));

            DrawYAxis(graphics, rectangle, YAxisTicks(rectangle, graphics, axisTickFont, yRange, isYRangeNumeric, yAxisScale, yAxisPrecision, table), tickSize, axisTickFont, gridLineColorPen, axisLineColorPen, axisForeColorBrush, false);
            DrawXAxis(graphics, rectangle, XAxisTicks(rectangle, graphics, axisTickFont, xRange, isXRangeNumeric, xAxisScale, xAxisPrecision, table), tickSize, axisTickFont, gridLineColorPen, axisLineColorPen, axisForeColorBrush, false);

            rectangle = new System.Drawing.RectangleF(rectangle.Left + 1, rectangle.Top, rectangle.Width, rectangle.Height - 1);



            // Plot
            string barColor = (string)styleStack["BarColor"];

            System.Drawing.Pen barPen = palette.Pen(barColor);

            float barWidth = rectangle.Width / table.Rows.Count;
            float bottom   = (float)Math.Ceiling(rectangle.Top) + (float)Math.Ceiling(rectangle.Height);
            float x        = rectangle.Left;

            OHLCBar[] bars = new OHLCBar[table.Rows.Count];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                Bamboo.DataStructures.Tuple row = table.Rows[i];

                System.Drawing.PointF openPoint  = Coordinate(i, System.Convert.ToSingle(xRange.Min), System.Convert.ToSingle(xRange.Max), rectangle.Left, rectangle.Width, System.Convert.ToSingle(row[1]), System.Convert.ToSingle(yRange.Min), System.Convert.ToSingle(yRange.Max), rectangle.Top, rectangle.Height);
                System.Drawing.PointF highPoint  = Coordinate(i, System.Convert.ToSingle(xRange.Min), System.Convert.ToSingle(xRange.Max), rectangle.Left, rectangle.Width, System.Convert.ToSingle(row[2]), System.Convert.ToSingle(yRange.Min), System.Convert.ToSingle(yRange.Max), rectangle.Top, rectangle.Height);
                System.Drawing.PointF lowPoint   = Coordinate(i, System.Convert.ToSingle(xRange.Min), System.Convert.ToSingle(xRange.Max), rectangle.Left, rectangle.Width, System.Convert.ToSingle(row[3]), System.Convert.ToSingle(yRange.Min), System.Convert.ToSingle(yRange.Max), rectangle.Top, rectangle.Height);
                System.Drawing.PointF closePoint = Coordinate(i, System.Convert.ToSingle(xRange.Min), System.Convert.ToSingle(xRange.Max), rectangle.Left, rectangle.Width, System.Convert.ToSingle(row[4]), System.Convert.ToSingle(yRange.Min), System.Convert.ToSingle(yRange.Max), rectangle.Top, rectangle.Height);

                bars[i].Left       = x;
                bars[i].Width      = barWidth;
                bars[i].OpenPoint  = openPoint;
                bars[i].HighPoint  = highPoint;
                bars[i].LowPoint   = lowPoint;
                bars[i].ClosePoint = closePoint;
                bars[i].Pen        = barPen;

                x += barWidth;                 //TODO this isn't pretty.
            }
            DrawOHLCBars(graphics, bars);



            styleStack.PopTag();
        }
Example #4
0
        protected override void RenderChart(System.Drawing.Graphics graphics, Palette palette, Bamboo.Css.StyleStack styleStack, System.Drawing.RectangleF rectangle, string title, Bamboo.DataStructures.Table table)
        {
            styleStack.PushTag("Table");



            string backColor = (string)styleStack["BackColor"];

            Bamboo.Css.Font font      = (Bamboo.Css.Font)styleStack["Font"];
            string          foreColor = (string)styleStack["ForeColor"];
            int             padding   = (int)styleStack["Padding"];

            //TODO put in stylesheet
            System.Drawing.Font titleFont = palette.Font(font.Name, font.Size * 1.2f, System.Drawing.FontStyle.Bold);

            System.Drawing.Pen   backColorPen   = palette.Pen(backColor);
            System.Drawing.Brush backColorBrush = palette.Brush(backColor);
            System.Drawing.Brush foreColorBrush = palette.Brush(foreColor);



            // Background
            graphics.DrawRectangle(backColorPen, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
            graphics.FillRectangle(backColorBrush, rectangle);
            rectangle = new System.Drawing.RectangleF(rectangle.Left + padding, rectangle.Top + padding, rectangle.Width - padding - padding, rectangle.Height - padding - padding);

            // Title
            if (title != null)
            {
                rectangle = DrawTitle(title, titleFont, foreColorBrush, graphics, rectangle);
            }



            List <Cell[]> cellsList = new List <Cell[]>();

            Cell[] cells;
            int    x;
            int    y = 0;


            //
            // Get a table of column headers.
            //
            int[] columnHeaders = new int[this._columnColumns.Count];
            for (int i = 0; i < columnHeaders.Length; i++)
            {
                columnHeaders[i] = i;
            }
            Bamboo.DataStructures.Table columnHeadersTable = Bamboo.Sql2.Iterators.GroupByIterator.Group(table, columnHeaders);


            //
            // Calculate the width of the cell arrays (i.e., table rows).
            //
            int width = (this._columnColumns.Count == 0) ? this._rowColumns.Count + this._dataColumns.Count : this._rowColumns.Count + (columnHeadersTable.Rows.Count * this._dataColumns.Count);


            //
            // Create the column headers.
            //
            styleStack.PushTag("Header");
            backColor = (string)styleStack["BackColor"];
            foreColor = (string)styleStack["ForeColor"];
            System.Drawing.ContentAlignment textAlign = (System.Drawing.ContentAlignment)styleStack["TextAlign"];
            for (int i = 0; i < this._columnColumns.Count; i++)
            {
                cells = new Cell[width];
                cellsList.Add(cells);
                x = this._rowColumns.Count;
                Bamboo.DataStructures.Tuple row = null;
                for (int j = 0; j < columnHeadersTable.Rows.Count; j++)
                {
                    Bamboo.DataStructures.Tuple row2 = columnHeadersTable.Rows[j];
                    if (row == null || !row.Equals(0, row2, 0, i + 1))
                    {
                        //TODO push header
//						Bamboo.Css.StyleRule styleRule = GetStyle(styleSheet, "Header", this2._columnColumns, row2, i + 1);

                        cells[x].HasValue = true;
                        cells[x].Type     = CellType.Header;
                        cells[x].Colspan  = this._dataColumns.Count;
                        cells[x].Rowspan  = 1;
                        cells[x].Value    = row2[i];
//						if (styleRule == null)
//						{
                        cells[x].BackgroundColor = backColor;
                        cells[x].ForegroundColor = foreColor;
                        cells[x].TextAlign       = textAlign;
//						}
//						else
//						{
//							cells[x].BackgroundColor = (string)styleRule["BackColor"];
//							cells[x].ForegroundColor = (string)styleRule["ForeColor"];
//							cells[x].TextAlign = (System.Drawing.ContentAlignment)styleRule["TextAlign"];
//						}
                    }
                    else
                    {
                        int k     = 1;
                        int index = x - k;
                        k++;
                        while (cells[index].Colspan == 0)
                        {
                            index = x - k;
                            k++;
                        }
                        cells[index].Colspan += this._dataColumns.Count;
                    }
                    row = row2;
                    x  += this._dataColumns.Count;
                }
                y++;
            }

            cells = new Cell[width];
            cellsList.Add(cells);
            x = this._rowColumns.Count;
            for (int i = 0; i < Math.Max(columnHeadersTable.Rows.Count, 1); i++)
            {
                for (int k = 0; k < this._dataColumns.Count; k++)
                {
//					Bamboo.Css.StyleRule styleRule = GetStyle(styleSheet, new Statibase.Tuple(new object[] { this2._dataColumns[k] }), 0, 1);

                    cells[x].HasValue = true;
                    cells[x].Type     = CellType.Header;
                    cells[x].Colspan  = 1;
                    cells[x].Rowspan  = 1;
                    cells[x].Value    = this._dataColumns[k];
//					if (styleRule == null)
//					{
                    cells[x].BackgroundColor = backColor;
                    cells[x].ForegroundColor = foreColor;
                    cells[x].TextAlign       = textAlign;
//					}
//					else
//					{
//						cells[x].BackgroundColor = (string)styleRule["BackColor"];
//						cells[x].ForegroundColor = (string)styleRule["ForeColor"];
//						cells[x].TextAlign = (System.Drawing.ContentAlignment)styleRule["TextAlign"];
//					}
                    x++;
                }
            }
            y++;
            styleStack.PopTag();



            backColor = (string)styleStack["BackColor"];

            //
            // Add the empty cell if necessary.
            //
            if (this._rowColumns.Count > 0)
            {
                cellsList[0][0].HasValue        = true;
                cellsList[0][0].Type            = CellType.Empty;
                cellsList[0][0].Colspan         = this._rowColumns.Count;
                cellsList[0][0].Rowspan         = this._columnColumns.Count + 1;
                cellsList[0][0].BackgroundColor = backColor;
            }



            //
            // Get a table of row headers.
            //
            int[] rowHeadersPlusData = new int[this._columnColumns.Count + this._rowColumns.Count + this._dataColumns.Count];
            int   ii = 0;

            for (int i = 0; i < this._rowColumns.Count; i++)
            {
                rowHeadersPlusData[ii] = this._columnColumns.Count + i;
                ii++;
            }
            for (int i = 0; i < this._columnColumns.Count; i++)
            {
                rowHeadersPlusData[ii] = i;
                ii++;
            }
            for (int i = 0; i < this._dataColumns.Count; i++)
            {
                rowHeadersPlusData[ii] = this._columnColumns.Count + this._rowColumns.Count + i;;
                ii++;
            }
            table = Bamboo.Sql2.Iterators.OrderByIterator.Sort(table, rowHeadersPlusData);


            //
            // Fill in the row headers and data.
            //
            if (this._rowColumns.Count > 0)
            {
                int[] rowHeaderIndexes = new int[this._rowColumns.Count];
                int[] yPositions       = new int[columnHeadersTable.Rows.Count + 1];

                cells = new Cell[width];
                styleStack.PushTag("Header");
                backColor = (string)styleStack["BackColor"];
                foreColor = (string)styleStack["ForeColor"];
                textAlign = (System.Drawing.ContentAlignment)styleStack["TextAlign"];
                for (int j = 0; j < this._rowColumns.Count; j++)
                {
//					Bamboo.Css.StyleRule styleRule = GetStyle(styleSheet, table.Rows[0], this2._columnColumns.Count + j, 1);

                    cells[j].HasValue = true;
                    cells[j].Type     = CellType.Header;
                    cells[j].Colspan  = 1;
                    cells[j].Rowspan  = 1;
                    cells[j].Value    = table.Rows[0][this._columnColumns.Count + j];
//					if (styleRule == null)
//					{
                    cells[j].BackgroundColor = backColor;
                    cells[j].ForegroundColor = foreColor;
                    cells[j].TextAlign       = textAlign;
//					}
//					else
//					{
//						cells[j].BackgroundColor = (string)styleRule["BackColor"];
//						cells[j].ForegroundColor = (string)styleRule["ForeColor"];
//						cells[j].TextAlign = (System.Drawing.ContentAlignment)styleRule["TextAlign"];
//					}
                    rowHeaderIndexes[j] = cellsList.Count;
                }
                styleStack.PopTag();

                styleStack.PushTag("Cell");
                backColor = (string)styleStack["BackColor"];
                foreColor = (string)styleStack["ForeColor"];
                textAlign = (System.Drawing.ContentAlignment)styleStack["TextAlign"];
                for (int k = this._rowColumns.Count; k < width; k++)
                {
                    cells[k].HasValue        = true;
                    cells[k].Type            = CellType.Cell;
                    cells[k].Colspan         = 1;
                    cells[k].Rowspan         = 1;
                    cells[k].BackgroundColor = backColor;
                    cells[k].ForegroundColor = foreColor;
                    cells[k].TextAlign       = textAlign;
                }
                styleStack.PopTag();

                for (int l = 0; l < yPositions.Length; l++)
                {
                    yPositions[l] = cellsList.Count;
                }
                cellsList.Add(cells);


                Bamboo.DataStructures.Tuple previousRow = null;
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    Bamboo.DataStructures.Tuple row = table.Rows[i];

                    if (previousRow != null)
                    {
                        for (int j = 0; j < this._rowColumns.Count; j++)
                        {
                            //TODO use an array of values instead of fetching the last row.
                            if (!cellsList[rowHeaderIndexes[j]][j].Value.Equals(row[this._columnColumns.Count + j]))
                            {
//								Bamboo.Css.StyleRule styleRule = GetStyle(styleSheet, row, this2._columnColumns.Count + j, 1);
                                styleStack.PushTag("Header");
                                backColor = (string)styleStack["BackColor"];
                                foreColor = (string)styleStack["ForeColor"];
                                textAlign = (System.Drawing.ContentAlignment)styleStack["TextAlign"];

                                // The row keys have changed.  Write a new row.
                                cells = new Cell[width];

                                cells[j].HasValue = true;
                                cells[j].Type     = CellType.Header;
                                cells[j].Colspan  = 1;
                                cells[j].Rowspan  = 1;
                                cells[j].Value    = row[this._columnColumns.Count + j];
//								if (styleRule == null)
//								{
                                cells[j].BackgroundColor = backColor;
                                cells[j].ForegroundColor = foreColor;
                                cells[j].TextAlign       = textAlign;
//								}
//								else
//								{
//									cells[j].BackgroundColor = (string)styleRule["BackColor"];
//									cells[j].ForegroundColor = (string)styleRule["ForeColor"];
//									cells[j].TextAlign = (System.Drawing.ContentAlignment)styleRule["TextAlign"];
//								}
                                rowHeaderIndexes[j] = cellsList.Count;

                                for (int k = 0; k < j; k++)
                                {
                                    cellsList[rowHeaderIndexes[k]][k].Rowspan++;
                                }
                                for (int k = (j + 1); k < this._rowColumns.Count; k++)
                                {
                                    cells[k].HasValue = true;
                                    cells[k].Type     = CellType.Header;
                                    cells[k].Colspan  = 1;
                                    cells[k].Rowspan  = 1;
                                    cells[k].Value    = row[this._columnColumns.Count + k];
//									if (styleRule == null)
//									{
                                    cells[k].BackgroundColor = backColor;
                                    cells[k].ForegroundColor = foreColor;
                                    cells[k].TextAlign       = textAlign;
//									}
//									else
//									{
//										cells[k].BackgroundColor = (string)styleRule["BackColor"];
//										cells[k].ForegroundColor = (string)styleRule["ForeColor"];
//										cells[k].TextAlign = (System.Drawing.ContentAlignment)styleRule["TextAlign"];
//									}
                                    rowHeaderIndexes[k] = cellsList.Count;
                                }
                                styleStack.PopTag();

                                styleStack.PushTag("Cell");
                                backColor = (string)styleStack["BackColor"];
                                foreColor = (string)styleStack["ForeColor"];
                                textAlign = (System.Drawing.ContentAlignment)styleStack["TextAlign"];
                                for (int k = this._rowColumns.Count; k < width; k++)
                                {
                                    cells[k].HasValue        = true;
                                    cells[k].Type            = CellType.Cell;
                                    cells[k].Colspan         = 1;
                                    cells[k].Rowspan         = 1;
                                    cells[k].BackgroundColor = backColor;
                                    cells[k].ForegroundColor = foreColor;
                                    cells[k].TextAlign       = textAlign;
                                }
                                styleStack.PopTag();

                                for (int l = 0; l < yPositions.Length; l++)
                                {
                                    yPositions[l] = cellsList.Count;
                                }
                                cellsList.Add(cells);
                                break;
                            }
                        }
                    }



                    if (columnHeadersTable.Rows.Count == 0)
                    {
                        int columnNumber = 0;
                        x = (columnNumber * this._dataColumns.Count) + this._rowColumns.Count;
                        y = yPositions[columnNumber];

                        while (y >= cellsList.Count)
                        {
                            cells = new Cell[width];

                            for (int k = 0; k < this._rowColumns.Count; k++)
                            {
                                cellsList[rowHeaderIndexes[k]][k].Rowspan++;
                            }

                            styleStack.PushTag("Cell");
                            backColor = (string)styleStack["BackColor"];
                            foreColor = (string)styleStack["ForeColor"];
                            textAlign = (System.Drawing.ContentAlignment)styleStack["TextAlign"];
                            for (int k = this._rowColumns.Count; k < width; k++)
                            {
                                cells[k].HasValue        = true;
                                cells[k].Type            = CellType.Cell;
                                cells[k].Colspan         = 1;
                                cells[k].Rowspan         = 1;
                                cells[k].BackgroundColor = backColor;
                                cells[k].ForegroundColor = foreColor;
                                cells[k].TextAlign       = textAlign;
                            }
                            styleStack.PopTag();

                            cellsList.Add(cells);
                        }

                        for (int j = 0; j < this._dataColumns.Count; j++)
                        {
                            cellsList[y][x + j].Value = row[this._columnColumns.Count + this._rowColumns.Count + j];
                        }
                        yPositions[columnNumber]++;
                    }
                    else
                    {
                        for (int m = 0; m < columnHeadersTable.Rows.Count; m++)
                        {
                            Bamboo.DataStructures.Tuple columnRow = columnHeadersTable.Rows[m];
                            if (row.Equals(0, columnRow, 0, this._columnColumns.Count))
                            {
                                int columnNumber = m;
                                x = (columnNumber * this._dataColumns.Count) + this._rowColumns.Count;
                                y = yPositions[columnNumber];

                                while (y >= cellsList.Count)
                                {
                                    cells = new Cell[width];

                                    for (int k = 0; k < this._rowColumns.Count; k++)
                                    {
                                        cellsList[rowHeaderIndexes[k]][k].Rowspan++;
                                    }

                                    styleStack.PushTag("Cell");
                                    backColor = (string)styleStack["BackColor"];
                                    foreColor = (string)styleStack["ForeColor"];
                                    textAlign = (System.Drawing.ContentAlignment)styleStack["TextAlign"];
                                    for (int k = this._rowColumns.Count; k < width; k++)
                                    {
                                        cells[k].HasValue        = true;
                                        cells[k].Type            = CellType.Cell;
                                        cells[k].Colspan         = 1;
                                        cells[k].Rowspan         = 1;
                                        cells[k].BackgroundColor = backColor;
                                        cells[k].ForegroundColor = foreColor;
                                        cells[k].TextAlign       = textAlign;
                                    }
                                    styleStack.PopTag();

                                    cellsList.Add(cells);
                                }

                                for (int j = 0; j < this._dataColumns.Count; j++)
                                {
                                    cellsList[y][x + j].Value = row[this._columnColumns.Count + this._rowColumns.Count + j];
                                }
                                yPositions[columnNumber]++;
                                break;
                            }
                        }
                    }



                    previousRow = row;
                }
            }


            Render(graphics, palette, styleStack, cellsList, rectangle);



            styleStack.PopTag();
        }
Example #5
0
        protected override void RenderChart(System.Drawing.Graphics graphics, Palette palette, Bamboo.Css.StyleStack styleStack, System.Drawing.RectangleF rectangle, string title, Bamboo.DataStructures.Table table)
        {
            styleStack.PushTag("ScatterGraph");



            string axisForeColor = (string)styleStack["AxisForeColor"];
            string axisLineColor = (string)styleStack["AxisLineColor"];
            string backColor     = (string)styleStack["BackColor"];

            Bamboo.Css.Font font          = (Bamboo.Css.Font)styleStack["Font"];
            string          foreColor     = (string)styleStack["ForeColor"];
            string          gridLineColor = (string)styleStack["GridLineColor"];
            int             padding       = (int)styleStack["Padding"];
            float           pointRadius   = System.Convert.ToSingle(styleStack["PointRadius"]);
            float           tickSize      = System.Convert.ToSingle(styleStack["TickSize"]);

            string[] colors = (string[])styleStack["Colors"];

            //TODO put in stylesheet
            System.Drawing.Font titleFont = palette.Font(font.Name, font.Size * 1.2f, System.Drawing.FontStyle.Bold);

            //TODO put in stylesheet
            System.Drawing.Font axisTickFont  = palette.Font(font.Name, font.Size * .66f);            //TODO put in stylesheet.
            System.Drawing.Font axisTitleFont = palette.Font(font.Name, font.Size, System.Drawing.FontStyle.Bold);

            System.Drawing.Brush axisForeColorBrush = palette.Brush(axisForeColor);
            System.Drawing.Pen   axisLineColorPen   = palette.Pen(axisLineColor);
            System.Drawing.Pen   backColorPen       = palette.Pen(backColor);
            System.Drawing.Brush backColorBrush     = palette.Brush(backColor);
            System.Drawing.Brush foreColorBrush     = palette.Brush(foreColor);
            System.Drawing.Pen   gridLineColorPen   = palette.Pen(gridLineColor);



            // Background
            graphics.DrawRectangle(backColorPen, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
            graphics.FillRectangle(backColorBrush, rectangle);
            rectangle = new System.Drawing.RectangleF(rectangle.Left + padding, rectangle.Top + padding, rectangle.Width - padding - padding, rectangle.Height - padding - padding);

            // Title
            if (title != null)
            {
                rectangle = DrawTitle(title, titleFont, foreColorBrush, graphics, rectangle);
            }

            //TODO
            // Legend
            rectangle = new System.Drawing.RectangleF(rectangle.Left, rectangle.Top, rectangle.Width - padding - padding, rectangle.Height);



            string  xAxisTitle = table.Columns[0].ToString();
            decimal x0         = System.Convert.ToDecimal(table.Rows[0][0]);
            decimal x_min      = x0;
            decimal x_max      = x0;

            for (int i = 1; i < table.Rows.Count; i++)
            {
                decimal x = System.Convert.ToDecimal(table.Rows[i][0]);
                if (x > x_max)
                {
                    x_max = x;
                }
                else if (x < x_min)
                {
                    x_min = x;
                }
            }
            Range xRange          = new Range(x_min, x_max);
            bool  isXRangeNumeric = true;
            int   xAxisScale      = CalculateScale(xRange);
            int   xAxisPrecision  = Math.Max(0, 0 - xAxisScale);


            string  yAxisTitle = table.Columns[1].ToString();
            decimal y0         = System.Convert.ToDecimal(table.Rows[0][1]);
            decimal y_min      = y0;
            decimal y_max      = y0;

            for (int j = 1; j < table.Rows.Count; j++)
            {
                for (int i = 1; i < table.Columns.Count; i++)
                {
                    decimal y = System.Convert.ToDecimal(table.Rows[j][i]);
                    if (y > y_max)
                    {
                        y_max = y;
                    }
                    else if (y < y_min)
                    {
                        y_min = y;
                    }
                }
            }
            Range yRange             = new Range(y_min, y_max);
            bool  isYRangeNumeric    = true;
            int   yAxisScale         = CalculateScale(yRange);
            int   yAxisPrecision     = Math.Max(0, 0 - yAxisScale);
            float maxYTickLabelWidth = Math.Max(
                MeasureString(graphics, y_min.ToString("N" + yAxisPrecision), axisTickFont).Width,
                MeasureString(graphics, y_max.ToString("N" + yAxisPrecision), axisTickFont).Width);



            System.Drawing.SizeF yAxisTitleSize = MeasureString(graphics, yAxisTitle, axisTitleFont);
            System.Drawing.SizeF xAxisTitleSize = MeasureString(graphics, xAxisTitle, axisTitleFont);
            float maxXTickLabelHeight           = MeasureString(graphics, "A", axisTickFont).Height;

            // Y-Axis Title
            rectangle = DrawYAxisTitle(graphics, rectangle, yAxisTitle, axisTitleFont, yAxisTitleSize, xAxisTitleSize.Height + maxXTickLabelHeight + tickSize, axisForeColorBrush);

            // X-Axis Title
            rectangle = DrawXAxisTitle(graphics, rectangle, xAxisTitle, axisTitleFont, xAxisTitleSize, yAxisTitleSize.Height + maxYTickLabelWidth + tickSize, axisForeColorBrush);

            rectangle = new System.Drawing.RectangleF(rectangle.Left + maxYTickLabelWidth + tickSize, rectangle.Top, rectangle.Width - (maxYTickLabelWidth + tickSize), rectangle.Height - (maxXTickLabelHeight + tickSize));

            DrawYAxis(graphics, rectangle, YAxisTicks(rectangle, graphics, axisTickFont, yRange, isYRangeNumeric, yAxisScale, yAxisPrecision, table), tickSize, axisTickFont, gridLineColorPen, axisLineColorPen, axisForeColorBrush, true);
            DrawXAxis(graphics, rectangle, XAxisTicks(rectangle, graphics, axisTickFont, xRange, isXRangeNumeric, xAxisScale, xAxisPrecision, table), tickSize, axisTickFont, gridLineColorPen, axisLineColorPen, axisForeColorBrush, true);

            rectangle = new System.Drawing.RectangleF(rectangle.Left + 1, rectangle.Top, rectangle.Width - 1, rectangle.Height - 1);



            // Plot
//			graphics.FillRectangle(B.WhiteSmoke, canvasX, canvasY - canvasHeight, canvasWidth, canvasHeight);
            System.Drawing.PointF[] points = new System.Drawing.PointF[table.Rows.Count];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                Bamboo.DataStructures.Tuple row = table.Rows[i];
                points[i] = Coordinate(System.Convert.ToSingle(row[0]), System.Convert.ToSingle(xRange.Min), System.Convert.ToSingle(xRange.Max), rectangle.Left, rectangle.Width, System.Convert.ToSingle(row[1]), System.Convert.ToSingle(yRange.Min), System.Convert.ToSingle(yRange.Max), rectangle.Top, rectangle.Height);
            }
            DrawPoints(graphics, palette.Brush(colors[0]), points, pointRadius);



            styleStack.PopTag();
        }
Example #6
0
        protected override void RenderChart(System.Drawing.Graphics graphics, Palette palette, Bamboo.Css.StyleStack styleStack, System.Drawing.RectangleF rectangle, string title, Bamboo.DataStructures.Table table)
        {
            styleStack.PushTag("PieChart");



            string backColor = (string)styleStack["BackColor"];

            Bamboo.Css.Font font      = (Bamboo.Css.Font)styleStack["Font"];
            string          foreColor = (string)styleStack["ForeColor"];
            int             padding   = (int)styleStack["Padding"];

            string[] colors = (string[])styleStack["Colors"];

            //TODO put in stylesheet.
            System.Drawing.Font titleFont = palette.Font(font.Name, font.Size * 1.2f, System.Drawing.FontStyle.Bold);

            //TODO put in stylesheet.
            System.Drawing.Font labelFont = palette.Font(font.Name, font.Size, System.Drawing.FontStyle.Bold);

            System.Drawing.Pen   backColorPen   = palette.Pen(backColor);
            System.Drawing.Brush backColorBrush = palette.Brush(backColor);
            System.Drawing.Brush foreColorBrush = palette.Brush(foreColor);



            // Background
            graphics.DrawRectangle(backColorPen, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
            graphics.FillRectangle(backColorBrush, rectangle);
            rectangle = new System.Drawing.RectangleF(rectangle.Left + padding, rectangle.Top + padding, rectangle.Width - padding - padding, rectangle.Height - padding - padding);

            // Title
            if (title != null)
            {
                rectangle = DrawTitle(title, titleFont, foreColorBrush, graphics, rectangle);
            }

            //TODO
            // Legend
//			rectangle = new System.Drawing.RectangleF(rectangle.Left, rectangle.Top, rectangle.Width - padding - padding, rectangle.Height);



            Slice[] slices = new Slice[table.Rows.Count];
            float   sum    = 0;

            for (int i = 0; i < table.Rows.Count; i++)
            {
                Bamboo.DataStructures.Tuple row = table.Rows[i];

                slices[i].Label = row[0].ToString();

                float value = System.Convert.ToSingle(row[1]);
                slices[i].Value = value;

                sum += value;
            }
            sum /= 360;



            float startAngle = -90;

            for (int i = 0; i < slices.Length; i++)
            {
                slices[i].StartAngle = startAngle;

                float sweepAngle = slices[i].Value / sum;
                slices[i].SweepAngle = sweepAngle;

                double startPlusHalfSweepRadian = DegreeToRadian(startAngle + (sweepAngle / 2));
                slices[i].LabelCos = (float)Math.Cos(startPlusHalfSweepRadian);
                slices[i].LabelSin = (float)Math.Sin(startPlusHalfSweepRadian);

                System.Drawing.SizeF sizef = MeasureString(graphics, slices[i].Label, labelFont);
                slices[i].LabelWidth  = sizef.Width;
                slices[i].LabelHeight = sizef.Height;

                slices[i].Adjacent   = slices[i].LabelCos * slices[i].LabelWidth;
                slices[i].Opposite   = slices[i].LabelSin * slices[i].LabelHeight;
                slices[i].Hypotenuse = (float)Math.Sqrt((slices[i].Adjacent * slices[i].Adjacent) + (slices[i].Opposite * slices[i].Opposite));

                slices[i].Brush = palette.Brush(colors[i % colors.Length]);

                startAngle += sweepAngle;
            }



            float x_min = 0;
            float x_max = 0;
            float y_min = 0;
            float y_max = 0;

            for (int i = 0; i < slices.Length; i++)
            {
                x_min = Math.Min(x_min, slices[i].Adjacent);
                x_max = Math.Max(x_max, slices[i].Adjacent);
                y_min = Math.Min(y_min, slices[i].Opposite);
                y_max = Math.Max(y_max, slices[i].Opposite);
            }
            float x_offset = Math.Max(Math.Abs(x_min), x_max);
            float y_offset = Math.Max(Math.Abs(y_min), y_max);

            rectangle = new System.Drawing.RectangleF(rectangle.Left + x_offset, rectangle.Top + y_offset, rectangle.Width - (x_offset * 2), rectangle.Height - (y_offset * 2));



            float halfInnerWidth  = rectangle.Width / 2;
            float halfInnerHeight = rectangle.Height / 2;
            float radius          = Math.Min(halfInnerWidth, halfInnerHeight);
            float diameter        = radius + radius;
            float x = rectangle.Left + halfInnerWidth - radius;
            float y = rectangle.Top + halfInnerHeight - radius;

            if (radius > 0)
            {
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                for (int i = 0; i < slices.Length; i++)
                {
                    graphics.FillPie(slices[i].Brush, x, y, diameter, diameter, slices[i].StartAngle, slices[i].SweepAngle);

                    float radiusPlusHalfHypotenuse = radius + slices[i].Hypotenuse / 2;
                    float labelLeft = x + radius - (slices[i].LabelWidth / 2) + (slices[i].LabelCos * radiusPlusHalfHypotenuse);
                    float labelTop  = y + radius - (slices[i].LabelHeight / 2) + (slices[i].LabelSin * radiusPlusHalfHypotenuse);

                    //TODO use a style for Brush
                    DrawString(graphics, slices[i].Label, labelFont, foreColorBrush, labelLeft, labelTop);
                }
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
            }



            styleStack.PopTag();
        }
Example #7
0
        protected void Render(System.Drawing.Graphics graphics, Palette palette, Bamboo.Css.StyleStack styleStack, List <Cell[]> cells, System.Drawing.RectangleF rectangle)
        {
            string borderColor = (string)styleStack["BorderColor"];
            int    borderWidth = (int)styleStack["BorderWidth"];

            Bamboo.Css.Font font             = (Bamboo.Css.Font)styleStack["Font"];
            bool            horizontalBorder = (bool)styleStack["HorizontalBorder"];
            bool            outerBorder      = (bool)styleStack["OuterBorder"];
            int             padding          = (int)styleStack["Padding"];
            int             precision        = (int)styleStack["Precision"];
            bool            verticalBorder   = (bool)styleStack["VerticalBorder"];

            System.Drawing.Pen borderColorPen = palette.Pen(borderColor);



            int columns = (cells.Count == 0) ? 0 : cells[0].Length;
            int rows    = cells.Count;

            bool  tooBig       = true;
            float excessWidth  = 0;
            float excessHeight = 0;

            Bamboo.Css.Font gridFont = new Bamboo.Css.Font(font.Name, font.Size, font.Bold);
//TODO DELETE			float gridFontSize = font.Size;
            while (tooBig && gridFont.Size > 0)
            {
                //
                // Prepare the grid for painting.
                //
                int cellLeft = (int)Math.Ceiling(rectangle.Left);
                int cellTop  = (int)Math.Ceiling(rectangle.Top);

                if (outerBorder)
                {
                    cellLeft += borderWidth;
                    cellTop  += borderWidth;
                }

                for (int y = 0; y < rows; y++)
                {
                    for (int x = 0; x < columns; x++)
                    {
                        object value           = cells[y][x].Value;
                        int    colspan         = cells[y][x].Colspan;
                        int    rowspan         = cells[y][x].Rowspan;
                        int    colspanMinusOne = colspan - 1;
                        int    rowspanMinusOne = rowspan - 1;



                        //
                        // Read:  Value
                        //
                        // Write: Text
                        //
                        if (value == null)
                        {
                            cells[y][x].Text = String.Empty;
                        }
                        else if (value is double ||
                                 value is decimal ||
                                 value is float ||
                                 value is int ||
                                 value is long)
                        {
                            double d = System.Convert.ToDouble(value);
                            //TODO
                            //						if(this._show_parentheses && n < 0)
                            //						{
                            //							return "(" + Math.Abs(d).ToString() + ")";
                            //						}
                            //						else
                            {
                                cells[y][x].Text = d.ToString("N" + precision);
                            }
                        }
                        else
                        {
                            cells[y][x].Text = value.ToString();
                        }



                        //
                        // Read:  HasValue
                        //        Text
                        //        Style
                        //        Rowspan
                        //        Colspan
                        //
                        // Write: MeasuredWidth
                        //        MeasuredHeight
                        //        BorderColspan (forward)
                        //        BorderRowspan (forward)
                        //        BackgroundColspan (forward)
                        //        BackgroundRowspan (forward)
                        //
                        if (cells[y][x].HasValue)
                        {
                            int backgroundColorArgb = palette.Color(cells[y][x].BackgroundColor).ToArgb();

                            // Measure the text in the cell.
                            System.Drawing.SizeF size = MeasureString(palette.Graphics, cells[y][x].Text, palette.Font(gridFont.Name, gridFont.Size));
                            size.Width  += padding + padding;
                            size.Height += padding + padding;
                            cells[y][x].MeasuredWidth  = size.Width;
                            cells[y][x].MeasuredHeight = size.Height;

                            for (int xx = 0; xx < colspan; xx++)
                            {
                                if (y + rowspanMinusOne < (rows - 1))
                                {
                                    if (horizontalBorder)
                                    {
                                        cells[y + rowspanMinusOne][x + xx].BorderColspan = 1;
                                    }
                                }
                            }

                            for (int yy = 0; yy < rowspan; yy++)
                            {
                                if (x + colspanMinusOne < (columns - 1))
                                {
                                    if (verticalBorder)
                                    {
                                        cells[y + yy][x + colspanMinusOne].BorderRowspan = 1;
                                    }
                                }
                            }

                            for (int xx = 0; xx < colspan; xx++)
                            {
                                for (int yy = 0; yy < rowspan; yy++)
                                {
                                    cells[y + yy][x + xx].BackgroundColspan   = 1;
                                    cells[y + yy][x + xx].BackgroundRowspan   = 1;
                                    cells[y + yy][x + xx].BackgroundColorArgb = backgroundColorArgb;
                                }
                            }
                        }
                    }
                }



                //
                // Make all the cells in the same column the same width.
                //
                // Read:  Colspan
                //        MeasuredWidth
                //
                // Write: ColumnWidth (x-forward)
                //        Left
                //        Colspan (x-forward)
                //
                for (int x = 0; x < columns; x++)
                {
                    int columnWidth = 0;

                    //
                    // Get greatest width.
                    //
                    for (int y = 0; y < rows; y++)
                    {
                        if (cells[y][x].Colspan == 1)
                        {
                            int w2 = (int)Math.Ceiling(cells[y][x].MeasuredWidth);
                            if (w2 > columnWidth)
                            {
                                columnWidth = w2;
                            }
                        }
                    }

                    for (int y = 0; y < rows; y++)
                    {
                        //
                        // Set width.
                        //
                        cells[y][x].ColumnWidth = columnWidth;
                        cells[y][x].Left        = cellLeft;

                        int colspan = cells[y][x].Colspan;
                        if (colspan > 1)
                        {
                            //
                            // Shift right.
                            //
                            cells[y][x + 1].Colspan       = colspan - 1;
                            cells[y][x + 1].MeasuredWidth = cells[y][x].MeasuredWidth - columnWidth;                             //TODO I'm not sure about this.
                        }
                    }

                    cellLeft += columnWidth;
                    if (verticalBorder && x < (columns - 1))
                    {
                        cellLeft += borderWidth;
                    }
                }



                //
                // Make all the cells in the same row the same height.
                //
                // Read:  Rowspan
                //        MeasuredHeight
                //
                // Write: RowHeight (y-forward)
                //        Top
                //        Rowspan (y-forward)
                //
                for (int y = 0; y < rows; y++)
                {
                    int rowHeight = 0;

                    //
                    // Get greatest height.
                    //
                    for (int x = 0; x < columns; x++)
                    {
                        if (cells[y][x].Rowspan == 1)
                        {
                            int h2 = (int)Math.Ceiling(cells[y][x].MeasuredHeight);
                            if (h2 > rowHeight)
                            {
                                rowHeight = h2;
                            }
                        }
                    }

                    for (int x = 0; x < columns; x++)
                    {
                        //
                        // Set height.
                        //
                        cells[y][x].RowHeight = rowHeight;
                        cells[y][x].Top       = cellTop;

                        int rowspan = cells[y][x].Rowspan;
                        if (rowspan > 1)
                        {
                            //
                            // Shift down.
                            //
                            cells[y + 1][x].Rowspan        = rowspan - 1;
                            cells[y + 1][x].MeasuredHeight = cells[y][x].MeasuredHeight - rowHeight;                             //TODO I'm not sure about this.
                        }
                    }

                    cellTop += rowHeight;
                    if (horizontalBorder && y < (rows - 1))
                    {
                        cellTop += borderWidth;
                    }
                }



                for (int y = 0; y < rows; y++)
                {
                    for (int x = 0; x < columns; x++)
                    {
                        int colspan             = cells[y][x].Colspan;
                        int colspanMinusOne     = colspan - 1;
                        int rowspan             = cells[y][x].Rowspan;
                        int rowspanMinusOne     = rowspan - 1;
                        int borderColspan       = cells[y][x].BorderColspan;
                        int borderRowspan       = cells[y][x].BorderRowspan;
                        int backgroundColspan   = cells[y][x].BackgroundColspan;
                        int backgroundRowspan   = cells[y][x].BackgroundRowspan;
                        int backgroundColorArgb = cells[y][x].BackgroundColorArgb;



                        //
                        // Read:  Colspan
                        //        Left (x-forward)
                        //        ColumnWidth (x-forward)
                        //
                        // Write: Width
                        //
                        if (colspan > 1)
                        {
                            cells[y][x].Width = cells[y][x + colspanMinusOne].Left + cells[y][x + colspanMinusOne].ColumnWidth - cells[y][x].Left;
                        }
                        else
                        {
                            cells[y][x].Width = cells[y][x].ColumnWidth;
                        }



                        //
                        // Read:  Rowspan
                        //        Top (y-forward)
                        //        RowHeight (y-forward)
                        //
                        // Write: Height
                        //
                        if (rowspan > 1)
                        {
                            cells[y][x].Height = cells[y + rowspanMinusOne][x].Top + cells[y + rowspanMinusOne][x].RowHeight - cells[y][x].Top;
                        }
                        else
                        {
                            cells[y][x].Height = cells[y][x].RowHeight;
                        }



                        //
                        // Read:  BorderColspan (x-forward)
                        //
                        // Write: BorderColspan
                        //
                        if (borderColspan > 0)
                        {
                            int length = borderColspan;
                            for (int xx = (x + 1); xx < columns; xx++)
                            {
                                if (cells[y][xx].BorderColspan > 0)
                                {
                                    length += cells[y][xx].BorderColspan;
                                    cells[y][xx].BorderColspan = 0;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            cells[y][x].BorderColspan = length;
                        }



                        //
                        // Read:  BorderRowspan (y-forward)
                        //
                        // Write: BorderRowspan
                        //
                        if (borderRowspan > 0)
                        {
                            int length = borderRowspan;
                            for (int yy = (y + 1); yy < rows; yy++)
                            {
                                if (cells[yy][x].BorderRowspan > 0)
                                {
                                    length += cells[yy][x].BorderRowspan;
                                    cells[yy][x].BorderRowspan = 0;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            cells[y][x].BorderRowspan = length;
                        }



                        if (backgroundColspan > 0)
                        {
                            //
                            // Read:  BackgroundColspan (x-forward)
                            //        BackgroundColor (x-forward)
                            //
                            // Write: BackgroundColspan (x-forward)
                            //
                            int length = backgroundColspan;
                            for (int xx = (x + 1); xx < columns; xx++)
                            {
                                if (cells[y][xx].BackgroundColspan > 0 &&
                                    backgroundColorArgb == cells[y][xx].BackgroundColorArgb)
                                {
                                    length += cells[y][xx].BackgroundColspan;
                                    cells[y][xx].BackgroundColspan = 0;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            backgroundColspan             = length;
                            cells[y][x].BackgroundColspan = length;
                        }



                        if (backgroundRowspan > 0)
                        {
                            //
                            // Read:  BackgroundRowspan (y-forward)
                            //        BackgroundColor (y-forward)
                            //
                            // Write: BackgroundRowspan (y-forward)
                            //
                            int length = backgroundRowspan;
                            for (int yy = (y + 1); yy < rows; yy++)
                            {
                                if (cells[yy][x].BackgroundRowspan > 0 &&
                                    backgroundColorArgb == cells[yy][x].BackgroundColorArgb)
                                {
                                    length += cells[yy][x].BackgroundRowspan;
                                    cells[yy][x].BackgroundRowspan = 0;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            backgroundRowspan             = length;
                            cells[y][x].BackgroundRowspan = length;
                        }



                        if (backgroundColspan == 0 || backgroundRowspan == 0)
                        {
                            //
                            // Read:  BackgroundColspan
                            //        BackgroundRowspan
                            //
                            // Write: BackgroundColspan
                            //        BackgroundRowspan
                            //
                            cells[y][x].BackgroundColspan = 0;
                            cells[y][x].BackgroundRowspan = 0;
                        }
                    }
                }



                //
                // Set outer border.
                //
                if (outerBorder)
                {
                    cellLeft += borderWidth;
                    cellTop  += borderWidth;
                }

                if (!_shrinkToFit)
                {
                    tooBig       = false;
                    excessWidth  = 0;
                    excessHeight = 0;
                    this.Width   = cellLeft - this.Left;
                    this.Height  = cellTop - this.Top;
                }
                else if (cellLeft < (rectangle.Left + rectangle.Width) && cellTop < (rectangle.Top + rectangle.Height))
                {
                    tooBig       = false;
                    excessWidth  = rectangle.Left + rectangle.Width - cellLeft;
                    excessHeight = rectangle.Top + rectangle.Height - cellTop;
                }
                else
                {
                    gridFont.Size -= 1;
                }
            }

            //TODO test for no data.
            //if (cells == null)
            //{
            //    //TODO paint it white.
            //    return;
            //}

            if (!tooBig)
            {
                columns = (cells.Count == 0) ? 0 : cells[0].Length;
                rows    = cells.Count;

                float x_padding = excessWidth / columns;
                float y_padding = excessHeight / rows;
                for (int y = 0; y < rows; y++)
                {
                    for (int x = 0; x < columns; x++)
                    {
                        cells[y][x].Left   += (int)Math.Ceiling(x * x_padding);
                        cells[y][x].Top    += (int)Math.Ceiling(y * y_padding);
                        cells[y][x].Width  += (int)Math.Ceiling(x_padding);
                        cells[y][x].Height += (int)Math.Ceiling(y_padding);
                    }
                    cells[y][columns - 1].Width = (int)Math.Ceiling(rectangle.Left) + (int)Math.Ceiling(rectangle.Width) - cells[y][columns - 1].Left;
                }
                for (int x = 0; x < columns; x++)
                {
                    cells[rows - 1][x].Height = (int)Math.Ceiling(rectangle.Top) + (int)Math.Ceiling(rectangle.Height) - cells[rows - 1][x].Top;
                }

                int halfBorderWidth = (borderWidth / 2);

                for (int y = 0; y < rows; y++)
                {
                    for (int x = 0; x < columns; x++)
                    {
                        int backgroundColspan = cells[y][x].BackgroundColspan;
                        int backgroundRowspan = cells[y][x].BackgroundRowspan;

                        // Background
                        if (backgroundColspan > 0 && backgroundRowspan > 0)
                        {
                            int backgroundColspanMinusOne = backgroundColspan - 1;
                            int backgroundRowspanMinusOne = backgroundRowspan - 1;
                            graphics.FillRectangle(palette.Brush(cells[y][x].BackgroundColor), cells[y][x].Left, cells[y][x].Top, cells[y][x + backgroundColspanMinusOne].Left + cells[y][x + backgroundColspanMinusOne].Width + borderWidth - cells[y][x].Left, cells[y + backgroundRowspanMinusOne][x].Top + cells[y + backgroundRowspanMinusOne][x].Height + borderWidth - cells[y][x].Top);
                        }
                    }
                }

                for (int y = 0; y < rows; y++)
                {
                    for (int x = 0; x < columns; x++)
                    {
                        //Borders
                        if (horizontalBorder && cells[y][x].BorderColspan > 0)
                        {
                            // Bottom border
                            float bottom = cells[y][x].Top + cells[y][x].Height + halfBorderWidth;
                            int   borderColspanMinusOne = cells[y][x].BorderColspan - 1;
                            graphics.DrawLine(borderColorPen, cells[y][x].Left, bottom, cells[y][x + borderColspanMinusOne].Left + cells[y][x + borderColspanMinusOne].Width + borderWidth - 1, bottom);
                        }
                        if (verticalBorder && cells[y][x].BorderRowspan > 0)
                        {
                            // Right border
                            float right = cells[y][x].Left + cells[y][x].Width + halfBorderWidth;
                            int   borderRowspanMinusOne = cells[y][x].BorderRowspan - 1;
                            graphics.DrawLine(borderColorPen, right, cells[y][x].Top, right, cells[y + borderRowspanMinusOne][x].Top + cells[y + borderRowspanMinusOne][x].Height + borderWidth - 1);
                        }

                        if (cells[y][x].Value != null && gridFont.Size > 0)
                        {
                            // Text
                            string text = cells[y][x].Text;
                            System.Drawing.Brush foregroundColorBrush = palette.Brush(cells[y][x].ForegroundColor);
                            System.Drawing.Font  textFont             = (gridFont.Bold) ? new System.Drawing.Font(gridFont.Name, gridFont.Size, System.Drawing.FontStyle.Bold) : new System.Drawing.Font(gridFont.Name, gridFont.Size);
                            float left;
                            float top;
                            switch (cells[y][x].TextAlign)
                            {
                            case System.Drawing.ContentAlignment.BottomCenter:
                            {
                                left = cells[y][x].Left + padding + ((cells[y][x].Width - cells[y][x].MeasuredWidth) / 2);
                                top  = cells[y][x].Top + padding + (cells[y][x].Height - cells[y][x].MeasuredHeight);
                                break;
                            }

                            case System.Drawing.ContentAlignment.BottomLeft:
                            {
                                left = cells[y][x].Left + padding;
                                top  = cells[y][x].Top + padding + (cells[y][x].Height - cells[y][x].MeasuredHeight);
                                break;
                            }

                            case System.Drawing.ContentAlignment.BottomRight:
                            {
                                left = cells[y][x].Left + padding + (cells[y][x].Width - cells[y][x].MeasuredWidth);
                                top  = cells[y][x].Top + padding + (cells[y][x].Height - cells[y][x].MeasuredHeight);
                                break;
                            }

                            case System.Drawing.ContentAlignment.MiddleCenter:
                            {
                                left = cells[y][x].Left + padding + ((cells[y][x].Width - cells[y][x].MeasuredWidth) / 2);
                                top  = cells[y][x].Top + padding + ((cells[y][x].Height - cells[y][x].MeasuredHeight) / 2);
                                break;
                            }

                            case System.Drawing.ContentAlignment.MiddleLeft:
                            {
                                left = cells[y][x].Left + padding;
                                top  = cells[y][x].Top + padding + ((cells[y][x].Height - cells[y][x].MeasuredHeight) / 2);
                                break;
                            }

                            case System.Drawing.ContentAlignment.MiddleRight:
                            {
                                left = cells[y][x].Left + padding + (cells[y][x].Width - cells[y][x].MeasuredWidth);
                                top  = cells[y][x].Top + padding + ((cells[y][x].Height - cells[y][x].MeasuredHeight) / 2);
                                break;
                            }

                            case System.Drawing.ContentAlignment.TopLeft:
                            {
                                left = cells[y][x].Left + padding;
                                top  = cells[y][x].Top + padding;
                                break;
                            }

                            case System.Drawing.ContentAlignment.TopCenter:
                            {
                                left = cells[y][x].Left + padding + ((cells[y][x].Width - cells[y][x].MeasuredWidth) / 2);
                                top  = cells[y][x].Top + padding;
                                break;
                            }

                            case System.Drawing.ContentAlignment.TopRight:
                            {
                                left = cells[y][x].Left + padding + (cells[y][x].Width - cells[y][x].MeasuredWidth);
                                top  = cells[y][x].Top + padding;
                                break;
                            }

                            default:
                            {
                                throw new System.Exception("Unknown ContentAlignment.");
                            }
                            }
                            DrawString(graphics, text, textFont, foregroundColorBrush, left, top);
                        }
                    }
                }



                if (outerBorder)
                {
                    // Left border
                    graphics.DrawLine(borderColorPen, rectangle.Left, rectangle.Top, rectangle.Left, rectangle.Top + rectangle.Height);

                    // Top border
                    graphics.DrawLine(borderColorPen, rectangle.Left, rectangle.Top, rectangle.Left + rectangle.Width, rectangle.Top);

                    // Right border
                    graphics.DrawLine(borderColorPen, rectangle.Left + rectangle.Width, rectangle.Top, rectangle.Left + rectangle.Width, rectangle.Top + rectangle.Height);

                    // Bottom border
                    graphics.DrawLine(borderColorPen, rectangle.Left, rectangle.Top + rectangle.Height, rectangle.Left + rectangle.Width, rectangle.Top + rectangle.Height);
                }
            }
        }