|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +"use client"; |
| 3 | + |
| 4 | +import React, { useMemo } from "react"; |
| 5 | +import { BarChart as CoreBarChart, Bar, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts"; |
| 6 | +// plane imports |
| 7 | +import { AXIS_LINE_CLASSNAME, LABEL_CLASSNAME } from "@plane/constants"; |
| 8 | +import { TBarChartProps } from "@plane/types"; |
| 9 | +// local components |
| 10 | +import { CustomXAxisTick, CustomYAxisTick } from "../tick"; |
| 11 | +import { CustomTooltip } from "../tooltip"; |
| 12 | +import { CustomBar } from "./bar"; |
| 13 | + |
| 14 | +export const BarChart = React.memo(<K extends string, T extends string>(props: TBarChartProps<K, T>) => { |
| 15 | + const { |
| 16 | + data, |
| 17 | + bars, |
| 18 | + xAxis, |
| 19 | + yAxis, |
| 20 | + barSize = 40, |
| 21 | + className = "w-full h-96", |
| 22 | + tickCount = { |
| 23 | + x: undefined, |
| 24 | + y: 10, |
| 25 | + }, |
| 26 | + showTooltip = true, |
| 27 | + } = props; |
| 28 | + // derived values |
| 29 | + const stackKeys = useMemo(() => bars.map((bar) => bar.key), [bars]); |
| 30 | + const stackDotClassNames = useMemo( |
| 31 | + () => bars.reduce((acc, bar) => ({ ...acc, [bar.key]: bar.dotClassName }), {}), |
| 32 | + [bars] |
| 33 | + ); |
| 34 | + |
| 35 | + const renderBars = useMemo( |
| 36 | + () => |
| 37 | + bars.map((bar) => ( |
| 38 | + <Bar |
| 39 | + key={bar.key} |
| 40 | + dataKey={bar.key} |
| 41 | + stackId={bar.stackId} |
| 42 | + fill={bar.fillClassName} |
| 43 | + shape={(shapeProps: any) => ( |
| 44 | + <CustomBar |
| 45 | + {...shapeProps} |
| 46 | + stackKeys={stackKeys} |
| 47 | + textClassName={bar.textClassName} |
| 48 | + showPercentage={bar.showPercentage} |
| 49 | + /> |
| 50 | + )} |
| 51 | + /> |
| 52 | + )), |
| 53 | + [stackKeys, bars] |
| 54 | + ); |
| 55 | + |
| 56 | + return ( |
| 57 | + <div className={className}> |
| 58 | + <ResponsiveContainer width="100%" height="100%"> |
| 59 | + <CoreBarChart |
| 60 | + data={data} |
| 61 | + margin={{ top: 10, right: 10, left: 10, bottom: 40 }} |
| 62 | + barSize={barSize} |
| 63 | + className="recharts-wrapper" |
| 64 | + > |
| 65 | + <XAxis |
| 66 | + dataKey={xAxis.key} |
| 67 | + tick={(props) => <CustomXAxisTick {...props} />} |
| 68 | + tickLine={{ |
| 69 | + stroke: "currentColor", |
| 70 | + className: AXIS_LINE_CLASSNAME, |
| 71 | + }} |
| 72 | + axisLine={{ |
| 73 | + stroke: "currentColor", |
| 74 | + className: AXIS_LINE_CLASSNAME, |
| 75 | + }} |
| 76 | + label={{ |
| 77 | + value: xAxis.label, |
| 78 | + dy: 28, |
| 79 | + className: LABEL_CLASSNAME, |
| 80 | + }} |
| 81 | + tickCount={tickCount.x} |
| 82 | + /> |
| 83 | + <YAxis |
| 84 | + domain={yAxis.domain} |
| 85 | + tickLine={{ |
| 86 | + stroke: "currentColor", |
| 87 | + className: AXIS_LINE_CLASSNAME, |
| 88 | + }} |
| 89 | + axisLine={{ |
| 90 | + stroke: "currentColor", |
| 91 | + className: AXIS_LINE_CLASSNAME, |
| 92 | + }} |
| 93 | + label={{ |
| 94 | + value: yAxis.label, |
| 95 | + angle: -90, |
| 96 | + position: "bottom", |
| 97 | + offset: -24, |
| 98 | + dx: -16, |
| 99 | + className: LABEL_CLASSNAME, |
| 100 | + }} |
| 101 | + tick={(props) => <CustomYAxisTick {...props} />} |
| 102 | + tickCount={tickCount.y} |
| 103 | + allowDecimals={!!yAxis.allowDecimals} |
| 104 | + /> |
| 105 | + {showTooltip && ( |
| 106 | + <Tooltip |
| 107 | + cursor={{ fill: "currentColor", className: "text-custom-background-90/80 cursor-pointer" }} |
| 108 | + content={({ active, label, payload }) => ( |
| 109 | + <CustomTooltip |
| 110 | + active={active} |
| 111 | + label={label} |
| 112 | + payload={payload} |
| 113 | + itemKeys={stackKeys} |
| 114 | + itemDotClassNames={stackDotClassNames} |
| 115 | + /> |
| 116 | + )} |
| 117 | + /> |
| 118 | + )} |
| 119 | + {renderBars} |
| 120 | + </CoreBarChart> |
| 121 | + </ResponsiveContainer> |
| 122 | + </div> |
| 123 | + ); |
| 124 | +}); |
| 125 | +BarChart.displayName = "BarChart"; |
0 commit comments