D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jalague
Full window
Github gist
Homework 1 Visualization1
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> .axis { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .x.axis path { display: ; } </style> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> <script> var margin = {top: 20, right: 20, bottom: 30, left: 60}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1); 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(10, ""); 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.csv("data2.csv", type, function(error, data) { if (error) throw error; x.domain(data.map(function(d) { return d.District; })); y.domain([0, 2400]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", -50) .attr("dy", ".71em") .attr("x", -125) .style("text-anchor", "end") .text("Number of Records") .style("font-weight","bold"); svg.selectAll(".bar") .data(data) .enter().append("rect") .attr({x: 400, y: 700}) .transition().duration(3000).ease("elastic").attr() .attr("class", "bar") .attr("x", function(d) { return x(d.District); }) .attr("width", x.rangeBand()) .attr("y", function(d) { return y(d.Records); }) .attr("height", function(d) { return height - y(d.Records); }) .style("fill", function(d) { if (d.Records === 0) { return "#513f00"; } else if (d.Records <= 950) { return "#ff93b7"; } else if (d.Records >= 950 && d.Records <= 1200) { return "#ff6b7c"; } else if (d.Records >= 1200 && d.Records <= 1850) { return "#fe5c43"; } else if (d.Records > 1850) { return "#d60007"; } return "#800026";}) }); function type(d) { d.Records = +d.Records; return d; } </script>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js