block by renecnielsen b2d16afee685c37df52b

Github Users Worldwide

Full Screen

Github Users Worldwide in the Times Projection

forked from syntagmatic‘s block: Github Users Worldwide

index.html

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
  background: #fff;
}

.stroke {
  fill: none;
  stroke: #888;
  stroke-width: 1px;
}

.fill {
  fill: none;
}

.graticule {
  fill: none;
  stroke: #bbb;
  stroke-width: .5px;
  stroke-opacity: .5
}

.land {
  fill: #ddd;
}

.boundary {
  fill: none;
  stroke: #fff;
  stroke-width: .5px;
}

circle {
  fill: #fb5;
  stroke: #111;
  stroke-width: 1px;
}

</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/d3.geo.projection.v0.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>

var width = 960,
    height = 500;

var projection = d3.geo.times()
    .scale(200)
    .translate([width / 2, height / 2])
    .precision(.1);

var path = d3.geo.path()
    .projection(projection);

var graticule = d3.geo.graticule();

var radius = d3.scale.sqrt()
  .range([1.5, 7]);

var color = d3.scale.sqrt()
  .range(["#359", "#fb5"])
  .interpolate(d3.interpolateLab);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

svg.append("defs").append("path")
    .datum({type: "Sphere"})
    .attr("id", "sphere")
    .attr("d", path);

svg.append("use")
    .attr("class", "stroke")
    .attr("xlink:href", "#sphere");

svg.append("use")
    .attr("class", "fill")
    .attr("xlink:href", "#sphere");

svg.append("path")
    .datum(graticule)
    .attr("class", "graticule")
    .attr("d", path);

d3.json("world-50m.json", function(error, world) {
  svg.insert("path", ".graticule")
      .datum(topojson.feature(world, world.objects.land))
      .attr("class", "land")
      .attr("d", path);

  svg.insert("path", ".graticule")
      .datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }))
      .attr("class", "boundary")
      .attr("d", path);

  d3.json("user_locations.json", function(error, raw) {
    var data = [];
    raw[0][1].forEach(function(d,i) {
      if (i % 3 == 0) {
        data.push({
          pos: [d] 
        })
      }
      if (i % 3 == 1) {
        data[data.length-1].pos[1]= d;
      }
      if (i % 3 == 2) {
        data[data.length-1].mag = d;
      };
    });

    color.domain(d3.extent(data.map(function(d) { return d.mag; })));

    radius.domain(d3.extent(data.map(function(d) { return d.mag; })));

    svg.selectAll("circle")
      .data(data)
      .enter().append("circle")
      .attr("transform", function(d) { return "translate(" + projection([d.pos[1],d.pos[0]]) + ")"; })
      .attr("r", function(d) { return radius(d.mag); })
      .style("fill", function(d) { return color(d.mag); });
  });
});

d3.select(self.frameElement).style("height", height + "px");

</script>