465 lines
16 KiB
JavaScript
465 lines
16 KiB
JavaScript
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
import sortBy from 'es-toolkit/compat/sortBy';
|
|
import get from 'es-toolkit/compat/get';
|
|
import { stack as shapeStack, stackOffsetExpand, stackOffsetNone, stackOffsetSilhouette, stackOffsetWiggle, stackOrderNone } from 'victory-vendor/d3-shape';
|
|
import { findEntryInArray, isNan, isNullish, isNumber, isNumOrStr, mathSign } from './DataUtils';
|
|
import { getSliced } from './getSliced';
|
|
export function getValueByDataKey(obj, dataKey, defaultValue) {
|
|
if (isNullish(obj) || isNullish(dataKey)) {
|
|
return defaultValue;
|
|
}
|
|
if (isNumOrStr(dataKey)) {
|
|
return get(obj, dataKey, defaultValue);
|
|
}
|
|
if (typeof dataKey === 'function') {
|
|
return dataKey(obj);
|
|
}
|
|
return defaultValue;
|
|
}
|
|
export var appendOffsetOfLegend = (offset, legendSettings, legendSize) => {
|
|
if (legendSettings && legendSize) {
|
|
var {
|
|
width: boxWidth,
|
|
height: boxHeight
|
|
} = legendSize;
|
|
var {
|
|
align,
|
|
verticalAlign,
|
|
layout
|
|
} = legendSettings;
|
|
if ((layout === 'vertical' || layout === 'horizontal' && verticalAlign === 'middle') && align !== 'center' && isNumber(offset[align])) {
|
|
return _objectSpread(_objectSpread({}, offset), {}, {
|
|
[align]: offset[align] + (boxWidth || 0)
|
|
});
|
|
}
|
|
if ((layout === 'horizontal' || layout === 'vertical' && align === 'center') && verticalAlign !== 'middle' && isNumber(offset[verticalAlign])) {
|
|
return _objectSpread(_objectSpread({}, offset), {}, {
|
|
[verticalAlign]: offset[verticalAlign] + (boxHeight || 0)
|
|
});
|
|
}
|
|
}
|
|
return offset;
|
|
};
|
|
export var isCategoricalAxis = (layout, axisType) => layout === 'horizontal' && axisType === 'xAxis' || layout === 'vertical' && axisType === 'yAxis' || layout === 'centric' && axisType === 'angleAxis' || layout === 'radial' && axisType === 'radiusAxis';
|
|
|
|
/**
|
|
* Calculate the Coordinates of grid
|
|
* @param {Array} ticks The ticks in axis
|
|
* @param {Number} minValue The minimum value of axis
|
|
* @param {Number} maxValue The maximum value of axis
|
|
* @param {boolean} syncWithTicks Synchronize grid lines with ticks or not
|
|
* @return {Array} Coordinates
|
|
*/
|
|
export var getCoordinatesOfGrid = (ticks, minValue, maxValue, syncWithTicks) => {
|
|
if (syncWithTicks) {
|
|
return ticks.map(entry => entry.coordinate);
|
|
}
|
|
var hasMin, hasMax;
|
|
var values = ticks.map(entry => {
|
|
if (entry.coordinate === minValue) {
|
|
hasMin = true;
|
|
}
|
|
if (entry.coordinate === maxValue) {
|
|
hasMax = true;
|
|
}
|
|
return entry.coordinate;
|
|
});
|
|
if (!hasMin) {
|
|
values.push(minValue);
|
|
}
|
|
if (!hasMax) {
|
|
values.push(maxValue);
|
|
}
|
|
return values;
|
|
};
|
|
|
|
/**
|
|
* A subset of d3-scale that Recharts is using
|
|
*/
|
|
|
|
/**
|
|
* Get the ticks of an axis
|
|
* @param {Object} axis The configuration of an axis
|
|
* @param {Boolean} isGrid Whether or not are the ticks in grid
|
|
* @param {Boolean} isAll Return the ticks of all the points or not
|
|
* @return {Array} Ticks
|
|
*/
|
|
export var getTicksOfAxis = (axis, isGrid, isAll) => {
|
|
if (!axis) {
|
|
return null;
|
|
}
|
|
var {
|
|
duplicateDomain,
|
|
type,
|
|
range,
|
|
scale,
|
|
realScaleType,
|
|
isCategorical,
|
|
categoricalDomain,
|
|
tickCount,
|
|
ticks,
|
|
niceTicks,
|
|
axisType
|
|
} = axis;
|
|
if (!scale) {
|
|
return null;
|
|
}
|
|
var offsetForBand = realScaleType === 'scaleBand' && scale.bandwidth ? scale.bandwidth() / 2 : 2;
|
|
var offset = (isGrid || isAll) && type === 'category' && scale.bandwidth ? scale.bandwidth() / offsetForBand : 0;
|
|
offset = axisType === 'angleAxis' && range && range.length >= 2 ? mathSign(range[0] - range[1]) * 2 * offset : offset;
|
|
|
|
// The ticks set by user should only affect the ticks adjacent to axis line
|
|
if (isGrid && (ticks || niceTicks)) {
|
|
var result = (ticks || niceTicks || []).map((entry, index) => {
|
|
var scaleContent = duplicateDomain ? duplicateDomain.indexOf(entry) : entry;
|
|
return {
|
|
// If the scaleContent is not a number, the coordinate will be NaN.
|
|
// That could be the case for example with a PointScale and a string as domain.
|
|
coordinate: scale(scaleContent) + offset,
|
|
value: entry,
|
|
offset,
|
|
index
|
|
};
|
|
});
|
|
return result.filter(row => !isNan(row.coordinate));
|
|
}
|
|
|
|
// When axis is a categorical axis, but the type of axis is number or the scale of axis is not "auto"
|
|
if (isCategorical && categoricalDomain) {
|
|
return categoricalDomain.map((entry, index) => ({
|
|
coordinate: scale(entry) + offset,
|
|
value: entry,
|
|
index,
|
|
offset
|
|
}));
|
|
}
|
|
if (scale.ticks && !isAll && tickCount != null) {
|
|
return scale.ticks(tickCount).map((entry, index) => ({
|
|
coordinate: scale(entry) + offset,
|
|
value: entry,
|
|
offset,
|
|
index
|
|
}));
|
|
}
|
|
|
|
// When axis has duplicated text, serial numbers are used to generate scale
|
|
return scale.domain().map((entry, index) => ({
|
|
coordinate: scale(entry) + offset,
|
|
value: duplicateDomain ? duplicateDomain[entry] : entry,
|
|
index,
|
|
offset
|
|
}));
|
|
};
|
|
var EPS = 1e-4;
|
|
export var checkDomainOfScale = scale => {
|
|
var domain = scale.domain();
|
|
if (!domain || domain.length <= 2) {
|
|
return;
|
|
}
|
|
var len = domain.length;
|
|
var range = scale.range();
|
|
var minValue = Math.min(range[0], range[1]) - EPS;
|
|
var maxValue = Math.max(range[0], range[1]) + EPS;
|
|
var first = scale(domain[0]);
|
|
var last = scale(domain[len - 1]);
|
|
if (first < minValue || first > maxValue || last < minValue || last > maxValue) {
|
|
scale.domain([domain[0], domain[len - 1]]);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Both value and domain are tuples of two numbers
|
|
* - but the type stays as array of numbers until we have better support in rest of the app
|
|
* @param value input that will be truncated
|
|
* @param domain boundaries
|
|
* @returns tuple of two numbers
|
|
*/
|
|
export var truncateByDomain = (value, domain) => {
|
|
if (!domain || domain.length !== 2 || !isNumber(domain[0]) || !isNumber(domain[1])) {
|
|
return value;
|
|
}
|
|
var minValue = Math.min(domain[0], domain[1]);
|
|
var maxValue = Math.max(domain[0], domain[1]);
|
|
var result = [value[0], value[1]];
|
|
if (!isNumber(value[0]) || value[0] < minValue) {
|
|
result[0] = minValue;
|
|
}
|
|
if (!isNumber(value[1]) || value[1] > maxValue) {
|
|
result[1] = maxValue;
|
|
}
|
|
if (result[0] > maxValue) {
|
|
result[0] = maxValue;
|
|
}
|
|
if (result[1] < minValue) {
|
|
result[1] = minValue;
|
|
}
|
|
return result;
|
|
};
|
|
|
|
/**
|
|
* Stacks all positive numbers above zero and all negative numbers below zero.
|
|
*
|
|
* If all values in the series are positive then this behaves the same as 'none' stacker.
|
|
*
|
|
* @param {Array} series from d3-shape Stack
|
|
* @return {Array} series with applied offset
|
|
*/
|
|
export var offsetSign = series => {
|
|
var n = series.length;
|
|
if (n <= 0) {
|
|
return;
|
|
}
|
|
for (var j = 0, m = series[0].length; j < m; ++j) {
|
|
var positive = 0;
|
|
var negative = 0;
|
|
for (var i = 0; i < n; ++i) {
|
|
var value = isNan(series[i][j][1]) ? series[i][j][0] : series[i][j][1];
|
|
|
|
/* eslint-disable prefer-destructuring, no-param-reassign */
|
|
if (value >= 0) {
|
|
series[i][j][0] = positive;
|
|
series[i][j][1] = positive + value;
|
|
positive = series[i][j][1];
|
|
} else {
|
|
series[i][j][0] = negative;
|
|
series[i][j][1] = negative + value;
|
|
negative = series[i][j][1];
|
|
}
|
|
/* eslint-enable prefer-destructuring, no-param-reassign */
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Replaces all negative values with zero when stacking data.
|
|
*
|
|
* If all values in the series are positive then this behaves the same as 'none' stacker.
|
|
*
|
|
* @param {Array} series from d3-shape Stack
|
|
* @return {Array} series with applied offset
|
|
*/
|
|
export var offsetPositive = series => {
|
|
var n = series.length;
|
|
if (n <= 0) {
|
|
return;
|
|
}
|
|
for (var j = 0, m = series[0].length; j < m; ++j) {
|
|
var positive = 0;
|
|
for (var i = 0; i < n; ++i) {
|
|
var value = isNan(series[i][j][1]) ? series[i][j][0] : series[i][j][1];
|
|
|
|
/* eslint-disable prefer-destructuring, no-param-reassign */
|
|
if (value >= 0) {
|
|
series[i][j][0] = positive;
|
|
series[i][j][1] = positive + value;
|
|
positive = series[i][j][1];
|
|
} else {
|
|
series[i][j][0] = 0;
|
|
series[i][j][1] = 0;
|
|
}
|
|
/* eslint-enable prefer-destructuring, no-param-reassign */
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Function type to compute offset for stacked data.
|
|
*
|
|
* d3-shape has something fishy going on with its types.
|
|
* In @definitelytyped/d3-shape, this function (the offset accessor) is typed as Series<> => void.
|
|
* However! When I actually open the storybook I can see that the offset accessor actually receives Array<Series<>>.
|
|
* The same I can see in the source code itself:
|
|
* https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/66042
|
|
* That one unfortunately has no types but we can tell it passes three-dimensional array.
|
|
*
|
|
* Which leads me to believe that definitelytyped is wrong on this one.
|
|
* There's open discussion on this topic without much attention:
|
|
* https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/66042
|
|
*/
|
|
|
|
var STACK_OFFSET_MAP = {
|
|
sign: offsetSign,
|
|
// @ts-expect-error definitelytyped types are incorrect
|
|
expand: stackOffsetExpand,
|
|
// @ts-expect-error definitelytyped types are incorrect
|
|
none: stackOffsetNone,
|
|
// @ts-expect-error definitelytyped types are incorrect
|
|
silhouette: stackOffsetSilhouette,
|
|
// @ts-expect-error definitelytyped types are incorrect
|
|
wiggle: stackOffsetWiggle,
|
|
positive: offsetPositive
|
|
};
|
|
export var getStackedData = (data, dataKeys, offsetType) => {
|
|
var offsetAccessor = STACK_OFFSET_MAP[offsetType];
|
|
var stack = shapeStack().keys(dataKeys).value((d, key) => Number(getValueByDataKey(d, key, 0))).order(stackOrderNone)
|
|
// @ts-expect-error definitelytyped types are incorrect
|
|
.offset(offsetAccessor);
|
|
return stack(data);
|
|
};
|
|
|
|
/**
|
|
* Stack IDs in the external props allow numbers; but internally we use it as an object key
|
|
* and object keys are always strings. Also, it would be kinda confusing if stackId=8 and stackId='8' were different stacks
|
|
* so let's just force a string.
|
|
*/
|
|
|
|
export function getNormalizedStackId(publicStackId) {
|
|
return publicStackId == null ? undefined : String(publicStackId);
|
|
}
|
|
export function getCateCoordinateOfLine(_ref) {
|
|
var {
|
|
axis,
|
|
ticks,
|
|
bandSize,
|
|
entry,
|
|
index,
|
|
dataKey
|
|
} = _ref;
|
|
if (axis.type === 'category') {
|
|
// find coordinate of category axis by the value of category
|
|
// @ts-expect-error why does this use direct object access instead of getValueByDataKey?
|
|
if (!axis.allowDuplicatedCategory && axis.dataKey && !isNullish(entry[axis.dataKey])) {
|
|
// @ts-expect-error why does this use direct object access instead of getValueByDataKey?
|
|
var matchedTick = findEntryInArray(ticks, 'value', entry[axis.dataKey]);
|
|
if (matchedTick) {
|
|
return matchedTick.coordinate + bandSize / 2;
|
|
}
|
|
}
|
|
return ticks[index] ? ticks[index].coordinate + bandSize / 2 : null;
|
|
}
|
|
var value = getValueByDataKey(entry, !isNullish(dataKey) ? dataKey : axis.dataKey);
|
|
|
|
// @ts-expect-error getValueByDataKey does not validate the output type
|
|
return !isNullish(value) ? axis.scale(value) : null;
|
|
}
|
|
export var getCateCoordinateOfBar = _ref2 => {
|
|
var {
|
|
axis,
|
|
ticks,
|
|
offset,
|
|
bandSize,
|
|
entry,
|
|
index
|
|
} = _ref2;
|
|
if (axis.type === 'category') {
|
|
return ticks[index] ? ticks[index].coordinate + offset : null;
|
|
}
|
|
var value = getValueByDataKey(entry, axis.dataKey, axis.scale.domain()[index]);
|
|
return !isNullish(value) ? axis.scale(value) - bandSize / 2 + offset : null;
|
|
};
|
|
export var getBaseValueOfBar = _ref3 => {
|
|
var {
|
|
numericAxis
|
|
} = _ref3;
|
|
var domain = numericAxis.scale.domain();
|
|
if (numericAxis.type === 'number') {
|
|
// @ts-expect-error type number means the domain has numbers in it but this relationship is not known to typescript
|
|
var minValue = Math.min(domain[0], domain[1]);
|
|
// @ts-expect-error type number means the domain has numbers in it but this relationship is not known to typescript
|
|
var maxValue = Math.max(domain[0], domain[1]);
|
|
if (minValue <= 0 && maxValue >= 0) {
|
|
return 0;
|
|
}
|
|
if (maxValue < 0) {
|
|
return maxValue;
|
|
}
|
|
return minValue;
|
|
}
|
|
return domain[0];
|
|
};
|
|
var getDomainOfSingle = data => {
|
|
var flat = data.flat(2).filter(isNumber);
|
|
return [Math.min(...flat), Math.max(...flat)];
|
|
};
|
|
var makeDomainFinite = domain => {
|
|
return [domain[0] === Infinity ? 0 : domain[0], domain[1] === -Infinity ? 0 : domain[1]];
|
|
};
|
|
export var getDomainOfStackGroups = (stackGroups, startIndex, endIndex) => {
|
|
if (stackGroups == null) {
|
|
return undefined;
|
|
}
|
|
return makeDomainFinite(Object.keys(stackGroups).reduce((result, stackId) => {
|
|
var group = stackGroups[stackId];
|
|
var {
|
|
stackedData
|
|
} = group;
|
|
var domain = stackedData.reduce((res, entry) => {
|
|
var sliced = getSliced(entry, startIndex, endIndex);
|
|
var s = getDomainOfSingle(sliced);
|
|
return [Math.min(res[0], s[0]), Math.max(res[1], s[1])];
|
|
}, [Infinity, -Infinity]);
|
|
return [Math.min(domain[0], result[0]), Math.max(domain[1], result[1])];
|
|
}, [Infinity, -Infinity]));
|
|
};
|
|
export var MIN_VALUE_REG = /^dataMin[\s]*-[\s]*([0-9]+([.]{1}[0-9]+){0,1})$/;
|
|
export var MAX_VALUE_REG = /^dataMax[\s]*\+[\s]*([0-9]+([.]{1}[0-9]+){0,1})$/;
|
|
|
|
/**
|
|
* Calculate the size between two category
|
|
* @param {Object} axis The options of axis
|
|
* @param {Array} ticks The ticks of axis
|
|
* @param {Boolean} isBar if items in axis are bars
|
|
* @return {Number} Size
|
|
*/
|
|
export var getBandSizeOfAxis = (axis, ticks, isBar) => {
|
|
if (axis && axis.scale && axis.scale.bandwidth) {
|
|
var bandWidth = axis.scale.bandwidth();
|
|
if (!isBar || bandWidth > 0) {
|
|
return bandWidth;
|
|
}
|
|
}
|
|
if (axis && ticks && ticks.length >= 2) {
|
|
var orderedTicks = sortBy(ticks, o => o.coordinate);
|
|
var bandSize = Infinity;
|
|
for (var i = 1, len = orderedTicks.length; i < len; i++) {
|
|
var cur = orderedTicks[i];
|
|
var prev = orderedTicks[i - 1];
|
|
bandSize = Math.min((cur.coordinate || 0) - (prev.coordinate || 0), bandSize);
|
|
}
|
|
return bandSize === Infinity ? 0 : bandSize;
|
|
}
|
|
return isBar ? undefined : 0;
|
|
};
|
|
export function getTooltipEntry(_ref4) {
|
|
var {
|
|
tooltipEntrySettings,
|
|
dataKey,
|
|
payload,
|
|
value,
|
|
name
|
|
} = _ref4;
|
|
return _objectSpread(_objectSpread({}, tooltipEntrySettings), {}, {
|
|
dataKey,
|
|
payload,
|
|
value,
|
|
name
|
|
});
|
|
}
|
|
export function getTooltipNameProp(nameFromItem, dataKey) {
|
|
if (nameFromItem) {
|
|
return String(nameFromItem);
|
|
}
|
|
if (typeof dataKey === 'string') {
|
|
return dataKey;
|
|
}
|
|
return undefined;
|
|
}
|
|
export var calculateCartesianTooltipPos = (coordinate, layout) => {
|
|
if (layout === 'horizontal') {
|
|
return coordinate.chartX;
|
|
}
|
|
if (layout === 'vertical') {
|
|
return coordinate.chartY;
|
|
}
|
|
return undefined;
|
|
};
|
|
export var calculatePolarTooltipPos = (rangeObj, layout) => {
|
|
if (layout === 'centric') {
|
|
return rangeObj.angle;
|
|
}
|
|
return rangeObj.radius;
|
|
}; |