On Github ariaz / Intro-to-d3-using-tributary
by Ariel Azoulay
@ariazou
@Outbrain
var width = 800, height = 800; var svg = d3.select("body") .append("svg") .attr("width", width) .attr("height", height);
Given some JSON Data:
var people = [{ "name": "John", "city": "Boston" }, { "name": "Peter", "city": "New York" }];
Bind it to the document with d3:
d3.select("body") // Top container .append("ul") // Data container .selectAll("li") // Element name .data(people) // The JSON .enter() // And bind it! .append("li") .text(function(d){return d.name + " is from " + d.city}) // Set Properties .style('color', 'red');
var width = 600, height = 800; svg = d3.select("svg").attr("width", width).attr("height", height); var data = [{x:205, y:200}, {x:200, y:400}, {x:400, y:200}, {x:408, y:400}]; var squares = svg .selectAll('rect') .data(data) .enter() .append('svg:rect'); squares .attr('rx',50) .attr('ry',20) .attr('x', function(d){return d.x}) .attr('y', function(d){return d.y}) .attr('width', 100) .attr('height',100) .attr('stroke','black') .style("fill", function(d) { return "#FF0000";});Let's see it live!!
var n = 2; var data = []; for (var i = 0; i < n; i++){ for(var j = 0; j < n; j++){ var point = {x:(i+1)/(n+1), y:(j+1)/(n+1)}; data.push(point); } }
x_scale = d3.scale.linear() .range([0,width]) .domain([0,1]); y_scale = d3.scale.linear() .range([height,0]) .domain([0,1]);Check it:
var n = 2; squareWidth = 200/n, squareHeight = 200/n; zoomIn = 100/n; //transitions squares .on("mouseover", function() { d3.select(this).transition() .attr('width', squareWidth + zoomIn) .attr('height', squareHeight + zoomIn) .attr('rx',100) .attr('ry',100) .delay(0) .duration(50) }) .on("mouseout", function() { d3.select(this).transition() .attr('width', squareWidth) .attr('height', squareHeight) .attr('rx',n/2) .attr('ry',n/2) .delay(0) .duration(50) });Go Live!