Beispiel #1
0
    void OnEnable()
    {
        Column = (int)RoundToNearest(transform.position.x, _cellSize);
        Row    = (int)RoundToNearest(transform.position.y, _cellSize);

        _levelSettings  = FindObjectOfType <LevelSettings>();
        _renderer       = GetComponent <Renderer>();
        _gameController = FindObjectOfType <GameController>();
        _cellAlignment  = GetComponent <CellAlignment>();
        _cellSize       = _cellAlignment.cellSize;
        _gameController.RegisterCell(this);
        _gameController.Dropped += HandleGameDrop;
        _initialScale            = transform.localScale;

        _initialColor = _levelSettings.cellColor;
        _fillColor    = _levelSettings.fillColor;
        _fillScale    = _levelSettings.fillScale;
        _fillSpeed    = _levelSettings.fillSpeed;

        _currentTargetColor  = _initialColor;
        _currentDesiredScale = _initialScale;



        Debug.Log($"Enabled new cell: {gameObject.name} with indexes [{Column}:{Row}]");
    }
Beispiel #2
0
        private CT_CellAlignment ConvertAlignment(CellAlignment a)
        {
            var res = new CT_CellAlignment
            {
                indent                   = (uint)a.Indent,
                indentSpecified          = a.Indent != 0,
                shrinkToFit              = a.ShrinkToFit,
                shrinkToFitSpecified     = a.ShrinkToFit,
                textRotation             = (uint)a.TextRotation,
                textRotationSpecified    = a.TextRotation != 0,
                wrapText                 = a.WrapText,
                wrapTextSpecified        = a.WrapText,
                justifyLastLine          = a.JustifyLastLine,
                justifyLastLineSpecified = a.JustifyLastLine
            };

            if (a.HAlign.HasValue)
            {
                res.horizontal          = (ST_HorizontalAlignment)a.HAlign.Value;
                res.horizontalSpecified = true;
            }

            if (a.VAlign.HasValue)
            {
                res.vertical          = (ST_VerticalAlignment)a.VAlign.Value;
                res.verticalSpecified = true;
            }

            return(res);
        }
        public EntityGrid <T> Align(Expression <Func <T, object> > field, CellAlignment alignment)
        {
            var hint = GetField(field);

            hint.CellAlignment = alignment;
            return(this);
        }
    public static Vector3 CellToPos(int cell, CellAlignment alignment, SceneLayer layer)
    {
        switch (alignment)
        {
        case CellAlignment.Bottom:
            return(CellToPosCBC(cell, layer));

        case CellAlignment.Left:
            return(CellToPosLCC(cell, layer));

        case CellAlignment.Right:
            return(CellToPosRCC(cell, layer));

        case CellAlignment.Top:
            return(CellToPosCTC(cell, layer));

        case CellAlignment.RandomInternal:
        {
            Vector3 b = new Vector3(UnityEngine.Random.Range(-0.3f, 0.3f), 0f, 0f);
            return(CellToPosCCC(cell, layer) + b);
        }

        default:
            return(CellToPosCCC(cell, layer));
        }
    }
Beispiel #5
0
 public void CopyFrom(DxfContentFormat from, CloneContext cloneContext)
 {
     if (from.TextStyle == null)
     {
         this.dxfTextStyle_0 = (DxfTextStyle)null;
     }
     else if (cloneContext.SourceModel == cloneContext.TargetModel)
     {
         this.dxfTextStyle_0 = from.dxfTextStyle_0;
     }
     else if (!cloneContext.TargetModel.TextStyles.TryGetValue(from.dxfTextStyle_0.Name, out this.dxfTextStyle_0))
     {
         if (cloneContext.ReferenceResolutionType == ReferenceResolutionType.CloneMissing)
         {
             this.dxfTextStyle_0 = (DxfTextStyle)from.dxfTextStyle_0.Clone(cloneContext);
             if (!cloneContext.CloneExact)
             {
                 cloneContext.TargetModel.TextStyles.Add(this.dxfTextStyle_0);
             }
         }
         else if (cloneContext.ReferenceResolutionType == ReferenceResolutionType.FailOnMissing)
         {
             throw new DxfException("Missing text style with name " + from.dxfTextStyle_0.Name + " in target model.");
         }
     }
     this.double_0         = from.double_0;
     this.cellAlignment_0  = from.cellAlignment_0;
     this.color_0          = from.color_0;
     this.dxfValueFormat_0 = from.dxfValueFormat_0.Clone();
     this.double_1         = from.double_1;
     this.short_0          = from.short_0;
     this.tableCellStylePropertyFlags_0 = from.tableCellStylePropertyFlags_0;
     this.tableCellStylePropertyFlags_1 = from.tableCellStylePropertyFlags_1;
     this.double_2 = from.double_2;
 }
Beispiel #6
0
 public ColumnItem(string field, double?minWidth, double?width, CellAlignment align, string stringFormat, string header)
 {
     Field        = field;
     MinWidth     = minWidth;
     Width        = width;
     Alignment    = align;
     StringFormat = stringFormat;
     Header       = header;
 }
Beispiel #7
0
 public ColumnItem(string field, double? minWidth, double? width, CellAlignment align, string stringFormat, string header)
 {
     Field = field;
     MinWidth = minWidth;
     Width = width;
     Alignment = align;
     StringFormat = stringFormat;
     Header = header;
 }
