scatter.js
var m = [30, 10, 10, 10],
w = 500,
h = 400,
dimensions = [],
xcol = 0,
ycol = 1,
last = [],
transition_count = 0,
xscale = d3.scale.linear(),
yscale = d3.scale.linear();
var color = {
"Baby Foods" : '#555555',
"Baked Products" : '#7f7f7f',
"Beverages" : '#c49c94',
"Breakfast Cereals" : '#9467bd',
"Cereal Grains and Pasta" : '#bcbd22',
"Dairy and Egg Products" : '#ff7f0e',
"Ethnic Foods" : '#e7ba52',
"Fast Foods" : '#dbdb8d',
"Fats and Oils" : '#ffbb78',
"Finfish and Shellfish Products" : '#e377c2',
"Fruits and Fruit Juices" : '#c5b0d5',
"Legumes and Legume Products" : '#f7b6d2',
"Meals, Entrees, and Sidedishes" : '#17becf',
"Nut and Seed Products" : '#8c564b',
"Pork Products" : "#00ee99",
"Poultry Products" : '#d62728',
"Restaurant Foods" : '#1f77b4',
"Sausages and Luncheon Meats" : '#ff9896',
"Snacks" : '#9edae5',
"Soups, Sauces, and Gravies" : '#98df8a',
"Spices and Herbs" : '#aec7e8',
"Sweets" : '#c7c7c7',
"Vegetables and Vegetable Products" : '#2ca02c'
};
canvas = d3.select("#chart")
.attr("width", w + "px")
.attr("height", h + "px");
ctx = canvas[0][0].getContext('2d');
ctx.strokeStyle = "rgba(0,0,0,0.8)";
ctx.lineWidth = "1.5";
d3.csv('nutrients.csv', function(data) {
var columns = d3.keys( data[0] ),
excluded = ['name', 'group', 'id'];
dimensions = _(columns)
.difference(excluded);
var extents = _(dimensions)
.map(function(col) {
return [0, d3.max(data, function(d) { return parseFloat(d[col]) })]
});
xscale.domain(extents[xcol]).range([m[3], w-m[1]]),
yscale.domain(extents[ycol]).range([h-m[2], m[0]]);
last = data.map(position)
last.forEach(circle);
function xaxis(i) {
xcol = i;
xscale.domain(extents[i]);
transition(++transition_count);
};
function yaxis(i) {
ycol = i;
yscale.domain(extents[i]);
transition(++transition_count);
};
d3.select("#xaxis")
.selectAll("option")
.data(dimensions)
.enter().append("option")
.attr("value", function(d,i) { return i; })
.text(function(d) { return d; })
.each(function(d,i) {
if (i == xcol) d3.select(this).attr("selected", "yes");
});
d3.select("#xaxis")
.on("change", function() { xaxis(this.selectedIndex) });
d3.select("#yaxis")
.selectAll("option")
.data(dimensions)
.enter().append("option")
.attr("value", function(d,i) { return i; })
.text(function(d) { return d; })
.each(function(d,i) {
if (i == ycol) d3.select(this).attr("selected", "yes");
});
d3.select("#yaxis")
.on("change", function() { yaxis(this.selectedIndex) });
window.data = data;
});
function transition(count) {
var next = data.map(position);
var transition = d3.interpolate(last, next);
d3.timer(function(t) {
if (count < transition_count) return true;
clear();
if (t > 1000) {
last = next;
last.forEach(circle);
return true
};
last = transition(t/1000);
last.forEach(circle);
});
};
function clear() {
ctx.clearRect(0,0,w,h);
};
function position(d) {
var x = xscale(d[dimensions[xcol]]);
var y = yscale(d[dimensions[ycol]]);
return [x,y,color[d.group]];
};
function circle(pos) {
ctx.fillStyle = pos[2];
ctx.beginPath();
ctx.arc(pos[0],pos[1],2,0,2*Math.PI);
ctx.stroke();
ctx.fill();
};