UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

384 lines (383 loc) 13.8 kB
'use client'; import _extends from "@babel/runtime/helpers/esm/extends"; import * as React from 'react'; import PropTypes from 'prop-types'; import { useThemeProps } from '@mui/material/styles'; import { BarPlot } from "./BarPlot.mjs"; import { ChartsAxis } from "../ChartsAxis/index.mjs"; import { ChartsTooltip } from "../ChartsTooltip/index.mjs"; import { ChartsLegend } from "../ChartsLegend/index.mjs"; import { ChartsAxisHighlight } from "../ChartsAxisHighlight/index.mjs"; import { ChartsClipPath } from "../ChartsClipPath/index.mjs"; import { ChartsGrid } from "../ChartsGrid/index.mjs"; import { ChartsOverlay } from "../ChartsOverlay/ChartsOverlay.mjs"; import { useBarChartProps } from "./useBarChartProps.mjs"; import { ChartsDataProvider } from "../ChartsDataProvider/index.mjs"; import { ChartsSurface } from "../ChartsSurface/index.mjs"; import { useChartsContainerProps } from "../ChartsContainer/useChartsContainerProps.mjs"; import { ChartsWrapper } from "../ChartsWrapper/index.mjs"; import { FocusedBar } from "./FocusedBar.mjs"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; /** * Demos: * * - [Bars](https://mui.com/x/react-charts/bars/) * - [Bar demonstration](https://mui.com/x/react-charts/bar-demo/) * - [Stacking](https://mui.com/x/react-charts/stacking/) * * API: * * - [BarChart API](https://mui.com/x/api/charts/bar-chart/) */ const BarChart = /*#__PURE__*/React.forwardRef(function BarChart(inProps, ref) { const props = useThemeProps({ props: inProps, name: 'MuiBarChart' }); const { chartsWrapperProps, chartsContainerProps, barPlotProps, gridProps, clipPathProps, clipPathGroupProps, overlayProps, chartsAxisProps, axisHighlightProps, legendProps, children } = useBarChartProps(props); const { chartsDataProviderProps, chartsSurfaceProps } = useChartsContainerProps(chartsContainerProps); const Tooltip = props.slots?.tooltip ?? ChartsTooltip; const Toolbar = props.slots?.toolbar; return /*#__PURE__*/_jsx(ChartsDataProvider, _extends({}, chartsDataProviderProps, { children: /*#__PURE__*/_jsxs(ChartsWrapper, _extends({}, chartsWrapperProps, { ref: ref, children: [props.showToolbar && Toolbar ? /*#__PURE__*/_jsx(Toolbar, _extends({}, props.slotProps?.toolbar)) : null, !props.hideLegend && /*#__PURE__*/_jsx(ChartsLegend, _extends({}, legendProps)), /*#__PURE__*/_jsxs(ChartsSurface, _extends({}, chartsSurfaceProps, { children: [/*#__PURE__*/_jsx(ChartsGrid, _extends({}, gridProps)), /*#__PURE__*/_jsxs("g", _extends({}, clipPathGroupProps, { children: [/*#__PURE__*/_jsx(BarPlot, _extends({}, barPlotProps)), /*#__PURE__*/_jsx(ChartsOverlay, _extends({}, overlayProps)), /*#__PURE__*/_jsx(ChartsAxisHighlight, _extends({}, axisHighlightProps)), /*#__PURE__*/_jsx(FocusedBar, {})] })), /*#__PURE__*/_jsx(ChartsAxis, _extends({}, chartsAxisProps)), /*#__PURE__*/_jsx(ChartsClipPath, _extends({}, clipPathProps)), children] })), !props.loading && /*#__PURE__*/_jsx(Tooltip, _extends({}, props.slotProps?.tooltip))] })) })); }); if (process.env.NODE_ENV !== "production") BarChart.displayName = "BarChart"; process.env.NODE_ENV !== "production" ? BarChart.propTypes = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit the TypeScript types and run "pnpm proptypes" | // ---------------------------------------------------------------------- apiRef: PropTypes.shape({ current: PropTypes.object }), /** * A gap added between axes when multiple axes are rendered on the same side of the chart. * @default 0 */ axesGap: PropTypes.number, /** * The configuration of axes highlight. * Default is set to 'band' in the bar direction. * Depends on `layout` prop. * @see See {@link https://mui.com/x/react-charts/highlighting/ highlighting docs} for more details. */ axisHighlight: PropTypes.shape({ x: PropTypes.oneOf(['band', 'line', 'none']), y: PropTypes.oneOf(['band', 'line', 'none']) }), /** * Defines the border radius of the bar element. */ borderRadius: PropTypes.number, /** * Configuration for the brush interaction. */ brushConfig: PropTypes.shape({ enabled: PropTypes.bool, preventHighlight: PropTypes.bool, preventTooltip: PropTypes.bool }), children: PropTypes.node, className: PropTypes.string, /** * Color palette used to colorize multiple series. * @default rainbowSurgePalette */ colors: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string), PropTypes.func]), /** * An array of objects that can be used to populate series and axes data using their `dataKey` property. */ dataset: PropTypes.arrayOf(PropTypes.object), /** * The description of the chart. * Used to provide an accessible description for the chart. */ desc: PropTypes.string, /** * If `true`, the charts will not listen to the mouse move event. * It might break interactive features, but will improve performance. * @default false */ disableAxisListener: PropTypes.bool, /** * If `true`, disables keyboard navigation for the chart. */ disableKeyboardNavigation: PropTypes.bool, /** * Options to enable features planned for the next major. */ experimentalFeatures: PropTypes.object, /** * Option to display a cartesian grid in the background. */ grid: PropTypes.shape({ horizontal: PropTypes.bool, vertical: PropTypes.bool }), /** * The height of the chart in px. If not defined, it takes the height of the parent element. */ height: PropTypes.number, /** * List of hidden series and/or items. * * Different chart types use different keys. * * @example * ```ts * [ * { * type: 'pie', * seriesId: 'series-1', * dataIndex: 3, * }, * { * type: 'line', * seriesId: 'series-2', * } * ] * ``` */ hiddenItems: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.shape({ dataIndex: PropTypes.number, seriesId: PropTypes.string.isRequired, type: PropTypes.oneOf(['bar']).isRequired }), PropTypes.shape({ dataIndex: PropTypes.number, seriesId: PropTypes.string.isRequired, type: PropTypes.oneOf(['bar']) })]).isRequired), /** * If `true`, the legend is not rendered. */ hideLegend: PropTypes.bool, /** * The controlled axis highlight. * Identified by the axis id, and data index. */ highlightedAxis: PropTypes.arrayOf(PropTypes.shape({ axisId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired, dataIndex: PropTypes.number.isRequired })), /** * The highlighted item. * Used when the highlight is controlled. */ highlightedItem: PropTypes.oneOfType([PropTypes.shape({ dataIndex: PropTypes.number, seriesId: PropTypes.string.isRequired }), PropTypes.shape({ dataIndex: PropTypes.number, seriesId: PropTypes.string.isRequired, type: PropTypes.oneOf(['bar']).isRequired })]), /** * This prop is used to help implement the accessibility logic. * If you don't provide this prop. It falls back to a randomly generated id. */ id: PropTypes.string, /** * List of initially hidden series and/or items. * Used for uncontrolled state. * * Different chart types use different keys. * * @example * ```ts * [ * { * type: 'pie', * seriesId: 'series-1', * dataIndex: 3, * }, * { * type: 'line', * seriesId: 'series-2', * } * ] * ``` */ initialHiddenItems: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.shape({ dataIndex: PropTypes.number, seriesId: PropTypes.string.isRequired, type: PropTypes.oneOf(['bar']).isRequired }), PropTypes.shape({ dataIndex: PropTypes.number, seriesId: PropTypes.string.isRequired, type: PropTypes.oneOf(['bar']) })]).isRequired), /** * The direction of the bar elements. * @default 'vertical' */ layout: PropTypes.oneOf(['horizontal', 'vertical']), /** * If `true`, a loading overlay is displayed. * @default false */ loading: PropTypes.bool, /** * Localized text for chart components. */ localeText: PropTypes.object, /** * The margin between the SVG and the drawing area. * It's used for leaving some space for extra information such as the x- and y-axis or legend. * * Accepts a `number` to be used on all sides or an object with the optional properties: `top`, `bottom`, `left`, and `right`. */ margin: PropTypes.oneOfType([PropTypes.number, PropTypes.shape({ bottom: PropTypes.number, left: PropTypes.number, right: PropTypes.number, top: PropTypes.number })]), /** * The function called for onClick events. * The second argument contains information about all line/bar elements at the current mouse position. * @param {MouseEvent} event The mouse event recorded on the `<svg/>` element. * @param {null | ChartsAxisData} data The data about the clicked axis and items associated with it. */ onAxisClick: PropTypes.func, /** * Callback fired when any hidden identifiers change. * @param {VisibilityIdentifierWithType[]} hiddenItems The new list of hidden identifiers. */ onHiddenItemsChange: PropTypes.func, /** * The callback fired when the highlighted item changes. * * @param {HighlightItemIdentifierWithType<SeriesType> | null} highlightedItem The newly highlighted item. */ onHighlightChange: PropTypes.func, /** * The function called when the pointer position corresponds to a new axis data item. * This update can either be caused by a pointer movement, or an axis update. * In case of multiple axes, the function is called if at least one axis is updated. * The argument contains the identifier for all axes with a `data` property. * @param {AxisItemIdentifier[]} axisItems The array of axes item identifiers. */ onHighlightedAxisChange: PropTypes.func, /** * Callback fired when a bar item is clicked. * @param {MouseEvent | React.MouseEvent<SVGElement, MouseEvent>} event The event source of the callback. * It is a native MouseEvent for `svg-batch` renderer and a React MouseEvent for `svg-single` renderer. * @param {BarItemIdentifier} barItemIdentifier The bar item identifier. */ onItemClick: PropTypes.func, /** * The function called when the pointer position corresponds to a new axis data item. * This update can either be caused by a pointer movement, or an axis update. * In case of multiple axes, the function is called if at least one axis is updated. * The argument contains the identifier for all axes with a `data` property. * @param {AxisItemIdentifier[]} axisItems The array of axes item identifiers. */ onTooltipAxisChange: PropTypes.func, /** * The callback fired when the tooltip item changes. * * @param {SeriesItemIdentifier<SeriesType> | null} tooltipItem The newly highlighted item. */ onTooltipItemChange: PropTypes.func, /** * The type of renderer to use for the bar plot. * - `svg-single`: Renders every bar in a `<rect />` element. * - `svg-batch`: Batch renders bars in `<path />` elements for better performance with large datasets, at the cost of some limitations. * Read more: https://mui.com/x/react-charts/bars/#performance * * @default 'svg-single' */ renderer: PropTypes.oneOf(['svg-batch', 'svg-single']), /** * The series to display in the bar chart. * An array of [[BarSeries]] objects. */ series: PropTypes.arrayOf(PropTypes.object).isRequired, /** * If true, shows the default chart toolbar. * @default false */ showToolbar: PropTypes.bool, /** * If `true`, animations are skipped. * If unset or `false`, the animations respects the user's `prefers-reduced-motion` setting. */ skipAnimation: PropTypes.bool, /** * The props used for each component slot. * @default {} */ slotProps: PropTypes.object, /** * Overridable component slots. * @default {} */ slots: PropTypes.object, sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]), theme: PropTypes.oneOf(['dark', 'light']), /** * The title of the chart. * Used to provide an accessible label for the chart. */ title: PropTypes.string, /** * The controlled axis tooltip. * Identified by the axis id, and data index. */ tooltipAxis: PropTypes.arrayOf(PropTypes.shape({ axisId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired, dataIndex: PropTypes.number.isRequired })), /** * The tooltip item. * Used when the tooltip is controlled. */ tooltipItem: PropTypes.oneOfType([PropTypes.shape({ dataIndex: PropTypes.number.isRequired, seriesId: PropTypes.string.isRequired }), PropTypes.shape({ dataIndex: PropTypes.number.isRequired, seriesId: PropTypes.string.isRequired, type: PropTypes.oneOf(['bar']).isRequired })]), /** * The width of the chart in px. If not defined, it takes the width of the parent element. */ width: PropTypes.number, /** * The configuration of the x-axes. * If not provided, a default axis config is used. * An array of [[AxisConfig]] objects. */ xAxis: PropTypes.arrayOf(PropTypes.object), /** * The configuration of the y-axes. * If not provided, a default axis config is used. * An array of [[AxisConfig]] objects. */ yAxis: PropTypes.arrayOf(PropTypes.object) } : void 0; export { BarChart };