JSFiddle - React, Tailwind, and code Playground
by onino
HTML
<!DOCTYPE html>
<meta charset="utf-8">
<style>
text {
font-family: sans-serif;
}
.bar {
fill: cornflowerblue;
}
.bar:hover {
fill: pink;
}
.axis--x path {
display: none;
}
</style>
<svg width="100%" height="500px"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// SETUP
var svg = d3.select("svg"),
margin = { top: 20, right: 20, bottom: 30, left: 40 },
x = d3.scaleBand().padding(0.1),
y = d3.scaleLinear(),
theData = undefined;
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
g.append("g")
.attr("class", "axis axis--x");
g.append("g")
.attr("class", "axis axis--y");
g.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Frequency");
// DRAWING
function draw() {
var bounds = svg.node().getBoundingClientRect(),
width = bounds.width - margin.left - margin.right,
height = bounds.height - margin.top - margin.bottom;
x.rangeRound([0, width]);
y.rangeRound([height, 0]);
g.select(".axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.select(".axis--y")
.call(d3.axisLeft(y).ticks(10, "%"));
var bars = g.selectAll(".bar")
.data(theData);
// ENTER
bars
.enter().append("rect")
.attr("class", "bar")
.attr("x", function (d) { return x(d.letter); })
.attr("y", function (d) { return y(d.frequency); })
.attr("width", x.bandwidth())
.attr("height", function (d) { return height - y(d.frequency); });
// UPDATE
bars.attr("x", function (d) { return x(d.letter); })
.attr("y", function (d) { return y(d.frequency); })
.attr("width", x.bandwidth())
.attr("height", function (d) { return height - y(d.frequency); });
// EXIT
bars.exit()
.remove();
}
// LOADING...