UNPKG

@awsui/components-react

Version:

On July 19th, 2022, we launched [Cloudscape Design System](https://cloudscape.design). Cloudscape is an evolution of AWS-UI. It consists of user interface guidelines, front-end components, design resources, and development tools for building intuitive, en

93 lines 3.62 kB
export const chartLegendMap = { line: 'line', bar: 'rectangle', threshold: 'dashed', }; // Starting from the given index, find the first x value in the x domain that has bar data attached to it. export const nextValidDomainIndex = (nextGroupIndex, barGroups, direction = 1) => { let index = nextGroupIndex; if (index < 0 || index >= barGroups.length) { index = 0; } do { if (barGroups[index].isValid && barGroups[index].hasData) { return index; } index += direction; // Loop back to the beginning if necessary if (index >= barGroups.length) { index = 0; } else if (index < 0) { index = barGroups.length - 1; } } while (index !== nextGroupIndex); return 0; }; /** * Find the subset of series that are individually navigable with keyboard. * Lines and thresholds are navigated individually, while bar series are grouped as one. */ export function findNavigableSeries(series) { const navigableSeries = []; let navigableBarSeriesIndex = -1; series.forEach(internalSeries => { if (internalSeries.series.type === 'bar') { // Only include the first bar series because all bar series are handled as one if (navigableBarSeriesIndex === -1) { navigableBarSeriesIndex = navigableSeries.length; navigableSeries.push(internalSeries.series); } } else { navigableSeries.push(internalSeries.series); } }); return { navigableSeries, navigableBarSeriesIndex }; } /** * Checks if two x values are equal. * With a special treat for Date values which need to be converted to numbers first. */ export const matchesX = (x1, x2) => { if (x1 instanceof Date && x2 instanceof Date) { return x1.getTime() === x2.getTime(); } return x1 === x2; }; // Unlike for regular bars, stacked bar series values depend on the predecessors. // The function computes all stacked values grouped by X and series index. export function calculateStackedBarValues(dataBySeries) { var _a, _b, _c, _d; const negativeValues = new Map(); const positiveValues = new Map(); const values = new Map(); for (let seriesIndex = 0; seriesIndex < dataBySeries.length; seriesIndex++) { for (const datum of dataBySeries[seriesIndex]) { const key = getKeyValue(datum.x); if (datum.y < 0) { negativeValues.set(key, ((_a = negativeValues.get(key)) !== null && _a !== void 0 ? _a : 0) + datum.y); } else { positiveValues.set(key, ((_b = positiveValues.get(key)) !== null && _b !== void 0 ? _b : 0) + datum.y); } const seriesValue = (_c = (datum.y < 0 ? negativeValues.get(key) : positiveValues.get(key))) !== null && _c !== void 0 ? _c : 0; const valuesByIndex = (_d = values.get(key)) !== null && _d !== void 0 ? _d : new Map(); valuesByIndex.set(seriesIndex, seriesValue); values.set(key, valuesByIndex); } } return values; } /** Returns string or number value for ChartDataTypes key */ export const getKeyValue = (key) => (key instanceof Date ? key.getTime() : key); export function isYThreshold(series) { return series.type === 'threshold' && 'y' in series; } export function isXThreshold(series) { return series.type === 'threshold' && 'x' in series; } export function isDataSeries(series) { return series.type === 'line' || series.type === 'bar'; } //# sourceMappingURL=utils.js.map