This is a migrated thread and some comments may be shown as answers.

Incorrect Aggregate Result

4 Answers 110 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Sam
Top achievements
Rank 1
Sam asked on 19 Dec 2015, 08:02 PM

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.

4 Answers, 1 is accepted

Sort by
0
Sam
Top achievements
Rank 1
answered on 19 Dec 2015, 08:42 PM
Aggregating on 'count' returns the correct result.
0
Sam
Top achievements
Rank 1
answered on 22 Dec 2015, 07:26 PM

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)
}

0
Sam
Top achievements
Rank 1
answered on 22 Dec 2015, 09:42 PM

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))
}

0
Kiril Nikolov
Telerik team
answered on 24 Dec 2015, 09:34 AM
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!
 
Tags
Data Source
Asked by
Sam
Top achievements
Rank 1
Answers by
Sam
Top achievements
Rank 1
Kiril Nikolov
Telerik team
Share this question
or