@patternfly/react-charts
Version:
This library provides a set of React chart components for use with the PatternFly reference implementation.
101 lines • 6.72 kB
JavaScript
import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { cloneElement, Fragment, useEffect } from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics';
import { Helpers } from 'victory-core';
import { VictoryPie } from 'victory-pie';
import { ChartContainer } from '../ChartContainer/ChartContainer';
import { ChartLegend } from '../ChartLegend/ChartLegend';
import { ChartCommonStyles } from '../ChartTheme/ChartStyles';
import { ChartTooltip } from '../ChartTooltip/ChartTooltip';
import { getComputedLegend, getLegendItemsExtraHeight, getLegendMaxTextWidth } from '../ChartUtils/chart-legend';
import { getPaddingForSide } from '../ChartUtils/chart-padding';
import { getPatternDefs, useDefaultPatternProps } from '../ChartUtils/chart-patterns';
import { getTheme } from '../ChartUtils/chart-theme';
import { ChartPoint } from '../ChartPoint/ChartPoint';
import { ChartLabel } from '../ChartLabel/ChartLabel';
export const ChartPie = (_a) => {
var { allowTooltip = true, ariaDesc, ariaTitle, colorScale, constrainToVisibleArea = false, containerComponent = _jsx(ChartContainer, {}), hasPatterns, legendAllowWrap = false, legendComponent = _jsx(ChartLegend, {}), legendData, legendPosition = ChartCommonStyles.legend.position, legendDirection = 'ltr', name, patternScale, patternUnshiftIndex, padding, radius, standalone = true, style, themeColor,
// destructure last
theme = getTheme(themeColor), labelComponent = allowTooltip ? (_jsx(ChartTooltip, { constrainToVisibleArea: constrainToVisibleArea, theme: theme })) : undefined, legendOrientation = theme.legend.orientation, height = theme.pie.height, width = theme.pie.width } = _a, rest = __rest(_a, ["allowTooltip", "ariaDesc", "ariaTitle", "colorScale", "constrainToVisibleArea", "containerComponent", "hasPatterns", "legendAllowWrap", "legendComponent", "legendData", "legendPosition", "legendDirection", "name", "patternScale", "patternUnshiftIndex", "padding", "radius", "standalone", "style", "themeColor", "theme", "labelComponent", "legendOrientation", "height", "width"]);
const defaultPadding = {
bottom: getPaddingForSide('bottom', padding, theme.pie.padding),
left: getPaddingForSide('left', padding, theme.pie.padding),
right: getPaddingForSide('right', padding, theme.pie.padding),
top: getPaddingForSide('top', padding, theme.pie.padding)
};
const { defaultColorScale, defaultPatternScale, isPatternDefs, patternId } = useDefaultPatternProps({
colorScale,
hasPatterns,
patternScale,
themeColorScale: theme.pie.colorScale
});
// Ensure non-negative value is returned
const getDefaultRadius = () => {
const result = Helpers.getRadius({
height,
width,
padding: defaultPadding
});
return result > -1 ? result : undefined;
};
const chartRadius = radius ? radius : getDefaultRadius();
// Merge pattern IDs with `style.data.fill` property
const getDefaultStyle = () => {
if (!defaultPatternScale) {
return style;
}
const _style = style ? Object.assign({}, style) : {};
_style.data = Object.assign({ fill: ({ slice }) => {
const pattern = defaultPatternScale[slice.index % defaultPatternScale.length];
return pattern ? pattern : defaultColorScale[slice.index % defaultColorScale.length];
} }, _style.data);
return _style;
};
const chart = (_jsx(VictoryPie, Object.assign({ colorScale: colorScale, height: height, labelComponent: labelComponent, name: name, padding: padding, radius: chartRadius, standalone: false, style: getDefaultStyle(), theme: theme, width: width }, rest), "pf-chart-pie"));
let legendXOffset = 0;
if (legendDirection === 'rtl') {
legendXOffset = getLegendMaxTextWidth(legendData, theme);
}
const legend = cloneElement(legendComponent, Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ colorScale, data: legendData }, (name && { name: `${name}-${legendComponent.type.displayName}` })), { key: 'pf-chart-pie-legend', orientation: legendOrientation, theme,
themeColor }), (legendDirection === 'rtl' && {
dataComponent: legendComponent.props.dataComponent ? (cloneElement(legendComponent.props.dataComponent, { transform: `translate(${legendXOffset})` })) : (_jsx(ChartPoint, { transform: `translate(${legendXOffset})` }))
})), (legendDirection === 'rtl' && {
labelComponent: legendComponent.props.labelComponent ? (cloneElement(legendComponent.props.labelComponent, { direction: 'rtl', dx: legendXOffset - 30 })) : (_jsx(ChartLabel, { direction: "rtl", dx: legendXOffset - 30 }))
})), legendComponent.props));
// Returns a computed legend
const getLegend = () => {
if (!legend.props.data) {
return null;
}
return getComputedLegend(Object.assign({ allowWrap: legendAllowWrap === true || typeof legendAllowWrap === 'function', chartType: 'pie', height, legendComponent: legend, padding: defaultPadding, position: legendPosition, theme,
width }, (defaultPatternScale && { patternScale: defaultPatternScale })));
};
// Clone so users can override container props
const container = standalone
? cloneElement(containerComponent, Object.assign({ desc: ariaDesc, height, title: ariaTitle, width,
theme }, containerComponent.props), [
chart,
getLegend(),
isPatternDefs && getPatternDefs({ patternId, colorScale: defaultColorScale, patternUnshiftIndex })
])
: null;
// Callback to compliment legendAllowWrap
const computedLegend = getLegend();
useEffect(() => {
if ((computedLegend === null || computedLegend === void 0 ? void 0 : computedLegend.props) && typeof legendAllowWrap === 'function') {
const extraHeight = getLegendItemsExtraHeight({
legendData: computedLegend.props.data,
legendOrientation: computedLegend.props.orientation,
legendProps: computedLegend.props,
theme
});
legendAllowWrap(extraHeight);
}
}, [computedLegend, legendAllowWrap, theme, width]);
return standalone ? (_jsx(Fragment, { children: container })) : (_jsxs(Fragment, { children: [chart, computedLegend, isPatternDefs && getPatternDefs({ patternId, colorScale: defaultColorScale, patternUnshiftIndex })] }));
};
ChartPie.displayName = 'ChartPie';
// Note: VictoryPie.role must be hoisted
hoistNonReactStatics(ChartPie, VictoryPie);
//# sourceMappingURL=ChartPie.js.map