Last active
February 23, 2018 18:12
-
-
Save lincolnfrias/9dd4ee79cb0de9f3ae2421fc31c4d355 to your computer and use it in GitHub Desktop.
scatterplot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>D3: A simple scatterplot with SVG</title> | |
<script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script> | |
<style type="text/css"> | |
/* No style rules here yet */ | |
</style> | |
</head> | |
<body> | |
Following Scott Murray book. | |
<script type="text/javascript"> | |
var w = 500; | |
var h = 200; | |
var dataset = [ | |
[15, 20], | |
[480, 90], | |
[250, 50], | |
[100, 33], | |
[330, 95], | |
[410, 12], | |
[475, 44], | |
[25, 67], | |
[85, 21], | |
[220, 88] | |
]; | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", w) | |
.attr("height", h); | |
svg.selectAll("circle") | |
.data(dataset) | |
.enter() | |
.append("circle") | |
.attr("cx", function(d) { | |
return d[0]; | |
}) | |
.attr("cy", function(d) { | |
return d[1]; | |
}) | |
.attr("r", function(d) { | |
return Math.sqrt(h - d[1]) // transforma raio em área (nesse caso, dividir por π é desnecessário) | |
}) | |
.attr('fill', 'darkmagenta'); | |
svg.selectAll('text') | |
.data(dataset) | |
.enter() | |
.append('text') | |
.text(function(d) { | |
return d[0] + ',' + d[1]; | |
}) | |
.attr('x', function(d) { | |
return d[0]; | |
}) | |
.attr('y', function(d) { | |
return d[1]; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment