Y Axis
bklit-ui/y-axisFreeComponent
Y-axis component for value labels in line and area charts
Install it
npx shadcn@latest add https://bklit.com/r/y-axis.jsonSource
1"use client";23import { memo, useEffect, useMemo, useState } from "react";4import { createPortal } from "react-dom";5import { useChartStable, useYScale } from "./chart-context";6import { DEFAULT_Y_DOMAIN_TWEEN_MS } from "./chart-phase";7import { LINE_LOADING_PULSE_EASE } from "./line-loading-timing";8import { resolveReferenceDataRange } from "./reference-area-geometry";9import type { YAxisOrientation } from "./y-axis-scales";10import { normalizeYAxisId } from "./y-axis-scales";11import {12 resolveYAxisTickCount,13 Y_AXIS_DEFAULT_TICK_COUNT,14} from "./y-axis-ticks";1516const Y_AXIS_POSITION_TWEEN_MS = DEFAULT_Y_DOMAIN_TWEEN_MS;1718export interface YAxisProps {19 /** Scale group id (Recharts `yAxisId`). Default: `"left"`. */20 yAxisId?: string | number;21 /** Which side of the chart to render labels. Default: `"left"`. */22 orientation?: YAxisOrientation;23 /**24 * Approximate tick count hint for `scale.ticks()` (d3). Actual label count may differ.25 * Clamped to {@link Y_AXIS_MIN_TICK_COUNT}–{@link Y_AXIS_MAX_TICK_COUNT}. Default: 5.26 */27 numTicks?: number;28 /** Format large numbers (e.g. 1000 as "1k"). Default: true */29 formatLargeNumbers?: boolean;30 /** Custom formatter for tick labels (e.g. USD). Overrides formatLargeNumbers when set. */31 formatValue?: (value: number) => string;32}3334function formatLabel(35 value: number,36 formatLargeNumbers: boolean,37 formatValue?: (value: number) => string38): string {39 if (formatValue) {40 return formatValue(value);41 }42 if (formatLargeNumbers && value >= 1000) {43 return `${(value / 1000).toFixed(0)}k`;44 }45 return String(value);46}4748function resolveTickLabelColor(49 tickY: number,50 axisId: string,51 yScale: ReturnType<typeof useYScale>,52 referenceAreas: ReturnType<typeof useChartStable>["referenceAreas"]53): string | undefined {54 for (const area of referenceAreas) {55 if (!area.axisLabelColor) {56 continue;57 }58 if (normalizeYAxisId(area.yAxisId) !== axisId) {59 continue;60 }61 const [low, high] = resolveReferenceDataRange(62 area.y1,63 area.y2,64 yScale.domain() as [number, number]65 );66 const topPixel = yScale(high) ?? 0;67 const bottomPixel = yScale(low) ?? 0;68 const bandTop = Math.min(topPixel, bottomPixel);69 const bandBottom = Math.max(topPixel, bottomPixel);70 if (tickY >= bandTop && tickY <= bandBottom) {71 return area.axisLabelColor;72 }73 }74 return undefined;75}7677export function YAxis(props: YAxisProps) {78// … truncated
What it pulls in
Other registry items
Files it writes
More from bklit-ui
All 56 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Area Chartarea-chart | bklit-ui | Components | Free | 4 deps · 14 files | |
| Chart Backgroundbackground | bklit-ui | Components | Free | 2 deps · 3 files | |
| Bar Chartbar-chart | bklit-ui | Components | Free | 4 deps · 13 files | |
| Bar Depthbar-depth | bklit-ui | Components | Free | 1 dep | |
| Candlestick Chartcandlestick-chart | bklit-ui | Components | Free | 4 deps · 4 files | |
| Chart Animationchart-animation | bklit-ui | Components | Free | 1 dep · 7 files | |
| Chart Contextchart-context | bklit-ui | Components | Free | 5 deps · 12 files | |
| Chart Series Layerchart-series | bklit-ui | Components | Free | 3 deps · 15 files |
