@patternfly/react-charts
Version:
This library provides a set of React chart components for use with the PatternFly reference implementation.
98 lines • 6.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChartPie = exports.ChartPieSortOrder = exports.ChartPieLegendPosition = exports.ChartPieLabelPosition = 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"));
const victory_core_1 = require("victory-core");
const victory_pie_1 = require("victory-pie");
const ChartContainer_1 = require("../ChartContainer");
const ChartLegend_1 = require("../ChartLegend");
const ChartTheme_1 = require("../ChartTheme");
const ChartTooltip_1 = require("../ChartTooltip");
const ChartUtils_1 = require("../ChartUtils");
var ChartPieLabelPosition;
(function (ChartPieLabelPosition) {
ChartPieLabelPosition["centroid"] = "centroid";
ChartPieLabelPosition["endAngle"] = "endAngle";
ChartPieLabelPosition["startAngle"] = "startAngle";
})(ChartPieLabelPosition = exports.ChartPieLabelPosition || (exports.ChartPieLabelPosition = {}));
var ChartPieLegendPosition;
(function (ChartPieLegendPosition) {
ChartPieLegendPosition["bottom"] = "bottom";
ChartPieLegendPosition["right"] = "right";
})(ChartPieLegendPosition = exports.ChartPieLegendPosition || (exports.ChartPieLegendPosition = {}));
var ChartPieSortOrder;
(function (ChartPieSortOrder) {
ChartPieSortOrder["ascending"] = "ascending";
ChartPieSortOrder["descending"] = "descending";
})(ChartPieSortOrder = exports.ChartPieSortOrder || (exports.ChartPieSortOrder = {}));
const ChartPie = (_a) => {
var { allowTooltip = true, ariaDesc, ariaTitle, colorScale, constrainToVisibleArea = false, containerComponent = React.createElement(ChartContainer_1.ChartContainer, null), hasPatterns, legendAllowWrap = false, legendComponent = React.createElement(ChartLegend_1.ChartLegend, null), legendData, legendPosition = ChartTheme_1.ChartCommonStyles.legend.position, name, patternScale, patternUnshiftIndex, padding, radius, standalone = true, style, themeColor,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
themeVariant,
// destructure last
theme = (0, ChartUtils_1.getTheme)(themeColor), labelComponent = allowTooltip ? (React.createElement(ChartTooltip_1.ChartTooltip, { constrainToVisibleArea: constrainToVisibleArea, theme: theme })) : (undefined), legendOrientation = theme.legend.orientation, height = theme.pie.height, width = theme.pie.width } = _a, rest = tslib_1.__rest(_a, ["allowTooltip", "ariaDesc", "ariaTitle", "colorScale", "constrainToVisibleArea", "containerComponent", "hasPatterns", "legendAllowWrap", "legendComponent", "legendData", "legendPosition", "name", "patternScale", "patternUnshiftIndex", "padding", "radius", "standalone", "style", "themeColor", "themeVariant", "theme", "labelComponent", "legendOrientation", "height", "width"]);
const defaultPadding = {
bottom: (0, ChartUtils_1.getPaddingForSide)('bottom', padding, theme.pie.padding),
left: (0, ChartUtils_1.getPaddingForSide)('left', padding, theme.pie.padding),
right: (0, ChartUtils_1.getPaddingForSide)('right', padding, theme.pie.padding),
top: (0, ChartUtils_1.getPaddingForSide)('top', padding, theme.pie.padding)
};
const { defaultColorScale, defaultPatternScale, isPatternDefs, patternId } = (0, ChartUtils_1.useDefaultPatternProps)({
colorScale,
hasPatterns,
patternScale,
themeColorScale: theme.pie.colorScale
});
// Ensure non-negative value is returned
const getDefaultRadius = () => {
const result = victory_core_1.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 = (React.createElement(victory_pie_1.VictoryPie, Object.assign({ colorScale: colorScale, height: height, key: "pf-chart-pie", labelComponent: labelComponent, name: name, padding: padding, radius: chartRadius, standalone: false, style: getDefaultStyle(), theme: theme, width: width }, rest)));
const legend = React.cloneElement(legendComponent, Object.assign(Object.assign(Object.assign({ colorScale, data: legendData }, (name && { name: `${name}-${legendComponent.type.displayName}` })), { key: 'pf-chart-pie-legend', orientation: legendOrientation, theme }), legendComponent.props));
// Returns a computed legend
const getLegend = () => {
if (!legend.props.data) {
return null;
}
return (0, ChartUtils_1.getComputedLegend)(Object.assign({ allowWrap: legendAllowWrap, chartType: 'pie', height, legendComponent: legend, padding: defaultPadding, position: legendPosition, theme,
width }, (defaultPatternScale && { patternScale: defaultPatternScale })));
};
// Clone so users can override container props
const container = standalone
? React.cloneElement(containerComponent, Object.assign({ desc: ariaDesc, height, title: ariaTitle, width,
theme }, containerComponent.props), [
chart,
getLegend(),
isPatternDefs && (0, ChartUtils_1.getPatternDefs)({ patternId, colorScale: defaultColorScale, patternUnshiftIndex })
])
: null;
return standalone ? (React.createElement(React.Fragment, null, container)) : (React.createElement(React.Fragment, null,
chart,
getLegend(),
isPatternDefs && (0, ChartUtils_1.getPatternDefs)({ patternId, colorScale: defaultColorScale, patternUnshiftIndex })));
};
exports.ChartPie = ChartPie;
exports.ChartPie.displayName = 'ChartPie';
// Note: VictoryPie.role must be hoisted
(0, hoist_non_react_statics_1.default)(exports.ChartPie, victory_pie_1.VictoryPie);
//# sourceMappingURL=ChartPie.js.map