javascript - Implement d3 Click-to-zoom -
i'm new web stuff know stupid question... still can't figure out i'm doing wrong though. code on site here: http://bl.ocks.org/mbostock/2206590 seems if copy , paste html document , merely modify link us.json points full file path. however, code merely pulls blank page. inspection of page source code on demo ( http://bl.ocks.org/mbostock/raw/2206590/ )is exact same code provided on main page. missing implement this?? thanks!!!
<!doctype html> <meta charset="utf-8"> <style> .background { fill: none; pointer-events: all; } #states { fill: #aaa; } #states .active { fill: orange; } #state-borders { fill: none; stroke: #fff; stroke-width: 1.5px; stroke-linejoin: round; stroke-linecap: round; pointer-events: none; } </style> <body> <script src="//d3js.org/d3.v3.min.js"></script> <script src="//d3js.org/topojson.v1.min.js"></script> <script> var width = 960, height = 500, centered; var projection = d3.geo.albersusa() .scale(1070) .translate([width / 2, height / 2]); var path = d3.geo.path() .projection(projection); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); svg.append("rect") .attr("class", "background") .attr("width", width) .attr("height", height) .on("click", clicked); var g = svg.append("g"); d3.json("/mbostock/raw/4090846/us.json", function(error, us) { if (error) throw error; g.append("g") .attr("id", "states") .selectall("path") .data(topojson.feature(us, us.objects.states).features) .enter().append("path") .attr("d", path) .on("click", clicked); g.append("path") .datum(topojson.mesh(us, us.objects.states, function(a, b) { return !== b; })) .attr("id", "state-borders") .attr("d", path); }); function clicked(d) { var x, y, k; if (d && centered !== d) { var centroid = path.centroid(d); x = centroid[0]; y = centroid[1]; k = 4; centered = d; } else { x = width / 2; y = height / 2; k = 1; centered = null; } g.selectall("path") .classed("active", centered && function(d) { return d === centered; }); g.transition() .duration(750) .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")scale(" + k + ")translate(" + -x + "," + -y + ")") .style("stroke-width", 1.5 / k + "px"); } </script>
download us.json
file , put in same directory index.html
. change path us.json
file:
d3.json("us.json", function(error, us) {
this worked me. if using chrome , accessing index.html
file on local computer, not remote webserver, need run local webserver work. if try open index.html
file in chrome, won't display due chrome's local file restrictions. needs accessed through webserver.
hope helps.
Comments
Post a Comment