// select svg canvas var m = [30, 10, 10, 10], // margins w = 500-m[1]-m[3], // width h = 400-m[0]-m[2], // height xcol = 0, // active x column ycol = 1; // active y column // create svg element, adjusted by margins svg = d3.select('#chart') .append("g") .attr("transform", "translate(" + m[1] + "," + m[0] + ")"); // load data from csv file d3.csv('nutrients.csv', function(data) { // get columns of csv, mark excluded columns var columns = d3.keys(data[0]), excluded = ['name', 'group', 'id']; // get quantitative dimensions var dimensions = _(columns) .difference(excluded); // extents for each dimension var extents = _(dimensions) .map(function(col) { return [0, d3.max(data, function(d) { return parseFloat(d[col]) })] }); // create scales var x = d3.scale.linear().domain(extents[xcol]).range([0, w]), y = d3.scale.linear().domain(extents[ycol]).range([h, 0]); // color scale 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' }; // bind data to chart svg.selectAll('circle') .data(data) .enter().append('circle') .attr("fill", function(d) { return color[d.group]; }) .attr("cx", function(d) { return x(d[dimensions[xcol]]) || 0; }) .attr("cy", function(d) { return y(d[dimensions[ycol]]) || h; }) .attr("r", 2.5); // change x axis function xaxis(i) { x.domain(extents[i]); svg.selectAll('circle') .transition() .duration(1500) .attr("cx", function(d) { return x(d[dimensions[i]]) || 0; }); }; // change y axis function yaxis(i) { y.domain(extents[i]); svg.selectAll('circle') .transition() .duration(1500) .attr("cy", function(d) { return y(d[dimensions[i]]) || h; }) }; // create dropdowns to change axes 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; });