Beispiel #8
0
 CellAlignment CalcAlignment(CellAlignment a, TypeHelper th)
 {
     if (th != null && a == CellAlignment.Auto)
     {
         if (th.IsNumericType)
         {
             return(CellAlignment.Right);
         }
     }
     return(a);
 }
 private CellAlignment ConvertAlignment(CT_CellAlignment al)
 {
     CellAlignment a = new CellAlignment
     {
         Indent = al.indentSpecified ? (int)al.indent : 0,
         WrapText = al.wrapTextSpecified ? al.wrapText : false,
         TextRotation = al.textRotationSpecified ? (int)al.textRotation : 0,
         ShrinkToFit = al.shrinkToFitSpecified ? al.shrinkToFit : false,
         VAlign = al.verticalSpecified ? (VerticalAlignment)al.vertical : (VerticalAlignment?)null,
         HAlign = al.horizontalSpecified ? (HorizontalAlignment)al.horizontal : (HorizontalAlignment?)null,
         JustifyLastLine = al.justifyLastLineSpecified ? al.justifyLastLine : false
     };
     return a;
 }
        private CellAlignment ConvertAlignment(CT_CellAlignment al)
        {
            CellAlignment a = new CellAlignment
            {
                Indent          = al.indentSpecified ? (int)al.indent : 0,
                WrapText        = al.wrapTextSpecified ? al.wrapText : false,
                TextRotation    = al.textRotationSpecified ? (int)al.textRotation : 0,
                ShrinkToFit     = al.shrinkToFitSpecified ? al.shrinkToFit : false,
                VAlign          = al.verticalSpecified ? (VerticalAlignment)al.vertical : (VerticalAlignment?)null,
                HAlign          = al.horizontalSpecified ? (HorizontalAlignment)al.horizontal : (HorizontalAlignment?)null,
                JustifyLastLine = al.justifyLastLineSpecified ? al.justifyLastLine : false
            };

            return(a);
        }
Beispiel #11
0
        /// <summary>
        /// Parameterized standard constructor
        /// </summary>
        /// <param name="field"></param>
        /// <param name="minWidth"></param>
        /// <param name="width"></param>
        /// <param name="align"></param>
        /// <param name="stringFormat"></param>
        /// <param name="header"></param>
        /// <param name="isColumnVisible"></param>
        public ColumnItemViewModel(string field,
                                   double minWidth, double width,
                                   CellAlignment align, string stringFormat,
                                   string header,
                                   bool isColumnVisible = true)
            : this()
        {
            Field        = field;
            MinWidth     = minWidth;
            Width        = width;
            Alignment    = align;
            StringFormat = stringFormat;
            Header       = header;

            this.IsColumnVisible = isColumnVisible;
        }
Beispiel #12
0
 public GridColumn(
     string name,
     string label,
     int order,
     ICellRenderer <T> cellRenderer,
     CellAlignment cellAlignment = CellAlignment.Left,
     FieldInfo fieldInfo         = null,
     PropertyInfo propertyInfo   = null,
     bool removed = false)
 {
     Name          = name;
     Label         = Utils.SplitCamelCase(name);
     Order         = order;
     CellRenderer  = cellRenderer;
     CellAlignment = cellAlignment;
     FieldInfo     = fieldInfo;
     PropertyInfo  = propertyInfo;
     Removed       = removed;
 }
Beispiel #13
0
 public static Polyline?CreateRectangle(this Point3d pt, double length, double width, CellAlignment alignment,
                                        Vector3d dir)
 {
     return(CreateRectangle(pt.Convert2d(), length, width, alignment, dir.Convert2d()));
 }
Beispiel #14
0
 CellAlignment CalcAlignment(CellAlignment a, TypeHelper th)
 {
     if (th!=null && a == CellAlignment.Auto)
         if (th.IsNumericType)
             return CellAlignment.Right;
     return a;
 }
Beispiel #15
0
 /// <summary>
 /// 设置表格中所有单元格中文本为同一对齐方式
 /// </summary>
 /// <param name="table">表格对象</param>
 /// <param name="align">单元格对齐方式</param>
 public static void SetAlignment(this Table table, CellAlignment align)
 {
     table.SetAlignment(align, AllRows);
 }
Beispiel #16
0
            private static void mergeRows(Table table, int col, int rowIndexFirst, int rowIndexLast, CellAlignment align = CellAlignment.MiddleCenter)
            {
                if (rowIndexLast <= rowIndexFirst)
                {
                    return;
                }
                var mCells = CellRange.Create(table, rowIndexFirst, col, rowIndexLast, col);

                table.MergeCells(mCells);
                mCells.Alignment = align;
            }
Beispiel #17
0
 public ColumnItem(string field, double?minWidth, double?width, CellAlignment align, string stringFormat)
     : this(field, minWidth, width, align, stringFormat, field)
 {
 }
Beispiel #18
0
 public ColumnItem(string field, double? minWidth, double? width, CellAlignment align, string stringFormat)
     : this(field, minWidth, width, align, stringFormat, field)
 {
 }
Beispiel #19
0
 public ColumnItem(string field, double? minWidth, double? width, CellAlignment align)
     : this(field, minWidth, width, align, string.Empty, field)
 {
 }
Beispiel #20
0
        public static Polyline?CreateRectangle(this Point2d pt, double length, double width, CellAlignment alignment, Vector2d dir)
        {
            if (length < 0 || width < 0)
            {
                return(null);
            }
            var      pl  = PlRel.Start(pt).Add(0, width).Add(length, 0).Add(0, -width).Create();
            Vector3d vec = default;

            switch (alignment)
            {
            case CellAlignment.BottomLeft:
                vec = new Vector3d();
                break;

            case CellAlignment.TopLeft:
                vec = new Vector3d(0, -width, 0);
                break;

            case CellAlignment.TopCenter:
                vec = new Vector3d(-length * 0.5, -width, 0);
                break;

            case CellAlignment.TopRight:
                vec = new Vector3d(-length, -width, 0);
                break;

            case CellAlignment.MiddleLeft:
                vec = new Vector3d(0, -width * 0.5, 0);
                break;

            case CellAlignment.MiddleCenter:
                vec = new Vector3d(-length * 0.5, -width * 0.5, 0);
                break;

            case CellAlignment.MiddleRight:
                vec = new Vector3d(-length, -width * 0.5, 0);
                break;

            case CellAlignment.BottomCenter:
                vec = new Vector3d(-length * 0.5, 0, 0);
                break;

            case CellAlignment.BottomRight:
                vec = new Vector3d(-length, 0, 0);
                break;
            }

            var move   = Matrix3d.Displacement(vec);
            var rotate = Matrix3d.Rotation(dir.Angle, Vector3d.ZAxis, pt.Convert3d() + vec);
            var matrix = move.PostMultiplyBy(rotate);

            pl.TransformBy(matrix);
            return(pl);
        }
