@patternfly/react-charts
Version:
This library provides a set of React chart components for use with the PatternFly reference implementation.
98 lines • 6.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Chart = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const hoist_non_react_statics_1 = tslib_1.__importDefault(require("hoist-non-react-statics"));
/* eslint-disable camelcase */
const chart_legend_Margin_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/chart_legend_Margin'));
const victory_chart_1 = require("victory-chart");
const ChartContainer_1 = require("../ChartContainer");
const ChartLegend_1 = require("../ChartLegend");
const ChartTheme_1 = require("../ChartTheme");
const ChartUtils_1 = require("../ChartUtils");
const Chart = (_a) => {
var { ariaDesc, ariaTitle, children, colorScale, hasPatterns, legendAllowWrap = false, legendComponent = React.createElement(ChartLegend_1.ChartLegend, null), legendData, legendPosition = ChartTheme_1.ChartCommonStyles.legend.position, name, padding, patternScale, showAxis = true, themeColor,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
themeVariant,
// destructure last
theme = (0, ChartUtils_1.getChartTheme)(themeColor, showAxis), containerComponent = React.createElement(ChartContainer_1.ChartContainer, null), legendOrientation = theme.legend.orientation, height = theme.chart.height, width = theme.chart.width } = _a, rest = tslib_1.__rest(_a, ["ariaDesc", "ariaTitle", "children", "colorScale", "hasPatterns", "legendAllowWrap", "legendComponent", "legendData", "legendPosition", "name", "padding", "patternScale", "showAxis", "themeColor", "themeVariant", "theme", "containerComponent", "legendOrientation", "height", "width"]);
const defaultPadding = {
bottom: (0, ChartUtils_1.getPaddingForSide)('bottom', padding, theme.chart.padding),
left: (0, ChartUtils_1.getPaddingForSide)('left', padding, theme.chart.padding),
right: (0, ChartUtils_1.getPaddingForSide)('right', padding, theme.chart.padding),
top: (0, ChartUtils_1.getPaddingForSide)('top', padding, theme.chart.padding)
};
const { defaultColorScale, defaultPatternScale, isPatternDefs, patternId } = (0, ChartUtils_1.useDefaultPatternProps)({
colorScale,
patternScale,
hasPatterns,
themeColorScale: theme.chart.colorScale
});
// Add pattern props for legend tooltip
let labelComponent;
if (containerComponent.props.labelComponent &&
containerComponent.props.labelComponent.type.displayName === 'ChartLegendTooltip') {
labelComponent = React.cloneElement(containerComponent.props.labelComponent, Object.assign(Object.assign({ theme }, (defaultPatternScale && { patternScale: defaultPatternScale })), containerComponent.props.labelComponent.props));
}
// Clone so users can override container props
const container = React.cloneElement(containerComponent, Object.assign(Object.assign(Object.assign({ desc: ariaDesc, title: ariaTitle, theme }, containerComponent.props), { className: (0, ChartUtils_1.getClassName)({ className: containerComponent.props.className }) }), (labelComponent && { labelComponent }) // Override label component props
));
const legend = React.cloneElement(legendComponent, Object.assign(Object.assign(Object.assign({ data: legendData }, (name && { name: `${name}-${legendComponent.type.displayName}` })), { orientation: legendOrientation, theme }), legendComponent.props));
// Returns a computed legend
const getLegend = () => {
if (!legend.props.data) {
return null;
}
let dx = 0;
let dy = 0;
let xAxisLabelHeight = 0;
let legendTitleHeight = legend.props.title ? 10 : 0;
// Adjust for axis label
React.Children.toArray(children).map((child) => {
if (child.type.role === 'axis' && child.props.label && !child.props.dependentAxis) {
xAxisLabelHeight = (0, ChartUtils_1.getLabelTextSize)({ text: child.props.label, theme }).height + 10;
legendTitleHeight = 0;
}
});
if (legendPosition === ChartLegend_1.ChartLegendPosition.bottom) {
dy += xAxisLabelHeight + legendTitleHeight;
}
else if (legendPosition === ChartLegend_1.ChartLegendPosition.bottomLeft) {
dy += xAxisLabelHeight + legendTitleHeight;
dx = -10;
}
// Adjust legend position when axis is hidden
if (!showAxis) {
dy -= chart_legend_Margin_1.default.value;
}
return (0, ChartUtils_1.getComputedLegend)(Object.assign({ allowWrap: legendAllowWrap, chartType: 'chart', colorScale,
dx,
dy,
height, legendComponent: legend, padding: defaultPadding, position: legendPosition, theme,
width }, (defaultPatternScale && { patternScale: defaultPatternScale })));
};
// Render children
const renderChildren = () => React.Children.toArray(children).map((child, index) => {
if (React.isValidElement(child)) {
const childProps = tslib_1.__rest(child.props, []);
return React.cloneElement(child, Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ colorScale }, (defaultPatternScale && { patternScale: defaultPatternScale })), (name &&
typeof child.name !== undefined && {
name: `${name}-${child.type.displayName}-${index}`
})), { theme }), childProps), (child.type.displayName === 'ChartPie' && {
data: (0, ChartUtils_1.getDefaultData)(childProps.data, defaultPatternScale)
}) // Override child props
));
}
return child;
});
// Note: containerComponent is required for theme
return (React.createElement(victory_chart_1.VictoryChart, Object.assign({ colorScale: colorScale, containerComponent: container, height: height, name: name, padding: defaultPadding, theme: theme, width: width }, rest),
renderChildren(),
getLegend(),
isPatternDefs && (0, ChartUtils_1.getPatternDefs)({ patternId, colorScale: defaultColorScale })));
};
exports.Chart = Chart;
exports.Chart.displayName = 'Chart';
(0, hoist_non_react_statics_1.default)(exports.Chart, victory_chart_1.VictoryChart);
//# sourceMappingURL=Chart.js.map