recharts
Version:
React charts
93 lines (90 loc) • 3.93 kB
JavaScript
import { createSelector } from 'reselect';
import { selectUnfilteredCartesianItems } from './axisSelectors';
import { selectBarRectangles } from './barSelectors';
var pickStackId = (state, stackId) => stackId;
var pickIsPanorama = (state, stackId, isPanorama) => isPanorama;
export var selectAllBarsInStack = createSelector([pickStackId, selectUnfilteredCartesianItems, pickIsPanorama], (stackId, allItems, isPanorama) => {
return allItems.filter(i => i.type === 'bar').filter(i => i.stackId === stackId).filter(i => i.isPanorama === isPanorama).filter(i => !i.hide);
});
var selectAllBarIdsInStack = createSelector([selectAllBarsInStack], allBars => {
return allBars.map(bar => bar.id);
});
/**
* Takes two rectangles and returns a new rectangle that encompasses both.
* It takes the minimum x and y, and the maximum width and height.
* It handles overlapping rectangles, and rectangles with a gap between them.
* @param rect1
* @param rect2
*/
export var expandRectangle = (rect1, rect2) => {
var first = rect1 !== null && rect1 !== void 0 ? rect1 : rect2;
if (!first) {
return undefined;
}
// A lone rectangle goes through the same normalization, so that negative values do not keep a negative height
var second = rect2 !== null && rect2 !== void 0 ? rect2 : first;
var x = Math.min(first.x, first.x + first.width, second.x, second.x + second.width);
var y = Math.min(first.y, first.y + first.height, second.y, second.y + second.height);
var maxX = Math.max(first.x, first.x + first.width, second.x, second.x + second.width);
var maxY = Math.max(first.y, first.y + first.height, second.y, second.y + second.height);
var width = maxX - x;
var height = maxY - y;
return {
x,
y,
width,
height
};
};
var combineStackRects = (state, stackId, isPanorama) => {
var allBarIds = selectAllBarIdsInStack(state, stackId, isPanorama);
var stackRects = [];
allBarIds.forEach(barId => {
var rectangles = selectBarRectangles(state, barId, isPanorama, undefined);
rectangles === null || rectangles === void 0 || rectangles.forEach(rect => {
var rectIndex = rect.originalDataIndex;
stackRects[rectIndex] = expandRectangle(stackRects[rectIndex], rect);
});
});
return stackRects;
};
export var selectStackRects = createSelector([state => state, pickStackId, pickIsPanorama], combineStackRects);
/** Complete non-negative number with an optional px suffix. Other units are rejected. */
var PIXEL_STROKE_WIDTH = /^\+?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?(?:px)?$/i;
var parseStrokeWidth = strokeWidth => {
if (typeof strokeWidth === 'string') {
var trimmed = strokeWidth.trim();
if (!PIXEL_STROKE_WIDTH.test(trimmed)) {
return 0;
}
// '1e400' matches but overflows to Infinity
var parsed = parseFloat(trimmed);
return Number.isFinite(parsed) ? parsed : 0;
}
if (strokeWidth == null || !Number.isFinite(strokeWidth) || strokeWidth < 0) {
return 0;
}
return strokeWidth;
};
export var combineMaxStrokeWidthOfStack = allBars => allBars.reduce((max, bar) => Math.max(max, parseStrokeWidth(bar.strokeWidth)), 0);
/**
* Selects the largest strokeWidth of all bars in the stack.
* The stack clip path grows by half of this value in every direction.
*/
export var selectMaxStrokeWidthOfStack = createSelector([selectAllBarsInStack], combineMaxStrokeWidthOfStack);
/**
* Grows the rectangle by the given amount in every direction.
* Rectangles with negative width or height (stackOffset="sign") are normalized
* so that the returned rectangle always has non-negative dimensions.
*/
export var inflateRectangle = (rect, inflation) => {
if (inflation === 0) {
return rect;
}
return {
x: Math.min(rect.x, rect.x + rect.width) - inflation,
y: Math.min(rect.y, rect.y + rect.height) - inflation,
width: Math.abs(rect.width) + 2 * inflation,
height: Math.abs(rect.height) + 2 * inflation
};
};