stallingOne
stallingOne

Reputation: 4006

How to round values of reduceSum in crossfilter?

I'm making a map with dc.js and the crossfilter function reduceSum() introduces some rounding errors.

simplified example :

group = dimension.group().reduceSum(function(e) { return 0.01; });

group.all() will contain a value of 32.21000000000216 when the number of records is 3221. It should be 32.21 (the sum of 3221 times 0.01).

You can see the effect I'm talking about here : https://epistat.wiv-isp.be/ (just hover your mouse over the map to see very long decimal numbers)

How can you round the values of a crossfilter group ?

The group has only these methods available:

Can you change the value of a group ?
I'm thinking of doing it with a loop but I can't find anything in the docs for changing the value of a group : https://github.com/square/crossfilter/wiki/API-Reference

Upvotes: 2

Views: 424

Answers (1)

stallingOne
stallingOne

Reputation: 4006

I found a workaround, it's not the a real answer I was looking for, but it's the best I found so far:

I get the closest floating number (closest to the real number) by having the biggest numbers in the data (before dimensions and groups) as possible. (I'm multiplying incidence with bigMultiplicator=99999999 ; from my tests the bigger the better)

then in the reduceSum I divide by that big number:

group = dimension.group().reduceSum(function(e) { return e.incidence / bigMultiplicator; });

And I finish by rounding the number in the title display of dc.js:

.title(function(d) {
    return d.key + ' : ' + Math.round(d.value * 100)/100 ;
})

I hope it helps someone else.

Upvotes: 1

Related Questions