Skip to content

Instantly share code, notes, and snippets.

@danielzurawski
Created August 21, 2012 10:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielzurawski/3414137 to your computer and use it in GitHub Desktop.
Save danielzurawski/3414137 to your computer and use it in GitHub Desktop.
D3.js Azimuthal, orthographic projection with pretty bubbles
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<style type="text/css">
body { font: 300 13px "Helvetica Neue", Helvetica; }
path { fill: #ccc; stroke: #F60; }
circle { stroke-width:0.5px; vector-effect:non-scaling-stroke }
circle.f { stroke:#ffffff; fill-opacity: 0.8}
circle.m { stroke:#ffffff; fill-opacity: 0.8}
</style>
</head>
<body>
<div id="chart"></div>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.5.0"></script>
<script type="text/javascript">
// Pretty circles --------->
color = d3.scale.linear()
.domain([12, 30])
.range(["red", "green"])
size = d3.scale.linear()
.domain([0, 10]);
// <----- Pretty circles
var countryFeature;
var datacentreFeature;
var projection = d3.geo.azimuthal()
.scale(380)
.origin([-71.03,42.37])
.mode('orthographic')
.translate([400, 400]);
//640 400
var circle = d3.geo.greatCircle().origin(projection.origin());
// TODO fix d3.geo.azimuthal to be consistent with scale
var scale = { orthographic: 380, stereographic: 380, gnomonic: 380,
equidistant: 380 / Math.PI * 2, equalarea: 380 / Math.SQRT2};
var path = d3.geo.path().projection(projection);
var svg = d3.select("#chart").append('svg:svg')
.attr('width', 800)
.attr('height', 600)
.on('mousedown', mousedown);
var countries = svg.append('g')
.attr('width', 800)
.attr('height', 600)
.attr('id', 'countries');
var datacentres = svg.append('g')
.attr('width', 800)
.attr('height', 600)
.attr('id', 'datacentres');
d3.json('world-countries.json', function(collection) {
countryFeature = countries.selectAll('path')
.data(collection.features)
.enter().append('svg:path')
.attr('d', clip);
countryFeature.append("svg:title").text(function(d) { return d.properties.name; }).attr('text-anchor', 'middle');
});
d3.json('datacentres.json', function(json) {
datacentreFeature = datacentres.selectAll('g')
.data(json.features)
.enter()
.append('g')
.append('svg:circle')
.attr('class', function(d) { return assignClass(d.properties.kgCO2e); })
.attr('r', function(d) { return 10; })
//return size(ballSize(d.properties.kgCO2e))
.attr("fill", function(d) { return color((10 * Math.exp((1 - d.properties.kgCO2e), 2))); });
datacentreFeature = datacentres.selectAll('g');
});
d3.select(window).on('mousemove', mousemove)
.on('mouseup', mouseup);
d3.select('select').on('change',
function() {
projection.mode(this.value).scale(scale[this.value]);
refresh(750);
});
var m0, o0;
function mousedown() {
m0 = [d3.event.pageX, d3.event.pageY];
o0 = projection.origin();
d3.event.preventDefault();
}
function mousemove() {
if (m0) {
var m1 = [d3.event.pageX, d3.event.pageY],
o1 = [o0[0] + (m0[0] - m1[0]) / 8, o0[1] + (m1[1] - m0[1]) / 8];
projection.origin(o1);
circle.origin(o1)
refresh();
}
}
function mouseup() {
if (m0) {
mousemove();
m0 = null;
}
}
function refresh(duration) {
(duration ?
countryFeature.transition().duration(duration)
: countryFeature)
.attr('d', clip);
(duration ?
datacentreFeature.transition().duration(duration)
: datacentreFeature)
.attr('transform', function (d) { return "translate(" + clip(d) + ")"; });
}
function clip(d) {
return path(circle.clip(d));
}
function ballSize (datum) {
return (datum.kgCO2e > 1) ? 10 : (10 * Math.exp((1 - datum.kgCO2e), 2));
}
function assignClass (datum) {
return (datum.kgCO2e > 0.2) ? "f" : "m";
}
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment