Skip to content
mbostock edited this page Jun 13, 2011 · 18 revisions

API ReferenceCore

D3 has a variety of internal methods that may be useful if you are implementing your own layout or chart template.

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.

# 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