UNPKG

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