dimanche 31 janvier 2016

d3.js function to count data belonging to a category and bigger than 0

I am trying to create a function that counts the people of each category that have a value bigger than 0. If this is my data...

DATA.CSV
name; category; value
name1; A; 10
name2; A; 0
name3; A; 5
name4; B; 7
name5; B; 3
name6; C; 0

...I should get the following results

count(dataset, "A")=2
count(dataset, "B")=2
count(dataset, "C")=0

I tried a for loop with an if statement inside but it is not working.

    function count (dataset, chosenCategory) {
    var count = 0;
    for (d in dataset) {
        if (d.category==chosenCategory && d.value>0) {
                count += 1;
            } else {
                count += 0;
            }

        }
    }

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire