-
-
Save ala747/5677310 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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