1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| <script type="text/javascript"> var height = window.innerHeight; var width = window.innerWidth;
var svg = d3.select('#container') .append('svg') .attr('width', width).attr('height', height) .append('g');
var projection = d3.geo.mercator(); var oldScala = projection.scale(); var oldTranslate = projection.translate();
xy = projection.scale(21000) .translate([width / 2, height / 2]).center([114.235521, 30.631975]);
var path = d3.geo.path().projection(xy);
var color = d3.scale.quantize() .range(["#feedde","#fdd0a2","#fdae6b","#fd8d3c","#f16913","#d94801","#8c2d04"])
svg.attr('width', width).attr('height', height);
d3.csv("wuhan_data.csv", function(data){ color.domain([ d3.min(data, function(d){return d.value;}), d3.max(data, function(d){return d.value;}) ]);
d3.json("wuhan_map.json", function(json){ for(var i = 0; i < data.length; i++) { var name = data[i].name; var value = data[i].value; for(var j = 0; j < json.features.length; j++) {
var map_name = json.features[j].properties.name; if(name == map_name) {
json.features[j].properties.value = value; break; } } }
svg.selectAll("path") .data(json.features) .enter() .append("path") .attr("d", path) .on('mouseover', function(data) { d3.select(this).style('opacity', 1); }) .on('mouseout', function(data) { d3.select(this).style('opacity', 0.7); }) //.attr('fill', 'rgba(128,124,139,0.61)') .attr('fill', function(d) { var value = d.properties.value; if(value) { return color(value); } return "rgba(128,124,139,0.61)"; }) .attr('stroke', 'rgba(255,255,255,1)') .attr('stroke-width', 1) .style('opacity', 0.7);
svg.selectAll(".place-label") .data(data) .enter() .append("text") .attr("class", "place-label") .attr("transform", function(d) { return "translate(" + projection([d.x, d.y]) + ")"; }) .text(function(d) { return d.name + ":" + d.value; }); }); }); </script>
|