Category Distribution Chart
category-distribution-chartuiReact mirror of @uipkge/category-distribution-chart — see the Vue registry item for the canonical description.
Installation
$pnpm dlx shadcn@latest add https://uipkge.dev/r/react/category-distribution-chart.json$npx shadcn@latest add https://uipkge.dev/r/react/category-distribution-chart.json$yarn dlx shadcn@latest add https://uipkge.dev/r/react/category-distribution-chart.json$bunx shadcn@latest add https://uipkge.dev/r/react/category-distribution-chart.jsonNamed registry:
npx shadcn@latest add @uipkge-react/category-distribution-chartInstalls to:components/ui/charts/category-distribution-chart/Examples
Loading interactive previews…
Loading interactive previews…
Props
| Name | Type / Values | Default | Required |
|---|---|---|---|
primaryValue | string | number | — | required |
primaryLabel | string | — | optional |
trend | { value: string; direction: 'up' | 'down' } | — | optional |
categories | DistributionSlice[] | — | required |
height | number | string | — | optional |
showLegend | boolean | — | optional |
colors | string[] | — | optional |
className | string | — | optional |
ariaLabelAccessible name announced for the chart image. Defaults to "Chart". | string | — | optional |
Schema
Type aliases from this item's source — use them to shape the data you pass in.
DistributionSliceinterface DistributionSlice {
label: string
/** Share of the whole; auto-normalised when the sum is not 100. */
percentage: number
value?: string | number
color?: string
}npm dependencies
Used by
Files installed (3)
components/ui/charts/category-distribution-chart/CategoryDistributionChart.tsx3.3 kB
'use client' import * as React from 'react' import { cn } from '@/lib/utils' import { ChartFrame } from '../shared' // CategoryDistributionChart — dependency-free KPI + share bar // ───────────────────────────────────────────────────────────────────────── export interface DistributionSlice { label: string /** Share of the whole; auto-normalised when the sum is not 100. */ percentage: number value?: string | number color?: string } export interface CategoryDistributionChartProps { primaryValue: string | number primaryLabel?: string trend?: { value: string; direction: 'up' | 'down' } categories: DistributionSlice[] height?: number | string showLegend?: boolean colors?: string[] className?: string /** Accessible name announced for the chart image. Defaults to "Chart". */ ariaLabel?: string } const DEFAULT_COLORS = ['var(--chart-1)', 'var(--chart-2)', 'var(--chart-3)', 'var(--chart-4)', 'var(--chart-5)'] export const CategoryDistributionChart = React.forwardRef<HTMLDivElement, CategoryDistributionChartProps>( ( { primaryValue, primaryLabel, trend, categories, height = 220, showLegend = true, colors = DEFAULT_COLORS, className, ariaLabel, }, ref, ) => { const total = categories.reduce((s, c) => s + c.percentage, 0) || 1 const slices = categories.map((c, i) => ({ ...c, share: (c.percentage / total) * 100, color: c.color ?? colors[i % colors.length], })) return ( <ChartFrame ref={ref} height={height} focusable={false} className={cn('flex flex-col justify-center', className)}> <div className="flex items-baseline gap-2"> <span className="text-foreground text-3xl font-bold tabular-nums">{primaryValue}</span> {trend && ( <span className="text-xs font-semibold tabular-nums" style={{ color: trend.direction === 'up' ? 'var(--chart-2)' : 'var(--destructive)' }} > {trend.direction === 'up' ? '+' : '−'} {trend.value} </span> )} {primaryLabel && <span className="text-muted-foreground text-xs">{primaryLabel}</span>} </div> <div className="mt-3 flex h-3 w-full overflow-hidden rounded-full" role="presentation"> {slices.map((s) => ( <div key={s.label} className="h-full" style={{ width: `${s.share}%`, background: s.color }} title={`${s.label} — ${Math.round(s.share)}%`} /> ))} </div> {showLegend && ( <ul className="mt-3 grid grid-cols-2 gap-x-4 gap-y-1.5 text-xs"> {slices.map((s) => ( <li key={s.label} className="flex min-w-0 items-center gap-2"> <span className="size-2.5 shrink-0 rounded-[3px]" style={{ background: s.color }} /> <span className="text-foreground truncate font-medium">{s.label}</span> <span className="text-muted-foreground ml-auto shrink-0 tabular-nums"> {s.value ?? `${Math.round(s.share)}%`} </span> </li> ))} </ul> )} </ChartFrame> ) }, ) CategoryDistributionChart.displayName = 'CategoryDistributionChart'components/ui/charts/category-distribution-chart/index.ts0.1 kB
export { CategoryDistributionChart, type CategoryDistributionChartProps, type DistributionSlice, } from './CategoryDistributionChart'components/ui/charts/shared.tsx3.7 kB
'use client' import * as React from 'react' import * as echartsCore from 'echarts/core' import { use } from 'echarts/core' import { CanvasRenderer } from 'echarts/renderers' import { LineChart as EChartsLineChart, BarChart as EChartsBarChart, PieChart as EChartsPieChart, ScatterChart as EChartsScatterChart, EffectScatterChart as EChartsEffectScatter, PictorialBarChart as EChartsPictorialBar, RadarChart as EChartsRadarChart, GaugeChart as EChartsGaugeChart, HeatmapChart as EChartsHeatmap, TreemapChart as EChartsTreemapChart, FunnelChart as EChartsFunnelChart, BoxplotChart as EChartsBoxplotChart, CandlestickChart as EChartsCandlestickChart, CustomChart as EChartsCustomChart, ChordChart as EChartsChordChart, MapChart as EChartsMapChart, GraphChart as EChartsGraphChart, ParallelChart as EChartsParallelChart, SankeyChart as EChartsSankeyChart, SunburstChart as EChartsSunburstChart, ThemeRiverChart, TreeChart as EChartsTreeChart, } from 'echarts/charts' import { GridComponent, PolarComponent, TooltipComponent, LegendComponent, MarkLineComponent, MarkAreaComponent, MarkPointComponent, GraphicComponent, RadarComponent, VisualMapComponent, CalendarComponent, DataZoomComponent, ParallelComponent, SingleAxisComponent, TitleComponent, } from 'echarts/components' import ReactECharts from 'echarts-for-react/esm/core' import { cn } from '@/lib/utils' export function heightToStyle(height: number | string): string { return /^\d+$/.test(String(height)) ? `${height}px` : String(height) } interface ChartFrameProps { height: number | string className?: string /** Apply the accessible role/tabindex/focus-ring chrome. Some chart * wrappers ship a bare `w-full` frame -- pass `false` for those. */ focusable?: boolean /** Accessible name for role="img". Defaults to "Chart". */ ariaLabel?: string } /** Shared `<div>` wrapper around the ECharts canvas. */ export const ChartFrame = React.forwardRef<HTMLDivElement, ChartFrameProps & { children: React.ReactNode }>( ({ height, className, focusable = true, ariaLabel, children }, ref) => ( <div ref={ref} role="img" aria-label={ariaLabel || 'Chart'} tabIndex={focusable ? 0 : undefined} style={{ height: heightToStyle(height) }} className={ focusable ? cn('focus-visible:ring-ring w-full focus-visible:ring-2 focus-visible:outline-none', className) : cn('w-full', className) } > {children} </div> ), ) ChartFrame.displayName = 'ChartFrame' /** ECharts canvas filling its parent frame. */ export function EChart({ option }: { option: any }) { return ( <ReactECharts echarts={echartsCore as any} option={option} notMerge lazyUpdate style={{ width: '100%', height: '100%' }} /> ) } export const echartsCoreModule = echartsCore // Register every chart type the wrappers need once at module load. use([ CanvasRenderer, EChartsLineChart, EChartsBarChart, EChartsPieChart, EChartsScatterChart, EChartsEffectScatter, EChartsPictorialBar, EChartsRadarChart, EChartsGaugeChart, EChartsHeatmap, EChartsTreemapChart, EChartsFunnelChart, EChartsBoxplotChart, EChartsCandlestickChart, EChartsCustomChart, EChartsChordChart, EChartsMapChart, EChartsGraphChart, EChartsParallelChart, EChartsSankeyChart, EChartsSunburstChart, ThemeRiverChart, EChartsTreeChart, GridComponent, PolarComponent, TooltipComponent, LegendComponent, MarkLineComponent, MarkAreaComponent, MarkPointComponent, GraphicComponent, RadarComponent, VisualMapComponent, CalendarComponent, DataZoomComponent, ParallelComponent, SingleAxisComponent, TitleComponent, ])
Raw manifest:https://uipkge.dev/r/react/category-distribution-chart.json