React Box Plot Component
react-boxplot is a React component for rendering simple SVG box plots. It provides flexibility for both horizontal and vertical orientations and aligns the major axis coordinate system with the original data values. The current stable version is 4.0.0. This library is a maintained fork of an originally abandoned repository, suggesting a slower, maintenance-driven release cadence focused on stability and bug fixes rather than rapid feature development. Its key differentiators include its pure SVG rendering, allowing for easy styling and scalability, and the option to either compute box plot statistics internally via `computeBoxplotStats` or provide pre-calculated statistics, catering to different data processing workflows.
Common errors
-
Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
cause Attempting to import `Boxplot` as a named export when it is a default export.fixChange `import { Boxplot } from 'react-boxplot'` to `import Boxplot from 'react-boxplot'`. -
TypeError: Cannot read properties of undefined (reading 'map') or similar errors related to data processing within the component.
cause The `stats` prop is either missing or contains invalid/incomplete data, specifically for the `outliers` array or other required statistical values.fixEnsure the `stats` prop is always provided with a valid object conforming to the expected structure, including `whiskerLow`, `quartile1`, `quartile2`, `quartile3`, `whiskerHigh`, and an `outliers` array (even if empty). Use `computeBoxplotStats()` to generate a valid `stats` object from raw data.
Warnings
- gotcha This library is a fork of an abandoned project. While currently maintained by its original author, future development or major feature additions might be limited. Evaluate long-term support needs carefully.
- breaking Version 4.0.0 requires React 15, 16, or 17. Projects using older or newer unsupported React versions may encounter compatibility issues or warnings related to peer dependency mismatches.
- gotcha When providing custom `stats` to the Boxplot component, ensure all required properties (whiskerLow, quartile1, quartile2, quartile3, whiskerHigh) are present and numerically valid. Missing or invalid statistics will result in an incorrectly rendered plot or runtime errors.
Install
-
npm install react-boxplot -
yarn add react-boxplot -
pnpm add react-boxplot
Imports
- Boxplot
import { Boxplot } from 'react-boxplot'import Boxplot from 'react-boxplot'
- computeBoxplotStats
import computeBoxplotStats from 'react-boxplot'
import { computeBoxplotStats } from 'react-boxplot' - BoxplotProps
import type { BoxplotProps } from 'react-boxplot'
Quickstart
import React from 'react';
import Boxplot, { computeBoxplotStats } from 'react-boxplot';
const values = [
14, 15, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 19, 19, 19, 20,
20, 20, 20, 20, 20, 21, 21, 22, 23, 24, 24, 29
];
const BoxplotExample = () => (
<Boxplot
width={400}
height={20}
orientation="horizontal"
min={0}
max={30}
stats={computeBoxplotStats(values)}
/>
);
export default BoxplotExample;