Imports Telerik.Pivot.Core
Imports Telerik.Pivot.Core.Aggregates
Imports Telerik.Pivot.Core.Totals

Public Class Form1
    Private provider As New LocalDataSourceProvider()

    Public Sub New()
        InitializeComponent()

        Me.RadPivotGrid1.DataProvider = provider

        Using RadPivotGrid1.PivotGridElement.DeferRefresh()
            Dim calculatedField As New CommissionCalculatedField()
            calculatedField.Name = "Commission"

            DirectCast(Me.RadPivotGrid1.DataProvider, LocalDataSourceProvider).CalculatedFields.Add(calculatedField)
        End Using

        Dim calculatedAggregate As New CalculatedAggregateDescription()
        calculatedAggregate.CalculatedFieldName = "Commission"
        DirectCast(RadPivotGrid1.DataProvider, LocalDataSourceProvider).AggregateDescriptions.Add(calculatedAggregate)

        FillWithData()
    End Sub

    Private Sub FillWithData()
        Me.RadPivotGrid1.RowGroupDescriptions.Add(New PropertyGroupDescription() With {
             .PropertyName = "Salesperson"
        })

        Me.RadPivotGrid1.ColumnGroupDescriptions.Add(New DateTimeGroupDescription() With {
             .PropertyName = "OrderDate",
             .[Step] = DateTimeStep.Year
        })

        Me.RadPivotGrid1.AggregateDescriptions.Add(New PropertyAggregateDescription() With {
             .PropertyName = "ExtendedPrice",
             .AggregateFunction = AggregateFunctions.Sum
        })

        Me.RadPivotGrid1.AggregateDescriptions.Add(New PropertyAggregateDescription() With {
             .PropertyName = "ExtendedPrice",
             .CustomName = "% of GrandTotal",
             .AggregateFunction = AggregateFunctions.Sum,
             .TotalFormat = New PercentOfGrandTotal()
        })

        Me.RadPivotGrid1.AggregateDescriptions.Add(New PropertyAggregateDescription() With {
             .PropertyName = "ExtendedPrice",
             .CustomName = "% of ColumnTotal",
             .AggregateFunction = AggregateFunctions.Sum,
             .TotalFormat = New PercentOfColumnTotal()
        })

        provider.ItemsSource = Invoice.GetInvoices()
        Me.RadPivotGrid1.ErrorString = "Error"
    End Sub

End Class
