UNPKG

11 kBMarkdownView Raw
1# d3plus-common
2
3[![NPM Release](http://img.shields.io/npm/v/d3plus-common.svg?style=flat)](https://www.npmjs.org/package/d3plus-common) [![Build Status](https://travis-ci.org/d3plus/d3plus-common.svg?branch=master)](https://travis-ci.org/d3plus/d3plus-common) [![Dependency Status](http://img.shields.io/david/d3plus/d3plus-common.svg?style=flat)](https://david-dm.org/d3plus/d3plus-common) [![Gitter](https://img.shields.io/badge/-chat_on_gitter-brightgreen.svg?style=flat&logo=gitter-white)](https://gitter.im/d3plus/) [![1.0 progress](https://img.shields.io/badge/1.0_progress-0%25-red.svg?style=flat)](https://github.com/d3plus/d3plus-common/projects/1)
4
5Common functions and methods used across D3plus modules.
6
7## Installing
8
9If you use NPM, run `npm install d3plus-common --save`. Otherwise, download the [latest release](https://github.com/d3plus/d3plus-common/releases/latest). The released bundle supports AMD, CommonJS, and vanilla environments. You can also load directly from [d3plus.org](https://d3plus.org):
10
11```html
12<script src="https://d3plus.org/js/d3plus-common.v0.6.full.min.js"></script>
13```
14
15
16## API Reference
17
18#####
19* [BaseClass](#BaseClass) - An abstract class that contains some global methods and functionality.
20
21#####
22* [accessor](#accessor) - Wraps an object key in a simple accessor function.
23* [assign](#assign) - A deeply recursive version of `Object.assign`.
24* [attrize](#attrize) - Applies each key/value in an object as an attr.
25* [closest](#closest) - Finds the closest numeric value in an array.
26* [configPrep](#configPrep) - Preps a config object for d3plus data, and optionally bubbles up a specific nested type. When using this function, you must bind a d3plus class' `this` context.
27* [constant](#constant) - Wraps non-function variables in a simple return function.
28* [elem](#elem) - Manages the enter/update/exit pattern for a single DOM element.
29* [isObject](#isObject) - Detects if a variable is a javascript Object.
30* [merge](#merge) - Combines an Array of Objects together and returns a new Object.
31* [parseSides](#parseSides) - Converts a string of directional CSS shorthand values into an object with the values expanded.
32* [prefix](#prefix) - Returns the appropriate CSS vendor prefix, given the current browser.
33* [stylize](#stylize) - Applies each key/value in an object as a style.
34* [uuid](#uuid) - Returns a unique identifier.
35
36#####
37* [RESET](#RESET) - String constant used to reset an individual config property.
38
39---
40
41<a name="BaseClass"></a>
42#### **BaseClass** [<>](https://github.com/d3plus/d3plus-common/blob/master/src/BaseClass.js#L28)
43
44
45This is a global class.
46
47
48* [BaseClass](#BaseClass)
49 * [.config([*value*])](#BaseClass.config) ↩︎
50 * [.on([*typenames*], [*listener*])](#BaseClass.on) ↩︎
51
52
53<a name="BaseClass.config" href="#BaseClass.config">#</a> BaseClass.**config**([*value*]) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/BaseClass.js#L50)
54
55If *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.
56
57
58This is a static method of [<code>BaseClass</code>](#BaseClass), and is chainable with other methods of this Class.
59
60
61<a name="BaseClass.on" href="#BaseClass.on">#</a> BaseClass.**on**([*typenames*], [*listener*]) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/BaseClass.js#L99)
62
63Adds 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](https://github.com/d3/d3-selection#selection_on) behavior.
64
65
66This is a static method of [<code>BaseClass</code>](#BaseClass), and is chainable with other methods of this Class.
67
68| Param | Type |
69| --- | --- |
70| [*typenames*] | <code>String</code> |
71| [*listener*] | <code>function</code> |
72
73By default, listeners apply globally to all objects, however, passing a namespace with the class name gives control over specific elements:
74
75```js
76new Plot
77 .on("click.Shape", function(d) {
78 console.log("data for shape clicked:", d);
79 })
80 .on("click.Legend", function(d) {
81 console.log("data for legend clicked:", d);
82 })
83```
84
85---
86
87<a name="accessor"></a>
88#### d3plus.**accessor**(key, [def]) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/accessor.js#L1)
89
90Wraps an object key in a simple accessor function.
91
92
93This is a global function.
94
95| Param | Type | Description |
96| --- | --- | --- |
97| key | <code>String</code> | The key to be returned from each Object passed to the function. |
98| [def] | <code>\*</code> | A default value to be returned if the key is not present. |
99
100this
101
102```js
103accessor("id");
104
105```
106returns this
107
108```js
109function(d) {
110 return d["id"];
111}
112```
113
114---
115
116<a name="assign"></a>
117#### d3plus.**assign**(...objects) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/assign.js#L3)
118
119A deeply recursive version of `Object.assign`.
120
121
122This is a global function.
123this
124
125```js
126assign({id: "foo", deep: {group: "A"}}, {id: "bar", deep: {value: 20}}));
127
128```
129returns this
130
131```js
132{id: "bar", deep: {group: "A", value: 20}}
133```
134
135---
136
137<a name="attrize"></a>
138#### d3plus.**attrize**(elem, attrs) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/attrize.js#L1)
139
140Applies each key/value in an object as an attr.
141
142
143This is a global function.
144
145| Param | Type | Description |
146| --- | --- | --- |
147| elem | <code>D3selection</code> | The D3 element to apply the styles to. |
148| attrs | <code>Object</code> | An object of key/value attr pairs. |
149
150
151---
152
153<a name="closest"></a>
154#### d3plus.**closest**(n, arr) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/closest.js#L1)
155
156Finds the closest numeric value in an array.
157
158
159This is a global function.
160
161| Param | Type | Description |
162| --- | --- | --- |
163| n | <code>Number</code> | The number value to use when searching the array. |
164| arr | <code>Array</code> | The array of values to test against. |
165
166
167---
168
169<a name="configPrep"></a>
170#### d3plus.**configPrep**([config], [type], [nest]) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/configPrep.js#L1)
171
172Preps a config object for d3plus data, and optionally bubbles up a specific nested type. When using this function, you must bind a d3plus class' `this` context.
173
174
175This is a global function.
176
177| Param | Type | Default | Description |
178| --- | --- | --- | --- |
179| [config] | <code>Object</code> | <code>this._shapeConfig</code> | The configuration object to parse. |
180| [type] | <code>String</code> | <code>&quot;shape&quot;</code> | The event classifier to user for "on" events. For example, the default event type of "shape" will apply all events in the "on" config object with that key, like "click.shape" and "mouseleave.shape", in addition to any gloval events like "click" and "mouseleave". |
181| [nest] | <code>String</code> | | An optional nested key to bubble up to the parent config level. |
182
183
184---
185
186<a name="constant"></a>
187#### d3plus.**constant**(value) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/constant.js#L1)
188
189Wraps non-function variables in a simple return function.
190
191
192This is a global function.
193this
194
195```js
196constant(42);
197
198```
199returns this
200
201```js
202function() {
203 return 42;
204}
205```
206
207---
208
209<a name="elem"></a>
210#### d3plus.**elem**(selector, params) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/elem.js#L6)
211
212Manages the enter/update/exit pattern for a single DOM element.
213
214
215This is a global function.
216
217| Param | Type | Default | Description |
218| --- | --- | --- | --- |
219| selector | <code>String</code> | | A D3 selector, which must include the tagname and a class and/or ID. |
220| params | <code>Object</code> | | Additional parameters. |
221| [params.condition] | <code>Boolean</code> | <code>true</code> | Whether or not the element should be rendered (or removed). |
222| [params.enter] | <code>Object</code> | <code>{}</code> | A collection of key/value pairs that map to attributes to be given on enter. |
223| [params.exit] | <code>Object</code> | <code>{}</code> | A collection of key/value pairs that map to attributes to be given on exit. |
224| [params.parent] | <code>D3Selection</code> | <code>d3.select(&quot;body&quot;)</code> | The parent element for this new element to be appended to. |
225| [params.transition] | <code>D3Transition</code> | <code>d3.transition().duration(0)</code> | The transition to use when animated the different life cycle stages. |
226| [params.update] | <code>Object</code> | <code>{}</code> | A collection of key/value pairs that map to attributes to be given on update. |
227
228
229---
230
231<a name="isObject"></a>
232#### d3plus.**isObject**(item) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/isObject.js#L1)
233
234Detects if a variable is a javascript Object.
235
236
237This is a global function.
238
239---
240
241<a name="merge"></a>
242#### d3plus.**merge**(objects, aggs) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/merge.js#L4)
243
244Combines an Array of Objects together and returns a new Object.
245
246
247This is a global function.
248
249| Param | Type | Description |
250| --- | --- | --- |
251| objects | <code>Array</code> | The Array of objects to be merged together. |
252| aggs | <code>Object</code> | 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. |
253
254this
255
256```js
257merge([
258 {id: "foo", group: "A", value: 10, links: [1, 2]},
259 {id: "bar", group: "A", value: 20, links: [1, 3]}
260]);
261
262```
263returns this
264
265```js
266{id: ["bar", "foo"], group: "A", value: 30, links: [1, 2, 3]}
267```
268
269---
270
271<a name="parseSides"></a>
272#### d3plus.**parseSides**(sides) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/parseSides.js#L1)
273
274Converts a string of directional CSS shorthand values into an object with the values expanded.
275
276
277This is a global function.
278
279---
280
281<a name="prefix"></a>
282#### d3plus.**prefix**() [<>](https://github.com/d3plus/d3plus-common/blob/master/src/prefix.js#L1)
283
284Returns the appropriate CSS vendor prefix, given the current browser.
285
286
287This is a global function.
288
289---
290
291<a name="stylize"></a>
292#### d3plus.**stylize**(elem, styles) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/stylize.js#L1)
293
294Applies each key/value in an object as a style.
295
296
297This is a global function.
298
299| Param | Type | Description |
300| --- | --- | --- |
301| elem | <code>D3selection</code> | The D3 element to apply the styles to. |
302| styles | <code>Object</code> | An object of key/value style pairs. |
303
304
305---
306
307<a name="uuid"></a>
308#### d3plus.**uuid**() [<>](https://github.com/d3plus/d3plus-common/blob/master/src/uuid.js#L10)
309
310
311This is a global function.
312
313---
314
315<a name="RESET"></a>
316#### **RESET** [<>](https://github.com/d3plus/d3plus-common/blob/master/src/RESET.js#L1)
317
318String constant used to reset an individual config property.
319
320
321This is a global constant.
322
323---
324
325
326
327###### <sub>Documentation generated on Thu, 31 May 2018 17:28:21 GMT</sub>