Skip to content

Commit 253c67f

Browse files
committed
integrating feedback from wed/thu.
1 parent fd8a3a9 commit 253c67f

File tree

8 files changed

+191
-101
lines changed

8 files changed

+191
-101
lines changed

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@ module.exports = function(opts) {
88

99
// group by true/false
1010
var data = _(values)
11-
.groupBy(function(d) {
12-
// extract string representations of values
13-
return d;
14-
})
15-
.defaults({false: [], true: []})
16-
.map(function(v, k) {
17-
return {
18-
x: k,
19-
y: v.length,
20-
tooltip: k
21-
};
22-
})
23-
.sortByOrder('x', [false]) // descending on y
24-
.value();
11+
.groupBy(function(d) {
12+
// extract string representations of values
13+
return d;
14+
})
15+
.defaults({
16+
false: [],
17+
true: []
18+
})
19+
.map(function(v, k) {
20+
return {
21+
x: k,
22+
y: v.length
23+
};
24+
})
25+
.sortByOrder('x', [false]) // descending on y
26+
.value();
2527

2628
var margin = {
2729
top: 10,

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ var many = require('./many');
66

77
function generateDefaults(n) {
88
var doc = {};
9-
_.each(_.range(n), function (d) { doc[d] = 0; });
9+
_.each(_.range(n), function(d) {
10+
doc[d] = 0;
11+
});
1012
return doc;
1113
}
1214

@@ -17,7 +19,7 @@ module.exports = function(opts) {
1719
// distinguish ObjectIDs from real dates
1820
if (values.length && values[0]._bsontype !== undefined) {
1921
if (values[0]._bsontype === 'ObjectID') {
20-
values = _.map(values, function (v) {
22+
values = _.map(values, function(v) {
2123
return v.getTimestamp();
2224
});
2325
}
@@ -49,13 +51,13 @@ module.exports = function(opts) {
4951
var upperMargin = 15;
5052

5153
// group by weekdays
52-
var weekdayLabels = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
54+
var weekdayLabels = moment.weekdays();
5355
var weekdays = _(values)
54-
.groupBy(function (d) {
56+
.groupBy(function(d) {
5557
return moment(d).weekday();
5658
})
5759
.defaults(generateDefaults(7))
58-
.map(function (d, i) {
60+
.map(function(d, i) {
5961
return {
6062
x: weekdayLabels[i],
6163
y: d.length,
@@ -67,11 +69,11 @@ module.exports = function(opts) {
6769
// group by hours
6870
var hourLabels = d3.range(24);
6971
var hours = _(values)
70-
.groupBy(function (d) {
72+
.groupBy(function(d) {
7173
return d.getHours();
7274
})
7375
.defaults(generateDefaults(23))
74-
.map(function (d, i) {
76+
.map(function(d, i) {
7577
return {
7678
x: hourLabels[i],
7779
y: d.length,
@@ -117,19 +119,25 @@ module.exports = function(opts) {
117119
});
118120

119121
var weekdayContainer = svg.append('g');
120-
many(weekdays, weekdayContainer, width / (upperRatio+1) - upperMargin, upperBarBottom, {
121-
'text-anchor': 'middle',
122-
'text': function (d) {
123-
return d.x[0];
122+
many(weekdays, weekdayContainer, width / (upperRatio + 1) - upperMargin, upperBarBottom, {
123+
bgbars: true,
124+
labels: {
125+
'text-anchor': 'middle',
126+
'text': function(d) {
127+
return d.x[0];
128+
}
124129
}
125130
});
126131

127132
var hourContainer = svg.append('g')
128-
.attr('transform', 'translate(' + (width/(upperRatio+1) + upperMargin) + ', 0)');
129-
130-
many(hours, hourContainer, width/(upperRatio+1)*upperRatio - upperMargin, upperBarBottom, {
131-
'text': function (d, i) {
132-
return (i % 6 === 0 || i === 23) ? d.x : '';
133+
.attr('transform', 'translate(' + (width / (upperRatio + 1) + upperMargin) + ', 0)');
134+
135+
many(hours, hourContainer, width / (upperRatio + 1) * upperRatio - upperMargin, upperBarBottom, {
136+
bgbars: true,
137+
labels: {
138+
'text': function(d, i) {
139+
return (i % 6 === 0 || i === 23) ? d.x : '';
140+
}
133141
}
134142
});
135143

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
var d3 = require('d3');
22
var _ = require('lodash');
3+
var tooltipHtml = require('./tooltip.jade');
34
var debug = require('debug')('scout-ui:minicharts:few');
45

56
require('d3-tip')(d3);
67

7-
module.exports = function (data, g, width, height) {
8+
module.exports = function(data, g, width, height, options) {
9+
10+
811
// @todo make barOffset equal to longest label
9-
var barOffset = width / 4;
12+
// var barOffset = width / 4;
13+
var barHeight = 30;
1014

1115
// data.x is still the label, and data.y the length of the bar
1216
var x = d3.scale.linear()
13-
.domain([0, d3.max(_.pluck(data, 'y'))])
14-
.range([0, width - barOffset]);
17+
.domain([0, d3.sum(_.pluck(data, 'y'))])
18+
.range([0, width]);
1519

1620
var y = d3.scale.ordinal()
1721
.domain(_.pluck(data, 'x'))
1822
.rangeBands([0, height], 0.3, 0.0);
1923

24+
var sumY = d3.sum(_.pluck(data, 'y'));
25+
2026
// set up tooltips
2127
var tip = d3.tip()
2228
.attr('class', 'd3-tip')
23-
.html(function(d) {
24-
return d.tooltip || d.x;
29+
.html(function(d, i) {
30+
if (typeof d.tooltip === 'function') {
31+
return d.tooltip(d, i);
32+
}
33+
return d.tooltip || tooltipHtml({
34+
label: d.x,
35+
value: Math.round(d.y / sumY * 100)
36+
});
2537
})
2638
.direction('n')
2739
.offset([-9, 0]);
@@ -30,45 +42,40 @@ module.exports = function (data, g, width, height) {
3042
g.selectAll('*').remove();
3143
g.call(tip);
3244

45+
debug('data', _.pluck(data, 'y'));
46+
3347
var bar = g.selectAll('.bar')
3448
.data(data)
3549
.enter().append('g')
3650
.attr('class', 'bar')
37-
.attr('transform', function(d) {
38-
return 'translate(0, ' + y(d.x) + ')';
51+
.attr('transform', function(d, i) {
52+
var xpos = _.sum(_(data)
53+
.slice(0, i)
54+
.pluck('y')
55+
.value()
56+
);
57+
return 'translate(' + x(xpos) + ', ' + (height - barHeight) / 2 + ')';
3958
});
4059

41-
bar.append('rect')
42-
.attr('class', 'bg')
43-
.attr('x', barOffset)
44-
.attr('width', width - barOffset)
45-
.attr('height', y.rangeBand());
46-
4760
bar.append('rect')
4861
.attr('class', 'fg')
4962
.attr('y', 0)
50-
.attr('x', barOffset)
63+
.attr('x', 0)
5164
.attr('width', function(d) {
5265
return x(d.y);
5366
})
54-
.attr('height', y.rangeBand());
55-
56-
bar.append('rect')
57-
.attr('class', 'glass')
58-
.attr('x', barOffset)
59-
.attr('width', width - barOffset)
60-
.attr('height', y.rangeBand())
67+
.attr('height', barHeight)
6168
.on('mouseover', tip.show)
6269
.on('mouseout', tip.hide);
6370

6471
bar.append('text')
65-
.attr('dx', '-10')
66-
.attr('dy', '0.4em')
67-
.attr('y', y.rangeBand() / 2)
68-
.attr('x', barOffset)
69-
.attr('text-anchor', 'end')
72+
.attr('y', barHeight / 2)
73+
.attr('dy', '0.3em')
74+
.attr('dx', 10)
75+
.attr('text-anchor', 'start')
7076
.text(function(d) {
7177
return d.x;
72-
});
78+
})
79+
.attr('fill', 'white');
7380

7481
};
Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
var d3 = require('d3');
22
var _ = require('lodash');
3+
var tooltipHtml = require('./tooltip.jade');
34
var debug = require('debug')('scout-ui:minicharts:many');
45

56
require('d3-tip')(d3);
67

7-
module.exports = function (data, g, width, height, labels) {
8+
module.exports = function(data, g, width, height, options) {
9+
10+
options = _.defaults(options || {}, {
11+
bgbars: false,
12+
bglines: false,
13+
legend: true,
14+
labels: false // label options will be set further below
15+
});
816

917
var x = d3.scale.ordinal()
1018
.domain(_.pluck(data, 'x'))
@@ -14,14 +22,19 @@ module.exports = function (data, g, width, height, labels) {
1422
.domain([0, d3.max(_.pluck(data, 'y'))])
1523
.range([height, 0]);
1624

25+
var sumY = d3.sum(_.pluck(data, 'y'));
26+
1727
// set up tooltips
1828
var tip = d3.tip()
1929
.attr('class', 'd3-tip')
2030
.html(function(d, i) {
2131
if (typeof d.tooltip === 'function') {
2232
return d.tooltip(d, i);
2333
}
24-
return d.tooltip || d.x;
34+
return d.tooltip || tooltipHtml({
35+
label: d.x,
36+
value: d.y
37+
});
2538
})
2639
.direction('n')
2740
.offset([-9, 0]);
@@ -30,6 +43,37 @@ module.exports = function (data, g, width, height, labels) {
3043
g.selectAll('*').remove();
3144
g.call(tip);
3245

46+
if (options.legend) {
47+
g.append('text')
48+
.attr('x', 0)
49+
.attr('y', -10)
50+
.attr('text-anchor', 'start')
51+
.text(sumY);
52+
}
53+
54+
if (options.bglines) {
55+
g.append('line')
56+
.attr('class', 'bg line')
57+
.attr('x1', 0)
58+
.attr('x2', width)
59+
.attr('y1', 0)
60+
.attr('y2', 0);
61+
62+
g.append('line')
63+
.attr('class', 'bg line')
64+
.attr('x1', 0)
65+
.attr('x2', width)
66+
.attr('y1', height / 2)
67+
.attr('y2', height / 2);
68+
69+
g.append('line')
70+
.attr('class', 'bg line')
71+
.attr('x1', 0)
72+
.attr('x2', width)
73+
.attr('y1', height)
74+
.attr('y2', height);
75+
}
76+
3377
var bar = g.selectAll('.bar')
3478
.data(data)
3579
.enter().append('g')
@@ -38,12 +82,14 @@ module.exports = function (data, g, width, height, labels) {
3882
return 'translate(' + x(d.x) + ', 0)';
3983
});
4084

41-
bar.append('rect')
42-
.attr('class', 'bg')
43-
.attr('width', x.rangeBand())
44-
.attr('height', height);
85+
if (options.bgbars) {
86+
bar.append('rect')
87+
.attr('class', 'bg')
88+
.attr('width', x.rangeBand())
89+
.attr('height', height);
90+
}
4591

46-
bar.append('rect')
92+
var fgbars = bar.append('rect')
4793
.attr('class', 'fg')
4894
.attr('x', 0)
4995
.attr('y', function(d) {
@@ -54,39 +100,46 @@ module.exports = function (data, g, width, height, labels) {
54100
return height - y(d.y);
55101
});
56102

103+
if (options.bgbars) {
57104
bar.append('rect')
58105
.attr('class', 'glass')
59106
.attr('width', x.rangeBand())
60107
.attr('height', height)
61108
.on('mouseover', tip.show)
62109
.on('mouseout', tip.hide);
110+
} else {
111+
// atach tooltips directly to foreground bars
112+
fgbars
113+
.on('mouseover', tip.show)
114+
.on('mouseout', tip.hide);
115+
}
116+
117+
if (options.labels) {
118+
var labels = options.labels;
119+
_.defaults(labels, {
120+
'x': labels['text-anchor'] === 'middle' ? x.rangeBand() / 2 : function(d, i) {
121+
if (i === 0) return 0;
122+
if (i === data.length - 1) return x.rangeBand();
123+
return x.rangeBand() / 2;
124+
},
125+
'y': height + 5,
126+
'dy': '0.75em',
127+
'text-anchor': function(d, i) {
128+
if (i === 0) return 'start';
129+
if (i === data.length - 1) return 'end';
130+
return 'middle';
131+
},
132+
'text': function(d) {
133+
return d.x;
134+
}
135+
});
63136

64-
if (labels) {
65-
66-
_.defaults(labels, {
67-
'x': labels['text-anchor'] === 'middle' ? x.rangeBand() / 2 : function (d, i) {
68-
if (i === 0) return 0;
69-
if (i === data.length - 1) return x.rangeBand();
70-
return x.rangeBand() / 2;
71-
},
72-
'y': height+5,
73-
'dy': '0.75em',
74-
'text-anchor': function (d, i) {
75-
if (i === 0) return 'start';
76-
if (i === data.length - 1) return 'end';
77-
return 'middle';
78-
},
79-
'text': function (d) {
80-
return d.x;
81-
}
82-
});
83-
84-
bar.append('text')
137+
bar.append('text')
85138
.attr('x', labels.x)
86139
.attr('dx', labels.dx)
87140
.attr('y', labels.y)
88141
.attr('dy', labels.dy)
89142
.attr('text-anchor', labels['text-anchor'])
90143
.text(labels.text);
91-
}
144+
}
92145
};

0 commit comments

Comments
 (0)