2

Per the docs, "The call operator always returns the current selection, regardless of the return value of the specified function." I'd like to know if there is a variant of call or reasonable workaround for getting call-behavior that returns values other than the selection.

Motivation:

I've got a chart and a datebrush, each encapsulated in a function

function trends_datebrush() {
    // Setup
    function chart(_selection) {

      _selection.each(function(_data) {
        // Do things
...});
}
  return chart;
};

(The chart follows a similar format but isn't called datebrush).

These are instantiated with:

d3.select("someDiv")
  .datum("data")
  .call(trends_datebrush());

// And then we call the chart

I'd like to return a subselection from brush to be used as the data variable in the chart call. As is I need to make them both aware of some higher order global state, which gets messy especially since I want other control functions to drill down on the data. If I could override call, then I could do something like

d3.select("someDiv")
  .datum("data")
  .call(trends_datebrush())
  .call(trends_chart());

And then if I were to implement some new filter I could throw it into the chain with another call statement.

tl;DR: Looking for ways to get chain chart calls s.t. they can pass transformed data to each other. I want monadic D3 charts! Except I don't really know monads so I might be misusing the word.

  • 1
    Have you considered using the alternative form of `.call()`, i.e. `trends_datebrush()(d3.select("someDiv").datum(data))`? Then you could use the return value in an (admittedly ugly) nested call: `trends_chart()(trends_datebrush()(d3.select("someDiv").datum(data)))`. – Lars Kotthoff Oct 28 '14 at 19:11
  • I had not considered that! Trying it now. – Connor Wilson Oct 28 '14 at 19:28
  • Possible duplicate of [Looking for a way to refactor D3.js-style method chaining pattern](http://stackoverflow.com/questions/14940040/looking-for-a-way-to-refactor-d3-js-style-method-chaining-pattern) – Paul Sweatte Nov 06 '15 at 20:34

0 Answers0