Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregates are wrong for non unique columns #30

Open
jamescasbon opened this issue Aug 13, 2011 · 0 comments
Open

Aggregates are wrong for non unique columns #30

jamescasbon opened this issue Aug 13, 2011 · 0 comments

Comments

@jamescasbon
Copy link

If you have a repeated value in a Data.Collection, the aggregation produces incorrect values:

Test data, note we have two german countries with the same population:

var countries_data = {
"properties": {
"name": {"name": "Country Name", "type": "string", "unique": true },
"official_language": {"name": "Official Language", "type": "string", "unique": false },
"population": { "name": "Population", "type": "number", "unique": false }
},
"items": {
0: {
"name": "Austria",
"official_language": "German",
"population": 8356700,
},

  'usa': {
    "name": "United States of America",
    "official_language": "English",
    "population": 310955497,
  },
  'ger': {
    "name": "Germany",
    "official_language": "German",
    "population": 82062200,
  },
  'ger2': {
    "name": "Germany2",
    "official_language": "German",
    "population": 82062200,
  }
}

}

var countries = new Data.Collection(countries_data);
console.log('oh sweet, just made a collection:');
console.log(countries_data);

var population = countries.properties().get('population');
console.log('get returns a Data.Property: ');
console.log(population);

console.log('which supports aggregation: ');
var population_sum = population.aggregate(Data.Aggregators.SUM) // => Returns total population of all countries in the collection.
console.log(population_sum);

equals(population_sum, 310955497 + 82062200 + 8356700 + 82062200, 'We can aggregate a column');

This test fails, I think because Data.Property.aggregate is defined:

aggregate: function (fn) {
  return fn(this.values("values"));
},

this.values produces a hash where each key is the value considered, so in this case it has three rather than 4 values do to the repeated value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant