Skip to content

Instantly share code, notes, and snippets.

@timgentry
Last active June 14, 2016 22:46
Show Gist options
  • Save timgentry/d6d4daf87ac8b562ff937aea5f245786 to your computer and use it in GitHub Desktop.
Save timgentry/d6d4daf87ac8b562ff937aea5f245786 to your computer and use it in GitHub Desktop.
Radius vs. Area
license: mit

This block demonstrates the subtle, but important, visual differences in making the area of an element proportional to it's given value rather than it's radius or length of side. For simplicity, values of 0 - 100 are used in this example.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
text {
font: 12px sans-serif;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {
top: 50,
bottom: 30,
left: 50,
right: 50
};
var width = 960;
var height = 400;
var xScale = d3.scale.linear()
.domain([0, 100])
.range([margin.left, width - margin.right]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var ticks = xScale.ticks(21);
var elementWidth = xScale(ticks[1]) - xScale(ticks[0]);
var maxRadius = elementWidth / 2;
var maxSide = Math.sqrt(Math.PI * maxRadius * maxRadius);
var yScale = d3.scale.linear()
.domain([0, 4])
.range([margin.top + maxRadius, height - margin.bottom - maxRadius]);
var percents = svg.selectAll('g.percent')
.data(ticks);
var enter = percents
.enter()
.append('g')
.attr('class', 'percent')
.attr('transform', function(d) { return "translate(" + (xScale(d)) + ", 0)"; });
enter.append('text')
.text(function(d) { return d; })
.attr('y', margin.top / 2)
.attr('text-anchor', 'middle');
// Linear square side scale
var rScale1 = d3.scale.linear()
.domain([0, 100])
.range([0, maxSide]);
enter.append("rect")
.attr('x', function(d) { return -(rScale1(d) / 2); })
.attr('y', function(d) { return yScale(0) - (rScale1(d) / 2); })
.attr("width", function(d) { return rScale1(d); })
.attr("height", function(d) { return rScale1(d); });
// Linear square area scale
var rScale2 = d3.scale.linear()
.domain([Math.sqrt(0), Math.sqrt(100)])
.range([0, maxSide]);
enter.append("rect")
.attr('x', function(d) { return -(rScale2(Math.sqrt(d)) / 2); })
.attr('y', function(d) { return yScale(1) - (rScale2(Math.sqrt(d)) / 2); })
.attr("width", function(d) { return rScale2(Math.sqrt(d)); })
.attr("height", function(d) { return rScale2(Math.sqrt(d)); });
// Linear circle radius scale
var rScale3 = d3.scale.linear()
.domain([0, 100])
.range([0, maxRadius]);
enter.append("circle")
.attr("cy", function(d) { return yScale(2); })
.attr("r", function(d) { return rScale3(d); });
// Linear circle area scale
var rScale4 = d3.scale.linear()
.domain([Math.sqrt(0 / Math.PI), Math.sqrt(100 / Math.PI)])
.range([0, maxRadius]);
enter.append("circle")
.attr("cy", function(d) { return yScale(3); })
.attr("r", function(d) { return rScale4(Math.sqrt(d / Math.PI)); });
// Square root power scale
var rScale5 = d3.scale.sqrt()
.domain([0, 100])
.range([0, maxRadius]);
enter.append("circle")
.attr("cy", function(d) { return yScale(4); })
.attr("r", function(d) { return rScale5(d); });
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment