Skip to content

Instantly share code, notes, and snippets.

@bendazz
Last active May 24, 2019 07:55
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 bendazz/7f6cf4ed46b83a292021cdae98c7092a to your computer and use it in GitHub Desktop.
Save bendazz/7f6cf4ed46b83a292021cdae98c7092a to your computer and use it in GitHub Desktop.
scatter plot
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0;padding: 20px;}
</style>
</head>
<body>
<script>
var svg = d3.select("body")
.append("svg")
.attr("width",500)
.attr("height",300)
.style("background-color","cornsilk")
.style("border","1px black solid")
var dataset = [[1,10],[7,8],[3,5],[9,2],[5,8]]
//x axis
var xScale = d3.scaleLinear()
.domain([0,10])
.range([50,450])
var xAxis = d3.axisBottom()
.scale(xScale)
svg.append("g")
.attr("transform","translate(0,250)")
.call(xAxis)
//y axis
var yScale = d3.scaleLinear()
.domain([0,10])
.range([250,50])
var yAxis = d3.axisLeft()
.scale(yScale)
svg.append("g")
.attr("transform","translate(50,0)")
.call(yAxis)
//points
svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx",d => xScale(d[0]))
.attr("cy",d => yScale(d[1]))
.attr("r",4)
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment