I'm using angular-nvd3 and I need to present some horizontal bars, each with a count and percentage. Problem is that the sum of the percentages is sometimes 101%. For example:
There are solutions to the 101% problem - for example substract 1 from one of the percent values if it's 101%. The count can stay the same (so the first bar will be
1 (16%)
).
But nvd3 works with a function that receives the bar value and needs to return the string that will be presented, which makes it harder to work out a preprocessing solution.
Any idea how to make this work?
Asked
Active
Viewed 150 times
0

Jenian
- 552
- 1
- 5
- 18
-
Could you add another significant figure e.g. 66.7% instead of 67%? – jeznag Apr 19 '17 at 23:50
-
No, I prefer to have integers. – Jenian Apr 20 '17 at 07:38
1 Answers
0
I think you could try this:
So you have to store index of current value, because valueFormat does not provide index by it self
var valueFormatIndex = 0;
var data = [{values: [ ... ] }];
var config = {
...,
valueFormat: function(d, index){
valueFormatIndex = valueFormatIndex + 1;
// if its a last value we should round to lowest
if(data[0].values.length == valueFormatIndex) {
return Math.floor(d)
}
return Math.ceil(d) // otherwise return round to top
},
...
}
JSFiddle example

Leguest
- 2,097
- 2
- 17
- 20