Skip to content

Instantly share code, notes, and snippets.

@emeeks
Last active March 18, 2016 04: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 emeeks/2d300876a4a02a04918f to your computer and use it in GitHub Desktop.
Save emeeks/2d300876a4a02a04918f to your computer and use it in GitHub Desktop.
Ch. 4, Fig. 7 - D3.js in Action

This is the code for Chapter 4, Figure 7 from D3.js in Action shows how it is necessary to style the d3.svg.axis() elements, such as path.domain and the lines that represent the ticks, to create a proper axis.

<html>
<head>
<title>D3 in Action Chapter 4 - Example 4</title>
<meta charset="utf-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
</head>
<style>
svg {
height: 500px;
width: 500px;
border: 1px solid gray;
}
</style>
<body>
<div id="viz">
<svg>
</svg>
</div>
</body>
<footer>
<script>
var scatterData = [{friends: 5, salary: 22000}, {friends: 3, salary: 18000}, {friends: 10, salary: 88000}, {friends: 0, salary: 180000}, {friends: 27, salary: 56000}, {friends: 8, salary: 74000}]
xExtent = d3.extent(scatterData, function(d) {return d.salary});
yExtent = d3.extent(scatterData, function(d) {return d.friends});
xScale = d3.scale.linear().domain(xExtent).range([0,500]);
yScale = d3.scale.linear().domain(yExtent).range([0,500]);
d3.select("svg")
.selectAll("circle")
.data(scatterData)
.enter()
.append("circle")
.attr("r", 5)
.attr("cx", function(d) {return xScale(d.salary)})
.attr("cy", function(d) {return yScale(d.friends)})
yAxis = d3.svg.axis().scale(yScale).orient("right");
d3.select("svg").append("g").attr("id", "yAxisG").call(yAxis);
xAxis = d3.svg.axis().scale(xScale).orient("bottom");
d3.select("svg").append("g").attr("id", "xAxisG").call(xAxis);
d3.selectAll("path.domain").style("fill", "none").style("stroke", "black");
d3.selectAll("line").style("stroke", "black");
</script>
</footer>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment