JSFiddle - React, Tailwind, and code Playground
by wrxsti85
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
CSS
.bar {
fill: #3498db;
}
.bar:hover {
fill: #2ecc71;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.legend {
padding: 5px;
font: 10px sans-serif;
background: yellow;
box-shadow: 2px 2px 1px #888;
}
.tooltip {
background: #eee;
box-shadow: 0 0 5px #999999;
color: #333;
font-size: 12px;
left: 130px;
padding: 10px;
position: absolute;
text-align: center;
top: 95px;
z-index: 10;
display: block;
opacity: 0;
}
JavaScript
var data=[
{"month": "Par 3", "to": 166,"from": 103},
{"month": "Par 4", "to": 420,"from": 202},
{"month": "Par 5", "to": 502,"from": 307},
]
var margin = {top: 50, right: 50, bottom: 50, left: 100},
width = 900 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var y = d3.scale.ordinal()
.rangeRoundBands([0, height], .08);
var x = d3.scale.linear()
.range([0,width]);
y.domain(data.map(function(d) { return d.month; }));
x.domain([d3.min(data,function(d){return d.from;}), d3.max(data,function(d){return d.to;})]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(15);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("x", width-75)
.attr("dx", ".71em")
.attr("dy", "-.71em")
.text("Yards");
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("y", function(d) { return y(d.month); })
.attr("height", y.rangeBand())
.attr("x", function(d) { return x(d.from); })
.attr("width", function(d) { return x(d.to)-x(d.from) });
// add legend
var legend = svg.append("g")
.attr("class", "legend")
legend
.append("rect")
.attr("x", width-margin.left)
.attr("y", -10)
.attr("width", 10)
...