2023-05-11 11:41:04 +00:00
|
|
|
// nivo
|
|
|
|
import { ResponsiveBar, BarSvgProps } from "@nivo/bar";
|
2023-05-15 05:52:06 +00:00
|
|
|
// helpers
|
|
|
|
import { generateYAxisTickValues } from "helpers/graph.helper";
|
2023-05-11 11:41:04 +00:00
|
|
|
// types
|
|
|
|
import { TGraph } from "./types";
|
|
|
|
// constants
|
|
|
|
import { CHARTS_THEME, DEFAULT_MARGIN } from "constants/graph";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
indexBy: string;
|
|
|
|
keys: string[];
|
2023-05-15 05:52:06 +00:00
|
|
|
customYAxisTickValues?: number[];
|
2023-05-11 11:41:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const BarGraph: React.FC<Props & TGraph & Omit<BarSvgProps<any>, "height" | "width">> = ({
|
|
|
|
indexBy,
|
|
|
|
keys,
|
2023-05-15 05:52:06 +00:00
|
|
|
customYAxisTickValues,
|
2023-05-11 11:41:04 +00:00
|
|
|
height = "400px",
|
|
|
|
width = "100%",
|
|
|
|
margin,
|
|
|
|
theme,
|
|
|
|
...rest
|
|
|
|
}) => (
|
|
|
|
<div style={{ height, width }}>
|
|
|
|
<ResponsiveBar
|
|
|
|
indexBy={indexBy}
|
|
|
|
keys={keys}
|
2023-05-11 12:08:46 +00:00
|
|
|
margin={{ ...DEFAULT_MARGIN, ...(margin ?? {}) }}
|
|
|
|
padding={rest.padding ?? rest.data.length > 7 ? 0.8 : 0.9}
|
2023-05-15 05:52:06 +00:00
|
|
|
axisLeft={{
|
|
|
|
tickSize: 0,
|
|
|
|
tickPadding: 10,
|
2023-11-08 15:01:46 +00:00
|
|
|
tickValues: customYAxisTickValues ? generateYAxisTickValues(customYAxisTickValues) : undefined,
|
2023-05-15 05:52:06 +00:00
|
|
|
}}
|
2023-05-11 12:08:46 +00:00
|
|
|
axisBottom={{
|
|
|
|
tickSize: 0,
|
|
|
|
tickPadding: 10,
|
|
|
|
tickRotation: rest.data.length > 7 ? -45 : 0,
|
|
|
|
}}
|
2023-05-11 11:41:04 +00:00
|
|
|
labelTextColor={{ from: "color", modifiers: [["darker", 1.6]] }}
|
2023-05-11 12:08:46 +00:00
|
|
|
theme={{ ...CHARTS_THEME, ...(theme ?? {}) }}
|
2023-11-01 07:00:21 +00:00
|
|
|
animate
|
2023-05-11 12:08:46 +00:00
|
|
|
enableLabel={rest.enableLabel ?? false}
|
2023-05-11 11:41:04 +00:00
|
|
|
{...rest}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|