Bar Chart

Bar charts are used to compare discrete values across categories.

import { Bars } from "nanoplot/Bars";
import { Graph } from "nanoplot/Graph";
import { Legend } from "nanoplot/Legend";
import { GridLines } from "nanoplot/GridLines";
import { XAxis } from "nanoplot/XAxis";
import { YAxis } from "nanoplot/YAxis";
import "nanoplot/styles.css";

export default function App() {
	return (
		<div className={"h-[350px] w-[100%] m-auto dark:bg-black p-10"}>
			<Graph
				data={[
					{
						name: "Male",
						fill: "linear-gradient(to bottom, #e93157 0%, #fbad26 100%)",
						data: [
							{ x: "Jan", y: 5_000 },
							{ x: "Feb", y: 20_000 },
							{ x: "Mar", y: 46_000 },
							{ x: "Apr", y: 20_000 },
						],
					},
					{
						name: "Female",
						fill: "linear-gradient(to bottom, #1c8cdc 0%, #4cc7b0 100%)",
						data: [
							{ x: "Jan", y: 45_000 },
							{ x: "Feb", y: 10_000 },
							{ x: "Mar", y: 15_000 },
							{ x: "Apr", y: 30_000 },
						],
					},
				]}
			>
					<Legend position={"top"} />
					<YAxis/>
					<GridLines />
					<Bars
						horizontal={false}
						labels={{
							position: "above",
							collision: true,
							display: (v) => {
								return new Intl.NumberFormat("en", {
									notation: "compact",
									compactDisplay: "short",
									maximumFractionDigits: 2,
								}).format(Number(v));
							},
						}}
					/>
					<XAxis/>
			</Graph>
		</div>
	);
};

Note

Bar charts are versatile and can support stacked bars for cumulative totals, 100% stacked bars to compare relative proportions, and differential bars showing positive and negative changes relative to an anchor value.

Props

NameDescriptionTypeRequiredDefault
labelsAdds chart labels and defines their positionboolean
| (value: string | number | Date) => string
|{position: "above" | "center"; collision?: boolean; display: (value: string | number | Date) => string;}
No-
horizontalHorizontal bar chartbooleanNofalse
anchorUsed to set the baseline for a diverging bar chart (positive/negative bars)numberNo0
sizeWidth of the bars as a percent (0-100)numberNo50
radiusRadius of the bars as an angle (0-360)numberNo0
glowAdds a glow effect around the barsbooleanNofalse
childrenCustom element rendered within the chartReactNodeNo-
loadingDisplays a loading skeletonbooleanNofalse

Styling

Custom styling can be applied to the bars and corresponding labels, by targeting the class names below.

Class NameElement
bars__barThe rect element representing each bar
bars__labelContainer for each data label
bars__label-textSpan containing the label text