How to create a new Aggregate Function PivotGrid - WPF

1 Answer 109 Views
PivotGrid
Diego
Top achievements
Rank 1
Diego asked on 02 Dec 2021, 06:09 PM

I was wondering how I can create a new Aggregation Function and add it to this list of existing Functions in "More Aggregation Options"
In this article I see that they managed to create the 'Sqrt Of Sum' function in WinForms

-> https://docs.telerik.com/devtools/winforms/controls/pivotgrid/custom-aggregation

I would like to know how I can be doing the same to add an Aggregation Function in Options in PivotGrid WPF:

Explanatory Image below:

 

Thank you!

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 06 Dec 2021, 02:35 PM

Hello Diego,

To add a new function to the list of available functions, you need to create a custom PropertyAggregateDescriptionBase and override its SupportedAggregateFunctions property:

        protected override IEnumerable<object> SupportedAggregateFunctions
        {
            get
            {
                return base.SupportedAggregateFunctions.Concat(this.AdditionalAggregateFunctions);
            }
        }

        private static SqrtSumAggregateFunction SqrtSum
        {
            get { return new SqrtSumAggregateFunction(); }
        }

        private IEnumerable<object> AdditionalAggregateFunctions
        {
            get { yield return SqrtSum; }
        }

Here I've used the SqrtSumAggregateFunction from the WinForms article you shared.

For your convenience, I've prepared a small sample project to demonstrate this in action. Please have a look and let me know if you find this useful.

Regards,
Dilyan Traykov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Diego
Top achievements
Rank 1
commented on 09 Dec 2021, 02:29 PM

On the telerik's main page I saw that there's something about weighted average, and that's exactly what I need to do instead in the Sqrt of Sum, the real problems is that same I take the examples in the doc and redo the codes perfectly, I don't I can make this work, are there any questions resolved here about weighted average?
Dilyan Traykov
Telerik team
commented on 10 Dec 2021, 05:17 PM

Hello Diego,

I came across the following resources available regarding the creation of a weighted average function:

If you have not come across them already, can you please have a look and let me know if you find them helpful?

If you're still not able to achieve the desired result, please share a small sample project which demonstrates your current setup and the expected outcome and I will gladly try to suggest a viable solution.

Diego
Top achievements
Rank 1
commented on 13 Dec 2021, 10:27 PM

Hi! Your answers helped me find a way to the solution. Thanks! However, I see that there is a problem, I am using the Sqrt Of Sum that you gave me as an example and what I want to do is:

In your example, it works perfectly and so far OK!

But when I remove the ExtendedPrice property and add it again, the aggregate function Sqrt Of Sum just disappears from the More Aggregation Options list and I need it always present!
I'll show you with images:

 

Dilyan Traykov
Telerik team
commented on 16 Dec 2021, 03:38 PM

Hi Diego,

Thank you for the provided images.

The reason for this behavior is that when you manually add the new field via the checkbox, a new PropertyAggregateDescription is added by default instead of a CustomPropertyAggregateDescription which brings the custom functions. To resolve this, you can handle the PrepareDescriptionForField event of the DataProvider in the following manner

        private void LocalDataSourceProvider_PrepareDescriptionForField(object sender, Telerik.Pivot.Core.PrepareDescriptionForFieldEventArgs e)
        {
            if (e.FieldInfo.Name == "ExtendedPrice")
            {
                e.Description = new CustomPropertyAggregateDescription() { PropertyName = "ExtendedPrice", AggregateFunction = AggregateFunctions.Sum };
            }
        }

Please give this a try and let me know if this approach provides the desired result.

Gabriel
Top achievements
Rank 1
commented on 20 Dec 2021, 01:17 PM

Hi good morning!

What I'm trying in my Custom Aggregation is the following: My DataSource is a List of this object: ViewCpaKpi.

In this override method AcumulateOverride() I wanted to know if there is a way this parameter object is the object of the line, the ViewCpaKpi, as shown in the image, because I need two columns in this context.

Currently it is receiving only the value of the cell, and I need the whole object to come.

Dilyan Traykov
Telerik team
commented on 23 Dec 2021, 09:53 AM

Hi Gabriel,
As the name of the AggregateValue class (which provides the AccumulateOverride method) implies, it is only used to aggregate the values of the pivot control during grouping - it cannot hold a reference of the actual items the provider is bound to.
Instead, I believe you can use a calculated field and pass the AbsOperationalTime and AbsAutomaticModeTime fields to the RequiredFields collection.
Tags
PivotGrid
Asked by
Diego
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Share this question
or