var margin = {top: 80, right: 180, bottom: 80, left: 180}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var colors = d3.scale.category20().domain([0, 100]); var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1, .3); var y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left") .ticks(8, "%"); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); d3.tsv("data.tsv", type, function(error, data) { x.domain(data.map(function(d) { return d.name; })); y.domain([0, d3.max(data, function(d) { return d.value; })]); svg.append("text") .attr("class", "title") .attr("x", x(data[0].name)) .attr("y", -26) .text("Why Are We Leaving Facebook?"); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .selectAll(".tick text") .call(wrap, x.rangeBand()) .selectAll("tspan") .style('stroke', 'none') .style('fill', function(d, i) { return colors(d.length) }); svg.append("g") .attr("class", "y axis") .call(yAxis); svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function(d) { return x(d.name); }) .attr("width", x.rangeBand()) .attr("y", function(d) { return y(d.value); }) .attr("height", function(d) { return height - y(d.value); }); }); function wrap(text, width) { text.each(function() { var text = d3.select(this), words = text.text().split(/\s+/).reverse(), word, lines = [], line = [], lineNumber = 0, lineHeight = 1.1, // ems y = text.attr("y"), dy = parseFloat(text.attr("dy")); gauge_tspan = text.text(null).append("tspan").style('opacity', 0); lines.push(line); while (word = words.pop()) { line.push(word); gauge_tspan.text(line.join(" ")); if (gauge_tspan.node().getComputedTextLength() > width) { line.pop(); line = [word]; lines.push(line); }; } var tspans = text .text(null) .selectAll('tpan') .data(lines.map(function(line) { return line.join(" ") })); tspans.exit().remove(); tspans.enter() .append("tspan") .attr("x", 0) .attr("y", y) tspans .text(function(line) { return line }) .attr("dy", function(line, index) { return dy + index * lineHeight + "em" }) }); } function mikes_wrap(text, width) { text.each(function() { var text = d3.select(this), words = text.text().split(/\s+/).reverse(), word, line = [], lineNumber = 0, lineHeight = 1.1, // ems y = text.attr("y"), dy = parseFloat(text.attr("dy")), tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em"); while (word = words.pop()) { line.push(word); tspan.text(line.join(" ")); if (tspan.node().getComputedTextLength() > width) { line.pop(); tspan.text(line.join(" ")); line = [word]; tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word); } } }); } function type(d) { d.value = +d.value; return d; }