toolkit/javascript/d3/examples/cartogram/cartogram.js
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     1 // Ratio of Obese (BMI >= 30) in U.S. Adults, CDC 2008
       
     2 var data = [
       
     3   , .187, .198, , .133, .175, .151, , .1, .125, .171, , .172, .133, , .108,
       
     4   .142, .167, .201, .175, .159, .169, .177, .141, .163, .117, .182, .153, .195,
       
     5   .189, .134, .163, .133, .151, .145, .13, .139, .169, .164, .175, .135, .152,
       
     6   .169, , .132, .167, .139, .184, .159, .14, .146, .157, , .139, .183, .16, .143
       
     7 ];
       
     8 
       
     9 var svg = d3.select("#chart").append("svg:svg")
       
    10     .attr("width", 960)
       
    11     .attr("height", 500);
       
    12 
       
    13 d3.json("../data/us-states.json", function(json) {
       
    14   var path = d3.geo.path();
       
    15 
       
    16   // A thick black stroke for the exterior.
       
    17   svg.append("svg:g")
       
    18       .attr("class", "black")
       
    19     .selectAll("path")
       
    20       .data(json.features)
       
    21     .enter().append("svg:path")
       
    22       .attr("d", path);
       
    23 
       
    24   // A white overlay to hide interior black strokes.
       
    25   svg.append("svg:g")
       
    26       .attr("class", "white")
       
    27     .selectAll("path")
       
    28       .data(json.features)
       
    29     .enter().append("svg:path")
       
    30       .attr("d", path);
       
    31 
       
    32   // The polygons, scaled!
       
    33   svg.append("svg:g")
       
    34       .attr("class", "grey")
       
    35     .selectAll("path")
       
    36       .data(json.features)
       
    37     .enter().append("svg:path")
       
    38       .attr("d", path)
       
    39       .attr("transform", function(d) {
       
    40         var centroid = path.centroid(d),
       
    41             x = centroid[0],
       
    42             y = centroid[1];
       
    43         return "translate(" + x + "," + y + ")"
       
    44             + "scale(" + Math.sqrt(data[+d.id] * 5 || 0) + ")"
       
    45             + "translate(" + -x + "," + -y + ")";
       
    46       })
       
    47       .style("stroke-width", function(d) {
       
    48         return 1 / Math.sqrt(data[+d.id] * 5 || 1);
       
    49       });
       
    50 
       
    51 });