Skip to content

Instantly share code, notes, and snippets.

@scottlittle
Last active March 16, 2017 01:59
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 scottlittle/07774e25cca022c3c09e1fa23e85da10 to your computer and use it in GitHub Desktop.
Save scottlittle/07774e25cca022c3c09e1fa23e85da10 to your computer and use it in GitHub Desktop.
basic d3 scatter plot
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v3.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
var dataset = [
[5, 20], [480, 90], [250, 50], [100, 33], [330, 95],
[410, 12], [475, 44], [25, 67], [85, 21], [220, 88]
];
//Width and height
var w = 500;
var h = 100;
var barPadding = 1;
var vscale = d3.scale.linear()
.domain([0, d3.max(dataset)])
.range([0, h]);
var vscale = d3.scale.linear()
.domain([0, d3.max(dataset)])
.range([0, w]);
//Create SVG element
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]);
});
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];
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "red");
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment