@data-ui/xy-chart
Version:
A package of charts with standard x- and y- axes. https://williaster.github.io/data-ui
125 lines (114 loc) • 3.21 kB
JavaScript
/* eslint no-magic-numbers: 'off' */
import { Children } from 'react';
export function callOrValue(maybeFn) {
if (typeof maybeFn === 'function') {
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
return maybeFn.apply(void 0, args);
}
return maybeFn;
}
export function componentName(component) {
if (component && component.type) {
return component.type.displayName || component.type.name || 'Component';
}
return '';
}
export function getChildWithName(name, children) {
var ChildOfInterest = Children.toArray(children).filter(function (c) {
return componentName(c) === name;
});
return ChildOfInterest.length ? ChildOfInterest[0] : null;
}
export function isDefined(val) {
return typeof val !== 'undefined' && val !== null;
}
export function isAxis(name) {
return /axis/gi.test(name);
}
export function isBarSeries(name) {
return /bar/gi.test(name);
}
export function isBrush(name) {
return name === 'Brush';
}
export function isCirclePackSeries(name) {
return name === 'CirclePackSeries';
}
export function isCrossHair(name) {
return /crosshair/gi.test(name);
}
export function isReferenceLine(name) {
return /reference/gi.test(name);
}
export function isSeries(name) {
return /series/gi.test(name);
}
export function isStackedSeries(name) {
return /stacked/gi.test(name);
}
export function numTicksForHeight(height) {
if (height <= 300) return 3;
if (height <= 600) return 5;
return 8;
}
export function numTicksForWidth(width) {
if (width <= 300) return 3;
if (width <= 400) return 5;
return 10;
}
export function propOrFallback(props, propName, fallback) {
return props && isDefined(props[propName]) ? props[propName] : fallback;
}
export function scaleInvert(scale, value) {
// Test if the scale is an ordinalScale or not,
// Since an ordinalScale doesn't support invert function.
if (!scale.invert) {
var _scale$range = scale.range(),
start = _scale$range[0],
end = _scale$range[1];
var i = 0;
var width = scale.step() * (end - start) / Math.abs(end - start);
if (width > 0) {
while (value > start + width * (i + 1)) {
i += 1;
}
} else {
while (value < start + width * (i + 1)) {
i += 1;
}
}
return i;
}
return scale.invert(value);
}
export function getDomainFromExtent(scale, start, end, tolerentDelta) {
var domain;
var invertedStart = scaleInvert(scale, start + (start < end ? -tolerentDelta : tolerentDelta));
var invertedEnd = scaleInvert(scale, end + (end < start ? -tolerentDelta : tolerentDelta));
var minValue = Math.min(invertedStart, invertedEnd);
var maxValue = Math.max(invertedStart, invertedEnd);
if (scale.invert) {
domain = {
start: minValue,
end: maxValue
};
} else {
var values = [];
var scaleDomain = scale.domain();
for (var i = minValue; i <= maxValue; i += 1) {
values.push(scaleDomain[i]);
}
domain = {
values: values
};
}
return domain;
}
export var DEFAULT_CHART_MARGIN = {
top: 64,
right: 64,
bottom: 64,
left: 64
};