Skip to content

Instantly share code, notes, and snippets.

@keighty
Last active August 29, 2015 13:56
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 keighty/9327604 to your computer and use it in GitHub Desktop.
Save keighty/9327604 to your computer and use it in GitHub Desktop.
Scaling D3
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<style>
body{margin:0px;}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
</style>
</head>
<body>
<script src="http://mbostock.github.com/d3/d3.v2.min.js"></script>
<script>
var width = 500,
height = 500,
margin = 50;
/***********************
Create Scales
***********************/
var scaleX=d3.scale.linear().domain([0,10]).range([margin,width-margin]);
var scaleY=d3.scale.linear().domain([0,10]).range([height-margin,margin]);
var svg=d3.select("body").append("svg")
.attr("width",width)
.attr("height",height);
var xAxis = d3.svg.axis()
.scale(scaleX)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(scaleY)
.orient("left");
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + (height - margin) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + margin + ",0)")
.call(yAxis);
d3.csv("test_data.csv",function(csv) {
svg.selectAll("circle").data(csv).enter()
.append("circle")
.attr("cx",getX)
.attr("cy",getY)
.attr("r", getR)
.style("fill", getColor);
function getX(d) { return scaleX(d.X); }
function getY(d) { return scaleY(d.Y); }
function getR(d) { return d.Quantity / 10; }
function getColor(d) { return d.Colors; }
)}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment