d3.js - How to display GeoJson data with d3js at a specific extent and zoom by default? -
i benefit regarding aspect. loaded basic geojson file d3js , trying display whole area default. currently, default behavior bit "zoomed-in" , cannot see entire area unless zoom out bit. if use smaller area (like county) "zoomed-out";
what know how can make area containing map show extent like.
the following example 1 "zoomed-in":
current behavior here: https://lh6.googleusercontent.com/-398lxviqwvg/u6widiwtlti/aaaaaaaaaje/heamhdgrgrs/s1600/wrong.png
desired behavoir here: https://lh6.googleusercontent.com/-w3uj6m3uwbi/u6wimurvuii/aaaaaaaaajm/ufcno-gzm30/s1600/good.png
my current code following:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>display map</title> <script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script> <style type="text/css"> body { font: 10px sans-serif; } </style> </head> <body> <script> // variables var svgwidth = 400, svgheight = 400, margin = {"top": 25, "right": 25, "bottom": 25, "left": 25}, width = svgwidth - margin.left - margin.right, height = svgheight - margin.top - margin.bottom; // mexico path (var boundarypath) taken // http://upload.wikimedia.org/wikipedia/commons/c/c5/blank_mexico_map%2c_no_states.svg // taken personal, learning purposes var boundarypath = "[][][]][][][]" // create svg viewport var svgviewport = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .style("border", "1px solid"); function zoomfunction() { d3.select("path") .attr("transform", "translate(" + d3.event.translate + ") scale (" + d3.event.scale + ")"); } var zoom = d3.behavior.zoom() .scaleextent([0.2, 10]) .on("zoom", zoomfunction); // define inner drawing space var innerspace = svgviewport.append("g") .attr("class", "inner_space") .attr("transform", "translate(" + margin.left + "," + margin.top + ")") .call(zoom); innerspace.append("g").attr("class", "hidden rectangle") .append("rect") .attr("class", "background") .attr("x", function(d, i) { return 0; }) .attr("y", function(d, i) { return 0; }) .attr("width", function(d, i) { return width; }) .attr("height", function(d, i) { return height; }) .style("fill", "white"); // draw country path var boundarypath = innerspace.append("path") .attr("d", boundarypath) .attr("fill", "gray"); </script> </body> </html>
thank time,
Comments
Post a Comment