Skip to content

Instantly share code, notes, and snippets.

@merrillcook0
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merrillcook0/80e6cc31d08e245329da to your computer and use it in GitHub Desktop.
Save merrillcook0/80e6cc31d08e245329da to your computer and use it in GitHub Desktop.
Basic_D3_Bar_Chart_funcionality
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Drawing Shapes W/ D3 -"/>
<meta charset="utf-8">
<title>Bar Chart Features</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<style type="text/css">
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar:hover{
fill:yellow;
}
div.tooltip {
position: absolute;
text-align: center;
width: 200px;
/* height: 28px; */
padding: 2px;
font: 12px sans-serif;
background: #008AB8;
border: 0px;
border-radius: 6px;
pointer-events: none;
}
</style>
</head>
<body>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 60},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var dataset=[];
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.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 + ")");
d3.tsv("http://localhost/data.tsv", type, function(error, data) {
x.domain(data.map(function(d) { return d.year; }));
y.domain([d3.min(data, function(d) { return d.percentage; }), d3.max(data, function(d) { return d.percentage; })]);
dataset=data;
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("x", width*.96)
.text("Year");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Percentage");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.year); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.percentage); })
.attr("height", function(d) { return height - y(d.percentage); })
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.html("<h3>Values</h3><br><strong>Year:</strong>" + d.year+"<br><strong>Percentage:</strong>"+d.percentage*100+"%"+"<br> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
});
function type(d) {
d.percentage = +d.percentage/100;
return d;
}
//append tooltips
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment