Package Exports
- d3plus-common
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (d3plus-common) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
d3plus-common
Common functions and methods used across D3plus modules.
Installing
If you use NPM, npm install d3plus-common. Otherwise, download the latest release. The released bundle supports AMD, CommonJS, and vanilla environments. Create a custom bundle using Rollup or your preferred bundler. You can also load directly from d3plus.org:
<script src="https://d3plus.org/js/d3plus-common.v0.5.full.min.js"></script>API Reference
Classes
- BaseClass
Functions
- accessor(key, [def])
Wraps an object key in a simple accessor function.
- attrize(elem, attrs)
Applies each key/value in an object as an attr.
- closest(n, arr)
Finds the closest numeric value in an array.
- constant(value)
Wraps non-function variables in a simple return function.
- elem(selector, params)
Manages the enter/update/exit pattern for a single DOM element.
- merge(objects, aggs)
Combines an Array of Objects together and returns a new Object.
- prefix()
Returns the appropriate CSS vendor prefix, given the current browser.
- stylize(elem, styles)
Applies each key/value in an object as a style.
BaseClass
Kind: global class
new BaseClass()
An abstract class that contains some global methods and functionality.
BaseClass.config([value])
If value is specified, sets the methods that correspond to the key/value pairs and returns this class. If value is not specified, returns the current configuration.
Kind: static method of BaseClass
| Param | Type |
|---|---|
| [value] | Object |
BaseClass.on([typenames], [listener])
Adds or removes a listener to each object for the specified event typenames. If a listener is not specified, returns the currently assigned listener for the specified event typename. Mirrors the core d3-selection behavior.
Kind: static method of BaseClass
| Param | Type |
|---|---|
| [typenames] | String |
| [listener] | function |
Example (By default, listeners apply globally to all objects, however, passing a namespace with the class name gives control over specific elements:)
new Plot
.on("click.Shape", function(d) {
console.log("data for shape clicked:", d);
})
.on("click.Legend", function(d) {
console.log("data for legend clicked:", d);
})accessor(key, [def])
Wraps an object key in a simple accessor function.
Kind: global function
| Param | Type | Description |
|---|---|---|
| key | String |
The key to be returned from each Object passed to the function. |
| [def] | * |
A default value to be returned if the key is not present. |
Example (this)
accessor("id");
Example (returns this)
function(d) {
return d["id"];
}attrize(elem, attrs)
Applies each key/value in an object as an attr.
Kind: global function
| Param | Type | Description |
|---|---|---|
| elem | D3selection |
The D3 element to apply the styles to. |
| attrs | Object |
An object of key/value attr pairs. |
closest(n, arr)
Finds the closest numeric value in an array.
Kind: global function
| Param | Type | Description |
|---|---|---|
| n | Number |
The number value to use when searching the array. |
| arr | Array |
The array of values to test against. |
constant(value)
Wraps non-function variables in a simple return function.
Kind: global function
| Param | Type | Description |
|---|---|---|
| value | Array | Number | Object | String |
The value to be returned from the function. |
Example (this)
constant(42);
Example (returns this)
function() {
return 42;
}elem(selector, params)
Manages the enter/update/exit pattern for a single DOM element.
Kind: global function
| Param | Type | Default | Description |
|---|---|---|---|
| selector | String |
A D3 selector, which must include the tagname and a class and/or ID. | |
| params | Object |
Additional parameters. | |
| [params.condition] | Boolean |
true |
Whether or not the element should be rendered (or removed). |
| [params.enter] | Object |
{} |
A collection of key/value pairs that map to attributes to be given on enter. |
| [params.exit] | Object |
{} |
A collection of key/value pairs that map to attributes to be given on exit. |
| [params.parent] | D3Selection |
d3.select("body") |
The parent element for this new element to be appended to. |
| [params.transition] | D3Transition |
d3.transition().duration(0) |
The transition to use when animated the different life cycle stages. |
| [params.update] | Object |
{} |
A collection of key/value pairs that map to attributes to be given on update. |
merge(objects, aggs)
Combines an Array of Objects together and returns a new Object.
Kind: global function
| Param | Type | Description |
|---|---|---|
| objects | Array |
The Array of objects to be merged together. |
| aggs | Object |
An object containing specific aggregation methods (functions) for each key type. By default, numbers are summed and strings are returned as an array of unique values. |
Example (this)
merge([
{id: "foo", group: "A", value: 10, links: [1, 2]},
{id: "bar", group: "A", value: 20, links: [1, 3]}
]);
Example (returns this)
{id: ["bar", "foo"], group: "A", value: 30, links: [1, 2, 3]}prefix()
Returns the appropriate CSS vendor prefix, given the current browser.
stylize(elem, styles)
Applies each key/value in an object as a style.
Kind: global function
| Param | Type | Description |
|---|---|---|
| elem | D3selection |
The D3 element to apply the styles to. |
| styles | Object |
An object of key/value style pairs. |