Incorrect Aggregate Result

5 posts, 0 answers
  1. Sam
    Sam avatar
    8 posts
    Member since:
    Sep 2015

    Posted 19 Dec 2015 Link to this post

    Hello,

     I am having issues with the aggregate result returning incorrect values. 

    this.data = new kendo.data.DataSource({
        transport: {
            read: (e) => {
                ....
            }
        },
        schema: <kendo.data.DataSourceSchemaWithOptionsModel> {
            data: 'Data',
            model: <kendo.data.DataSourceSchemaModelWithFieldsObject> {
                fields:  {
                    Foo: {
                        type: 'number',
                        editable: false,
                        parse: x => x.toFixed(2)
                    },
                    Bar: {
                        type: 'number',
                        editable: false,
                        parse: x => x.toFixed(2)
                    },
                    Qux: {
                        type: 'number',
                        editable: false,
                        parse: x => x.toFixed(2)
                    }
     
                }
            }
        },
        aggregate: [
            { field: 'Foo', aggregate: 'average' },
            { field: 'Bar', aggregate: 'average' },
            { field: 'Qux', aggregate: 'average' }
        ],
        sort: {
            field: 'TimeSlot',
            dir: 'asc'
        }
    });
     
    console.log(this.data);

    This returns the last row of data returned ie:

    this.data._aggregateResult:

        Foo.average: 19

        Bar.average: 90

        Qux.average: 100

     and this.data._data[this.data._data.length] contains:

        Foo: 19

        Bar: 90

        Qux: 100

    Instead of creating the average/max/min/sum the aggregate just returns data from the last object in the list.

  2. Sam
    Sam avatar
    8 posts
    Member since:
    Sep 2015

    Posted 19 Dec 2015 Link to this post

    Aggregating on 'count' returns the correct result.
  3. Kendo UI is VS 2017 Ready
  4. Sam
    Sam avatar
    8 posts
    Member since:
    Sep 2015

    Posted 22 Dec 2015 Link to this post

    Found out what the problem was! Although I was saying that they were type 'number', the objects being returned were actually strings. They do not get casted to numbers so make sure that you parse them properly and it will work.

    Foo: {
        type: 'number',
        editable: false,
        parse: x => x.toFixed(2)
    },
    Bar: {
        type: 'number',
        editable: false,
        parse: x => x.toFixed(2)
    },
    Qux: {
        type: 'number',
        editable: false,
        parse: x => x.toFixed(2)
    }

  5. Sam
    Sam avatar
    8 posts
    Member since:
    Sep 2015

    Posted 22 Dec 2015 Link to this post

    Here is the correct version taking numbers that are passed in as strings and converting them to numbers using the parse call:

     

    Foo: {
        type: 'number',
        editable: false,
        parse: x => Number(x.toFixed(2))
    },
    Bar: {
        type: 'number',
        editable: false,
        parse: x => Number(x.toFixed(2))
    },
    Qux: {
        type: 'number',
        editable: false,
        parse: x => Number(x.toFixed(2))
    }

  6. Kiril Nikolov
    Admin
    Kiril Nikolov avatar
    2564 posts

    Posted 24 Dec 2015 Link to this post

    Hello Sam,

    I am happy to hear that the problem is resolved and you shared with us the solution. 

    Thanks!

    Regards,
    Kiril Nikolov
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
Back to Top
Kendo UI is VS 2017 Ready