1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603 |
1×
19×
8×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
5×
64×
1583×
1×
1×
1×
1×
1×
1×
1×
7×
7×
7×
7×
7×
7×
7×
7×
14×
14×
14×
10×
10×
14×
7×
1×
33×
5×
28×
28×
28×
28×
28×
1×
3×
3×
3×
12×
12×
12×
12×
12×
3×
3×
3×
1×
42×
35×
7×
1×
24×
1×
66×
66×
66×
66×
66×
66×
66×
66×
66×
66×
66×
66×
66×
3×
63×
53×
63×
63×
39×
24×
24×
24×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
31×
31×
31×
31×
31×
31×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
66×
66×
35×
31×
30×
1×
1×
1×
1×
6×
6×
1×
40×
1×
22×
1×
37×
37×
16×
16×
16×
30×
16×
21×
1×
3×
3×
3×
3×
3×
3×
3×
3×
3×
10×
10×
3×
1×
10×
10×
3×
3×
7×
1×
263×
263×
| 'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (EObject.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _SCALE_FUNCTIONS;
exports._getSmallestDistanceIndex = _getSmallestDistanceIndex;
exports.getScaleObjectFromProps = getScaleObjectFromProps;
exports.getAttributeScale = getAttributeScale;
exports.getAttributeFunctor = getAttributeFunctor;
exports.getAttr0Functor = getAttr0Functor;
exports.getAttributeValue = getAttributeValue;
exports.getScalePropTypesByAttribute = getScalePropTypesByAttribute;
var _d3Scale = require('d3-scale');
var _d3Scale2 = _interopRequireDefault(_d3Scale);
var _d3Array = require('d3-array');
var _d3Array2 = _interopRequireDefault(_d3Array);
var _d3Collection = require('d3-collection');
var _d3Collection2 = _interopRequireDefault(_d3Collection);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _warning = require('warning');
var _warning2 = _interopRequireDefault(_warning);
var _dataUtils = require('./data-utils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (EArray.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _defineProperty(obj, key, value) { if (Ikey in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } // Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/**
* Linear scale name.
* @type {string}
* @const
*/
var LINEAR_SCALE_TYPE = 'linear';
/**
* Ordinal scale name.
* @type {string}
* @const
*/
var ORDINAL_SCALE_TYPE = 'ordinal';
/**
* Category scale.
* @type {string}
* @const
*/
var CATEGORY_SCALE_TYPE = 'category';
/**
* Log scale name.
* @type {string}
* @const
*/
var LOG_SCALE_TYPE = 'log';
/**
* Time scale name.
* @type {string}
* @const
*/
var TIME_SCALE_TYPE = 'time';
/**
* Scale functions that are supported in the library.
* @type {Object}
* @const
*/
var SCALE_FUNCTIONS = (_SCALE_FUNCTIONS = {}, _defineProperty(_SCALE_FUNCTIONS, LINEAR_SCALE_TYPE, _d3Scale2.default.scaleLinear), _defineProperty(_SCALE_FUNCTIONS, ORDINAL_SCALE_TYPE, _d3Scale2.default.scaleOrdinal), _defineProperty(_SCALE_FUNCTIONS, CATEGORY_SCALE_TYPE, _d3Scale2.default.scaleOrdinal), _defineProperty(_SCALE_FUNCTIONS, LOG_SCALE_TYPE, _d3Scale2.default.scaleLog), _defineProperty(_SCALE_FUNCTIONS, TIME_SCALE_TYPE, _d3Scale2.default.scaleTime), _SCALE_FUNCTIONS);
/**
* Find the smallest distance between the values on a given scale and return
* the index of the element, where the smallest distance was found.
* It returns the first occurrence of i where
* `scale(value[i]) - scale(value[i - 1])` is minimal
* @param {Array} values Array of values.
* @param {Object} scaleObject Scale object.
* @returns {number} Index of an element where the smallest distance was found.
* @private
*/
function _getSmallestDistanceIndex(values, scaleObject) {
var scaleFn = _getScaleFnFromScaleObject(scaleObject);
var result = 0;
if (EscaleFn) {
var nextValue = void 0;
var currentValue = scaleFn(values[0]);
var distance = Infinity;
var nextDistance = void 0;
for (var i = 1; i < values.length; i++) {
nextValue = scaleFn(values[i]);
nextDistance = Math.abs(nextValue - currentValue);
if (nextDistance < distance) {
distance = nextDistance;
result = i;
}
currentValue = nextValue;
}
}
return result;
}
/**
* Crate a scale function from the scale object.
* @param {Object} scaleObject Scale object.
* @returns {*} Scale function.
* @private
*/
function _getScaleFnFromScaleObject(scaleObject) {
if (!scaleObject) {
return null;
}
var type = scaleObject.type;
var domain = scaleObject.domain;
var range = scaleObject.range;
if (Etype !== ORDINAL_SCALE_TYPE) {
return SCALE_FUNCTIONS[type]().domain(domain).range(range);
}
return SCALE_FUNCTIONS[type]().domain(domain).rangePoints(range, 1);
}
/**
* Get the domain from the array of data.
* @param {Array} allData All data.
* @param {string} attr Property name.
* @param {string} type Scale type.
* @returns {Array} Domain.
* @private
*/
function _getDomainByAttr(allData, attr, type) {
var domain = void 0;
var attr0 = attr + '0';
// Collect both attr and available attr0 values from the array of data.
var values = allData.reduce(function (data, d) {
var value = d[attr];
var value0 = d[attr0];
data.push(value);
if (I_isDefined(value0)) {
data.push(value0);
}
return data;
}, []);
// Create proper domain depending on the type of the scale.
if (Etype !== ORDINAL_SCALE_TYPE && type !== CATEGORY_SCALE_TYPE) {
domain = _d3Array2.default.extent(values);
} else {
domain = _d3Collection2.default.set(values).values();
}
return domain;
}
/**
* Create custom scale object from the value. When the scale is created from
* this object, it should return the same value all time.
* @param {string} attr Attribute.
* @param {*} value Value.
* @returns {Object} Custom scale object.
* @private
*/
function _createScaleObjectForValue(attr, value) {
if (typeof value === 'undefined') {
return null;
}
return {
type: 'category',
range: [value],
domain: [],
distance: 0,
attr: attr,
baseValue: undefined,
isValue: true
};
}
/**
* Create a regular scale object for a further use from the existing parameters.
* @param {Array} domain Domain.
* @param {Array} range Range.
* @param {string} type Type.
* @param {number} distance Distance.
* @param {string} attr Attribute.
* @param {number} baseValue Base value.
* @returns {Object} Scale object.
* @private
*/
function _createScaleObjectForFunction(domain, range, type, distance, attr, baseValue) {
return {
domain: domain,
range: range,
type: type,
distance: distance,
attr: attr,
baseValue: baseValue,
isValue: false
};
}
/**
* Get scale object from props. E. g. object like {xRange, xDomain, xDistance,
* xType} is transformed into {range, domain, distance, type}.
* @param {Object} props Props.
* @param {string} attr Attribute.
* @returns {*} Null or an object with the scale.
* @private
*/
function _collectScaleObjectFromProps(props, attr) {
var _ref;
var _props$_allData = props._allData;
var data = _props$_allData === undefined ? [] : _props$_allData;
var value = props[attr];
var fallbackValue = props['_' + attr + 'Value'];
var initialDomain = props[attr + 'Domain'];
var range = props[attr + 'Range'];
var _props$ = props[attr + 'Distance'];
var distance = _props$ === undefined ? 0 : _props$;
var baseValue = props[attr + 'BaseValue'];
var _props$2 = props[attr + 'Type'];
var type = _props$2 === undefined ? LINEAR_SCALE_TYPE : _props$2;
// Return value-based scale if the value is assigned.
if (typeof value !== 'undefined') {
return _createScaleObjectForValue(attr, value);
}
var filteredData = data.filter(function (d) {
return d;
});
var allData = (_ref = []).concat.apply(_ref, _toConsumableArray(filteredData));
// Make sure that the minimum necessary properties exist.
if (!range || !allData.length || !(0, _dataUtils.isObjectPropertyInUse)(allData, attr)) {
// Try to use the fallback value if it is available.
return _createScaleObjectForValue(attr, fallbackValue);
}
// Pick up the domain from the properties and create a new one if it's not
// available.
var domain = initialDomain || _getDomainByAttr(allData, attr, type);
if (Itypeof baseValue !== 'undefined') {
domain = (0, _dataUtils.addValueToArray)(domain, baseValue);
}
return _createScaleObjectForFunction(domain, range, type, distance, attr, baseValue);
}
/**
* Compute left domain adjustment for the given values.
* @param {Array} values Array of values.
* @returns {number} Domain adjustment.
* @private
*/
function _computeLeftDomainAdjustment(values) {
if (Evalues.length > 1) {
return (values[1] - values[0]) / 2;
}
if (values.length === 1) {
return values[0] - 0.5;
}
return 0;
}
/**
* Compute right domain adjustment for the given values.
* @param {Array} values Array of values.
* @returns {number} Domain adjustment.
* @private
*/
function _computeRightDomainAdjustment(values) {
if (Evalues.length > 1) {
return (values[values.length - 1] - values[values.length - 2]) / 2;
}
if (values.length === 1) {
return values[0] - 0.5;
}
return 0;
}
/**
* Compute distance for the given values.
* @param {Array} values Array of values.
* @param {Array} domain Domain.
* @param {number} bestDistIndex Index of a best distance found.
* @param {function} scaleFn Scale function.
* @returns {number} Domain adjustment.
* @private
*/
function _computeScaleDistance(values, domain, bestDistIndex, scaleFn) {
if (Evalues.length > 1) {
// Avoid zero indexes.
var i = Math.max(bestDistIndex, 1);
return Math.abs(scaleFn(values[i]) - scaleFn(values[i - 1]));
}
if (values.length === 1) {
return Math.abs(scaleFn(domain[1]) - scaleFn(domain[0]));
}
return 0;
}
/**
* Get the distance, the smallest and the largest value of the domain.
* @param {Array} data Array of data for the single series.
* @param {Object} scaleObject Scale object.
* @returns {{domain0: number, domainN: number, distance: number}} Resuylt.
* @private
*/
function _getScaleDistanceAndAdjustedDomain(data, scaleObject) {
var attr = scaleObject.attr;
var domain = scaleObject.domain;
var values = (0, _dataUtils.getUniquePropertyValues)(data, attr);
var index = _getSmallestDistanceIndex(values, scaleObject);
var adjustedDomain = [].concat(domain);
adjustedDomain[0] -= _computeLeftDomainAdjustment(values);
adjustedDomain[domain.length - 1] += _computeRightDomainAdjustment(values);
// Fix log scale if it's too small.
if (IscaleObject.type === LOG_SCALE_TYPE && domain[0] <= 0) {
adjustedDomain[0] = Math.min(domain[1] / 10, 1);
}
var adjustedScaleFn = _getScaleFnFromScaleObject(_extends({}, scaleObject, {
domain: adjustedDomain
}));
var distance = _computeScaleDistance(values, adjustedDomain, index, adjustedScaleFn);
return {
domain0: adjustedDomain[0],
domainN: adjustedDomain[adjustedDomain.length - 1],
distance: distance
};
}
/**
* Returns true if scale adjustments are possible for a given scale.
* @param {Object} props Props.
* @param {Object} scaleObject Scale object.
* @returns {boolean} True if scale adjustments possible.
* @private
*/
function _isScaleAdjustmentPossible(props, scaleObject) {
var attr = scaleObject.attr;
var _props$_adjustBy = props._adjustBy;
var adjustBy = _props$_adjustBy === undefined ? [] : _props$_adjustBy;
var _props$_adjustWhat = props._adjustWhat;
var adjustWhat = _props$_adjustWhat === undefined ? [] : _props$_adjustWhat;
// The scale cannot be adjusted if there's no attributes to adjust, no
// suitable values
return adjustWhat.length && adjustBy.length && adjustBy.indexOf(attr) !== -1;
}
/**
* Adjust continuous scales (e.g. 'linear', 'log' and 'time') by adding the
* space from the left and right of them and by computing the best distance.
* @param {Object} props Props.
* @param {Object} scaleObject Scale object.
* @returns {*} Scale object with adjustments.
* @private
*/
function _adjustContinuousScale(props, scaleObject) {
var allSeriesData = props._allData;
var _props$_adjustWhat2 = props._adjustWhat;
var adjustWhat = _props$_adjustWhat2 === undefined ? [] : _props$_adjustWhat2;
// Assign the initial values.
var domainLength = scaleObject.domain.length;
var domain = scaleObject.domain;
var scaleDomain0 = domain[0];
var scaleDomainN = domain[domainLength - 1];
var scaleDistance = scaleObject.distance;
// Find the smallest left position of the domain, the largest right position
// of the domain and the best distance for them.
allSeriesData.forEach(function (data, index) {
if (IadjustWhat.indexOf(index) === -1) {
return;
}
if (Edata && data.length) {
var _getScaleDistanceAndA = _getScaleDistanceAndAdjustedDomain(data, scaleObject);
var domain0 = _getScaleDistanceAndA.domain0;
var domainN = _getScaleDistanceAndA.domainN;
var distance = _getScaleDistanceAndA.distance;
scaleDomain0 = Math.min(scaleDomain0, domain0);
scaleDomainN = Math.max(scaleDomainN, domainN);
scaleDistance = Math.max(scaleDistance, distance);
}
});
scaleObject.domain = [scaleDomain0].concat(_toConsumableArray(domain.slice(1, -1)), [scaleDomainN]);
scaleObject.distance = scaleDistance;
return scaleObject;
}
/**
* Get an adjusted scale. Suitable for 'category' and 'ordinal' scales.
* @param {Object} scaleObject Scale object.
* @returns {*} Scale object with adjustments.
* @private
*/
function _adjustCategoricalScale(scaleObject) {
var scaleFn = _getScaleFnFromScaleObject(scaleObject);
var domain = scaleObject.domain;
var range = scaleObject.range;
if (domain.length > 1) {
scaleObject.distance = scaleFn(domain[1]) - scaleFn(domain[0]);
} else {
scaleObject.distance = range[1] - range[0];
}
return scaleObject;
}
/**
* Retrieve a scale object or a value from the properties passed.
* @param {Object} props Object of props.
* @param {string} attr Attribute.
* @returns {*} Scale object, value or null.
*/
function getScaleObjectFromProps(props, attr) {
// Create the initial scale object.
var scaleObject = _collectScaleObjectFromProps(props, attr);
if (!scaleObject) {
return null;
}
// Make sure if it's possible to add space to the scale object. If not,
// return the object immediately.
if (!_isScaleAdjustmentPossible(props, scaleObject)) {
return scaleObject;
}
var type = scaleObject.type;
// Depending on what type the scale is, apply different adjustments. Distances
// for the ordinal and category scales are even, equal domains cannot be
// adjusted.
if (Itype === ORDINAL_SCALE_TYPE || type === CATEGORY_SCALE_TYPE) {
return _adjustCategoricalScale(scaleObject);
}
return _adjustContinuousScale(props, scaleObject);
}
/**
* Get d3 scale for a given prop.
* @param {Object} props Props.
* @param {string} attr Attribute.
* @returns {function} d3 scale function.
*/
function getAttributeScale(props, attr) {
var scaleObject = getScaleObjectFromProps(props, attr);
return _getScaleFnFromScaleObject(scaleObject);
}
/**
* Get the value of `attr` from the object.
* @param {Object} d Object.
* @param {string} attr Attribute.
* @returns {*} Value of the point.
* @private
*/
function _getAttrValue(d, attr) {
return d.data ? d.data[attr] : d[attr];
}
function _isDefined(value) {
return typeof value !== 'undefined';
}
/**
* Get prop functor (either a value or a function) for a given attribute.
* @param {Object} props Series props.
* @param {string} attr Property name.
* @returns {*} Function or value.
*/
function getAttributeFunctor(props, attr) {
var scaleObject = getScaleObjectFromProps(props, attr);
if (scaleObject) {
var _ret = function () {
var scaleFn = _getScaleFnFromScaleObject(scaleObject);
return {
v: function v(d) {
return scaleFn(_getAttrValue(d, attr));
}
};
}();
if (E(typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
return null;
}
/**
* Get the functor which extracts value form [attr]0 property. Use baseValue if
* no attr0 property for a given object is defined. Fall back to domain[0] if no
* base value is available.
* @param {Object} props Object of props.
* @param {string} attr Attribute name.
* @returns {*} Function which returns value or null if no values available.
*/
function getAttr0Functor(props, attr) {
var scaleObject = getScaleObjectFromProps(props, attr);
if (EscaleObject) {
var _ret2 = function () {
var attr0 = attr + '0';
var domain = scaleObject.domain;
var _scaleObject$baseValu = scaleObject.baseValue;
var baseValue = _scaleObject$baseValu === undefined ? domain[0] : _scaleObject$baseValu;
var scaleFn = _getScaleFnFromScaleObject(scaleObject);
return {
v: function v(d) {
var value = _getAttrValue(d, attr0);
return scaleFn(_isDefined(value) ? value : baseValue);
}
};
}();
if (E(typeof _ret2 === 'undefined' ? 'undefined' : _typeof(_ret2)) === "object") return _ret2.v;
}
return null;
}
/**
* Tries to get the string|number value of the attr and falls back to
* a fallback property in case if the value is a scale.
* @param {Object} props Series props.
* @param {string} attr Property name.
* @returns {*} Function or value.
*/
function getAttributeValue(props, attr) {
var scaleObject = getScaleObjectFromProps(props, attr);
if (scaleObject) {
if (I!scaleObject.isValue) {
(0, _warning2.default)(false, 'Cannot use data defined ' + attr + ' for this series' + 'type. Using fallback value instead.');
}
return scaleObject.range[0];
}
return null;
}
/**
* Get prop types by the attribute.
* @param {string} attr Attribute.
* @returns {Object} Object of xDomain, xRange, xType, xDistance and _xValue,
* where x is an attribute passed to the function.
*/
function getScalePropTypesByAttribute(attr) {
var _ref2;
return _ref2 = {}, _defineProperty(_ref2, '_' + attr + 'Value', _react2.default.PropTypes.any), _defineProperty(_ref2, attr + 'Domain', _react2.default.PropTypes.array), _defineProperty(_ref2, attr + 'Range', _react2.default.PropTypes.array), _defineProperty(_ref2, attr + 'Type', _react2.default.PropTypes.oneOf(Object.keys(SCALE_FUNCTIONS))), _defineProperty(_ref2, attr + 'Distance', _react2.default.PropTypes.number), _defineProperty(_ref2, attr + 'BaseValue', _react2.default.PropTypes.any), _ref2;
} |