index.html
<!DOCTYPE html>
<canvas id="bubble" height="600" width="400"></canvas>
<script src=//documentcloud.github.com/underscore/underscore.js></script>
<script src=//mbostock.github.com/d3/d3.v2.js?2.8.1></script>
<script>
d3.csv("flavorbible2.csv", function(csv) {
var data = _.filter(csv, function(d) {
return d["ingredient1"] == "chard";
});
console.log(data[0]["quality"]);
var taste_quality = {
heaven: 2,
high_rec: 1.4,
freq_rec: .8,
rec: .2
};
var data_for_graph = _.map(data, function(elm){
var w = 400, h = 400, max_r = 40,
r = taste_quality[elm["quality"]];
var x = Math.random();
while (((x*w) - (max_r * r) < 0) || ((x*w) + (max_r * r) > w))
{
x = Math.random();
}
var y = Math.random();
while (((h*y) - (max_r * r) < 0) || ((h*y) + (max_r *r) > h))
{
y = Math.random();
}
return [x, y, taste_quality[elm["quality"]], 'blue', elm["ingredient2"]];
});
graph(data_for_graph);
});
function graph(data) {
var canvas = document.getElementById('bubble');
var ctx = canvas.getContext('2d');
var w = 400,
h = 400,
max_r = 40;
_(data).each(bubble);
function bubble(d) {
ctx.fillStyle = d[3];
ctx.beginPath();
ctx.arc( w*d[0],
h*d[1],
max_r*d[2],
0,
2*Math.PI);
ctx.closePath();
ctx.fill();
ctx.font = "20pt Calibri";
ctx.fillStyle = "yellow";
ctx.fillText(d[4], w*d[0]-(max_r*d[2]), h*d[1]);
};
};
</script>
flavorbible2.csv
ingredient1,ingredient2,quality
chard,anchovies,heaven
chard,bacon,high_rec
chard,basil,freq_rec
chard,bcheese,rec