Geoffrey Hing
Geoffrey Hing

Reputation: 1765

Can I update DOM elements in a D3 selector based on a data join to a different data object?

tl;dr

Can I create elements using data that looks like:

[{"id": 1, ...}, {"id: 2, ...}, ..]

and update attributes of the elements using data that looks like:

[2, 4, 7]

(where the elements of the array are a subset of the ids of the initial data set).

Long version

I have data that looks like this:

[
{
    "id": 1, 
    "latitude": 38.314552, 
    "longitude": -88.9025347755102, 
    "name": "JEFFERSON COUNTY JAIL ", 
    "official_name": "Jefferson County Justice Center", 
    "url": "https://www.ice.gov/detention-facilities/facilities/jeffeil.htm"
}, 
{
    "id": 2, 
    "latitude": 41.875702, 
    "longitude": -87.63072, 
    "name": "CHICAGO HOLD ROOM ", 
    "official_name": "Chicago Hold Room", 
    "url": ""
}, 
{
    "id": 3, 
    "latitude": 43.407029, 
    "longitude": -88.704349, 
    "name": "DODGE COUNTY JAIL, JUNEAU ", 
    "official_name": "Dodge Detention Facility", 
    "url": "https://www.ice.gov/detention-facilities/facilities/dodgewi.htm"
}, 
...
]

I put it on an SVG map like this:

var projection = d3.geo.albersUsa()
    .scale(scale)
    .translate([width / 2, height / 2]);

var facilityCircles = svg.append("svg:g")
    .attr("id", "facilities");

d3.json("data/facilities.json", function(facilities) {
  var positions = [];
  var positionsByFacilityId = {};
  var pointSize = 5;
  facilities.forEach(function(facility) {
    var loc = [facility.longitude, facility.latitude];
    var pos = projection(loc);
    positions.push(pos);
    positionsByFacilityId[facility.id] = pos; 
  });
  facilityCircles.selectAll("circle")
                 .data(facilities, function(d) { return d.id; })
                 .enter().append("svg:circle")
                 .attr("data-facility-id", function(d, i) { return d.id; })
                 .attr("cx", function(d, i) { return positionsByFacilityId[d.id][0]; })
                 .attr("cy", function(d, i) { return positionsByFacilityId[d.id][1]; })
                 .attr("r", function(d, i) { return pointSize; });
 }

However, later on, I want to update attributes of the circles based on another data object, which would just be an array of ids representing a subset of the id properties from the initial data, e.g. [2, 4, 5]

Can I do something like this to update the attributes of only selected elements mapped to data objects with the given ids?

      facilitiesSubset = [2, 4, 5];
      facilityCircles.selectAll("circle")
                 .data(facilitiesSubset)
                 .attr("fill", "green");

or, should I just extract the ids from the initial data and use those in the call to data() that is used to create the elements?

Upvotes: 0

Views: 161

Answers (1)

Adam Pearce
Adam Pearce

Reputation: 9293

To make this a little easier, I'd suggest changing the naming convention a little bit:

var facilityCircleContainer = svg.append("svg:g")
    .attr("id", "facilities");

Since the svg:g object isn't actually the circles, it is just holding them. So then:

  var facilityCircles = facilityCircleContainer.selectAll("circle")
                 .data(facilities, function(d) { return d.id; })
                 .enter().append("svg:circle")
                 .attr("data-facility-id", function(d, i) { return d.id; })
                 .ect(...)

facilityCircles now refers to the circles with their attached data now. Updating the fill based on the array is pretty simple:

facilityCircles.attr("fill", function(d){
   facilitiesSubset.indexOf(d.id) != -1 ? "green" : "red"});

Upvotes: 1

Related Questions