Word Cloud Chart
word-cloud-chartuiReact mirror of @uipkge/word-cloud-chart — see the Vue registry item for the canonical description.
Installation
$pnpm dlx shadcn@latest add https://uipkge.dev/r/react/word-cloud-chart.json$npx shadcn@latest add https://uipkge.dev/r/react/word-cloud-chart.json$yarn dlx shadcn@latest add https://uipkge.dev/r/react/word-cloud-chart.json$bunx shadcn@latest add https://uipkge.dev/r/react/word-cloud-chart.jsonNamed registry:
npx shadcn@latest add @uipkge-react/word-cloud-chartInstalls to:components/ui/charts/word-cloud-chart/Examples
Loading interactive previews…
Loading interactive previews…
Props
| Name | Type / Values | Default | Required |
|---|---|---|---|
data | WordDatum[] | — | required |
height | number | string | 280 | optional |
colorsOptional palette override. Defaults to chart-1..5 tokens. | 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.
WordDatuminterface WordDatum {
name: string
value: number
}npm dependencies
Used by
Files installed (3)
components/ui/charts/word-cloud-chart/WordCloudChart.tsx2.3 kB
'use client' import * as React from 'react' import { cn } from '@/lib/utils' import { ChartFrame } from '../shared' // WordCloudChart — dependency-free frequency-sized words // ───────────────────────────────────────────────────────────────────────── export interface WordDatum { name: string value: number } export interface WordCloudChartProps { data: WordDatum[] height?: number | string /** Optional palette override. Defaults to chart-1..5 tokens. */ 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 WordCloudChart = React.forwardRef<HTMLDivElement, WordCloudChartProps>( ({ data, height = 280, colors = DEFAULT_COLORS, className, ariaLabel }, ref) => { const words = React.useMemo(() => { if (!data.length) return [] const vals = data.map((d) => d.value) const min = Math.min(...vals) const max = Math.max(...vals) const span = Math.max(1, max - min) return [...data] .sort((a, b) => b.value - a.value) .map((d, i) => ({ ...d, size: 14 + ((d.value - min) / span) * 30, color: colors[i % colors.length], weight: d.value === max ? 700 : d.value >= min + span * 0.66 ? 600 : 500, opacity: 0.55 + ((d.value - min) / span) * 0.45, })) }, [data, colors]) return ( <ChartFrame ref={ref} height={height} className={cn('overflow-hidden', className)} ariaLabel={ariaLabel}> <div className="flex h-full w-full flex-wrap items-center justify-center gap-x-4 gap-y-1 p-4"> {words.map((w) => ( <span key={w.name} title={`${w.name}: ${w.value}`} style={{ fontSize: `${Math.round(w.size)}px`, color: w.color, fontWeight: w.weight, opacity: w.opacity, lineHeight: 1.15, }} className="cursor-default transition-transform duration-150 hover:scale-110" > {w.name} </span> ))} </div> </ChartFrame> ) }, ) WordCloudChart.displayName = 'WordCloudChart'components/ui/charts/word-cloud-chart/index.ts0.1 kB
export { WordCloudChart, type WordCloudChartProps, type WordDatum } from './WordCloudChart'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/word-cloud-chart.json