Beispiel #21
0
 internal void method_2(CellAlignment cellAlignment)
 {
     this.cellAlignment_0 = cellAlignment;
 }
Beispiel #22
0
        public override void Render(Report report, Flow fm, DataContext dataContext)
        {
            HashSet<String> fieldNames = new HashSet<string>();

            foreach (var c in Columns)
            {
                if (c.DataField != "#")
                    fieldNames.Add(c.DataField);
                if (c.AggregateWeightDataField != null)
                    fieldNames.Add(c.AggregateWeightDataField);
            }

            foreach (var g in Groups)
                foreach (var sc in g.GroupByColumns)
                    fieldNames.Add(sc.DataField);

            var data = dataContext.CreateTable(DataTable, fieldNames.ToArray());

            for (int i = 0; i < Columns.Length; i++)
            {
                Columns[i]._Index = i;
                Columns[i]._DataFieldIndex = data.GetColumnIndex(Columns[i].DataField);

                if (Columns[i].AggregateWeightDataField != null)
                {
                    Columns[i]._AggregateWeightDataFieldIndex = data.GetColumnIndex(Columns[i].AggregateWeightDataField);
                    if (Columns[i]._AggregateWeightDataFieldIndex == -1)
                        throw new InvalidOperationException(String.Format("Weight column '{0}' not found.", Columns[i].AggregateWeightDataField));
                }
            }

            List<SortColumn> sort = new List<SortColumn>();
            List<GroupData> groupData = new List<GroupData>();

            int gi = 0;
            if (Groups != null)
            {
                foreach (var g in Groups)
                {
                    var gd = new GroupData();
                    var gc = new List<SortColumn>();
                    if (g.GroupByColumns!=null)
                        foreach (var c in g.GroupByColumns)
                        {
                            var ci = data.GetColumnIndex(c.DataField);
                            if (ci != -1)
                                gc.Add(new SortColumn
                                {
                                    ColumnIndex = ci,
                                    SortDirection = c.SortDirection == SortDirection.None ? SortDirection.Ascending : c.SortDirection
                                });
                        }
                    gd.Columns = gc.ToArray();
                    sort.AddRange(gd.Columns);
                    gd.GroupIndex = gi++;
                    gd.Group = g;

                    if (g.CaptionFormat != null)
                    {
                        String[] names;
                        String format;
                        StringFormatHelper.PrepareFormatWithNames(g.CaptionFormat, out format, out names);

                        gd.PreparedCaptionFormat = format;
                        gd.PreparedCaptionColumns = names != null ? names.Select(a => data.GetColumnIndex(a)).ToArray() : null;
                    }

                    if (g.FooterFormat != null)
                    {
                        String[] names;
                        String format;
                        StringFormatHelper.PrepareFormatWithNames(g.FooterFormat, out format, out names);

                        gd.PreparedFooterFormat = format;
                        gd.PreparedFooterColumns = names!=null ? names.Select(a => data.GetColumnIndex(a)).ToArray() : null;
                    }

                    gd.GroupLevel = Groups.Length - gd.GroupIndex - 1;
                    gd.GroupOpen = false;
                    groupData.Add(gd);
                }
            }

            sort.AddRange(from c in Columns
                          where c.SortDirection != SortDirection.None
                          orderby c.SortIndex
                          select new SortColumn
                          {
                              ColumnIndex = c._DataFieldIndex,
                              SortDirection = c.SortDirection
                          });

            if (sort.Count != 0)
                data.Sort(sort.ToArray());

            var rows = data.Rows;
            var rect = fm.GetRect(Position, Columns.Length, 0);
            var pos = new RowCol { Col = rect.Col1, Row = rect.Row1 };
            var startRow = pos.Row;
            List<Cell> cells = new List<Cell>();

            object[] accumulator = new object[Columns.Length];
            decimal[] count = new decimal[Columns.Length];
            CellAlignment[] align = new CellAlignment[Columns.Length];

            for (int c = 0; c < Columns.Length; c++)
            {
                var th = Columns[c]._DataFieldIndex >= 0 ? data.TypeHelper[Columns[c]._DataFieldIndex] : null;
                if (th != null)
                    switch (Columns[c].AggregateFunction)
                    {
                        case AggregateFunction.Sum:
                            accumulator[c] = th.Math.Zero;
                            break;
                        case AggregateFunction.Count:
                            accumulator[c] = 0;
                            break;
                        case AggregateFunction.Avg:
                            if (Columns[c].AggregateWeightDataField != null)
                            {
                                count[c] = 0;
                                accumulator[c] = 0m;
                            }
                            else
                            {
                                accumulator[c] = th.Math.Zero;
                                count[c] = 0;
                            }
                            break;
                        case AggregateFunction.Product:
                            accumulator[c] = th.Math.One;
                            break;
                    }
                align[c] = th != null ? CalcAlignment(Columns[c].CellAlignment, th) : CellAlignment.Right;
            }

            Data.Row prevRow = null;
            for (int r = 0, rowIndex = 0; r <= rows.Length; r++, rowIndex++)
            {
                var row = r < rows.Length ? rows[r] : null;
                var closeToGroupLevel = row!=null ? groupData.Count : 0;

                if (prevRow != null && row != null)
                    for (int g = 0; g < groupData.Count; g++)
                        if (RowComparer.Compare(row, prevRow, groupData[g].Columns) != 0)
                        {
                            closeToGroupLevel = g;
                            break;
                        }

                //close higher groups first
                for (int g = groupData.Count - 1; g >= closeToGroupLevel; g--)
                {
                    var gd = groupData[g];
                    if (gd.GroupOpen)
                    {
                        gd.GroupOpen = false;
                        //close group
                        if (gd.Group.ShowFooter)
                        {
                            for (int c = 0; c < Columns.Length; c++)
                            {
                                if (Columns[c].AggregateFunction == AggregateFunction.Avg)
                                    gd.GroupAccumulator[c] = CalculateAggregate(Columns[c].AggregateFunction, gd.GroupAccumulator[c], gd.GroupCounter[c]);

                                var style = gd.GetFooterCellStyle(Columns[c].ColumnType);
                                switch (Columns[c].FooterType)
                                {
                                    case ColumnFooterType.FooterText:
                                        cells.Add(new Cell { Column = pos.Col + c, Row = pos.Row, FormattedValue = Columns[c].FooterText, Value = Columns[c].FooterText, CellStyleIndex = style, Alignment = Columns[c].FooterAlignment });
                                        break;
                                    case ColumnFooterType.AggregateValue:
                                        String fv = (Columns[c].FooterFormat != null) ? String.Format(Columns[c].FooterFormat, gd.GroupAccumulator[c]) : (gd.GroupAccumulator[c] != null ? gd.GroupAccumulator[c].ToString() : null);
                                        var al = Columns[c]._DataFieldIndex >= 0 ? CalcAlignment(Columns[c].FooterAlignment, data.TypeHelper[Columns[c]._DataFieldIndex]) : CellAlignment.Auto;
                                        cells.Add(new Cell { Column = pos.Col + c, Row = pos.Row, FormattedValue = fv, Value = gd.GroupAccumulator[c], CellStyleIndex = style, Alignment = al, Format = Columns[c].FooterFormat });
                                        break;
                                    case ColumnFooterType.GroupFooter:
                                        String gfv = gd.PreparedFooterColumns == null ? gd.PreparedFooterFormat : String.Format(gd.PreparedFooterFormat, prevRow.GetMany(gd.PreparedFooterColumns));
                                        var gal = Columns[c]._DataFieldIndex >= 0 ? CalcAlignment(Columns[c].FooterAlignment, data.TypeHelper[Columns[c]._DataFieldIndex]) : CellAlignment.Auto;
                                        cells.Add(new Cell { Column = pos.Col + c, Row = pos.Row, FormattedValue = gfv, Value = gfv, CellStyleIndex = style, Alignment = gal });
                                        break;
                                }
                                if (Columns[c].FooterColSpan > 1)
                                {
                                    report.MergedCells.Add(new Rect { Col1 = pos.Col + c, Col2 = pos.Col + c + Columns[c].FooterColSpan - 1, Row1 = pos.Row, Row2 = pos.Row });
                                    c += Columns[c].FooterColSpan - 1;
                                }
                            }
                            pos.Row++;
                        }
                    }
                }

                for (int g = 0; g < groupData.Count; g++) {
                    var gd = groupData[g];
                    //add row
                    if (row != null)
                    {
                        if (!gd.GroupOpen)
                        {
                            gd.GroupOpen = true;
                            rowIndex = 0;

                            if (gd.Group.ShowCaption)
                            {
                                String caption = gd.PreparedCaptionColumns == null ? gd.PreparedCaptionFormat : String.Format(gd.PreparedCaptionFormat, row.GetMany(gd.PreparedCaptionColumns));

                                cells.Add(new Cell { Column = pos.Col, Row = pos.Row, FormattedValue = caption, Value = caption, CellStyleIndex = gd.GetCaptionStyle()});
                                report.MergedCells.Add(new Rect { Col1 = pos.Col, Col2 = pos.Col + Columns.Length - 1, Row1 = pos.Row, Row2 = pos.Row });
                                pos.Row++;
                            }

                            if (gd.Group.ShowHeader)
                            {
                                for (int c = 0; c < Columns.Length; c++)
                                {
                                    var ht = Columns[c].HeaderText;
                                    var style = gd.GetHeaderCellStyle(Columns[c].ColumnType);
                                    var a = Columns[c]._DataFieldIndex >= 0 ? CalcAlignment(Columns[c].HeaderAlignment, data.TypeHelper[Columns[c]._DataFieldIndex]) : CellAlignment.Right;

                                    cells.Add(new Cell
                                    {
                                        Column = pos.Col + c,
                                        Row = pos.Row,
                                        FormattedValue = ht,
                                        Value = ht,
                                        CellStyleIndex = style,
                                        Alignment = a
                                    });
                                }
                                pos.Row++;
                            }

                            gd.GroupAccumulator = new object[Columns.Length];
                            gd.GroupCounter = new decimal[Columns.Length];

                            //reset group accumulator
                            for (int c = 0; c < Columns.Length; c++)
                            {
                                var th = Columns[c]._DataFieldIndex >= 0 ? data.TypeHelper[Columns[c]._DataFieldIndex] : null;
                                if (th != null)
                                    switch (Columns[c].AggregateFunction)
                                    {
                                        case AggregateFunction.Sum:
                                            gd.GroupAccumulator[c] = th.Math.Zero;
                                            break;
                                        case AggregateFunction.Count:
                                            gd.GroupAccumulator[c] = 0;
                                            break;
                                        case AggregateFunction.Avg:
                                            if (Columns[c].AggregateWeightDataField != null)
                                            {
                                                gd.GroupAccumulator[c] = 0m;
                                                gd.GroupCounter[c] = 0;
                                            }
                                            else
                                            {
                                                gd.GroupAccumulator[c] = th.Math.Zero;
                                                gd.GroupCounter[c] = 0;
                                            }
                                            break;
                                        case AggregateFunction.Product:
                                            gd.GroupAccumulator[c] = th.Math.One;
                                            break;
                                    }
                            }
                        }

                        for (int c = 0; c < Columns.Length; c++)
                        {
                            var dfi = Columns[c]._DataFieldIndex;
                            var v = row[dfi];

                            var th = dfi >= 0 ? data.TypeHelper[dfi] : null;
                            if (th != null)
                            {
                                switch (Columns[c].AggregateFunction)
                                {
                                    case AggregateFunction.Sum:
                                        gd.GroupAccumulator[c] = th.Math.SumNullAsZero(gd.GroupAccumulator[c], v);
                                        break;
                                    case AggregateFunction.Count:
                                        if (v != null)
                                            gd.GroupAccumulator[c] = (int)gd.GroupAccumulator[c] + 1;
                                        break;
                                    case AggregateFunction.Product:
                                        gd.GroupAccumulator[c] = th.Math.Multiply(gd.GroupAccumulator[c], v);
                                        break;
                                    case AggregateFunction.Avg:
                                        if (v != null)
                                        {
                                            if (Columns[c].AggregateWeightDataField != null)
                                            {
                                                var w = row[Columns[c]._AggregateWeightDataFieldIndex];
                                                if (w != null)
                                                {
                                                    var wd = Convert.ToDecimal(w);
                                                    var wv = wd * Convert.ToDecimal(v);
                                                    gd.GroupAccumulator[c] = (decimal)gd.GroupAccumulator[c] + wv;
                                                    gd.GroupCounter[c] += wd;
                                                }
                                            }
                                            else
                                            {
                                                gd.GroupAccumulator[c] = th.Math.Sum(gd.GroupAccumulator[c], v);
                                                ++gd.GroupCounter[c];
                                            }
                                        }
                                        break;
                                    case AggregateFunction.Min:
                                        if (gd.GroupAccumulator[c] == null)
                                            gd.GroupAccumulator[c] = v;
                                        else if (v != null)
                                            gd.GroupAccumulator[c] = th.Math.Min(gd.GroupAccumulator[c], v);
                                        break;
                                    case AggregateFunction.Max:
                                        if (gd.GroupAccumulator[c] == null)
                                            gd.GroupAccumulator[c] = v;
                                        else if (v != null)
                                            gd.GroupAccumulator[c] = th.Math.Max(gd.GroupAccumulator[c], v);
                                        break;
                                }
                            }

                            switch (Columns[c].CellDisplayMode)
                            {
                                default:
                                case CellDisplayMode.Normal:
                                    v = Columns[c].DataField == "#" ? rowIndex + 1 : row[dfi];
                                    break;
                                case CellDisplayMode.RowNumber:
                                    v = rowIndex + 1;
                                    break;
                                case CellDisplayMode.AccumulatorValue:
                                    v = CalculateAggregate(Columns[c].AggregateFunction, gd.GroupAccumulator[c], gd.GroupCounter[c]);
                                    break;
                            }

                            CellStyle addStyle = null;
                            if (Columns[c].ConditionalFormatting != null)
                                addStyle = Columns[c].ConditionalFormatting(v);

                            String fv = (Columns[c].Format != null) ? String.Format(Columns[c].Format, v) : (v != null ? v.ToString() : null);

                            if (g + 1 == groupData.Count)
                                cells.Add(new Cell
                                {
                                    Column = pos.Col + c,
                                    Row = pos.Row,
                                    Value = v,
                                    FormattedValue = fv,
                                    CellStyleIndex = Columns[c].ColumnType == TableColumnType.HeaderColumn ? CellStyleIndex.TableRowHeader : Columns[c].ColumnType == TableColumnType.FooterColumn ? CellStyleIndex.TableRowFooter : CellStyleIndex.TableRow,
                                    Alignment = align[c],
                                    Format = Columns[c].Format,
                                    CustomStyle = addStyle
                                });
                        }

                        if (g + 1 == groupData.Count)
                            pos.Row++;
                    }
                }

                prevRow = row;
            }

            fm.GetRect(RowCol.Zero, 0, pos.Row - startRow);

            report.Cells.AddRange(cells);
        }
