JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://d3js.org/d3.v3.min.js"></script>
<div id="chart"></div>
CSS
/*
rect:hover {
fill: #FF7575;
stroke: #DD3737;
shape-rendering: crispEdges;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
*/
JavaScript
var margin = {
top: 0,
right: 20,
bottom: 80,
left: 55
},
width = 600 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
data = [{
"label": "ONE",
"value": 194
}, {
"label": "TWO",
"value": 567
}, {
"label": "THREE",
"value": 1314
}, {
"label": "FOUR",
"value": 793
}, {
"label": "FIVE",
"value": 1929
}, {
"label": "SIX",
"value": 1383
}];
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .05, .3);
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")
.tickFormat(d3.format(".2s"));
data.sort(function (a, b) {
return b.value - a.value;
});
var svg = d3.select("#chart").append("svg")
.style("font", "12pt 'Segoe UI'")
.style("stroke", "none")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
x.domain(data.map(function (d) {
return d.label;
}));
y.domain([0, d3.max(data, function (d) {
return d.value;
}) + (d3.max(data, function (d) {
return d.value
}) / 10)]);
svg.append("g")
.attr("class", "grid")
.style("stroke", "#ddd")
.style("shape-rendering", "crispEdges")
.call(make_y_axis()
.tickSize(-width, 0, 0)
.tickFormat(""))
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.style("fill", "#FF5858")
.style("stroke", "#C52F0F")
.style("shape-rendering", "crispEdges")
.on("mouseover", function (d) {
d3.select(this)
.style("fill", "#FF7575")
})
.on("mouseout", function (d) {
d3.select(this)
.style("fill", "#FF5858")
})
.attr("class", "bar")
.attr("x", function (d) {
return x(d.label);
})
.attr("width",...