D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
daveeisen
Full window
Github gist
New England Minimum Wage
Built with
blockbuilder.org
<!DOCTYPE html> <style> .bar:hover { opacity: 0.7; } .label { font-family: Arial; font-size: 14pt; } div.tooltip { position: absolute; text-align: center; width: 100px; height: 28px; padding: 2px; font: 12px sans-serif; background: black; border: 0px; border-radius: 8px; pointer-events: none; color: white; } .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: Arial; font-size: 14px; } </style> <svg width="500" height="500"></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), margin = {top: 20, right: 20, bottom: 100, left: 80}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom, g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var x0 = d3.scaleBand() .rangeRound([0, width]) .paddingInner(0.1); var x1 = d3.scaleBand() .padding(0.05); var y = d3.scaleLinear() .rangeRound([height, 0]); var z = d3.scaleOrdinal() .range(["#3B88AD", "#FFA500"]); function round2Fixed(value) { value = +value; if (isNaN(value)) return NaN; // Shift value = value.toString().split('e'); value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + 2) : 2))); // Shift back value = value.toString().split('e'); return (+(value[0] + 'e' + (value[1] ? (+value[1] - 2) : -2))).toFixed(2); } var yAxisLabelText = "Minimum Hourly Wage ($)"; var yAxisLabelOffset = 35; var tooltip = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); d3.csv("MinimumWage.csv", function(d, i, columns) { for (var i = 1, n = columns.length; i < n; ++i) d[columns[i]] = +d[columns[i]]; return d; }, function(error, data) { if (error) throw error; var keys = data.columns.slice(1); x0.domain(data.map(function(d) { return d.State; })); x1.domain(keys).rangeRound([0, x0.bandwidth()]); y.domain([0, 15]); var bar = g.selectAll(".bar") .data(data) .enter().append("g") .attr("transform", function(d) { return "translate(" + x0(d.State) + ",0)"; }); bar.selectAll("rect") .data(function(d) { return keys.map(function(key) { return {key: key, value: d[key]}; }); }) .enter().append("rect") .attr("class", "bar") .attr("x", function(d) { return x1(d.key); }) .attr("y", function(d) { return y(d.value); }) .attr("width", x1.bandwidth()) .attr("height", function(d) { return height - y(d.value); }) .attr("fill", function(d) { return z(d.key); }); bar .on("mouseover", function(d) { tooltip.transition() .duration(200) .style("opacity", .9); var elements = document.querySelectorAll(':hover'); l = elements.length l = l-1 elementData = elements[l].__data__ tooltip.html(d.State + ":<br/>$" + round2Fixed(elementData.value) + " in " + elementData.key) .style("left", (d3.event.pageX + 5) + "px") .style("top", (d3.event.pageY - 28) + "px"); }) .on("mouseout", function(d) { tooltip.transition() .duration(500) .style("opacity", 0); }) g.append("g") .attr("transform", "translate(0, "+y(7.25)+")") .append("line") .attr("x2", width) .style("stroke", "Red") .style("stroke-width", "2px"); g.append("g") .attr("class", "axis") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x0)) .selectAll("text") .attr("transform", "translate(-12,4), rotate(-45)") .style("text-anchor", "end"); g.append("g") .attr("class", "axis") .call(d3.axisLeft(y).ticks(4)) .append("text") .attr("x", 2) .attr("y", y(y.ticks().pop()) + 0.5) .attr("dy", "0.32em") .attr("fill", "#000") .attr("font-weight", "bold") .attr("text-anchor", "start"); var yAxisLabel = g.append("text") .style("text-anchor", "middle") .attr("transform", "translate(-" + yAxisLabelOffset + "," + (height / 2) + ") rotate(-90)") .attr("class", "label") .text(yAxisLabelText); var legend = g.append("g") .attr("font-family", "sans-serif") .attr("font-size", 12) .attr("text-anchor", "end") .selectAll("g") .data(keys.slice().reverse()) .enter().append("g") .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); legend.append("rect") .attr("x", width - 19) .attr("width", 19) .attr("height", 19) .attr("fill", z); legend.append("text") .attr("x", width - 24) .attr("y", 9.5) .attr("dy", "0.32em") .text(function(d) { return d; }); g.append("rect") .attr("x", width - 19) .attr("y", 50) .attr("width", 19) .attr("height", 2) .attr("fill", "red"); g.append("text") .attr("transform","translate(" + (width-24) + ", 54)") .attr("font-family", "sans-serif") .attr("font-size", 12) .attr("text-anchor", "end") .text("Federal Minimum Wage: $7.25"); }); </script>
https://d3js.org/d3.v4.min.js