20

I'm working with d3. I create a globe of countries from a json file. The globe has svg paths, and each path has an id. I want to select a path with a particular ID. How would I do that, please?

handleGlobe();

$('#panel div').click(function(){

if (this.className == 'represented') {
thisID = $(this).attr('id');
focusedCountry = d3.select('path') //??? not sure how to say this
p = d3.geo.centroid(focusedCountry);
}

...

handleGlobe() {
var feature;

        var projection = d3.geo.azimuthal()
            .scale(380)
            .origin([-71.03,42.37])
            .mode("orthographic")
            .translate([380, 400]);

        var circle = d3.geo.greatCircle()
            .origin(projection.origin());

        // TODO fix d3.geo.azimuthal to be consistent with scale
        var scale = {
          orthographic: 380,
          stereographic: 380,
          gnomonic: 380,
          equidistant: 380 / Math.PI * 2,
          equalarea: 380 / Math.SQRT2
        };

        var path = d3.geo.path()
            .projection(projection);

        var svg = d3.select("#globe").append("svg:svg")
            .attr("width", 800)
            .attr("height", 800)
            .on("mousedown", mousedown);



        d3.json("world-countries.json", function(collection) {

         feature = svg.selectAll("path")
              .data(collection.features)
              .enter().append("svg:path")
              .attr("d", clip)
              .attr("id", function(d) { return d.id; })
              .on("mouseover", pathOver)
              .on("mouseout", pathOut)
              .on("click", click);

          feature.append("svg:title")
              .text(function(d) { return d.properties.name; });

          feature.each(function(){

             for (var i=0; i<unrepresented.length; i++){
                if ($(this).attr('id') == unrepresented[i]) {
                    d3.select(this).style("fill", "#ededed");
                } 

             }
             if (($(this).attr('id') == 'GRL') || ($(this).attr('id') == 'ATA')) { //Greenland and Antarctica are shapes, but not countries
                d3.select(this).style("fill", "#ededed");
             }
          });


        });

        d3.select(window)
            .on("mousemove", mousemove)
            .on("mouseup", mouseup)
            ;

        d3.select("select").on("change", function() {
          projection.mode(this.value).scale(scale[this.value]);
          refresh(750);
        });

        var m0,
            o0;

        function mousedown() {
          m0 = [d3.event.pageX, d3.event.pageY];
          o0 = projection.origin();
          d3.event.preventDefault();
        }

        function mousemove() {
          if (m0) {
            var m1 = [d3.event.pageX, d3.event.pageY],
                o1 = [o0[0] + (m0[0] - m1[0]) / 8, o0[1] + (m1[1] - m0[1]) / 8];
            projection.origin(o1);
            circle.origin(o1)
            refresh();
          }
        }

        function mouseup() {
          if (m0) {
            mousemove();
            m0 = null;
          }
        }

        function refresh(duration) {
          (duration ? feature.transition().duration(duration) : feature).attr("d", clip);
        }

        function clip(d) {
          return path(circle.clip(d));
        }

        function click() {

        }

        function pathOver() {

        }

        function pathOut() {

        }
    //end globe

}
6
  • 1
    d3.select("#<id of path>") Commented Mar 12, 2014 at 18:36
  • Actually, I have two elements with this id. One is just a div, the other is in #globe svg path. How do I indicate the path?
    – LauraNMS
    Commented Mar 12, 2014 at 18:43
  • focusedCountry = d3.select('#globe svg path #' + thisID);
    – LauraNMS
    Commented Mar 12, 2014 at 18:46
  • 2
    If the other one is a div, you can use d3.select("path#ID"). Does that solve it for you? Oh and you really shouldn't have two elements with the same ID. Commented Mar 12, 2014 at 18:51
  • My next line of code is p = d3.geo.centroid(focusedCountry); and when I put that in, I get the message TypeError: d3.geo.centroid is not a function.
    – LauraNMS
    Commented Mar 12, 2014 at 18:56

1 Answer 1

46

You can select an element by ID by prefixing the ID with "#" and using that as a selector:

d3.select("#ID");

or to select a path with that ID

d3.select("path#ID");

3 Comments

Enter at least 15 characters
Just be careful, d3.select() does not like it if id is all numeric.
Enter at least 15 characters
Enter at least 15 characters
@mmatt All numeric IDs are not valid according to the DOM spec -- nothing to do with D3 as such.
Enter at least 15 characters
Enter at least 15 characters
Good to know, jQuery did not make it a problem so I thought it was d3 being mean :)
Enter at least 15 characters
Enter at least 15 characters
Enter at least 15 characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.