D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Ljfernando
Full window
Github gist
HW1 Chart2
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> .bar { fill: steelblue; } .bar:hover { fill: brown; } .axis--x path { display: none; } .grid line { stroke: lightgrey; stroke-opacity: 0.7; shape-rendering: crispEdges; } .grid path { stroke-width: 0; } </style> <svg width="960" height="500"></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), margin = {top: 40, right: 50, bottom: 50, left: 200}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom; var x = d3.scaleLinear().rangeRound([0, width]), y = d3.scaleBand().rangeRound([0, height]).padding(0.1); var g = svg.append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); function make_x_gridlines() { return d3.axisBottom(x) .ticks(7) } d3.tsv("data2.tsv", function(d) { console.log(); d.Count = +d.Count; console.log(d.Count) return d; }, function(error, data) { if (error) throw error; x.domain([0, d3.max(data, function(d) { return d.Count; })]); y.domain(data.map(function(d) {return d.Category; })); g.append("g") .attr("class", "axis") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); g.append("text") // text label for the x axis .attr("x", (width / 2) ) .attr("y", height + margin.bottom ) .style("text-anchor", "middle") .text("Number of Records"); g.append("g") .attr("class", "axis axis--y") .call(d3.axisLeft(y)); g.append("g") .attr("class", "grid") .call(make_x_gridlines() .tickSize(height) .tickFormat("")) g.append("text") .attr("transform", "translate(-75,-10)") .attr("dy", "0.71em") .attr("fill", "#000") .text("Category"); g.append("text") .attr("x", (width / 2)) .attr("y", 0 - (margin.top / 2)) .attr("text-anchor", "middle") .style("font-size", "20px") .style("font-family", "Times New Roman") .text("Number of Calls Per Category"); g.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("y", function(d) { return y(d.Category); }) .attr("height", y.bandwidth()) .attr("width", function(d) { return x(d.Count); }); }); </script>
https://d3js.org/d3.v4.min.js