UNPKG

12.4 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-100%25-brightgreen.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* [unique](#unique) - ES5 implementation to reduce an Array of values to unique instances.
35* [uuid](#uuid) - Returns a unique identifier.
36
37#####
38* [RESET](#RESET) - String constant used to reset an individual config property.
39
40---
41
42<a name="BaseClass"></a>
43#### **BaseClass** [<>](https://github.com/d3plus/d3plus-common/blob/master/src/BaseClass.js#L28)
44
45
46This is a global class.
47
48
49* [BaseClass](#BaseClass)
50 * [.config([*value*])](#BaseClass.config) ↩︎
51 * [.locale([*value*])](#BaseClass.locale) ↩︎
52 * [.on([*typenames*], [*listener*])](#BaseClass.on) ↩︎
53
54
55<a name="BaseClass.config" href="#BaseClass.config">#</a> BaseClass.**config**([*value*]) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/BaseClass.js#L51)
56
57If *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.
58
59
60This is a static method of [<code>BaseClass</code>](#BaseClass), and is chainable with other methods of this Class.
61
62
63<a name="BaseClass.locale" href="#BaseClass.locale">#</a> BaseClass.**locale**([*value*]) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/BaseClass.js#L102)
64
65If *value* is specified, sets the locale to the specified string and returns the current class instance. This method supports the locales defined in [d3plus-format](https://github.com/d3plus/d3plus-format/blob/master/src/locale.js). In another case, you can define an Object with a custom locale.
66
67
68This is a static method of [<code>BaseClass</code>](#BaseClass), and is chainable with other methods of this Class.
69
70
71```js
72{
73 separator: "",
74 suffixes: ["y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "B", "t", "q", "Q", "Z", "Y"],
75 grouping: [3],
76 delimiters: {
77 thousands: ",",
78 decimal: "."
79 },
80 currency: ["$", ""]
81 }
82```
83
84
85<a name="BaseClass.on" href="#BaseClass.on">#</a> BaseClass.**on**([*typenames*], [*listener*]) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/BaseClass.js#L121)
86
87Adds 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.
88
89
90This is a static method of [<code>BaseClass</code>](#BaseClass), and is chainable with other methods of this Class.
91
92| Param | Type |
93| --- | --- |
94| [*typenames*] | <code>String</code> |
95| [*listener*] | <code>function</code> |
96
97By default, listeners apply globally to all objects, however, passing a namespace with the class name gives control over specific elements:
98
99```js
100new Plot
101 .on("click.Shape", function(d) {
102 console.log("data for shape clicked:", d);
103 })
104 .on("click.Legend", function(d) {
105 console.log("data for legend clicked:", d);
106 })
107```
108
109---
110
111<a name="accessor"></a>
112#### d3plus.**accessor**(key, [def]) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/accessor.js#L1)
113
114Wraps an object key in a simple accessor function.
115
116
117This is a global function.
118
119| Param | Type | Description |
120| --- | --- | --- |
121| key | <code>String</code> | The key to be returned from each Object passed to the function. |
122| [def] | <code>\*</code> | A default value to be returned if the key is not present. |
123
124this
125
126```js
127accessor("id");
128
129```
130returns this
131
132```js
133function(d) {
134 return d["id"];
135}
136```
137
138---
139
140<a name="assign"></a>
141#### d3plus.**assign**(...objects) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/assign.js#L14)
142
143A deeply recursive version of `Object.assign`.
144
145
146This is a global function.
147this
148
149```js
150assign({id: "foo", deep: {group: "A"}}, {id: "bar", deep: {value: 20}}));
151
152```
153returns this
154
155```js
156{id: "bar", deep: {group: "A", value: 20}}
157```
158
159---
160
161<a name="attrize"></a>
162#### d3plus.**attrize**(elem, attrs) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/attrize.js#L1)
163
164Applies each key/value in an object as an attr.
165
166
167This is a global function.
168
169| Param | Type | Description |
170| --- | --- | --- |
171| elem | <code>D3selection</code> | The D3 element to apply the styles to. |
172| attrs | <code>Object</code> | An object of key/value attr pairs. |
173
174
175---
176
177<a name="closest"></a>
178#### d3plus.**closest**(n, arr) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/closest.js#L1)
179
180Finds the closest numeric value in an array.
181
182
183This is a global function.
184
185| Param | Type | Description |
186| --- | --- | --- |
187| n | <code>Number</code> | The number value to use when searching the array. |
188| arr | <code>Array</code> | The array of values to test against. |
189
190
191---
192
193<a name="configPrep"></a>
194#### d3plus.**configPrep**([config], [type], [nest]) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/configPrep.js#L1)
195
196Preps 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.
197
198
199This is a global function.
200
201| Param | Type | Default | Description |
202| --- | --- | --- | --- |
203| [config] | <code>Object</code> | <code>this._shapeConfig</code> | The configuration object to parse. |
204| [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". |
205| [nest] | <code>String</code> | | An optional nested key to bubble up to the parent config level. |
206
207
208---
209
210<a name="constant"></a>
211#### d3plus.**constant**(value) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/constant.js#L1)
212
213Wraps non-function variables in a simple return function.
214
215
216This is a global function.
217this
218
219```js
220constant(42);
221
222```
223returns this
224
225```js
226function() {
227 return 42;
228}
229```
230
231---
232
233<a name="elem"></a>
234#### d3plus.**elem**(selector, params) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/elem.js#L6)
235
236Manages the enter/update/exit pattern for a single DOM element.
237
238
239This is a global function.
240
241| Param | Type | Default | Description |
242| --- | --- | --- | --- |
243| selector | <code>String</code> | | A D3 selector, which must include the tagname and a class and/or ID. |
244| params | <code>Object</code> | | Additional parameters. |
245| [params.condition] | <code>Boolean</code> | <code>true</code> | Whether or not the element should be rendered (or removed). |
246| [params.enter] | <code>Object</code> | <code>{}</code> | A collection of key/value pairs that map to attributes to be given on enter. |
247| [params.exit] | <code>Object</code> | <code>{}</code> | A collection of key/value pairs that map to attributes to be given on exit. |
248| [params.parent] | <code>D3Selection</code> | <code>d3.select(&quot;body&quot;)</code> | The parent element for this new element to be appended to. |
249| [params.transition] | <code>D3Transition</code> | <code>d3.transition().duration(0)</code> | The transition to use when animated the different life cycle stages. |
250| [params.update] | <code>Object</code> | <code>{}</code> | A collection of key/value pairs that map to attributes to be given on update. |
251
252
253---
254
255<a name="isObject"></a>
256#### d3plus.**isObject**(item) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/isObject.js#L1)
257
258Detects if a variable is a javascript Object.
259
260
261This is a global function.
262
263---
264
265<a name="merge"></a>
266#### d3plus.**merge**(objects, aggs) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/merge.js#L5)
267
268Combines an Array of Objects together and returns a new Object.
269
270
271This is a global function.
272
273| Param | Type | Description |
274| --- | --- | --- |
275| objects | <code>Array</code> | The Array of objects to be merged together. |
276| 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. |
277
278this
279
280```js
281merge([
282 {id: "foo", group: "A", value: 10, links: [1, 2]},
283 {id: "bar", group: "A", value: 20, links: [1, 3]}
284]);
285
286```
287returns this
288
289```js
290{id: ["bar", "foo"], group: "A", value: 30, links: [1, 2, 3]}
291```
292
293---
294
295<a name="parseSides"></a>
296#### d3plus.**parseSides**(sides) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/parseSides.js#L1)
297
298Converts a string of directional CSS shorthand values into an object with the values expanded.
299
300
301This is a global function.
302
303---
304
305<a name="prefix"></a>
306#### d3plus.**prefix**() [<>](https://github.com/d3plus/d3plus-common/blob/master/src/prefix.js#L1)
307
308Returns the appropriate CSS vendor prefix, given the current browser.
309
310
311This is a global function.
312
313---
314
315<a name="stylize"></a>
316#### d3plus.**stylize**(elem, styles) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/stylize.js#L1)
317
318Applies each key/value in an object as a style.
319
320
321This is a global function.
322
323| Param | Type | Description |
324| --- | --- | --- |
325| elem | <code>D3selection</code> | The D3 element to apply the styles to. |
326| styles | <code>Object</code> | An object of key/value style pairs. |
327
328
329---
330
331<a name="unique"></a>
332#### d3plus.**unique**(objects) [<>](https://github.com/d3plus/d3plus-common/blob/master/src/unique.js#L1)
333
334ES5 implementation to reduce an Array of values to unique instances.
335
336
337This is a global function.
338this
339
340```js
341unique(["apple", "banana", "apple"]);
342
343```
344returns this
345
346```js
347["apple", "banana"]
348```
349
350---
351
352<a name="uuid"></a>
353#### d3plus.**uuid**() [<>](https://github.com/d3plus/d3plus-common/blob/master/src/uuid.js#L10)
354
355
356This is a global function.
357
358---
359
360<a name="RESET"></a>
361#### **RESET** [<>](https://github.com/d3plus/d3plus-common/blob/master/src/RESET.js#L1)
362
363String constant used to reset an individual config property.
364
365
366This is a global constant.
367
368---
369
370
371
372###### <sub>Documentation generated on Thu, 18 Jul 2019 17:52:53 GMT</sub>