rveciana - d3-tutorial-iv-adding-points

D3 tutorial IV: Adding points

Open raw page in new tab

This example belongs to the presentation made to the OSGeo local group in Barcelona Geoinquiets The example is an adaptation from Mike Bostock’s Symbol Map example

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

.states {
  fill: #ccc;
  stroke: #fff;

.symbol {
  fill: steelblue;
  fill-opacity: .8;
  stroke: #fff;

<button type="button" id="change">Change</button>

<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>

var width = 960,
    height = 500;
var states, points;

var radius = d3.scale.sqrt()
    .domain([0, 1e6])
    .range([0, 10]);

var projection = d3.geo.mercator()
    .scale(4000 / 2 / Math.PI)
    .translate([width / 2, height / 2]);

var path = d3.geo.path()

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

    .defer(d3.json, "/mbostock/raw/4090846/us.json")
    .defer(d3.json, "/mbostock/raw/3750900/us-state-centroids.json")

function ready(error, us, centroid) {
states = svg.append("path")
      .attr("class", "states")
      .datum(topojson.object(us, us.objects.states))
      .attr("d", path);

    points =  svg.selectAll(".symbol")
      .data(centroid.features.sort(function(a, b) { return b.properties.population - a.properties.population; }))
      .attr("class", "symbol")
      .attr("d", path.pointRadius(function(d) { if(d.properties) { return radius(d.properties.population);} else {return;} }));

d3.select("#change").on("click", function() {
  projection = d3.geo.albers();
      .attr("d", path);
      .attr("d", path);