Skip to content

Instantly share code, notes, and snippets.

@wrobstory
Last active December 29, 2015 06:38
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 wrobstory/7629900 to your computer and use it in GitHub Desktop.
Save wrobstory/7629900 to your computer and use it in GitHub Desktop.
D3 Brush and Tooltip I
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.point {
fill: #2f225d;
stroke: #afa2dc;
}
.selected {
fill: #afa2dc;
stroke: #2f225d;
}
.axis {
font: 10px sans-serif;
}
p {
font: 12px sans-serif;
margin: 0 0 2px 0;
padding: 0;
}
.clear-button {
font: 14px sans-serif;
cursor: pointer;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.brush .extent {
stroke: #fff;
fill-opacity: .125;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var data = [];
var values = [];
for (var i = 2; i <= 50; i++) {
var val = Math.floor(Math.random() * (50 - 5 + 1) + 5);
data.push({index: i, value: val});
values.push(val);
}
var margin = {top: 20, right: 20, bottom: 60, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width])
.domain([0, 50]);
var y = d3.scale.linear()
.range([height, 0])
.domain([0, d3.max(values) + 5]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(11);
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("clip-path", "url(#clip)")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
points = svg.selectAll(".point")
.data(data)
.enter().append("circle")
.attr("class", "point")
.attr("clip-path", "url(#clip)")
.attr("r", function(d){return Math.floor(Math.random() * (20 - 5 + 1) + 5);})
.attr("cx", function(d) { return x(d.index); })
.attr("cy", function(d) { return y(d.value); });
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment