Skip to content
mbostock edited this page Oct 29, 2011 · 11 revisions

API ReferenceCore

D3 has a few internal methods defined in the core module that are re-used by other modules; these are not considered part of the public API and should be used with caution outside of D3 as they are not guaranteed to remain backwards-compatible.

Functions

# d3.functor(value)

If the specified value is a function, returns the specified value. Otherwise, returns a function that returns the specified value. This method is used internally as a lazy way of upcasting constant values to functions, in cases where a property may be specified either as a function or a constant. For example, many D3 layouts allow properties to be specified this way, and it simplifies the implementation if we automatically convert constant values to functions.

# d3.rebind(object, function)

Wraps the specified function. If the returned function is called with no arguments, function is invoked and its return value is returned; this is the "getter" mode. If the returned function is called with any arguments, function is invoked and object is returned; this is the "setter" mode. The rebind operator allows inherited methods (mix-ins) to be rebound to a subclass on a different object.

Events

D3 uses a dispatcher to handle custom events.

# d3.dispatch(types…)

Creates a new dispatcher object for the specified types. Each argument is a string representing the name of the event type, such as "zoom" or "change". The returned object is an associative array; each type name is associated with a dispatch object. For example, if you wanted to create an event dispatcher for "start" and "end" events, you can say:

var event = d3.dispatch("start", "end");

Then, you can access the dispatchers for the different event types as event.start and event.end. For example, you might add an event listener:

event.start.add(listener);

And then later dispatch an event to all registered listeners:

event.start.dispatch();

The dispatch method will preserve the context in which it is called, and pass any arguments, along to the registered listeners. Thus, by passing different arguments to the dispatch method, you can pass whatever arguments you want to the listeners; most commonly, you might create an object that represents the event, or pass along the current datum (d) and index (i). You can also control the "this" context of the listeners using call or apply.

# dispatch.add(listener)

Adds the specified listener to this dispatcher. If the listener is already registered, this method has no effect.

# dispatch.remove(listener)

Removes the specified listener from this dispatcher. If the listener is not registered, this method has no effect.

# dispatch.dispatch(arguments…)

Notifies each registered listener, passing the listener the specified arguments. The this context of the dispatch method will be used as the context of the registered listeners. For example, to invoke all registered listeners with the context foo and the argument bar, say dispatch.call(foo, bar).

Clone this wiki locally