CalculatedField - custom default fomat

2 posts, 1 answers
  1. Cristiano
    Cristiano avatar
    1 posts
    Member since:
    Jul 2014

    Posted 14 Jul 2014 Link to this post

    Hi,
    I have a problem with the customization of the format in a calculated measure.
    I want to reach this behaviour: if a user doesn't select any custom format by the control RadPivotFieldList, I set a default format for this value (the same of the other measures).
    In my opinion, I could reach this goal with a different visibility of the field formattedValue of your abstract class AggregateaValue, but the visibility is internal and I can't read the value in the overriden method toString().
    Any suggestion?
    Thanks in advance.
    Cris


    [DataContract]
        public class CustomBynaryOperatorCalculatedField : CalculatedField
        {
            RequiredField field1;
            RequiredField field2;
     
            Func<double?, double?, double?> add = (a, b) =>
                {
                    return NullableDoubleHelper.Add(a,b);
                };
     
            Func<double?, double?, double?> sub = (a, b) =>
                {
                    if (a.HasValue && b.HasValue)
                        return a.Value - b.Value;
                    else
                        return null;
                };
            Func<double?, double?, double?> mul = (a, b) =>
                {
                    if (a.HasValue && b.HasValue)
                    {
                        try
                        {
                            return a.Value * b.Value;
                        }
                        catch(Exception)
                        {
                            return null;
                        }
                    }
                    else
                        return null;
                };
            Func<double?, double?, double?> div = (a, b) =>
            {
                if (a.HasValue && b.HasValue && b.Value!=0)
                    return a.Value / b.Value;
                else
                    return null;
            };
                 
                //b != 0 ? a / b : 0;
     
     
            Func<double?, double?, double?> operation;
     
            public CustomBynaryOperatorCalculatedField(string name, int op, string nameField1, string nameField2)
            {
                Name = name;
                this.field1 = RequiredField.ForProperty(nameField1);
                this.field2 = RequiredField.ForProperty(nameField2);
     
                switch (op)
                {
                    case (int)OperationType.Addition:
                        operation = add;
                        break;
                    case (int)OperationType.Subtraction:
                        operation = sub;
                        break;
                    case (int)OperationType.Multiplication:
                        operation = mul;
                        break;
                    case (int)OperationType.Division:
                        operation = div;
                        break;
                    default:
                        break;
                }
            }
     
            public CustomBynaryOperatorCalculatedField()
            {
                 
            }
     
            protected override IEnumerable<RequiredField> RequiredFields()
            {
                yield return this.field1;
                yield return this.field2;
            }
     
            protected override Telerik.Pivot.Core.Aggregates.AggregateValue CalculateValue(IAggregateValues aggregateValues)
            {
                var aggregateValue = aggregateValues.GetAggregateValue(field1);
                if (aggregateValue.IsError())
                {
                    return aggregateValue;
                }
     
                var aggregateValue1 = aggregateValues.GetAggregateValue(field2);
                if (aggregateValue1.IsError())
                {
                    return aggregateValue1;
                }
     
                try
                {
                    double? v1 = aggregateValue.ConvertOrDefault<double>();
                    double? v2 = aggregateValue1.ConvertOrDefault<double>();
     
                    var v = operation(v1, v2);
     
                     
                    if(v.HasValue)
                        return new CustomDoubleAggregateValue(v.Value);
                    else
                        return null;
                }
                catch (AggregateException)
                {
                    return null;
                }
            }
        }
    ...
    public class CustomDoubleAggregateValue : CustomAggregateValueBase
        {
             
            private double? result;
     
             /// <summary>
            /// Initializes a new instance of the <see cref="T:Telerik.Pivot.Core.Aggregates.DoubleAggregateValue" /> class.
            /// </summary>
            /// <param name="value">The default value.</param>
            public CustomDoubleAggregateValue(double? value)
            {
                this.result = value;
            }
             
            /// <inheritdoc />
            protected override object GetValueOverride()
            {
                return this.result;
            }
     
            //bool Telerik.Pivot.Core.Aggregates.IConvertibleAggregateValue<System.Double?>.TryConvertValue(out double? value)
            //{
            //    if (IsError)
            //    {
            //        value = 0;
            //        return false;
            //    }
            //    value = this.result;
            //    return true;
            //}
     
            //public override string ToString()
            //{
            //    return base.formattedValue ??
            //        Convert.ToString(this.GetValue(), CultureInfo.CurrentCulture);
            //}
             
            public override string ToString()
            {
    //if formatted value is not null or empty return formattedValues else return result with a custom format
                return base.ToString();
            }
     
            protected override void AccumulateOverride(object value)
            {
                RaiseError();
            }
     
            protected override void MergeOverride(Telerik.Pivot.Core.Aggregates.AggregateValue childAggregate)
            {
                RaiseError();
            }
        }
    }
  2. Answer
    Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 15 Jul 2014 Link to this post

    Hi Cris,

    The idea of the aggregated value is to get the real value of the item. The formatting is applied via StringFormat property when you add the CalculatedField to your Aggregate Descriptions:
    <pivot:LocalDataSourceProvider.AggregateDescriptions>
            <pivot:PropertyAggregateDescription PropertyName="ExtendedPrice" AggregateFunction="Sum" />
            <pivot:CalculatedAggregateDescription CalculatedFieldName="Commission" StringFormat="0##0.###0$" />
    </pivot:LocalDataSourceProvider.AggregateDescriptions>
    <pivot:LocalDataSourceProvider.CalculatedFields>
            <local:CommissionCalculatedField Name="Commission" />
    </pivot:LocalDataSourceProvider.CalculatedFields>

    Hope this helps.

    Regards,
    Rosen Vladimirov
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. DevCraft banner
Back to Top