Skip to content

Commit a96b419

Browse files
committed
Merge pull request #20 from 10gen/minichart-fixes
lots of minichart fixes
2 parents 92e1516 + 1439796 commit a96b419

File tree

14 files changed

+363
-175
lines changed

14 files changed

+363
-175
lines changed

scout-ui/src/field-list/basic-field.jade

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
.schema-field-name(data-hook='name')
66
div(data-hook='types-container')
77
.col-sm-7.col-sm-offset-1
8-
div(data-hook='minicharts-container')
8+
div(data-hook='minichart-container')

scout-ui/src/field-list/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var BasicFieldView = View.extend({
4444
},
4545
render: function() {
4646
this.renderWithTemplate(this);
47-
this.viewSwitcher = new ViewSwitcher(this.queryByHook('minicharts-container'));
47+
this.viewSwitcher = new ViewSwitcher(this.queryByHook('minichart-container'));
4848
},
4949
switchView: function(typeModel) {
5050
var type = typeModel._id.toLowerCase();

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
var d3 = require('d3');
22
var _ = require('lodash');
33
var few = require('./few');
4+
var shared = require('./shared');
45
var debug = require('debug')('scout-ui:minicharts:boolean');
56

67
module.exports = function(opts) {
78
var values = opts.data.values.toJSON();
89

910
// group by true/false
1011
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();
25-
26-
var margin = {
27-
top: 10,
28-
right: 0,
29-
bottom: 10,
30-
left: 0
31-
};
12+
.groupBy(function(d) {
13+
// extract string representations of values
14+
return d;
15+
})
16+
.defaults({
17+
false: [],
18+
true: []
19+
})
20+
.map(function(v, k) {
21+
return {
22+
label: k,
23+
value: v.length
24+
};
25+
})
26+
.sortByOrder('label', [false]) // order: false, true
27+
.value();
28+
29+
var margin = shared.margin;
3230

3331
var width = opts.width - margin.left - margin.right;
3432
var height = opts.height - margin.top - margin.bottom;

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

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
var d3 = require('d3');
22
var _ = require('lodash');
33
var moment = require('moment');
4+
var shared = require('./shared');
45
var debug = require('debug')('scout-ui:minicharts:date');
56
var many = require('./many');
67

8+
require('d3-tip')(d3);
9+
710
function generateDefaults(n) {
811
var doc = {};
9-
_.each(_.range(n), function (d) { doc[d] = 0; });
12+
_.each(_.range(n), function(d) {
13+
doc[d] = 0;
14+
});
1015
return doc;
1116
}
1217

@@ -17,7 +22,7 @@ module.exports = function(opts) {
1722
// distinguish ObjectIDs from real dates
1823
if (values.length && values[0]._bsontype !== undefined) {
1924
if (values[0]._bsontype === 'ObjectID') {
20-
values = _.map(values, function (v) {
25+
values = _.map(values, function(v) {
2126
return v.getTimestamp();
2227
});
2328
}
@@ -26,13 +31,7 @@ module.exports = function(opts) {
2631
// A formatter for dates
2732
var format = d3.time.format('%Y-%m-%d %H:%M:%S');
2833

29-
var margin = {
30-
top: 10,
31-
right: 0,
32-
bottom: 10,
33-
left: 0
34-
};
35-
34+
var margin = shared.margin;
3635
var width = opts.width - margin.left - margin.right;
3736
var height = opts.height - margin.top - margin.bottom;
3837
var el = opts.el;
@@ -45,20 +44,20 @@ module.exports = function(opts) {
4544
.range([0, width]);
4645

4746
var upperBarBottom = height / 2 - 20;
48-
var upperRatio = 2;
47+
var upperRatio = 2.5;
4948
var upperMargin = 15;
5049

5150
// group by weekdays
52-
var weekdayLabels = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
51+
var weekdayLabels = moment.weekdays();
5352
var weekdays = _(values)
54-
.groupBy(function (d) {
53+
.groupBy(function(d) {
5554
return moment(d).weekday();
5655
})
5756
.defaults(generateDefaults(7))
58-
.map(function (d, i) {
57+
.map(function(d, i) {
5958
return {
60-
x: weekdayLabels[i],
61-
y: d.length,
59+
label: weekdayLabels[i],
60+
value: d.length,
6261
tooltip: weekdayLabels[i]
6362
};
6463
})
@@ -67,25 +66,35 @@ module.exports = function(opts) {
6766
// group by hours
6867
var hourLabels = d3.range(24);
6968
var hours = _(values)
70-
.groupBy(function (d) {
69+
.groupBy(function(d) {
7170
return d.getHours();
7271
})
73-
.defaults(generateDefaults(23))
74-
.map(function (d, i) {
72+
.defaults(generateDefaults(24))
73+
.map(function(d, i) {
7574
return {
76-
x: hourLabels[i],
77-
y: d.length,
78-
tooltip: hourLabels[i] + 'h'
75+
label: hourLabels[i] + ':00',
76+
value: d.length,
77+
tooltip: hourLabels[i] + ':00'
7978
};
8079
})
8180
.value();
8281

8382
// clear element first
8483
d3.select(el).selectAll('*').remove();
8584

85+
// set up tooltips
86+
var tip = d3.tip()
87+
.attr('class', 'd3-tip')
88+
.html(function(d, i) {
89+
return format(d);
90+
})
91+
.direction('n')
92+
.offset([-9, 0]);
93+
8694
var svg = d3.select(el)
8795
.append('g')
88-
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
96+
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
97+
.call(tip);
8998

9099
var line = svg.selectAll('.line')
91100
.data(values)
@@ -98,7 +107,9 @@ module.exports = function(opts) {
98107
.attr('x2', function(d) {
99108
return barcodeX(d);
100109
})
101-
.attr('y2', barcodeBottom);
110+
.attr('y2', barcodeBottom)
111+
.on('mouseover', tip.show)
112+
.on('mouseout', tip.hide);
102113

103114
var text = svg.selectAll('.text')
104115
.data(barcodeX.domain())
@@ -112,24 +123,36 @@ module.exports = function(opts) {
112123
.attr('text-anchor', function(d, i) {
113124
return i ? 'end' : 'start';
114125
})
115-
.text(function(d) {
116-
return format(d);
126+
.text(function(d, i) {
127+
if (format(barcodeX.domain()[0]) === format(barcodeX.domain()[1])) {
128+
if (i === 0) {
129+
return 'inserted: ' + format(d);
130+
}
131+
} else {
132+
return (i ? 'last: ' : 'first: ') + format(d);
133+
}
117134
});
118135

119136
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];
137+
many(weekdays, weekdayContainer, width / (upperRatio + 1) - upperMargin, upperBarBottom, {
138+
bgbars: true,
139+
labels: {
140+
'text-anchor': 'middle',
141+
'text': function(d) {
142+
return d.label[0];
143+
}
124144
}
125145
});
126146

127147
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 : '';
148+
.attr('transform', 'translate(' + (width / (upperRatio + 1) + upperMargin) + ', 0)');
149+
150+
many(hours, hourContainer, width / (upperRatio + 1) * upperRatio - upperMargin, upperBarBottom, {
151+
bgbars: true,
152+
labels: {
153+
'text': function(d, i) {
154+
return (i % 6 === 0 || i === 23) ? d.label : '';
155+
}
133156
}
134157
});
135158

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
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-
// @todo make barOffset equal to longest label
9-
var barOffset = width / 4;
8+
module.exports = function(data, g, width, height, options) {
9+
10+
var barHeight = 25;
11+
var values = _.pluck(data, 'value');
12+
var sumValues = d3.sum(values);
1013

1114
// data.x is still the label, and data.y the length of the bar
1215
var x = d3.scale.linear()
13-
.domain([0, d3.max(_.pluck(data, 'y'))])
14-
.range([0, width - barOffset]);
15-
16-
var y = d3.scale.ordinal()
17-
.domain(_.pluck(data, 'x'))
18-
.rangeBands([0, height], 0.3, 0.0);
16+
.domain([0, sumValues])
17+
.range([0, width]);
1918

2019
// set up tooltips
2120
var tip = d3.tip()
2221
.attr('class', 'd3-tip')
23-
.html(function(d) {
24-
return d.tooltip || d.x;
22+
.html(function(d, i) {
23+
if (typeof d.tooltip === 'function') {
24+
return d.tooltip(d, i);
25+
}
26+
return d.tooltip || tooltipHtml({
27+
label: d.label,
28+
value: Math.round(d.value / sumValues * 100)
29+
});
2530
})
2631
.direction('n')
2732
.offset([-9, 0]);
@@ -33,42 +38,38 @@ module.exports = function (data, g, width, height) {
3338
var bar = g.selectAll('.bar')
3439
.data(data)
3540
.enter().append('g')
36-
.attr('class', 'bar')
37-
.attr('transform', function(d) {
38-
return 'translate(0, ' + y(d.x) + ')';
39-
});
41+
.attr('class', 'bar few')
42+
.attr('transform', function(d, i) {
4043

41-
bar.append('rect')
42-
.attr('class', 'bg')
43-
.attr('x', barOffset)
44-
.attr('width', width - barOffset)
45-
.attr('height', y.rangeBand());
44+
var xpos = _.sum(_(data)
45+
.slice(0, i)
46+
.pluck('value')
47+
.value()
48+
);
49+
return 'translate(' + x(xpos) + ', ' + (height - barHeight) / 2 + ')';
50+
});
4651

4752
bar.append('rect')
48-
.attr('class', 'fg')
53+
.attr('class', function(d, i) {
54+
return 'fg-' + i;
55+
})
4956
.attr('y', 0)
50-
.attr('x', barOffset)
57+
.attr('x', 0)
5158
.attr('width', function(d) {
52-
return x(d.y);
59+
return x(d.value);
5360
})
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())
61+
.attr('height', barHeight)
6162
.on('mouseover', tip.show)
6263
.on('mouseout', tip.hide);
6364

6465
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')
66+
.attr('y', barHeight / 2)
67+
.attr('dy', '0.3em')
68+
.attr('dx', 10)
69+
.attr('text-anchor', 'start')
7070
.text(function(d) {
71-
return d.x;
72-
});
71+
return d.label;
72+
})
73+
.attr('fill', 'white');
7374

7475
};

0 commit comments

Comments
 (0)