javascript - Access a specific path element using given ID -
i using topojson , world-110m.json visualize map of world. trying change fill property of 2 particular countries click event.
the first country selected click user side , id of path retrieved using:
var current_country = d3.select(this).style("fill", "red"); var current_country_id = d3.select(this).attr('id');
the second country changed given (does not matter) , defined id. have tried using this:
var top = d3.select("path#643").style("fill", "green");
as suggested here: how select d3 svg path particular id
seems pretty straight forward same error no matter try:
uncaught syntaxerror: failed execute 'queryselector' on 'document': 'path#643' not valid selector.
i have tried many things (all combinations come mind) , have seen lot of similar questions posted here have failed find proper solution. path id in fact exist. , of countries stored in world variable , seem ok me.
here full code:
var width = 2000; var height = 2000; var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); var projection = d3.geo.mercator() .center([50,70]) .scale(150) .rotate([0,0,0]); var path = d3.geo.path().projection(projection); var g = svg.append("g"); var country_selector = d3.select("body").append("div").attr("class", "country_selector"); queue() .defer(d3.json, "world-110m.json") .defer(d3.csv, "country_data2.csv") .await(main); function main(error, world, countrydata){ var country_names = {}; var countries = topojson.feature(world, world.objects.countries).features; countrydata.foreach(function(d) { country_names[d.id] = d.name; }); var world = g.selectall("path") .data(countries) .enter() .append("svg:path") .attr("d", path) .attr("id", function(d) { return d.id; }) .attr("class", function(d) { return d.id; }) .on("mouseover", function(d) { country_selector.text(country_names[d.id]) .style("left", (d3.event.pagex + 7) + "px") .style("top", (d3.event.pagey - 15) + "px") .style("display", "block") .style("opacity", 0.8); }) .on("mouseout", function(d) { country_selector.style("opacity", 0) .style("display", "none"); }) .on("mousemove", function(d) { country_selector.style("left", (d3.event.pagex + 7) + "px") .style("top", (d3.event.pagey - 15) + "px"); }) .on("click", function(d) { // rouble part var current_country = d3.select(this).style("fill", "red") var current_country_id = d3.select(this).attr('id') console.log(current_country) console.log(current_country_id) console.log(world) var top = d3.select("path#643").style("fill", "green"); // bad guy console.log(top) }) }; // end of main function var zooming = d3.behavior.zoom() .on("zoom",function() { g.attr("transform","translate("+ d3.event.translate.join(",")+")scale("+d3.event.scale+")"); g.selectall("path") .attr("d", path.projection(projection)); }); svg.call(zooming).on("dblclick.zoom", null);
any appreciated.
id
s can't start numbers. id
invalid. can change id
in html _643
, in javascript
var top = d3.select("path#_643").style("fill", "green");
here's example using css show id
validity
#643 { background-color: orange; color: #fff; } #_643 { background-color: orange; color: #fff; } #-643 { background-color: orange; color: #fff; } #six43 { background-color: orange; color: #fff; }
<ul> <li id="643">643</li> <li id="_643">_643</li> <li id="-643">-643</li> <li id="six43">six43</li> </ul>
Comments
Post a Comment