Hi folks,
I am trying to test out using a MeasureAggregator, and am getting this exception. Not sure why the viewModel.Measures array would be empty but it appears to be. Thoughts?
void DrillDown_Loaded(object sender, RoutedEventArgs e) {
IEnumerable sampleData = SampleDataGenerator.GenerateSales(500);
//(xamPivotGrid1.DataSource as FlatDataSource).ItemsSource = sampleData; FlatDataSource flatDataSource = new FlatDataSource() { //Data is the IEnumerable to be used as a source ItemsSource = sampleData, Cube = DataSourceBase.GenerateInitialCube("Sale"), Rows = DataSourceBase.GenerateInitialItems("[Date]"), Columns = DataSourceBase.GenerateInitialItems("[City]"), Measures = DataSourceBase.GenerateInitialItems("NumberOfUnits, AmountOfSale") };
xamPivotGrid1.DataSource = flatDataSource;
xamPivotDataSelector1.DataSource = flatDataSource;
xamPivotGrid1.DataSource.Columns.CollectionChanged += new NotifyCollectionChangedEventHandler(Columns_CollectionChanged);
IOlapViewModel viewModel = xamPivotGrid1.DataSource;
viewModel.SetMeasureAggregator((IMeasureViewModel)viewModel.Measures[0], MeasureAggregator.Average);
viewModel.RefreshGrid(); }
Hello,
At the moment when you try to access the measure at index 0 the Measures are not initialized yet. One thing you can do is to listen for the CollectionChanged event of Measures collection and then you can apply the aggregator you want.The better approach I think is to add dimension metadata for this particular measure:
CubeMetadata cubeMetadata = new CubeMetadata { DataTypeFullName = "[ItemsSourceClassName]", DisplayName = "[CubeDisplayName]" };
flatDataSource.CubesSettings.Add(cubeMetadata);
DimensionMetadata numberOfUnitsMetadata = new DimensionMetadata
{
SourcePropertyName = "Units",
DisplayName = "Units (Avg)",
DimensionType = DimensionType.Measure,
AggregatorType = AggregatorType.Average,
DisplayFormat = "{0:0.##}"
};
cubeMetadata.DimensionSettings.Add(numberOfUnitsMetadata);
You can do it from XAML as well.Now this measure will be initialized to perform average operations.
If you want multiple measures over single property then set:
flatDataSource.DimensionsGenerationMode = DimensionsGenerationMode.Mixed;
and add the additional metadata for each additional measure you want.
DimensionMetadata unitsMetadata = new DimensionMetadata
DisplayFormat = "{0} pcs."
cubeMetadata.DimensionSettings.Add(unitsMetadata);
And now you will have two measures built over Units property as one of them calculates the average result.
Best regards.PPilev.
Thanks for the reply, PPilev,
Unfortunately, this code doesn't compile for me... I can't resolve the class "DimensionType". I am using v10.3... Do I need to upgrade to 10.4?
Thanks,
-Jesse
Okay, I got it compiling... But now I'm not seeing the averages. Should they just appear in the totals row at the bottom? Here is my code:
//(xamPivotGrid1.DataSource as FlatDataSource).ItemsSource = sampleData; FlatDataSource flatDataSource = new FlatDataSource() { //Data is the IEnumerable to be used as a source ItemsSource = sampleData, Cube = DataSourceBase.GenerateInitialCube("Sale"), Rows = DataSourceBase.GenerateInitialItems("[Seller]"), Columns = DataSourceBase.GenerateInitialItems("[Product]"), Measures = DataSourceBase.GenerateInitialItems("AmountOfSale") };
CubeMetadata cubeMetadata = new CubeMetadata { DataTypeFullName = "[Sale]", DisplayName = "MY CUBE" };
DimensionMetadata numberOfUnitsMetadata = new DimensionMetadata { SourcePropertyName = "AmountOfSale", DisplayName = "AmountOfSale (Avg)", DimensionType = DimensionType.Measure, AggregatorType = AggregatorType.Average, DisplayFormat = "{0:0.##}" };
Sorry but I may misleaded you about this part here:DataTypeFullName = "[Sale]"Instead [Sale] put the full class name of your items.
Thank you! That did the trick!