ComponentOne FlexPivot for WinForms
Data Blending Features / Calculating Weighted Averages and Sums
In This Topic
    Calculating Weighted Averages and Sums
    In This Topic

    The FlexPivot control allows users to calculate average or sum of the data displayed on the FlexPivotGrid control. This can be done both at runtime as well as in code.

    Consider a scenario where the user wants to find the average price for a group of products, taking into account the quantity of each purchased product. You can weigh the price average by the number of units purchased.

    At Runtime

    1. Right-click the field in the Values area of the FlexPivotPanel control and select Field Settings option.
    2. Click the Subtotals tab and select the type of subtotal you want to calculate.
    3. In the Weigh by drop-down list, select the field from your data table to be used as a weight as illustrated in the image below.
    4. Click OK to close the Field Settings dialog.

    fieldsettings dialog

    In Code

    Use the WeightField property to specify the field to be used as weight. The code given below uses Quantity field as the Weight.

    //setting weight field in code
    var fp = this.FlexPivotPage1.PivotEngine;
    var field = fp.Fields["Quantity"];
    field.WeightField = fp.Fields["Quantity"];
    
    //setting weight field in code
    var fp = this.FlexPivotPage1.PivotEngine;
    var field = fp.Fields["Quantity"];
    field.WeightField = fp.Fields["Quantity"];