D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
marreguin523
Full window
Github gist
Chart2
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <style> .grid line { stroke: #2e34e5; stroke-opacity: 0.1; shape-rendering: crispEdges; } .grid path { stroke-width: 0; } </style> </head> <body> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var margin = {top: 35, right: 80, bottom: 80, left: 80}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scaleLinear().range([0, width]), y = d3.scaleBand().range([height, 0]); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom); var g = svg.append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); function make_x_gridlines() { return d3.axisBottom(x) .ticks(20) } d3.csv("data3.csv", function(error, data) { data.forEach(function(d) { d.District = d.District d.Count = +d.Count; }); x.domain([0, 800]); y.domain(data.map(function(d) { return d.District; })).padding(0.1); svg.append("g") .attr("class", "grid") .attr("transform", "translate("+margin.left+"," +height*1.1+")") .call(make_x_gridlines() .tickSize(-height) .tickFormat("") ); g.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x).ticks(20)) .append("text") .attr("dx", 425) .attr("dy", 40) .style("font", "10px sans-serif") .attr("fill", "#000") .text("Number of Cases"); g.append("g") .attr("class", "y axis") .call(d3.axisLeft(y)); g.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("fill", "#2a5783") .attr("height", y.bandwidth) .attr("x", 0) .attr("y", function(d) { return y(d.District); }) .attr("width", function(d) { return x(d.Count); }); }); </script> </body>
https://d3js.org/d3.v4.min.js