javascript - d3 - how to resize an individual state map drawn from a full US clipPath -
i'm working d3 framework , i'm curious how/if it's possible go making individual county level state map created clippath of state boundaries responsive without having redraw map scratch.
i tried adjust width , height attrbutes of svg using:
d3.selectall(".ismap").attr("width", width).attr("height", width * 1.5); d3.selectall(".ismap").selectall("path").attr("d", path);
however did not return expected result (a small blip of land display on resize event).
anyway here i'm at:
var $width = $(window).width(); var $height = $width * .8; var width = $width, height = $height; var projection = d3.geo.albers(); var path = d3.geo.path().projection(projection); var svg = d3.select('.map-wrapper').append("svg").attr("width", width).attr("height", height).attr("class", "state-map ismap"); d3.json("map_json/us_county_land.json", function(error, us) { if (error) throw error; var states = topojson.feature(us, us.objects.states), state = states.features.filter(function(d) { return d.id === 36; })[0]; projection.scale(1).translate([0, 0]); var b = path.bounds(state), s = .95 / math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height), t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2]; projection.scale(s).translate(t); svg.append("path").datum(topojson.mesh(us, us.objects.states, function(a, b) { return !== b; })).attr("class", "mesh").attr("d", path); svg.append("path").datum(state).attr("d", path).attr("id", "land"); svg.append("clippath").attr("id", "clip-land").append("use").attr("xlink:href", "#land"); svg.selectall("path").data(topojson.feature(us, us.objects.counties).features).enter().append("path").attr("d", path).attr("data-county", function(d) { return d.id; }).attr("clip-path", "url(#clip-land").attr("class", "county").on('mouseover', function(d, i) { d3.select(this.parentnode.appendchild(this)).transition().duration(300) .style({ 'stroke-opacity': 1, 'stroke': '#000', 'stroke-width': '1px' }); }).on('mouseout', function(d, i) { d3.select(this.parentnode.appendchild(this)).transition().duration(300) .style({ 'stroke-opacity': 1, 'stroke': '#fff', 'stroke-width': '1px' }); }); });
this did trick:
svg.attr("viewbox", "0 0 " + width + " " + height ).attr("preserveaspectratio", "xminymin")
Comments
Post a Comment