Skip to content

Instantly share code, notes, and snippets.

@joshcarr
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save joshcarr/cb618db1da61149f54b2 to your computer and use it in GitHub Desktop.

Select an option

Save joshcarr/cb618db1da61149f54b2 to your computer and use it in GitHub Desktop.
Scatterplot
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
</head>
<body>
<script>
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
function drawScatterplot(){
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 500 - margin.left - margin.right,
height = 480 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
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 + ")");
var data = [],
i;
for (i = 0; i < 500; i++) {
data.push({
temperature: getRandomArbitrary(20,30),
humidity: getRandomArbitrary(20,60),
location: "Location One"
});
}
for (i = 0; i < 500; i++) {
data.push({
temperature: getRandomArbitrary(25,35),
humidity: getRandomArbitrary(15,40),
location: "Location Two"
});
}
for (i = 0; i < 500; i++) {
data.push({
temperature: getRandomArbitrary(10,35),
humidity: getRandomArbitrary(10,50),
location: "Location Three"
});
}
data.forEach(function(d) {
d.temperature = +d.temperature;
d.humidity = +d.humidity;
});
x.domain([0,80]).nice();
y.domain([0, 40]).nice();
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Humidity (%RH)");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Temperature (deg C)");
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 2.75)
.attr("cx", function(d) { return x(d.humidity); })
.attr("cy", function(d) { return y(d.temperature); })
.style("fill", function(d) { return color(d.location); });
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
}
drawScatterplot();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment