Hi,
I've got a XamPivotGrid with a FlatDataSource working and I haven't been able to get the custom/ calculated measures working.
I need to achieve a weighted sum where the values and the weights are two measures present on the pivot table.
I was following the response from https://es.infragistics.com/community/forums/f/ultimate-ui-for-wpf/45131/calculated-custom-measures-for-flatdatasource-in-xampivotgrid?ReplySortBy=CreatedDate
but the example link provided died and the pasted code has issues.
thanks
Great job! Let us know if you have any additional questions.
(I wrote a reply and somehow it didn't got posted, so here I go again)
Turns out, it can be done.
I managed to get a weighted average by setting a custom aggregator.
First some sources:
How to setup custom aggregators:
https://es.infragistics.com/help/wpf/xampivotgrid-us-customaggregators
Using a custom aggregator example with weighted average help class (has some issues but the fix is in a reply)
https://es.infragistics.com/community/forums/f/ultimate-ui-for-wpf/45131/calculated-custom-measures-for-flatdatasource-in-xampivotgrid/331675#331675
what is missing from there is how the custom aggregator is actually set into the FlatDataSource. The example calls for flatDataSource.CubesSettings[0] but that was null at the moment.
flatDataSource.CubesSettings[0] but that was null at the moment.
So what was missing is this:
CubeMetadata cube = new CubeMetadata(); //weirdType is a dynamicaly created type. Use typeof(*your data source type*).FullName cube.DataTypeFullName = weirdType.FullName; cube.DisplayName = "weird type"; DimensionMetadata testMetadata = new DimensionMetadata { SourcePropertyName = "valueProp", DisplayName = "test", DimensionType = DimensionType.Measure, Aggregator = new IgWeightedAverageAggregator("valueProp", "weightProp") }; cube.DimensionSettings.Add(testMetadata); FlatDataSource dataSource = new FlatDataSource(); dataSource.CubesSettings.Add(cube); //then set that data soruce to the pivot control
After that the "test" measure appeared and it has the expected values.
By setting a custom aggregator and manually adding it to the dimension settings of the cube in the flat data source, you can add a custom measure that may be calculated in any form needed. (a weighted average in my case).
Sources:
setting up custom aggregators:
Aggregator example with weighted sum help class. (although it has some issues that were fixed in a comment below)
The only thing that was missing from those two references is how the aggregator should be actually applied to the FlatDataSource, because in the first example, the agregator is applied to CubeSettings[0] but that list was empty thus throwing a null reff exception.
For me it looked something like this.
CubeMetadata cube = new CubeMetadata(); //weirdType is a dynamically generated type, use typeof(**yourtype**).FullName for other cases. cube.DataTypeFullName = weirdType.FullName; cube.DisplayName = "weird type"; DimensionMetadata testMetadata = new DimensionMetadata { SourcePropertyName = "prop1", DisplayName = "test", DimensionType = DimensionType.Measure, Aggregator = new TypeBuilderNamespace.IgWeightedAverageAggregator("prop1", "weightProp") }; cube.DimensionSettings.Add(testMetadata); FlatDataSource dataSource = new FlatDataSource(); dataSource.CubesSettings.Add(cube); //data is a collection of weirdType objects dataSource.ItemsSource = data; //then set that dataSource.
so the weird type has a property "prop1" that has values that should be weighted by "weightProp". the "test" displayed doesn't exists as a property of weirdType
Hello Claudio,
The FlatDataSource does not currently support calculated measures. Calculated measures are only supported on XMLA data sources.
If you need a calculated measure then I recommend adding another field to your underlying data source that represents the calculated measure. For example:
public class Sale{ public double AmountOfSale { get; set; } public double DoubledAmountOfSale { get { return AmountOfSale * 2.0; } }}
We have a online topic that walks through on creating calculated measures. You may also refer to the following sample for more details. See below.
PercentageAggregator.zip
You can suggest new product ideas for future versions (or vote for existing ones) at <https://es.infragistics.com/community/ideas>. Submitting your idea will allow you to communicate directly with our product management team, track the progress of your idea at any time, see how many votes it got, read comments from other developers in the community, and see if someone from the product team has additional questions for you. Remember when submitting your idea to explain the context in which a feature would be used and why it is needed as well as anything that would prevent you from accomplishing this today. You can even add screenshots to build a stronger case. Remember that for your suggestion to be successful, you need other members of the community to vote for it. You can also link back to this thread for additional details.
Let me know if you have any questions.