Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select d3 node by its datum

I’d like to select a node in a callback without using d3.select(this).

I have some code that draws a pie…

function drawPie(options) {
  options || (options = {});
  var data = options.data || [],
      element = options.element,
      radius = options.radius || 100,
      xOffset = Math.floor(parseInt(d3.select(element).style('width'), 10) / 2),
      yOffset = radius + 20;

  var canvas = d3.select(element)
              .append("svg:svg")
              .data([data])
              .attr("width", options.width)
              .attr("height", options.height)
              .append("svg:g")
              .attr("transform", "translate(" + xOffset + "," + yOffset + ")");

  var arc = d3.svg.arc()
    .outerRadius(radius);

  var pie = d3.layout.pie()
    .value(function(data) {
      return data.percentageOfSavingsGoalValuation;
    });

  var arcs = canvas.selectAll("g.slice")
    .data(pie)
    .enter()
    .append("svg:g")
    .attr("class", "slice");

  arcs.append("svg:path")
    .on("mouseover", divergeSlice);

You’ll notice at the end I have a call to divergeSlice(). That looks like this:

function divergeSlice(datum, index) {
  var angle = (datum.endAngle + datum.startAngle) / 2,
      x = Math.sin(angle) * 10,
      y = -Math.cos(angle) * 10;

  d3.select(this)
    .transition()
    .attr("transform", "translate(" + x + ", " + y + ")");
}

This works, but I’d like to accomplish this without using this as I mentioned earlier. When I log the datum object, I get something like the following:

{
  data: {
    uniqueID: "XX00X0XXXX00"
    name: "Name of value"
    percentageOfValuation: 0.4
    totalNetAssetValue: 0
  }
  endAngle: 5.026548245743669
  innerRadius: 80
  outerRadius: 120
  startAngle: 2.5132741228718345
  value: 0.4
}

How could I use d3.select() to find a path that holds datum.data.uniqueID that is equal to "XX00X0XXXX00"?

like image 771
Jezen Thomas Avatar asked Mar 19 '14 13:03

Jezen Thomas


People also ask

What is datum in d3?

datum(data) bypasses the data-join process altogether. This simply assigns the entirety of data to all elements in the selection as a whole without splitting it up as in the case of data-joins. So if you want to bind an entire array data = [1, 2, 3] to every DOM element in your selection , then selection.

What does selectAll do in d3?

selectAll() function in D3. js is used to select all the element that matches the specified selector string. Parameters: This function accepts single parameter HTML tag as a parameter. Return Value: This function returns the selected elements.

What is d3 js explain select () selectAll () and data () in brief?

D3 allows you to select DOM elements and manipulate them using functions provided by the library. Elements are selected with select() and selectAll() methods, which return selections. Selections have operators that allow you to modify, add, or remove elements.


1 Answers

You can't do this directly with .select() as that uses DOM selectors. What you can do is select all the candidates and then filter:

d3.selectAll("g")
  .filter(function(d) { return d.data.uniqueID === myDatum.data.uniqueID; });

However, it would be much easier to simply assign this ID as an ID to the DOM element and then select based on that:

var arcs = canvas.selectAll("g.slice")
  .data(pie)
  .enter()
  .append("svg:g")
  .attr("id", function(d) { return d.data.uniqueID; })
  .attr("class", "slice");

d3.select("#" + myDatum.data.uniqueID);
like image 115
Lars Kotthoff Avatar answered Sep 20 '22 21:09

Lars Kotthoff