Beispiel #23
0
        public ColumnBuilder <TModel> Column(Expression <Func <TModel, object> > expression, string caption, CellAlignment alignment = CellAlignment.Left)
        {
            var member = expression.Body as MemberExpression ?? (MemberExpression)(((UnaryExpression)expression.Body).Operand);
            var name   = member.Member.Name;

            _columns.Add(new ColumnDef {
                Name      = name,
                Caption   = caption,
                Alignment = alignment
            });

            return(this);
        }
Beispiel #24
0
 public ColumnItem(string field, double?minWidth, double?width, CellAlignment align)
     : this(field, minWidth, width, align, string.Empty, field)
 {
 }
Beispiel #25
0
 /// <summary>
 /// 设置单元格中文本的对齐方式
 /// </summary>
 /// <param name="table">表格对象</param>
 /// <param name="align">单元格对齐方式</param>
 /// <param name="rowType">行的标志位</param>
 public static void SetAlignment(this Table table, CellAlignment align, RowType rowType)
 {
     table.SetAlignment(align, (int)rowType);
 }
Beispiel #26
0
        public override void Render(Report report, Flow fm, DataContext dataContext)
        {
            HashSet <String> fieldNames = new HashSet <string>();

            foreach (var c in Columns)
            {
                if (c.DataField != "#")
                {
                    fieldNames.Add(c.DataField);
                }
                if (c.AggregateWeightDataField != null)
                {
                    fieldNames.Add(c.AggregateWeightDataField);
                }
            }

            foreach (var g in Groups)
            {
                foreach (var sc in g.GroupByColumns)
                {
                    fieldNames.Add(sc.DataField);
                }
            }

            var data = dataContext.CreateTable(DataTable, fieldNames.ToArray());


            for (int i = 0; i < Columns.Length; i++)
            {
                Columns[i]._Index          = i;
                Columns[i]._DataFieldIndex = data.GetColumnIndex(Columns[i].DataField);

                if (Columns[i].AggregateWeightDataField != null)
                {
                    Columns[i]._AggregateWeightDataFieldIndex = data.GetColumnIndex(Columns[i].AggregateWeightDataField);
                    if (Columns[i]._AggregateWeightDataFieldIndex == -1)
                    {
                        throw new InvalidOperationException(String.Format("Weight column '{0}' not found.", Columns[i].AggregateWeightDataField));
                    }
                }
            }



            List <SortColumn> sort      = new List <SortColumn>();
            List <GroupData>  groupData = new List <GroupData>();

            int gi = 0;

            if (Groups != null)
            {
                foreach (var g in Groups)
                {
                    var gd = new GroupData();
                    var gc = new List <SortColumn>();
                    if (g.GroupByColumns != null)
                    {
                        foreach (var c in g.GroupByColumns)
                        {
                            var ci = data.GetColumnIndex(c.DataField);
                            if (ci != -1)
                            {
                                gc.Add(new SortColumn
                                {
                                    ColumnIndex   = ci,
                                    SortDirection = c.SortDirection == SortDirection.None ? SortDirection.Ascending : c.SortDirection
                                });
                            }
                        }
                    }
                    gd.Columns = gc.ToArray();
                    sort.AddRange(gd.Columns);
                    gd.GroupIndex = gi++;
                    gd.Group      = g;

                    if (g.CaptionFormat != null)
                    {
                        String[] names;
                        String   format;
                        StringFormatHelper.PrepareFormatWithNames(g.CaptionFormat, out format, out names);

                        gd.PreparedCaptionFormat  = format;
                        gd.PreparedCaptionColumns = names != null?names.Select(a => data.GetColumnIndex(a)).ToArray() : null;
                    }

                    if (g.FooterFormat != null)
                    {
                        String[] names;
                        String   format;
                        StringFormatHelper.PrepareFormatWithNames(g.FooterFormat, out format, out names);

                        gd.PreparedFooterFormat  = format;
                        gd.PreparedFooterColumns = names != null?names.Select(a => data.GetColumnIndex(a)).ToArray() : null;
                    }

                    gd.GroupLevel = Groups.Length - gd.GroupIndex - 1;
                    gd.GroupOpen  = false;
                    groupData.Add(gd);
                }
            }

            sort.AddRange(from c in Columns
                          where c.SortDirection != SortDirection.None
                          orderby c.SortIndex
                          select new SortColumn
            {
                ColumnIndex   = c._DataFieldIndex,
                SortDirection = c.SortDirection
            });

            if (sort.Count != 0)
            {
                data.Sort(sort.ToArray());
            }

            var rows = data.Rows;
            var rect = fm.GetRect(Position, Columns.Length, 0);
            var pos  = new RowCol {
                Col = rect.Col1, Row = rect.Row1
            };
            var         startRow = pos.Row;
            List <Cell> cells    = new List <Cell>();


            object[]        accumulator = new object[Columns.Length];
            decimal[]       count       = new decimal[Columns.Length];
            CellAlignment[] align       = new CellAlignment[Columns.Length];

            for (int c = 0; c < Columns.Length; c++)
            {
                var th = Columns[c]._DataFieldIndex >= 0 ? data.TypeHelper[Columns[c]._DataFieldIndex] : null;
                if (th != null)
                {
                    switch (Columns[c].AggregateFunction)
                    {
                    case AggregateFunction.Sum:
                        accumulator[c] = th.Math.Zero;
                        break;

                    case AggregateFunction.Count:
                        accumulator[c] = 0;
                        break;

                    case AggregateFunction.Avg:
                        if (Columns[c].AggregateWeightDataField != null)
                        {
                            count[c]       = 0;
                            accumulator[c] = 0m;
                        }
                        else
                        {
                            accumulator[c] = th.Math.Zero;
                            count[c]       = 0;
                        }
                        break;

                    case AggregateFunction.Product:
                        accumulator[c] = th.Math.One;
                        break;
                    }
                }
                align[c] = th != null?CalcAlignment(Columns[c].CellAlignment, th) : CellAlignment.Right;
            }

            Data.Row prevRow = null;
            for (int r = 0, rowIndex = 0; r <= rows.Length; r++, rowIndex++)
            {
                var row = r < rows.Length ? rows[r] : null;
                var closeToGroupLevel = row != null ? groupData.Count : 0;

                if (prevRow != null && row != null)
                {
                    for (int g = 0; g < groupData.Count; g++)
                    {
                        if (RowComparer.Compare(row, prevRow, groupData[g].Columns) != 0)
                        {
                            closeToGroupLevel = g;
                            break;
                        }
                    }
                }

                //close higher groups first
                for (int g = groupData.Count - 1; g >= closeToGroupLevel; g--)
                {
                    var gd = groupData[g];
                    if (gd.GroupOpen)
                    {
                        gd.GroupOpen = false;
                        //close group
                        if (gd.Group.ShowFooter)
                        {
                            for (int c = 0; c < Columns.Length; c++)
                            {
                                if (Columns[c].AggregateFunction == AggregateFunction.Avg)
                                {
                                    gd.GroupAccumulator[c] = CalculateAggregate(Columns[c].AggregateFunction, gd.GroupAccumulator[c], gd.GroupCounter[c]);
                                }

                                var style = gd.GetFooterCellStyle(Columns[c].ColumnType);
                                switch (Columns[c].FooterType)
                                {
                                case ColumnFooterType.FooterText:
                                    cells.Add(new Cell {
                                        Column = pos.Col + c, Row = pos.Row, FormattedValue = Columns[c].FooterText, Value = Columns[c].FooterText, CellStyleIndex = style, Alignment = Columns[c].FooterAlignment
                                    });
                                    break;

                                case ColumnFooterType.AggregateValue:
                                    String fv = (Columns[c].FooterFormat != null) ? String.Format(Columns[c].FooterFormat, gd.GroupAccumulator[c]) : (gd.GroupAccumulator[c] != null ? gd.GroupAccumulator[c].ToString() : null);
                                    var    al = Columns[c]._DataFieldIndex >= 0 ? CalcAlignment(Columns[c].FooterAlignment, data.TypeHelper[Columns[c]._DataFieldIndex]) : CellAlignment.Auto;
                                    cells.Add(new Cell {
                                        Column = pos.Col + c, Row = pos.Row, FormattedValue = fv, Value = gd.GroupAccumulator[c], CellStyleIndex = style, Alignment = al, Format = Columns[c].FooterFormat
                                    });
                                    break;

                                case ColumnFooterType.GroupFooter:
                                    String gfv = gd.PreparedFooterColumns == null ? gd.PreparedFooterFormat : String.Format(gd.PreparedFooterFormat, prevRow.GetMany(gd.PreparedFooterColumns));
                                    var    gal = Columns[c]._DataFieldIndex >= 0 ? CalcAlignment(Columns[c].FooterAlignment, data.TypeHelper[Columns[c]._DataFieldIndex]) : CellAlignment.Auto;
                                    cells.Add(new Cell {
                                        Column = pos.Col + c, Row = pos.Row, FormattedValue = gfv, Value = gfv, CellStyleIndex = style, Alignment = gal
                                    });
                                    break;
                                }
                                if (Columns[c].FooterColSpan > 1)
                                {
                                    report.MergedCells.Add(new Rect {
                                        Col1 = pos.Col + c, Col2 = pos.Col + c + Columns[c].FooterColSpan - 1, Row1 = pos.Row, Row2 = pos.Row
                                    });
                                    c += Columns[c].FooterColSpan - 1;
                                }
                            }
                            pos.Row++;
                        }
                    }
                }

                for (int g = 0; g < groupData.Count; g++)
                {
                    var gd = groupData[g];
                    //add row
                    if (row != null)
                    {
                        if (!gd.GroupOpen)
                        {
                            gd.GroupOpen = true;
                            rowIndex     = 0;

                            if (gd.Group.ShowCaption)
                            {
                                String caption = gd.PreparedCaptionColumns == null ? gd.PreparedCaptionFormat : String.Format(gd.PreparedCaptionFormat, row.GetMany(gd.PreparedCaptionColumns));

                                cells.Add(new Cell {
                                    Column = pos.Col, Row = pos.Row, FormattedValue = caption, Value = caption, CellStyleIndex = gd.GetCaptionStyle()
                                });
                                report.MergedCells.Add(new Rect {
                                    Col1 = pos.Col, Col2 = pos.Col + Columns.Length - 1, Row1 = pos.Row, Row2 = pos.Row
                                });
                                pos.Row++;
                            }

                            if (gd.Group.ShowHeader)
                            {
                                for (int c = 0; c < Columns.Length; c++)
                                {
                                    var ht    = Columns[c].HeaderText;
                                    var style = gd.GetHeaderCellStyle(Columns[c].ColumnType);
                                    var a     = Columns[c]._DataFieldIndex >= 0 ? CalcAlignment(Columns[c].HeaderAlignment, data.TypeHelper[Columns[c]._DataFieldIndex]) : CellAlignment.Right;

                                    cells.Add(new Cell
                                    {
                                        Column         = pos.Col + c,
                                        Row            = pos.Row,
                                        FormattedValue = ht,
                                        Value          = ht,
                                        CellStyleIndex = style,
                                        Alignment      = a
                                    });
                                }
                                pos.Row++;
                            }

                            gd.GroupAccumulator = new object[Columns.Length];
                            gd.GroupCounter     = new decimal[Columns.Length];

                            //reset group accumulator
                            for (int c = 0; c < Columns.Length; c++)
                            {
                                var th = Columns[c]._DataFieldIndex >= 0 ? data.TypeHelper[Columns[c]._DataFieldIndex] : null;
                                if (th != null)
                                {
                                    switch (Columns[c].AggregateFunction)
                                    {
                                    case AggregateFunction.Sum:
                                        gd.GroupAccumulator[c] = th.Math.Zero;
                                        break;

                                    case AggregateFunction.Count:
                                        gd.GroupAccumulator[c] = 0;
                                        break;

                                    case AggregateFunction.Avg:
                                        if (Columns[c].AggregateWeightDataField != null)
                                        {
                                            gd.GroupAccumulator[c] = 0m;
                                            gd.GroupCounter[c]     = 0;
                                        }
                                        else
                                        {
                                            gd.GroupAccumulator[c] = th.Math.Zero;
                                            gd.GroupCounter[c]     = 0;
                                        }
                                        break;

                                    case AggregateFunction.Product:
                                        gd.GroupAccumulator[c] = th.Math.One;
                                        break;
                                    }
                                }
                            }
                        }

                        for (int c = 0; c < Columns.Length; c++)
                        {
                            var dfi = Columns[c]._DataFieldIndex;
                            var v   = row[dfi];

                            var th = dfi >= 0 ? data.TypeHelper[dfi] : null;
                            if (th != null)
                            {
                                switch (Columns[c].AggregateFunction)
                                {
                                case AggregateFunction.Sum:
                                    gd.GroupAccumulator[c] = th.Math.SumNullAsZero(gd.GroupAccumulator[c], v);
                                    break;

                                case AggregateFunction.Count:
                                    if (v != null)
                                    {
                                        gd.GroupAccumulator[c] = (int)gd.GroupAccumulator[c] + 1;
                                    }
                                    break;

                                case AggregateFunction.Product:
                                    gd.GroupAccumulator[c] = th.Math.Multiply(gd.GroupAccumulator[c], v);
                                    break;

                                case AggregateFunction.Avg:
                                    if (v != null)
                                    {
                                        if (Columns[c].AggregateWeightDataField != null)
                                        {
                                            var w = row[Columns[c]._AggregateWeightDataFieldIndex];
                                            if (w != null)
                                            {
                                                var wd = Convert.ToDecimal(w);
                                                var wv = wd * Convert.ToDecimal(v);
                                                gd.GroupAccumulator[c] = (decimal)gd.GroupAccumulator[c] + wv;
                                                gd.GroupCounter[c]    += wd;
                                            }
                                        }
                                        else
                                        {
                                            gd.GroupAccumulator[c] = th.Math.Sum(gd.GroupAccumulator[c], v);
                                            ++gd.GroupCounter[c];
                                        }
                                    }
                                    break;

                                case AggregateFunction.Min:
                                    if (gd.GroupAccumulator[c] == null)
                                    {
                                        gd.GroupAccumulator[c] = v;
                                    }
                                    else if (v != null)
                                    {
                                        gd.GroupAccumulator[c] = th.Math.Min(gd.GroupAccumulator[c], v);
                                    }
                                    break;

                                case AggregateFunction.Max:
                                    if (gd.GroupAccumulator[c] == null)
                                    {
                                        gd.GroupAccumulator[c] = v;
                                    }
                                    else if (v != null)
                                    {
                                        gd.GroupAccumulator[c] = th.Math.Max(gd.GroupAccumulator[c], v);
                                    }
                                    break;
                                }
                            }

                            switch (Columns[c].CellDisplayMode)
                            {
                            default:
                            case CellDisplayMode.Normal:
                                v = Columns[c].DataField == "#" ? rowIndex + 1 : row[dfi];
                                break;

                            case CellDisplayMode.RowNumber:
                                v = rowIndex + 1;
                                break;

                            case CellDisplayMode.AccumulatorValue:
                                v = CalculateAggregate(Columns[c].AggregateFunction, gd.GroupAccumulator[c], gd.GroupCounter[c]);
                                break;
                            }

                            CellStyle addStyle = null;
                            if (Columns[c].ConditionalFormatting != null)
                            {
                                addStyle = Columns[c].ConditionalFormatting(v);
                            }

                            String fv = (Columns[c].Format != null) ? String.Format(Columns[c].Format, v) : (v != null ? v.ToString() : null);

                            if (g + 1 == groupData.Count)
                            {
                                cells.Add(new Cell
                                {
                                    Column         = pos.Col + c,
                                    Row            = pos.Row,
                                    Value          = v,
                                    FormattedValue = fv,
                                    CellStyleIndex = Columns[c].ColumnType == TableColumnType.HeaderColumn ? CellStyleIndex.TableRowHeader : Columns[c].ColumnType == TableColumnType.FooterColumn ? CellStyleIndex.TableRowFooter : CellStyleIndex.TableRow,
                                    Alignment      = align[c],
                                    Format         = Columns[c].Format,
                                    CustomStyle    = addStyle
                                });
                            }
                        }

                        if (g + 1 == groupData.Count)
                        {
                            pos.Row++;
                        }
                    }
                }

                prevRow = row;
            }

            fm.GetRect(RowCol.Zero, 0, pos.Row - startRow);

            report.Cells.AddRange(cells);
        }
        public static int GetStyleIndex(SpreadsheetDocument document, int numFmt, int font, int fill, int border, CellAlignment alignment, bool hidden, bool locked)
        {
            XElement xf = new XElement(S.xf, new XAttribute(NoNamespace.numFmtId, numFmt),
                new XAttribute(NoNamespace.fontId, font), new XAttribute(NoNamespace.fillId, fill),
                new XAttribute(NoNamespace.borderId, border), new XAttribute(NoNamespace.xfId, 0),
                new XAttribute(NoNamespace.applyNumberFormat, (numFmt == 0) ? 0 : 1),
                new XAttribute(NoNamespace.applyFont, (font == 0) ? 0 : 1),
                new XAttribute(NoNamespace.applyFill, (fill == 0) ? 0 : 1),
                new XAttribute(NoNamespace.applyBorder, (border == 0) ? 0 : 1));
            if (alignment != null)
            {
                xf.Add(new XAttribute(NoNamespace.applyAlignment, "1"));
                xf.Add(alignment.GetXElement());
            }
            else
                xf.Add(new XAttribute(NoNamespace.applyAlignment, "0"));
            if (hidden || locked)
            {
                XElement prot = new XElement(S.protection);
                if (hidden)
                    prot.Add(new XAttribute(NoNamespace.hidden, true));
                if (locked)
                    prot.Add(new XAttribute(NoNamespace.locked, true));
                xf.Add(prot);
                xf.Add(new XAttribute(NoNamespace.applyProtection, "1"));
            }
            else
                xf.Add(new XAttribute(NoNamespace.applyProtection, "0"));

            XDocument styles = document.WorkbookPart.WorkbookStylesPart.GetXDocument();
            XElement cellXfs = styles.Root.Element(S.cellXfs);
            int index = Array.FindIndex(cellXfs.Elements(S.xf).ToArray(),
                z => XElement.DeepEquals(z, xf));
            if (index != -1)
                return index;
            cellXfs.Add(xf);
            cellXfs.Attribute(NoNamespace.count).Value = cellXfs.Elements(S.xf).Count().ToString();
            document.WorkbookPart.WorkbookStylesPart.PutXDocument();
            return cellXfs.Elements(S.xf).Count() - 1;
        }