Skip to content

Instantly share code, notes, and snippets.

@ala747
Forked from javisantana/graph.js
Created May 30, 2013 11:47
Show Gist options
  • Save ala747/5677310 to your computer and use it in GitHub Desktop.
Save ala747/5677310 to your computer and use it in GitHub Desktop.
function graph(data, w, h, color) {
var canvas = document.createElement('canvas');
var max = 0;
// 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