| 5 | import type {PlotFrame, Ticks} from '../common/types.ts'; |
| 6 | |
| 7 | export const YAxis = ({ |
| 8 | className, |
| 9 | tickFormatter, |
| 10 | yTicks, |
| 11 | yMin, |
| 12 | yMax, |
| 13 | yTitle, |
| 14 | plotFrame, |
| 15 | tickSize, |
| 16 | tickGap, |
| 17 | axisWidth, |
| 18 | }: { |
| 19 | readonly className?: string; |
| 20 | readonly tickFormatter?: (tick: number) => string; |
| 21 | readonly yTicks: Ticks; |
| 22 | readonly yMin: number | undefined; |
| 23 | readonly yMax: number | undefined; |
| 24 | readonly yTitle: string; |
| 25 | readonly plotFrame: PlotFrame; |
| 26 | readonly tickSize: number; |
| 27 | readonly tickGap: number; |
| 28 | readonly axisWidth: number; |
| 29 | }) => { |
| 30 | const [plotX, plotY, , plotHeight] = plotFrame; |
| 31 | return isNullish(yMin) || isNullish(yMax) ? null : ( |
| 32 | <g className={isNullish(className) ? 'y' : `y ${className}`}> |
| 33 | <path |
| 34 | className="line" |
| 35 | d={`M${plotX},${plotY}v${plotHeight}`} |
| 36 | fill="none" |
| 37 | stroke={CURRENT_COLOR} |
| 38 | strokeOpacity={0.5} |
| 39 | strokeWidth={1} |
| 40 | /> |
| 41 | <g className="ticks" dominantBaseline="middle" textAnchor="end"> |
| 42 | {arrayMap(yTicks, (tick) => { |
| 43 | const y = plotHeight - getScale(tick, yMin, yMax, plotHeight); |
| 44 | return ( |
| 45 | <text key={tick} x={plotX - tickSize - tickGap} y={plotY + y}> |
| 46 | {tickFormatter?.(tick) ?? string(tick)} |
| 47 | </text> |
| 48 | ); |
| 49 | })} |
| 50 | </g> |
| 51 | <text |
| 52 | className="title" |
| 53 | dominantBaseline="text-before-edge" |
| 54 | textAnchor="middle" |
| 55 | transform={`translate(${plotX - axisWidth} ${ |
| 56 | plotFrame[1] + plotFrame[3] / 2 |
| 57 | }) rotate(-90)`} |
| 58 | > |
| 59 | {yTitle} |
| 60 | </text> |
| 61 | </g> |
| 62 | ); |
| 63 | }; |