|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + ResponsiveContainer, |
| 4 | + BarChart, |
| 5 | + Bar, |
| 6 | + XAxis, |
| 7 | + YAxis, |
| 8 | + CartesianGrid, |
| 9 | + Tooltip, |
| 10 | + LabelList, |
| 11 | +} from 'recharts'; |
| 12 | +import { Card, CardBody } from 'reactstrap'; |
| 13 | +import styles from '../LBDashboard.module.css'; |
| 14 | + |
| 15 | +export function CompareBarGraph({ |
| 16 | + title, |
| 17 | + metricLabel, |
| 18 | + tooltipLabel, |
| 19 | + showMetricPill = false, |
| 20 | + orientation, |
| 21 | + data, |
| 22 | + nameKey, |
| 23 | + valueKey, |
| 24 | + xLabel, |
| 25 | + yLabel, |
| 26 | + barColor = '#3b82f6', |
| 27 | + valueFormatter = v => v, |
| 28 | + headerChips = [], |
| 29 | + xDomain, |
| 30 | + yDomain, |
| 31 | + xTicks, |
| 32 | + yTicks, |
| 33 | + barSize, |
| 34 | + height = 320, |
| 35 | + yCategoryWidth = 140, |
| 36 | + margins = { top: 8, right: 24, bottom: 36, left: 36 }, |
| 37 | + maxBars, |
| 38 | + showYAxisTitle = true, |
| 39 | + yTickFormatter, |
| 40 | +}) { |
| 41 | + const isHorizontal = orientation === 'horizontal'; |
| 42 | + |
| 43 | + return ( |
| 44 | + <Card className={styles.graphCard}> |
| 45 | + <CardBody> |
| 46 | + {/* Title row + chips */} |
| 47 | + <div className={styles.graphTitle} style={{ display: 'flex', alignItems: 'center' }}> |
| 48 | + <span style={{ flex: 1 }}>{title}</span> |
| 49 | + {showMetricPill && ( |
| 50 | + <span className={styles.metricPill} style={{ marginRight: 12 }}> |
| 51 | + {metricLabel} |
| 52 | + </span> |
| 53 | + )} |
| 54 | + |
| 55 | + {/* chips on the right */} |
| 56 | + <div style={{ display: 'flex', gap: 16 }}> |
| 57 | + {headerChips.map((c, i) => ( |
| 58 | + <div key={i} style={{ textAlign: 'center', lineHeight: 1.1 }}> |
| 59 | + <div style={{ fontSize: 12, fontWeight: 600 }}>{c.label}</div> |
| 60 | + <div style={{ fontSize: 11, color: '#777', letterSpacing: 0.2 }}> |
| 61 | + {String(c.value).toUpperCase()} |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + ))} |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + |
| 68 | + {/* chart */} |
| 69 | + <div className={styles.graphCanvas}> |
| 70 | + <ResponsiveContainer width="100%" height={height}> |
| 71 | + <BarChart |
| 72 | + data={data} |
| 73 | + layout={isHorizontal ? 'vertical' : 'horizontal'} |
| 74 | + margin={margins} |
| 75 | + > |
| 76 | + <CartesianGrid strokeDasharray="3 3" /> |
| 77 | + {isHorizontal ? ( |
| 78 | + <> |
| 79 | + <XAxis |
| 80 | + type="number" |
| 81 | + domain={xDomain || ['dataMin', 'dataMax']} |
| 82 | + ticks={xTicks} |
| 83 | + tickFormatter={valueFormatter} |
| 84 | + label={{ value: xLabel, position: 'insideBottom', offset: -4 }} |
| 85 | + /> |
| 86 | + <YAxis |
| 87 | + type="category" |
| 88 | + dataKey={nameKey} |
| 89 | + width={yCategoryWidth} |
| 90 | + label={ |
| 91 | + showYAxisTitle |
| 92 | + ? { |
| 93 | + value: yLabel, |
| 94 | + angle: -90, |
| 95 | + position: 'insideLeft', |
| 96 | + offset: 6, |
| 97 | + style: { fontSize: 12, fill: '#8c8c8c', fontWeight: 600 }, |
| 98 | + } |
| 99 | + : undefined |
| 100 | + } |
| 101 | + /> |
| 102 | + </> |
| 103 | + ) : ( |
| 104 | + <> |
| 105 | + <XAxis |
| 106 | + dataKey={nameKey} |
| 107 | + interval={0} |
| 108 | + label={{ value: xLabel, position: 'insideBottom', offset: -4 }} |
| 109 | + /> |
| 110 | + <YAxis |
| 111 | + domain={yDomain || ['dataMin', 'dataMax']} |
| 112 | + ticks={yTicks} |
| 113 | + tickFormatter={valueFormatter} |
| 114 | + label={{ value: yLabel, angle: -90, position: 'insideLeft', offset: 10 }} |
| 115 | + /> |
| 116 | + </> |
| 117 | + )} |
| 118 | + |
| 119 | + <Tooltip |
| 120 | + formatter={v => [valueFormatter(v), tooltipLabel || metricLabel || title]} |
| 121 | + labelFormatter={lbl => `${lbl}`} |
| 122 | + /> |
| 123 | + <Bar dataKey={valueKey} radius={[4, 4, 4, 4]} fill={barColor} barSize={barSize}> |
| 124 | + <LabelList |
| 125 | + dataKey={valueKey} |
| 126 | + position={isHorizontal ? 'right' : 'top'} |
| 127 | + formatter={valueFormatter} |
| 128 | + /> |
| 129 | + </Bar> |
| 130 | + </BarChart> |
| 131 | + </ResponsiveContainer> |
| 132 | + </div> |
| 133 | + </CardBody> |
| 134 | + </Card> |
| 135 | + ); |
| 136 | +} |
0 commit comments