DevExpress MVC Pivotgrid:如何在分组级别上进行不同的汇总/聚合

时间:2016-03-17 19:08:03

标签: model-view-controller grid devexpress pivot aggregation

我有一个DevExpress MVC PivotGrid。它有一个标题,最高级别是Quarter,然后是Months,然后是Weeks。传递给网格的数据需要对本周的数据求和,而平均每月的周数据,并平均该季度的数据(周或月)。

以下是网格的屏幕截图:

enter image description here

我希望201632,201633,201634,201635周为0.1,但2016年7月也应为0.1(因为这是平均值),9月也是如此。那么2016年第二季度,2016年第四季度也应该有0.1(这是平均值)。

以下是网格字段的代码

public class prog6c
{
    public static void main (String [] args)
{
    int x = 1;
    while (x<5000){
        for (x=1; x<5; ++x)
            System.out.print(" "+x);

    }
}
}

我尝试使用

settings.Fields.Add(field =>
{
    field.Area = PivotArea.ColumnArea;
    field.AreaIndex = 0;
    field.TotalsVisibility = PivotTotalsVisibility.None;
    field.CellStyle.Wrap = DefaultBoolean.False;
    field.Width = 150;
    field.Caption = "Quarter Year";
    field.FieldName = "YYYYQ";
    field.CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
    field.Options.AllowSort = DefaultBoolean.False;
    field.Options.AllowFilter = DefaultBoolean.False;
});

settings.Fields.Add(field =>
{
    field.Area = PivotArea.ColumnArea;
    field.AreaIndex = 1;
    field.TotalsVisibility = PivotTotalsVisibility.None;
    field.CellStyle.Wrap = DefaultBoolean.False;
    field.Width = 150;
    field.Caption = "Month Year";
    field.FieldName = "YYYYMM";
    field.CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
    field.Options.AllowSort = DefaultBoolean.False;
    field.Options.AllowFilter = DefaultBoolean.False;
});

settings.Fields.Add(field =>
{
    field.Area = PivotArea.ColumnArea;
    field.AreaIndex = 2;
    field.CellStyle.Wrap = DefaultBoolean.False;
    field.Width = 150;
    field.Caption = "Work Week";
    field.FieldName = "YYYYWW";
    field.Options.AllowSort = DefaultBoolean.False;
    field.Options.AllowFilter = DefaultBoolean.False;
});

settings.Fields.Add(field =>
{
    field.Area = PivotArea.DataArea;
    field.FieldName = summaryField;
    field.Caption = summaryField;
    field.CellFormat.FormatType = FormatType.Numeric;
    field.CellFormat.FormatString = format;
});

在季度和月份,但没有效果。我尝试了很多其他的东西,但似乎没有任何改变计算。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我明白了。我需要使用自定义摘要类型

以下是PivotGrid的代码

@using DevExpressPivotGridTest;

@{
    string summaryField = ViewBag.SummaryField;
    WWMMQ wwmmq = new WWMMQ();
}

@Html.DevExpress().PivotGrid(settings =>
{
    settings.Name = "PivotGridProject";
    settings.CallbackRouteValues = new { Controller = "PivotGrid", Action = "Project"};
    settings.OptionsPager.RowsPerPage = 100;
    settings.OptionsView.ShowFilterHeaders = false;

    settings.OptionsView.ColumnTotalsLocation = PivotTotalsLocation.Near;

    settings.CustomSummary = (sender, e) =>
    {
        int nbrWWs;
        string fieldName = "";

        if (e.ColumnField == null)
            fieldName = "";
        else
            fieldName = e.ColumnField.FieldName;


        switch (fieldName)
        {
            case "YYYYMM":
                nbrWWs = wwmmq.GetWorkWeeksInMonth(e.ColumnFieldValue.ToInt());
                e.CustomValue = e.SummaryValue.Summary.ToDecimal() / nbrWWs;
                break;

            case "YYYYQ":
                nbrWWs = wwmmq.GetWorkWeeksInQuarter(e.ColumnFieldValue.ToInt());
                e.CustomValue = e.SummaryValue.Summary.ToDecimal() / nbrWWs;
                break;

            case "YYYYWW":
            default:
                e.CustomValue = e.SummaryValue.Summary;
                break;
        }
    };

    settings.Fields.Add(field =>
    {
        field.Area = PivotArea.RowArea;
        field.AreaIndex = 0;
        field.FieldName = "ProjectName";
        field.Caption = "Project";
        field.Options.AllowFilter = DefaultBoolean.False;
    });

    settings.Fields.Add(field =>
    {
        field.Area = PivotArea.RowArea;
        field.AreaIndex = 2;
        field.FieldName = "SupplierName";
        field.Caption = "Supplier";
        field.Options.AllowFilter = DefaultBoolean.False;
    });

    settings.Fields.Add(field =>
    {
        field.Area = PivotArea.ColumnArea;
        field.AreaIndex = 0;
        field.TotalsVisibility = PivotTotalsVisibility.None;
        field.CellStyle.Wrap = DefaultBoolean.False;
        field.Width = 150;
        field.Caption = "Quarter Year";
        field.FieldName = "YYYYQ";
        field.CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
        field.Options.AllowSort = DefaultBoolean.False;
        field.Options.AllowFilter = DefaultBoolean.False;
    });

    settings.Fields.Add(field =>
    {
        field.Area = PivotArea.ColumnArea;
        field.AreaIndex = 1;
        field.TotalsVisibility = PivotTotalsVisibility.None;
        field.CellStyle.Wrap = DefaultBoolean.False;
        field.Width = 150;
        field.Caption = "Month Year";
        field.FieldName = "YYYYMM";
        field.CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
        field.Options.AllowSort = DefaultBoolean.False;
        field.Options.AllowFilter = DefaultBoolean.False;
    });

    settings.Fields.Add(field =>
    {
        field.Area = PivotArea.ColumnArea;
        field.AreaIndex = 2;
        field.CellStyle.Wrap = DefaultBoolean.False;
        field.Width = 150;
        field.Caption = "Work Week";
        field.FieldName = "YYYYWW";
        field.Options.AllowSort = DefaultBoolean.False;
        field.Options.AllowFilter = DefaultBoolean.False;
    });

    settings.Fields.Add(field =>
    {
        field.SummaryType = PivotSummaryType.Custom;

        field.Area = PivotArea.DataArea;
        field.FieldName = summaryField;
        field.Caption = summaryField;
        field.CellFormat.FormatType = FormatType.Numeric;
        field.CellFormat.FormatString = "n2";
    });
}).Bind(Model).GetHtml()
public class WWMMQ
{
    private List<MyDates> dates;   

    public WWMMQ()
    {
        dates = MyDates.Get();
    }

    public WWMMQ(string startDate, string endDate)
    {
        dates = MyDates.Get(startDate, endDate);
    }

    public int GetWorkWeeksInMonth(int month)
    {
        // some months have 4, some months have 5
        return dates.Count(d => d.YYYYMM == month);
    }

    public int GetWorkWeeksInQuarter(int quarter)
    {
        // most quarters have 13, some quarters have 14
        return dates.Count(d => d.YYYYQ == quarter);
    }

诀窍是定义自定义摘要功能。 DevExpress已经计算了所有聚合。对于本周汇总,我只是正常使用Sum值,但是对于月份和季度,我使用Sum除以月份或季度中的周数来得到平均值。