|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
|
|
.bar { |
|
fill: #9FD0EC; |
|
// fill: #C7DC94; |
|
} |
|
|
|
.bar:hover { |
|
// fill: #F7E7CE; |
|
} |
|
|
|
.axis { |
|
font: 10px sans-serif; |
|
} |
|
|
|
.tick line{ |
|
opacity: 0.8; |
|
stroke-dasharray: 5 5; |
|
} |
|
|
|
div.tooltip { |
|
position: absolute; |
|
text-align: center; |
|
width: 60px; |
|
height: 28px; |
|
padding: 0.7em; |
|
font: 12px sans-serif; |
|
background: white; |
|
border: solid 1px; |
|
border-color: #E8E9E8; |
|
/* border-radius: 8px; */ |
|
pointer-events: none; |
|
} |
|
|
|
.axis path, |
|
.axis line { |
|
style: dot; |
|
fill: none; |
|
stroke: #E4E4E5; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
/* disable left ticking text*/ |
|
.y.axis text{ |
|
// display: none; |
|
} |
|
|
|
|
|
.must-show{ |
|
display: inline; |
|
} |
|
|
|
.x.axis path { |
|
/* display: inline; */ |
|
} |
|
|
|
</style> |
|
<body> |
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> |
|
<script> |
|
|
|
var margin = {top: 20, right: 40, bottom: 30, left: 40}, |
|
width = 400 - margin.left - margin.right, |
|
height = 300 - margin.top - margin.bottom; |
|
|
|
/*spacing for space between bars*/ |
|
var spacing = 0.35; |
|
var x = d3.scale.ordinal() |
|
.rangeRoundBands([0, width], spacing); |
|
|
|
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") |
|
.tickSize(-width) |
|
.ticks(10); |
|
|
|
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 + ")"); |
|
|
|
var div = d3.select("body").append("div") |
|
.attr("class", "tooltip") |
|
.style("opacity", 0); |
|
|
|
d3.tsv("newdatafile.txt", type, function(error, data) { |
|
if (error) throw error; |
|
|
|
x.domain(data.map(function(d) { return d.letter; })); |
|
y.domain([0, d3.max(data, function(d) { return d.frequency; })]); |
|
|
|
svg.append("g") |
|
.attr("class", "x axis") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(xAxis); |
|
|
|
svg.append("g") |
|
.attr("class", "y axis") |
|
.call(yAxis) |
|
.append("text") |
|
.attr("transform", "rotate(-90)") |
|
.attr("class", 'must-show') |
|
.attr("y", 6) |
|
.attr("dy", ".71em") |
|
.style("text-anchor", "end"); |
|
//.text("Total # of Cases"); |
|
|
|
svg.selectAll(".bar") |
|
.data(data) |
|
.enter().append("rect") |
|
.on("mouseover", function(d) { |
|
div.transition() |
|
.duration(200) |
|
.style("opacity", .9); |
|
div.html("hahaha" + "<br/>" + d.close) |
|
.style("left", (d3.event.pageX) + "px") |
|
.style("top", (d3.event.pageY - 28) + "px"); |
|
}) |
|
.on("mouseout", function(d) { |
|
div.transition() |
|
.duration(500) |
|
.style("opacity", 0); |
|
}) |
|
.attr("class", "bar") |
|
.attr("x", function(d) { return x(d.letter); }) |
|
.attr("width", x.rangeBand()) |
|
.attr("y", function(d) { return y(d.frequency); }) |
|
.attr("height", function(d) { return height - y(d.frequency); }); |
|
}); |
|
|
|
function type(d) { |
|
d.frequency = +d.frequency; |
|
return d; |
|
} |
|
|
|
</script> |