Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var Canvas = require('canvas'),
fs = require('fs');
var c = new Canvas(1000, 400);
var ctx = c.getContext('2d');
var w = 1000;
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, 1000, 400);
ctx.fillStyle = '#000';
var f = fs.readFileSync('keystrokes.log', 'utf8');
var lines = f.split('\n').slice(1);
var minutely = {};
for (var i = 0; i < lines.length; i++) {
var pts = lines[i].split(',');
var d = new Date(pts[0] * 1000);
var n = (d.getDay() * 24 * 60) + (d.getHours() * 60) + d.getMinutes();
if (!minutely[n]) minutely[n] = [];
minutely[n].push(+pts[1]);
}
var minutes = [];
for (var x in minutely) {
minutes.push([+x, minutely[x]]);
}
minutes = minutes.sort(function(a, b) { return a[0] - b[0] });
var days = 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday'.split(' ');
for (var i = 0; i < 7; i++) {
ctx.fillText(days[i], 10 + (i / 7) * w, 370);
}
ctx.fillText('keystroke activity over the course of four months', 10, 385);
minutes.map(function(m) {
m[1].map(function(j) {
ctx.fillRect(~~((m[0] / 10080) * w), ~~(350 - j / 3), 1, 1);
});
});
fs.writeFileSync('weekgram.png', c.toBuffer());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment