question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add method for grouped selection?

See original GitHub issue

A simple question relating to the project structure that I have not been able to figure it out for the life of me.

If I want to add a method to selection that works on both plain selections and grouped selections how do I do that?

As an example I have this function for short-handing transitions:

d3.selection.prototype.move_to = function ({ x = 0, y = 0 }) {
  const eval_pos = function (p) {
    if (typeof p === "function") {
      return p(this.datum());
    } else {
      return p;
    }
  };
  return this.attr("transform", `translate(${eval_pos(x)},${eval_pos(y)})`);
};

It works great on simple selections. E.g.

svg.select('g').move_to({x:margin.left,y:margin.top});

But not on grouped selections:

svg.selectAll('g.chart_elements').move_to({x:x_pos,y:y_pos});

I tried adding the prototype to d3.selectionAll but that didn’t work either.

Clearly I am attaching the prototype to the wrong object but I can’t figure out what the correct object type is as I only see one call to the prototype assignment here.

It looks like Selection is the proper object but I don’t see that getting exported as its prototypes just get merged with selection and then selection is exported. Is this correct and there’s no way to add a prototype to the group selection when operating on the bundled code?

Thanks and I hope this is the appropriate area for this question!

Best, Nick

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mbostockcommented, Jul 3, 2020

So you want to allow x and y to be specified as separate arguments? You might instead consider something like this:

selection.moveTo(d => ({x: d.x, y: d.y}))

Or more simply, since d is already in the right shape:

selection.moveTo(d => d)

This could then be implemented as:

function translate({x, y}) {
  return `translate(${x},${y})`;
}

function translateFunction(p) {
  return function() {
    return translate(p.apply(this, arguments));
  };
}

d3.selection.prototype.moveTo = function(p) {
  return this.attr("transform", (typeof p === "function"
    ? translateFunction
    : translate)(p));
};

If you want x and y to be specified as separate functions, you have to decide whether you want to compute all the x-values first and then all the y-values, or if you want to interleave the computation. But I think it’s simpler if you only allow the function to be at the top level, rather than allowing an object-of-functions.

0reactions
mbostockcommented, Jul 4, 2020

If that’s what you want, you can simplify like so:

d3.selection.prototype.move_to = function({ x = 0, y = 0 }) {
  return this.attr("transform", function() {
    const x_pos = typeof x === "function" ? x.apply(this, arguments) : x;
    const y_pos = typeof y === "function" ? y.apply(this, arguments) : y;
    return `translate(${x_pos},${y_pos})`;
  });
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Group rows of data (Power Query) - Microsoft Support
Select Home > Group by. In the Group by dialog box, select Advanced to select more than one column to group by. To...
Read more >
Create/edit Content Groups - Analytics Help - Google Support
In the VIEW column, click Content Grouping. Click +New Content Grouping. Enter a name for the new grouping. Select the methods you want...
Read more >
How to Group Excel Pivot Table Data Fix Problems
Press the Ctrl key, and select the items that you want to add to the group; Right-click on one of the selected items,...
Read more >
Permanently Grouping Elements - MicroStation
Use the Element Selection tool to select the element(s) to be added to the named group. Select the Add to Graphic Group tool....
Read more >
Select multiple fields group by and sum - Stack Overflow
Updated : If you're trying to avoid grouping for all the fields, you can group just by Id : data.GroupBy(d => d.Id) .Select(...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found