Created
January 9, 2013 16:30
-
-
Save Dillie-O/4494557 to your computer and use it in GitHub Desktop.
Redraw icons/labels at calculated position after SVG transform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// After applying the transform to the cities, redraw them at their new coordinates | |
// Select all cities with a prefix that matches the Id of the state selected. | |
var citySelector = "*[id^='city_" + stateId.substring(0, 2) + "']"; | |
d3.selectAll(citySelector) | |
.each(function (d, i) { | |
var jurisdictionId = d3.select(this).attr("data-id"); | |
d3.select("#cities") | |
.append("svg:circle") | |
.text(d3.select(this).attr("data-name")) | |
.attr("id", "city_point_" + jurisdictionId) | |
.attr("visibility", "visible") | |
.attr("r", "6") | |
.attr("cx", d3.select(this).node().getBoundingClientRect().left - $("#map-svg").offset().left) | |
.attr("cy", d3.select(this).node().getBoundingClientRect().top - $("#map-svg").offset().top) | |
var cityLabel = d3.select("#city_labels").append("g").attr("id", "city_label_group_" + jurisdictionId); | |
cityLabel.append("svg:rect") | |
.attr("x", (d3.select(this).node().getBoundingClientRect().left - $("#map-svg").offset().left) + 10) | |
.attr("y", d3.select(this).node().getBoundingClientRect().top - $("#map-svg").offset().top - 10) | |
.attr("width", "100") | |
.attr("height", "20") | |
.attr("rx", "5") | |
.attr("ry", "5") | |
.attr("visibility", "visible") | |
cityLabel.append("svg:text") | |
.text(d3.select(this).attr("data-name")) | |
.attr("visibility", "visible") | |
.attr("x", (d3.select(this).node().getBoundingClientRect().left - $("#map-svg").offset().left) + 12) | |
.attr("y", d3.select(this).node().getBoundingClientRect().top - $("#map-svg").offset().top + 6) | |
// Update the width of the rectangle to match the text, with a little padding | |
var textWidth = d3.select("#city_label_group_" + jurisdictionId + " text").node().getBBox().width; | |
var textHeight = d3.select("#city_label_group_" + jurisdictionId + " text").node().getBBox().height; | |
d3.select("#city_label_group_" + jurisdictionId + " rect").attr("width", textWidth + 5); | |
d3.select("#city_label_group_" + jurisdictionId + " rect").attr("height", textHeight + 5); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment