FlexPivot for WPF | ComponentOne
Data Blending / Subtotals / Weighted Averages and Sums
In This Topic
    Weighted Averages and Sums
    In This Topic

    The FlexPivot control allows users to calculate weighted average or sum of the data. This can be done both at runtime as well as in code. For example, 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.

    Calculate Weighted Average at Runtime

    1. Right-click the field in the Values list in the FlexPivotPanel 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.

    Calculate Weighted Average Programmatically

    You can use the Subtotal property to calculate subtotals programmatically and the WeightField property to specify the field to be used as weight. The following code uses Quantity field as the Weight. This implementation uses the sample created in Quick Start topic.

    C#
    Copy Code
    //Setting weight field in code           
    var Qfield = fpEngine.Fields["Quantity"];
    Qfield.WeightField = fpEngine.Fields["Quantity"];
    //Show weighted average price
    fpEngine.Fields["ExtendedPrice"].Subtotal = Subtotal.Average;