Skip to content

Commit 9180aa2

Browse files
imlucaskangas
authored andcommitted
Add raf + minichart speedups
- @see http://npm.im/raf - Add function names to make flame graphs easier to read - date minicharts were each always re-listing weekdays which are a const - date mini charts embed two other mini charts, each of which can be rendered on their own animation frames - defer rendering of minichart subviews and defer again before calling d3
1 parent 045919b commit 9180aa2

File tree

7 files changed

+40
-37
lines changed

7 files changed

+40
-37
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"phantomjs-polyfill": "0.0.1",
8686
"pluralize": "^1.1.2",
8787
"qs": "^3.1.0",
88+
"raf": "^3.0.0",
8889
"scout-brain": "http://bin.mongodb.org/js/scout-brain/v0.0.2/scout-brain-0.0.2.tar.gz",
8990
"scout-client": "http://bin.mongodb.org/js/scout-client/v0.0.3/scout-client-0.0.3.tar.gz",
9091
"scout-server": "http://bin.mongodb.org/js/scout-server/v0.0.4/scout-server-0.0.4.tar.gz",

src/minicharts/d3fns/boolean.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var _ = require('lodash');
33
var few = require('./few');
44
var shared = require('./shared');
55

6-
module.exports = function(opts) {
6+
var minicharts_d3fns_boolean = function(opts) {
77
var values = opts.data.values.toJSON();
88

99
// group by true/false
@@ -41,3 +41,4 @@ module.exports = function(opts) {
4141
few(data, g, width, height);
4242
};
4343

44+
module.exports = minicharts_d3fns_boolean;

src/minicharts/d3fns/date.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var _ = require('lodash');
33
var moment = require('moment');
44
var shared = require('./shared');
55
var many = require('./many');
6+
var raf = require('raf');
67

78
require('../d3-tip')(d3);
89

@@ -14,7 +15,9 @@ function generateDefaults(n) {
1415
return doc;
1516
}
1617

17-
module.exports = function(opts) {
18+
var weekdayLabels = moment.weekdays();
19+
20+
var minicharts_d3fns_date = function(opts) {
1821
var values = opts.data.values.toJSON();
1922

2023
// distinguish ObjectIDs from real dates
@@ -46,7 +49,6 @@ module.exports = function(opts) {
4649
var upperMargin = 20;
4750

4851
// group by weekdays
49-
var weekdayLabels = moment.weekdays();
5052
var weekdays = _(values)
5153
.groupBy(function(d) {
5254
return moment(d).weekday();
@@ -131,14 +133,16 @@ module.exports = function(opts) {
131133

132134
var weekdayContainer = svg.append('g');
133135

134-
many(weekdays, weekdayContainer, width / (upperRatio + 1) - upperMargin, upperBarBottom, {
135-
bgbars: true,
136-
labels: {
137-
'text-anchor': 'middle',
138-
text: function(d) {
139-
return d.label[0];
136+
raf(function() {
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+
}
140144
}
141-
}
145+
});
142146
});
143147

144148
// calendar icon
@@ -154,14 +158,15 @@ module.exports = function(opts) {
154158

155159
var hourContainer = svg.append('g')
156160
.attr('transform', 'translate(' + (width / (upperRatio + 1) + upperMargin) + ', 0)');
157-
158-
many(hours, hourContainer, width / (upperRatio + 1) * upperRatio - upperMargin, upperBarBottom, {
159-
bgbars: true,
160-
labels: {
161-
text: function(d, i) {
162-
return i % 6 === 0 || i === 23 ? d.label : '';
161+
raf(function() {
162+
many(hours, hourContainer, width / (upperRatio + 1) * upperRatio - upperMargin, upperBarBottom, {
163+
bgbars: true,
164+
labels: {
165+
text: function(d, i) {
166+
return i % 6 === 0 || i === 23 ? d.label : '';
167+
}
163168
}
164-
}
169+
});
165170
});
166171

167172
// clock icon
@@ -175,3 +180,5 @@ module.exports = function(opts) {
175180
.attr('font-family', 'FontAwesome')
176181
.text('\uf017');
177182
};
183+
184+
module.exports = minicharts_d3fns_date;

src/minicharts/d3fns/many.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var shared = require('./shared');
55

66
require('../d3-tip')(d3);
77

8-
module.exports = function(data, g, width, height, options) {
8+
var minicharts_d3fns_many = function(data, g, width, height, options) {
99
options = _.defaults(options || {}, {
1010
bgbars: false,
1111
scale: false,
@@ -156,3 +156,4 @@ module.exports = function(data, g, width, height, options) {
156156
.text(labels.text);
157157
}
158158
};
159+
module.exports = minicharts_d3fns_many;

src/minicharts/d3fns/shared.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function decimalPlaces(number) {
55
return ((+number).toFixed(20)).replace(/^-?\d*\.?|0+$/g, '').length;
66
}
77

8-
module.exports = {
8+
var minicharts_d3fns_shared = {
99

1010
margin: {
1111
top: 10,
@@ -33,3 +33,4 @@ module.exports = {
3333
};
3434
}
3535
};
36+
module.exports = minicharts_d3fns_shared;

src/minicharts/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var AmpersandView = require('ampersand-view');
33
var UniqueMinichartView = require('./unique');
44
var vizFns = require('./d3fns');
55
var _ = require('lodash');
6+
var raf = require('raf');
67

78
// a wrapper around VizView to set common default values
89
module.exports = AmpersandView.extend({
@@ -33,7 +34,8 @@ module.exports = AmpersandView.extend({
3334
} else {
3435
this.subview = new VizView(this.viewOptions);
3536
}
36-
37-
this.renderSubview(this.subview, this.queryByHook('minichart'));
37+
raf(function() {
38+
this.renderSubview(this.subview, this.queryByHook('minichart'));
39+
}.bind(this));
3840
}
3941
});

src/minicharts/viz.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var AmpersandView = require('ampersand-view');
22
var _ = require('lodash');
3+
var raf = require('raf');
34
var $ = require('jquery');
45

56
var VizView = AmpersandView.extend({
@@ -121,30 +122,19 @@ var VizView = AmpersandView.extend({
121122

122123
// call viz function
123124
if (this.vizFn) {
124-
this.vizFn({
125+
var opts = {
125126
width: this.width,
126127
height: this.height,
127128
data: this.data,
128129
el: this.el
130+
};
131+
var vizFn = this.vizFn.bind(this, opts);
132+
raf(function minicharts_viz_call_vizfn() {
133+
vizFn();
129134
});
130135
}
131136
return this;
132137
}
133-
// redraw: function() {
134-
// this._chooseDataSource();
135-
// this.data = this.transform(this.data);
136-
//
137-
// this._measure();
138-
//
139-
// if (this.vizFn) {
140-
// this.vizFn({
141-
// width: this.width,
142-
// height: this.height,
143-
// data: this.data,
144-
// el: this.el,
145-
// });
146-
// }
147-
// }
148138
});
149139

150140
module.exports = VizView;

0 commit comments

Comments
 (0)