Skip to content

Instantly share code, notes, and snippets.

@javisantana
Last active December 17, 2015 21:48
Show Gist options
  • Save javisantana/5677084 to your computer and use it in GitHub Desktop.
Save javisantana/5677084 to your computer and use it in GitHub Desktop.
function graph(data, w, h, color) {
var canvas = document.createElement('canvas');
var max = Number.MIN_VALUE;
// normalize
var len = data.length, i;
for(i = 0; i < len; ++i) {
max = Math.max(data[i], max);
}
for(i = 0; i < len; ++i) {
data[i] /= max;
}
//render
canvas.width = w;
canvas.height = h;
var barw = w/len;
var ctx = canvas.getContext('2d');
ctx.fillStyle = color;
for(i = 0; i < len; ++i) {
var hh = (h * data[i]) >> 0;
ctx.fillRect(i*barw, h - hh, barw, h);
}
return canvas;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment