Skip to content

Commit 0fb32f3

Browse files
committed
WIP: add new percentFormat() function
it returns 3 values, top percentage, halfway percentage, 0%.
1 parent fd26e63 commit 0fb32f3

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

scout-ui/src/minicharts/d3fns/few.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = function(data, g, width, height, options) {
2626
}
2727
return d.tooltip || tooltipHtml({
2828
label: d.label,
29-
value: shared.percentFormat(d.value / sumValues)
29+
value: shared.percentFormat(d.value / sumValues)[2]
3030
});
3131
})
3232
.direction('n')

scout-ui/src/minicharts/d3fns/many.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = function(data, g, width, height, options) {
3535
}
3636
return d.tooltip || tooltipHtml({
3737
label: d.label,
38-
value: shared.percentFormat(d.value / sumValues)
38+
value: shared.percentFormat(d.value / sumValues)[2]
3939
});
4040
})
4141
.direction('n')
@@ -47,9 +47,17 @@ module.exports = function(data, g, width, height, options) {
4747

4848
if (options.scale) {
4949
var maxVal = d3.max(y.domain());
50+
var scaleLabels = percentFormat(maxVal);
5051

5152
// @todo use a scale and wrap both text and line in g element
5253
var legend = g.append('g')
54+
.attr('class', 'legend')
55+
.data(scaleLabes)
56+
.enter()
57+
.append('text');
58+
59+
60+
g.append('g')
5361
.attr('class', 'legend');
5462

5563
legend.append('text')

scout-ui/src/minicharts/d3fns/shared.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ module.exports = {
99
left: 40
1010
},
1111

12-
percentFormat: d3.format('%.1f')
12+
percentFormat: function(v) {
13+
// round max value to 1 digit precision
14+
var prec1Format = d3.format('.1r');
15+
var intFormat = d3.format('.0f');
1316

17+
// multiply by 100 for percentages
18+
v *= 100;
19+
20+
var top = v > 1 ? intFormat(v) : prec1Format(v);
21+
var mid = parseFloat(top) / 2;
22+
23+
return ['0%', mid + '%', top + '%'];
24+
}
1425
};

scout-ui/test/minicharts.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var shared = require('../src/minicharts/d3fns/shared');
2+
var assert = require('assert');
3+
4+
describe('shared components', function() {
5+
it('should return percentages for bottom, middle and top scale correctly', function() {
6+
assert.deepEqual(shared.percentFormat(2.1), ['0%', '105%', '210%']);
7+
assert.deepEqual(shared.percentFormat(2.0), ['0%', '100%', '200%']);
8+
assert.deepEqual(shared.percentFormat(1.0), ['0%', '50%', '100%']);
9+
assert.deepEqual(shared.percentFormat(0.995), ['0%', '50%', '100%']);
10+
assert.deepEqual(shared.percentFormat(0.99), ['0%', '49.5%', '99%']);
11+
assert.deepEqual(shared.percentFormat(0.9900001), ['0%', '49.5%', '99%']);
12+
assert.deepEqual(shared.percentFormat(0.49999), ['0%', '25%', '50%']);
13+
assert.deepEqual(shared.percentFormat(0.011), ['0%', '0.5%', '1%']);
14+
assert.deepEqual(shared.percentFormat(0.009), ['0%', '0.45%', '0.9%']);
15+
assert.deepEqual(shared.percentFormat(0.004), ['0%', '0.2%', '0.4%']);
16+
assert.deepEqual(shared.percentFormat(0.0), ['0%', '0%', '0%']);
17+
assert.deepEqual(shared.percentFormat(-0.015), ['0%', '-1%', '-2%']);
18+
});
19+
});

0 commit comments

Comments
 (0)