From 9af928672433dcabe2645658e4d3113ac17e593d Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Tue, 7 Jul 2026 14:59:36 +0900 Subject: [PATCH] =?UTF-8?q?260707=5F01=5FENH:=20Chart=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 139 ++++++++++++++++- package-lock.json | 92 +++++++++-- package.json | 4 +- registry/components/chart/BaseEChart.tsx | 147 ++++++++++++++++++ registry/components/chart/KriBarChart.tsx | 59 +++++++ registry/components/chart/KriLineChart.tsx | 58 +++++++ registry/components/chart/KriPieChart.tsx | 50 ++++++ registry/components/chart/KriRadarChart.tsx | 55 +++++++ registry/components/chart/KriScatterChart.tsx | 55 +++++++ .../components/chart/KriStackedBarChart.tsx | 62 ++++++++ registry/components/chart/chart.config.ts | 6 + registry/components/chart/chart.types.ts | 71 +++++++++ registry/components/chart/chart.utils.ts | 34 ++++ registry/components/chart/index.ts | 29 ++++ registry/components/chart/useResizeKey.ts | 31 ++++ registry/components/registry.json | 72 +++++++++ 16 files changed, 941 insertions(+), 23 deletions(-) create mode 100644 registry/components/chart/BaseEChart.tsx create mode 100644 registry/components/chart/KriBarChart.tsx create mode 100644 registry/components/chart/KriLineChart.tsx create mode 100644 registry/components/chart/KriPieChart.tsx create mode 100644 registry/components/chart/KriRadarChart.tsx create mode 100644 registry/components/chart/KriScatterChart.tsx create mode 100644 registry/components/chart/KriStackedBarChart.tsx create mode 100644 registry/components/chart/chart.config.ts create mode 100644 registry/components/chart/chart.types.ts create mode 100644 registry/components/chart/chart.utils.ts create mode 100644 registry/components/chart/index.ts create mode 100644 registry/components/chart/useResizeKey.ts diff --git a/README.md b/README.md index 4d7805f..5d9b4ba 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # KRI UI Registry -사내 shadcn UI Registry 소스 프로젝트입니다. `dist/r`의 정적 JSON을 IIS 또는 Nginx에 배포합니다. +KRI 공통 shadcn UI Registry 프로젝트입니다. `npm run build`로 생성되는 `dist/r` 정적 JSON 파일을 IIS 또는 Nginx에 배포해서 사용합니다. ## 빌드 및 검증 @@ -11,13 +11,13 @@ npm run verify npm run preview ``` -배포된 Registry 주소: +배포 Registry 주소: ```text https://ui-registry.kric.com/r/registry.json ``` -로컬 프리뷰 주소 (`npm run preview` 실행 시): +로컬 preview 주소 (`npm run preview` 실행 후): ```text http://localhost:4100/r/registry.json @@ -26,6 +26,7 @@ http://localhost:4100/r/registry.json ## Registry 항목 ```text +kri-chart kri-select kri-date-range-picker kri-date-picker @@ -40,9 +41,11 @@ kri-layout kri-dialog ``` -전체 설치 예시: +개별 설치 예시: ```powershell +npx shadcn@latest add https://ui-registry.kric.com/r/kri-chart.json +npx shadcn@latest add https://ui-registry.kric.com/r/kri-select.json npx shadcn@latest add https://ui-registry.kric.com/r/kri-password.json npx shadcn@latest add https://ui-registry.kric.com/r/kri-date-range-picker.json npx shadcn@latest add https://ui-registry.kric.com/r/kri-date-picker.json @@ -56,9 +59,133 @@ npx shadcn@latest add https://ui-registry.kric.com/r/kri-layout.json npx shadcn@latest add https://ui-registry.kric.com/r/kri-dialog.json ``` +## KRI Chart + +`kri-chart`는 `echarts`, `echarts-for-react` 기반 공통 차트 컴포넌트입니다. 내부적으로 `BaseEChart`를 사용하며, 모든 wrapper는 멀티 시리즈를 지원합니다. + +제공 컴포넌트: + +```ts +BaseEChart +KriLineChart +KriBarChart +KriStackedBarChart +KriPieChart +KriRadarChart +KriScatterChart +``` + +### Line + +```tsx +import { KriLineChart } from '@/components/chart' + + +``` + +### Bar / Horizontal Bar + +```tsx +import { KriBarChart } from '@/components/chart' + + +``` + +### Stacked Bar + +```tsx +import { KriStackedBarChart } from '@/components/chart' + + +``` + +### Pie + +```tsx +import { KriPieChart } from '@/components/chart' + + +``` + +### Radar + +```tsx +import { KriRadarChart } from '@/components/chart' + + +``` + +### Scatter + +```tsx +import { KriScatterChart } from '@/components/chart' + + +``` + +공통 props: + +- `height`: 차트 wrapper 높이, 기본값 `CHART_HEIGHTS.DEFAULT` +- `className`, `contentClassName`, `chartClassName`: wrapper className +- `notMerge`, `lazyUpdate`, `onEvents`: `echarts-for-react` 전달 옵션 +- `option`: wrapper가 생성한 ECharts option을 덮어쓰는 override 옵션 +- `normalizeValues`, `decimalPlaces`: 숫자 데이터 반올림 정규화 옵션 + ## KriDialog Footer 규칙 -기본값은 `닫기` 버튼이 있는 Footer입니다. +기본값은 닫기 버튼이 있는 Footer입니다. ```tsx @@ -115,5 +242,5 @@ npx shadcn@latest add https://ui-registry.kric.com/r/kri-dialog.json - 편집 값은 HTML 문자열입니다. - 이미지 업로드 함수는 URL 문자열 또는 `{ url }`을 반환할 수 있습니다. -- `toolbar={false}`로 툴바 전체를 감출 수 있습니다. +- `toolbar={false}`로 toolbar 전체를 숨길 수 있습니다. - SMS/LMS 기본 제한은 각각 90/2,000바이트입니다. diff --git a/package-lock.json b/package-lock.json index 7ce8265..ac03330 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "kri-ui-registry", - "version": "0.2.0", + "version": "0.4.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "kri-ui-registry", - "version": "0.2.0", + "version": "0.4.1", "devDependencies": { "@tanstack/react-table": "^8.21.3", "@tiptap/extension-color": "^3.27.1", @@ -27,6 +27,8 @@ "@types/react-dom": "^19.2.3", "clsx": "^2.1.1", "dompurify": "^3.4.11", + "echarts": "^6.1.0", + "echarts-for-react": "^3.0.6", "lucide-react": "^1.23.0", "rimraf": "^6.0.0", "serve": "^14.2.4", @@ -67,6 +69,7 @@ "integrity": "sha512-RgHBCvtjbOK2gXSNBNIkNoEc9qoVEtau3hj8gEqKQuL3HZAibKarWFEI3Lfm6EYKkLalOh8eSrj9b+ch9H/VBA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@babel/code-frame": "^7.29.7", "@babel/generator": "^7.29.7", @@ -642,18 +645,6 @@ "@floating-ui/utils": "^0.2.11" } }, - "node_modules/@floating-ui/dom": { - "version": "1.7.6", - "resolved": "https://registry.npmjs.org/@floating-ui/dom/-/dom-1.7.6.tgz", - "integrity": "sha512-9gZSAI5XM36880PPMm//9dfiEngYoC6Am2izES1FF406YFsjvyBMmeJ2g4SAju3xWwtuynNRFL2s9hgxpLI5SQ==", - "dev": true, - "license": "MIT", - "optional": true, - "dependencies": { - "@floating-ui/core": "^1.7.5", - "@floating-ui/utils": "^0.2.11" - } - }, "node_modules/@floating-ui/utils": { "version": "0.2.11", "resolved": "https://registry.npmjs.org/@floating-ui/utils/-/utils-0.2.11.tgz", @@ -865,6 +856,7 @@ "integrity": "sha512-rV6Qn4wmC6BxfF+4mu6bqGWj9vA4oXXhsrpXaJL2uhjxeHAGofjwcHof2X84VYzeyXgdlsGmqKie4TAppVXZUQ==", "dev": true, "license": "MIT", + "peer": true, "funding": { "type": "github", "url": "https://github.com/sponsors/ueberdosis" @@ -1145,6 +1137,7 @@ "integrity": "sha512-c2Upru7lj0/ZV/Ibww6cNz6sUS8m6Dp/9uygFhYcZOd3X8M0xBIEk42c6m6SQehkPziVA8QOgNJz7sMqsbz1OQ==", "dev": true, "license": "MIT", + "peer": true, "funding": { "type": "github", "url": "https://github.com/sponsors/ueberdosis" @@ -1244,6 +1237,7 @@ "integrity": "sha512-tNB8kjxo0+XPremnWkd6NUikpeJ+JQrss7HntL8GgIxX4t0gkdnLn9yUWb0JzcaYP7Y8iTZZHdkPs0P154DG8Q==", "dev": true, "license": "MIT", + "peer": true, "funding": { "type": "github", "url": "https://github.com/sponsors/ueberdosis" @@ -1329,6 +1323,7 @@ "integrity": "sha512-J48WIl+6YDYTFPhWXUBQk+u7+AKVUqTdvrZOQyPYCGuQMgHrYzgWrI5+HeEifUgXJ5rMIWWP3qytp7KhVVqpDQ==", "dev": true, "license": "MIT", + "peer": true, "funding": { "type": "github", "url": "https://github.com/sponsors/ueberdosis" @@ -1357,6 +1352,7 @@ "integrity": "sha512-1Tdx9faw8k0/83V6X+xCDVhV8yElGt95JxeW3YMkKQJI56QdlPz0xOdJPlMiSGJKinPyVier+x9LJD/YZUZIaw==", "dev": true, "license": "MIT", + "peer": true, "funding": { "type": "github", "url": "https://github.com/sponsors/ueberdosis" @@ -1372,6 +1368,7 @@ "integrity": "sha512-Ffjx+vimmBU7zH/KrpXzJid3+pziCe/VL2aexSTP63cyQwKQ65LkFkCKaIsSpFdQQuakVZBGWjCA5RoBV852pw==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "prosemirror-changeset": "^2.3.0", "prosemirror-commands": "^1.6.2", @@ -1475,6 +1472,7 @@ "integrity": "sha512-MXfmqaVPEVgkBT/aY0aGCkRWWtByiYQXo3xdQ8r5RzuFrPiRn8Gar2tQdXSUQ2GKV3bkXckek89V8wQBY2Q/Aw==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "csstype": "^3.2.2" } @@ -1485,6 +1483,7 @@ "integrity": "sha512-jp2L/eY6fn+KgVVQAOqYItbF0VY/YApe5Mz2F0aykSO8gx31bYCZyvSeYxCHKvzHG5eZjc+zyaS5BrBWya2+kQ==", "dev": true, "license": "MIT", + "peer": true, "peerDependencies": { "@types/react": "^19.2.0" } @@ -1873,6 +1872,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "baseline-browser-mapping": "^2.10.38", "caniuse-lite": "^1.0.30001799", @@ -2581,6 +2581,39 @@ "dev": true, "license": "MIT" }, + "node_modules/echarts": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/echarts/-/echarts-6.1.0.tgz", + "integrity": "sha512-q0yaFPggC9FUdsWH4blavRWFmxdrIodbkoKNAjJudAI6CA9gNPxHtV2RcZNEepZVlk4yvBYkOkbk6HIVpIyHZA==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "tslib": "2.3.0", + "zrender": "6.1.0" + } + }, + "node_modules/echarts-for-react": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/echarts-for-react/-/echarts-for-react-3.0.6.tgz", + "integrity": "sha512-4zqLgTGWS3JvkQDXjzkR1k1CHRdpd6by0988TWMJgnvDytegWLbeP/VNZmMa+0VJx2eD7Y632bi2JquXDgiGJg==", + "dev": true, + "license": "MIT", + "dependencies": { + "fast-deep-equal": "^3.1.3", + "size-sensor": "^1.0.1" + }, + "peerDependencies": { + "echarts": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0", + "react": "^15.0.0 || >=16.0.0" + } + }, + "node_modules/echarts/node_modules/tslib": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz", + "integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==", + "dev": true, + "license": "0BSD" + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -3301,6 +3334,7 @@ "integrity": "sha512-1yrb/+w6HWQJrUCLkJ2IF5jNIPvvFkblV5RNOYl6bV+OA6p9GLcMpHFFGTosSvHvcAUibuUukRqhlYI4z32C7Q==", "dev": true, "license": "MIT", + "peer": true, "engines": { "node": ">=16.9.0" } @@ -4445,6 +4479,7 @@ "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", "dev": true, "license": "MIT", + "peer": true, "engines": { "node": ">=12" }, @@ -5110,8 +5145,7 @@ "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.27.0.tgz", "integrity": "sha512-eNv+WrVbKu1f3vbYJT/xtiF5syA5HPIMtf9IgY/nKg0sWqzAUEvqY/xm7OcZc/qafLx/iO9FgOmeSAp4v5ti/Q==", "dev": true, - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/semver": { "version": "6.3.1", @@ -5599,6 +5633,13 @@ "dev": true, "license": "MIT" }, + "node_modules/size-sensor": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/size-sensor/-/size-sensor-1.0.3.tgz", + "integrity": "sha512-+k9mJ2/rQMiRmQUcjn+qznch260leIXY8r4FyYKKyRBO/s5UoeMAHGkCJyE1R/4wrIhTJONfyloY55SkE7ve3A==", + "dev": true, + "license": "ISC" + }, "node_modules/sonner": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/sonner/-/sonner-2.0.7.tgz", @@ -5957,6 +5998,7 @@ "integrity": "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw==", "dev": true, "license": "Apache-2.0", + "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -6255,6 +6297,7 @@ "integrity": "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==", "dev": true, "license": "MIT", + "peer": true, "funding": { "url": "https://github.com/sponsors/colinhacks" } @@ -6268,6 +6311,23 @@ "peerDependencies": { "zod": "^3.25.28 || ^4" } + }, + "node_modules/zrender": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/zrender/-/zrender-6.1.0.tgz", + "integrity": "sha512-oEGMDB6pOP2S6OwRR4PdVv610zrjnA3Bh+JnSG12fYJlBKjtNAoEb5fSUoCOOINlH96I2fU38/A2UpRKs67xYQ==", + "dev": true, + "license": "BSD-3-Clause", + "dependencies": { + "tslib": "2.3.0" + } + }, + "node_modules/zrender/node_modules/tslib": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz", + "integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==", + "dev": true, + "license": "0BSD" } } } diff --git a/package.json b/package.json index 091bc68..ff24b44 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kri-ui-registry", - "version": "0.4.1", + "version": "0.5.0", "private": true, "description": "KRI internal shadcn UI registry", "scripts": { @@ -29,6 +29,8 @@ "@types/react-dom": "^19.2.3", "clsx": "^2.1.1", "dompurify": "^3.4.11", + "echarts": "^6.1.0", + "echarts-for-react": "^3.0.6", "lucide-react": "^1.23.0", "rimraf": "^6.0.0", "serve": "^14.2.4", diff --git a/registry/components/chart/BaseEChart.tsx b/registry/components/chart/BaseEChart.tsx new file mode 100644 index 0000000..66a4fb5 --- /dev/null +++ b/registry/components/chart/BaseEChart.tsx @@ -0,0 +1,147 @@ +'use client' + +import { useEffect, useMemo, useState } from 'react' +import type { ComponentType, CSSProperties } from 'react' +import type { EChartsOption } from 'echarts' + +import { CHART_HEIGHTS } from './chart.config' +import { useResizeKey } from './useResizeKey' + +type EChartsSeries = NonNullable +type ReactEChartsProps = { + option: EChartsOption + className?: string + style?: CSSProperties + notMerge?: boolean + lazyUpdate?: boolean + onEvents?: Record void> +} + +export type BaseEChartProps = { + option: EChartsOption + height?: number | string + className?: string + contentClassName?: string + chartClassName?: string + style?: CSSProperties + contentStyle?: CSSProperties + chartStyle?: CSSProperties + notMerge?: boolean + lazyUpdate?: boolean + onEvents?: Record void> + resizeDebounceMs?: number + normalizeValues?: boolean + decimalPlaces?: number +} + +const roundNumber = (value: number, decimalPlaces: number) => { + const factor = 10 ** decimalPlaces + return Math.round((value + Number.EPSILON) * factor) / factor +} + +const normalizeDataValue = (value: unknown, decimalPlaces: number): unknown => { + if (typeof value === 'number') { + return roundNumber(value, decimalPlaces) + } + + if (Array.isArray(value)) { + return value.map((item) => normalizeDataValue(item, decimalPlaces)) + } + + if (value && typeof value === 'object' && 'value' in value) { + const dataItem = value as Record + + return { + ...dataItem, + value: normalizeDataValue(dataItem.value, decimalPlaces), + } + } + + return value +} + +const normalizeSeries = ( + series: EChartsOption['series'] | undefined, + decimalPlaces: number, +): EChartsOption['series'] => { + if (!series) return series + + const normalizeOne = (seriesOption: EChartsSeries): EChartsSeries => { + const candidate = seriesOption as Record + const data = candidate.data + + if (!Array.isArray(data)) { + return seriesOption + } + + return { + ...candidate, + data: data.map((value) => normalizeDataValue(value, decimalPlaces)), + } as EChartsSeries + } + + return (Array.isArray(series) ? series.map((item) => normalizeOne(item as EChartsSeries)) : normalizeOne(series)) as EChartsOption['series'] +} + +export function BaseEChart({ + option, + height = CHART_HEIGHTS.DEFAULT, + className, + contentClassName, + chartClassName, + style, + contentStyle, + chartStyle, + notMerge = true, + lazyUpdate = false, + onEvents, + resizeDebounceMs = 150, + normalizeValues = true, + decimalPlaces = 2, +}: BaseEChartProps) { + const resizeKey = useResizeKey(resizeDebounceMs) + const [ReactECharts, setReactECharts] = useState | null>(null) + + useEffect(() => { + let cancelled = false + + void import('echarts-for-react').then((module) => { + if (!cancelled) { + setReactECharts(() => module.default as ComponentType) + } + }) + + return () => { + cancelled = true + } + }, []) + + const normalizedOption = useMemo(() => { + if (!normalizeValues) return option + + return { + ...option, + series: normalizeSeries(option.series, decimalPlaces), + } + }, [decimalPlaces, normalizeValues, option]) + + return ( +
+
+ {ReactECharts ? ( + + ) : null} +
+
+ ) +} + +export default BaseEChart diff --git a/registry/components/chart/KriBarChart.tsx b/registry/components/chart/KriBarChart.tsx new file mode 100644 index 0000000..73d7f39 --- /dev/null +++ b/registry/components/chart/KriBarChart.tsx @@ -0,0 +1,59 @@ +'use client' + +import { useMemo } from 'react' +import type { EChartsOption } from 'echarts' + +import { BaseEChart } from './BaseEChart' +import type { KriCartesianChartProps } from './chart.types' +import { defaultGrid, defaultLegend, mergeChartOptions, mergeObjectOption } from './chart.utils' + +export type KriBarChartProps = KriCartesianChartProps & { + isHorizontal?: boolean + barWidth?: number | string + barMaxWidth?: number | string +} + +export function KriBarChart({ + categories, + series, + isHorizontal = false, + barWidth, + barMaxWidth, + title, + legend, + tooltip, + grid, + xAxis, + yAxis, + option, + ...baseChartProps +}: KriBarChartProps) { + const chartOption = useMemo(() => { + const categoryAxis = { type: 'category', data: categories } + const valueAxis = { type: 'value' } + const generated: EChartsOption = { + title, + tooltip: tooltip ?? { trigger: 'axis', axisPointer: { type: 'shadow' } }, + legend: legend ?? defaultLegend(series.length), + grid: grid ?? defaultGrid(series.length), + xAxis: mergeObjectOption(isHorizontal ? valueAxis : categoryAxis, xAxis) as EChartsOption['xAxis'], + yAxis: mergeObjectOption(isHorizontal ? categoryAxis : valueAxis, yAxis) as EChartsOption['yAxis'], + series: series.map((item) => ({ + type: 'bar', + name: item.name, + data: item.data, + ...(barWidth ? { barWidth } : {}), + ...(barMaxWidth ? { barMaxWidth } : {}), + ...(item.color ? { color: item.color } : {}), + ...item.option, + })) as EChartsOption['series'], + } + + return mergeChartOptions(generated, option) + }, [barMaxWidth, barWidth, categories, grid, isHorizontal, legend, option, series, title, tooltip, xAxis, yAxis]) + + return +} + +export default KriBarChart + diff --git a/registry/components/chart/KriLineChart.tsx b/registry/components/chart/KriLineChart.tsx new file mode 100644 index 0000000..e7bc68b --- /dev/null +++ b/registry/components/chart/KriLineChart.tsx @@ -0,0 +1,58 @@ +'use client' + +import { useMemo } from 'react' +import type { EChartsOption } from 'echarts' + +import { BaseEChart } from './BaseEChart' +import type { KriCartesianChartProps } from './chart.types' +import { defaultGrid, defaultLegend, mergeChartOptions, mergeObjectOption } from './chart.utils' + +export type KriLineChartProps = KriCartesianChartProps & { + smooth?: boolean + area?: boolean + showSymbol?: boolean +} + +export function KriLineChart({ + categories, + series, + smooth = false, + area = false, + showSymbol = true, + title, + legend, + tooltip, + grid, + xAxis, + yAxis, + option, + ...baseChartProps +}: KriLineChartProps) { + const chartOption = useMemo(() => { + const generated: EChartsOption = { + title, + tooltip: tooltip ?? { trigger: 'axis' }, + legend: legend ?? defaultLegend(series.length), + grid: grid ?? defaultGrid(series.length), + xAxis: mergeObjectOption({ type: 'category', data: categories, boundaryGap: false }, xAxis) as EChartsOption['xAxis'], + yAxis: mergeObjectOption({ type: 'value' }, yAxis) as EChartsOption['yAxis'], + series: series.map((item) => ({ + type: 'line', + name: item.name, + data: item.data, + smooth, + showSymbol, + ...(area ? { areaStyle: {} } : {}), + ...(item.color ? { color: item.color } : {}), + ...item.option, + })) as EChartsOption['series'], + } + + return mergeChartOptions(generated, option) + }, [area, categories, grid, legend, option, series, showSymbol, smooth, title, tooltip, xAxis, yAxis]) + + return +} + +export default KriLineChart + diff --git a/registry/components/chart/KriPieChart.tsx b/registry/components/chart/KriPieChart.tsx new file mode 100644 index 0000000..b832a9b --- /dev/null +++ b/registry/components/chart/KriPieChart.tsx @@ -0,0 +1,50 @@ +'use client' + +import { useMemo } from 'react' +import type { EChartsOption } from 'echarts' + +import { BaseEChart } from './BaseEChart' +import type { KriChartBaseProps, KriPieChartSeries } from './chart.types' +import { defaultLegend, mergeChartOptions } from './chart.utils' + +export type KriPieChartProps = KriChartBaseProps & { + series: KriPieChartSeries[] +} + +const getPieCenter = (index: number, count: number) => { + if (count === 1) return ['50%', '55%'] + + return [`${((index + 0.5) / count) * 100}%`, '55%'] +} + +export function KriPieChart({ + series, + title, + legend, + tooltip, + option, + ...baseChartProps +}: KriPieChartProps) { + const chartOption = useMemo(() => { + const generated: EChartsOption = { + title, + tooltip: tooltip ?? { trigger: 'item' }, + legend: legend ?? defaultLegend(series.length), + series: series.map((item, index) => ({ + type: 'pie', + name: item.name, + data: item.data, + radius: item.radius ?? (series.length > 1 ? ['32%', '52%'] : '65%'), + center: item.center ?? getPieCenter(index, series.length), + ...item.option, + })) as EChartsOption['series'], + } + + return mergeChartOptions(generated, option) + }, [legend, option, series, title, tooltip]) + + return +} + +export default KriPieChart + diff --git a/registry/components/chart/KriRadarChart.tsx b/registry/components/chart/KriRadarChart.tsx new file mode 100644 index 0000000..8112ae9 --- /dev/null +++ b/registry/components/chart/KriRadarChart.tsx @@ -0,0 +1,55 @@ +'use client' + +import { useMemo } from 'react' +import type { EChartsOption } from 'echarts' + +import { BaseEChart } from './BaseEChart' +import type { KriChartBaseProps, KriRadarChartSeries, KriRadarDataItem, KriRadarIndicator } from './chart.types' +import { defaultLegend, mergeChartOptions, mergeObjectOption } from './chart.utils' + +export type KriRadarChartProps = KriChartBaseProps & { + indicators: KriRadarIndicator[] + series: KriRadarChartSeries[] + radar?: EChartsOption['radar'] +} + +const normalizeRadarData = (series: KriRadarChartSeries): KriRadarDataItem[] => { + if (series.data.every((value) => typeof value === 'number')) { + return [{ name: series.name, value: series.data as number[] }] + } + + return series.data as KriRadarDataItem[] +} + +export function KriRadarChart({ + indicators, + series, + radar, + title, + legend, + tooltip, + option, + ...baseChartProps +}: KriRadarChartProps) { + const chartOption = useMemo(() => { + const generated: EChartsOption = { + title, + tooltip: tooltip ?? { trigger: 'item' }, + legend: legend ?? defaultLegend(series.length), + radar: mergeObjectOption({ indicator: indicators }, radar) as EChartsOption['radar'], + series: series.map((item) => ({ + type: 'radar', + name: item.name, + data: normalizeRadarData(item), + ...item.option, + })) as EChartsOption['series'], + } + + return mergeChartOptions(generated, option) + }, [indicators, legend, option, radar, series, title, tooltip]) + + return +} + +export default KriRadarChart + diff --git a/registry/components/chart/KriScatterChart.tsx b/registry/components/chart/KriScatterChart.tsx new file mode 100644 index 0000000..573bf90 --- /dev/null +++ b/registry/components/chart/KriScatterChart.tsx @@ -0,0 +1,55 @@ +'use client' + +import { useMemo } from 'react' +import type { EChartsOption } from 'echarts' + +import { BaseEChart } from './BaseEChart' +import type { KriChartBaseProps, KriScatterChartSeries } from './chart.types' +import { defaultGrid, defaultLegend, mergeChartOptions, mergeObjectOption } from './chart.utils' + +export type KriScatterChartProps = KriChartBaseProps & { + series: KriScatterChartSeries[] + grid?: EChartsOption['grid'] + xAxis?: EChartsOption['xAxis'] + yAxis?: EChartsOption['yAxis'] + symbolSize?: number | ((value: unknown, params: unknown) => number) +} + +export function KriScatterChart({ + series, + grid, + xAxis, + yAxis, + symbolSize, + title, + legend, + tooltip, + option, + ...baseChartProps +}: KriScatterChartProps) { + const chartOption = useMemo(() => { + const generated: EChartsOption = { + title, + tooltip: tooltip ?? { trigger: 'item' }, + legend: legend ?? defaultLegend(series.length), + grid: grid ?? defaultGrid(series.length), + xAxis: mergeObjectOption({ type: 'value' }, xAxis) as EChartsOption['xAxis'], + yAxis: mergeObjectOption({ type: 'value' }, yAxis) as EChartsOption['yAxis'], + series: series.map((item) => ({ + type: 'scatter', + name: item.name, + data: item.data, + ...(symbolSize ? { symbolSize } : {}), + ...(item.color ? { color: item.color } : {}), + ...item.option, + })) as EChartsOption['series'], + } + + return mergeChartOptions(generated, option) + }, [grid, legend, option, series, symbolSize, title, tooltip, xAxis, yAxis]) + + return +} + +export default KriScatterChart + diff --git a/registry/components/chart/KriStackedBarChart.tsx b/registry/components/chart/KriStackedBarChart.tsx new file mode 100644 index 0000000..00cd454 --- /dev/null +++ b/registry/components/chart/KriStackedBarChart.tsx @@ -0,0 +1,62 @@ +'use client' + +import { useMemo } from 'react' +import type { EChartsOption } from 'echarts' + +import { BaseEChart } from './BaseEChart' +import type { KriCartesianChartProps } from './chart.types' +import { defaultGrid, defaultLegend, mergeChartOptions, mergeObjectOption } from './chart.utils' + +export type KriStackedBarChartProps = KriCartesianChartProps & { + isHorizontal?: boolean + stackName?: string + barWidth?: number | string + barMaxWidth?: number | string +} + +export function KriStackedBarChart({ + categories, + series, + isHorizontal = false, + stackName = 'total', + barWidth, + barMaxWidth, + title, + legend, + tooltip, + grid, + xAxis, + yAxis, + option, + ...baseChartProps +}: KriStackedBarChartProps) { + const chartOption = useMemo(() => { + const categoryAxis = { type: 'category', data: categories } + const valueAxis = { type: 'value' } + const generated: EChartsOption = { + title, + tooltip: tooltip ?? { trigger: 'axis', axisPointer: { type: 'shadow' } }, + legend: legend ?? defaultLegend(series.length), + grid: grid ?? defaultGrid(series.length), + xAxis: mergeObjectOption(isHorizontal ? valueAxis : categoryAxis, xAxis) as EChartsOption['xAxis'], + yAxis: mergeObjectOption(isHorizontal ? categoryAxis : valueAxis, yAxis) as EChartsOption['yAxis'], + series: series.map((item) => ({ + type: 'bar', + name: item.name, + data: item.data, + stack: item.stack ?? stackName, + ...(barWidth ? { barWidth } : {}), + ...(barMaxWidth ? { barMaxWidth } : {}), + ...(item.color ? { color: item.color } : {}), + ...item.option, + })) as EChartsOption['series'], + } + + return mergeChartOptions(generated, option) + }, [barMaxWidth, barWidth, categories, grid, isHorizontal, legend, option, series, stackName, title, tooltip, xAxis, yAxis]) + + return +} + +export default KriStackedBarChart + diff --git a/registry/components/chart/chart.config.ts b/registry/components/chart/chart.config.ts new file mode 100644 index 0000000..d802853 --- /dev/null +++ b/registry/components/chart/chart.config.ts @@ -0,0 +1,6 @@ +export const CHART_HEIGHTS = { + DEFAULT: 320, + SMALL: 240, + LARGE: 420, +} as const + diff --git a/registry/components/chart/chart.types.ts b/registry/components/chart/chart.types.ts new file mode 100644 index 0000000..c0c98b3 --- /dev/null +++ b/registry/components/chart/chart.types.ts @@ -0,0 +1,71 @@ +import type { EChartsOption } from 'echarts' + +import type { BaseEChartProps } from './BaseEChart' + +export type KriChartBaseProps = Omit & { + option?: EChartsOption + title?: EChartsOption['title'] + legend?: EChartsOption['legend'] + tooltip?: EChartsOption['tooltip'] +} + +export type KriCategoryValue = string | number + +export type KriCategoryChartSeries = { + name: string + data: unknown[] + color?: string + stack?: string + option?: Record +} + +export type KriCartesianChartProps = KriChartBaseProps & { + categories: KriCategoryValue[] + series: KriCategoryChartSeries[] + grid?: EChartsOption['grid'] + xAxis?: EChartsOption['xAxis'] + yAxis?: EChartsOption['yAxis'] +} + +export type KriNamedChartData = { + name: string + value: unknown + [key: string]: unknown +} + +export type KriPieChartSeries = { + name?: string + data: KriNamedChartData[] + radius?: string | number | Array + center?: Array + option?: Record +} + +export type KriRadarIndicator = { + name: string + max?: number + min?: number + color?: string +} + +export type KriRadarDataItem = { + name?: string + value: number[] + [key: string]: unknown +} + +export type KriRadarChartSeries = { + name: string + data: number[] | KriRadarDataItem[] + option?: Record +} + +export type KriScatterDataItem = [number, number] | [string, number] | KriNamedChartData + +export type KriScatterChartSeries = { + name: string + data: KriScatterDataItem[] + color?: string + option?: Record +} + diff --git a/registry/components/chart/chart.utils.ts b/registry/components/chart/chart.utils.ts new file mode 100644 index 0000000..4440ab6 --- /dev/null +++ b/registry/components/chart/chart.utils.ts @@ -0,0 +1,34 @@ +import type { EChartsOption } from 'echarts' + +export const mergeChartOptions = (generated: EChartsOption, option?: EChartsOption): EChartsOption => { + if (!option) return generated + + return { + ...generated, + ...option, + series: option.series ?? generated.series, + } +} + +export const mergeObjectOption = (defaults: Record, option?: TOption): TOption | Record => { + if (!option) return defaults + if (Array.isArray(option)) return option + if (typeof option === 'object') return { ...defaults, ...(option as Record) } + + return option +} + +export const defaultLegend = (seriesCount: number): EChartsOption['legend'] | undefined => { + return seriesCount > 1 ? { top: 0 } : undefined +} + +export const defaultGrid = (seriesCount: number): EChartsOption['grid'] => { + return { + top: seriesCount > 1 ? 44 : 24, + right: 24, + bottom: 24, + left: 24, + containLabel: true, + } +} + diff --git a/registry/components/chart/index.ts b/registry/components/chart/index.ts new file mode 100644 index 0000000..ccd30f0 --- /dev/null +++ b/registry/components/chart/index.ts @@ -0,0 +1,29 @@ +export { BaseEChart, default } from './BaseEChart' +export type { BaseEChartProps } from './BaseEChart' +export { KriBarChart } from './KriBarChart' +export type { KriBarChartProps } from './KriBarChart' +export { KriLineChart } from './KriLineChart' +export type { KriLineChartProps } from './KriLineChart' +export { KriPieChart } from './KriPieChart' +export type { KriPieChartProps } from './KriPieChart' +export { KriRadarChart } from './KriRadarChart' +export type { KriRadarChartProps } from './KriRadarChart' +export { KriScatterChart } from './KriScatterChart' +export type { KriScatterChartProps } from './KriScatterChart' +export { KriStackedBarChart } from './KriStackedBarChart' +export type { KriStackedBarChartProps } from './KriStackedBarChart' +export { CHART_HEIGHTS } from './chart.config' +export type { + KriCartesianChartProps, + KriCategoryChartSeries, + KriCategoryValue, + KriChartBaseProps, + KriNamedChartData, + KriPieChartSeries, + KriRadarChartSeries, + KriRadarDataItem, + KriRadarIndicator, + KriScatterChartSeries, + KriScatterDataItem, +} from './chart.types' +export { useResizeKey } from './useResizeKey' diff --git a/registry/components/chart/useResizeKey.ts b/registry/components/chart/useResizeKey.ts new file mode 100644 index 0000000..cc6fbb5 --- /dev/null +++ b/registry/components/chart/useResizeKey.ts @@ -0,0 +1,31 @@ +'use client' + +import { useEffect, useState } from 'react' + +export function useResizeKey(delay = 150) { + const [resizeKey, setResizeKey] = useState(0) + + useEffect(() => { + if (typeof window === 'undefined') return + + let timer: ReturnType | undefined + + const handleResize = () => { + if (timer) window.clearTimeout(timer) + + timer = window.setTimeout(() => { + setResizeKey((key) => key + 1) + }, delay) + } + + window.addEventListener('resize', handleResize) + + return () => { + if (timer) window.clearTimeout(timer) + window.removeEventListener('resize', handleResize) + } + }, [delay]) + + return resizeKey +} + diff --git a/registry/components/registry.json b/registry/components/registry.json index 7cc8d8e..9ff38cb 100644 --- a/registry/components/registry.json +++ b/registry/components/registry.json @@ -1,6 +1,78 @@ { "$schema": "https://ui.shadcn.com/schema/registry.json", "items": [ + { + "name": "kri-chart", + "type": "registry:component", + "title": "KRI Chart", + "description": "KRI common ECharts wrapper component with resize remounting and optional numeric data normalization.", + "dependencies": [ + "echarts", + "echarts-for-react" + ], + "files": [ + { + "path": "chart/BaseEChart.tsx", + "type": "registry:component", + "target": "@components/chart/BaseEChart.tsx" + }, + { + "path": "chart/KriLineChart.tsx", + "type": "registry:component", + "target": "@components/chart/KriLineChart.tsx" + }, + { + "path": "chart/KriBarChart.tsx", + "type": "registry:component", + "target": "@components/chart/KriBarChart.tsx" + }, + { + "path": "chart/KriStackedBarChart.tsx", + "type": "registry:component", + "target": "@components/chart/KriStackedBarChart.tsx" + }, + { + "path": "chart/KriPieChart.tsx", + "type": "registry:component", + "target": "@components/chart/KriPieChart.tsx" + }, + { + "path": "chart/KriRadarChart.tsx", + "type": "registry:component", + "target": "@components/chart/KriRadarChart.tsx" + }, + { + "path": "chart/KriScatterChart.tsx", + "type": "registry:component", + "target": "@components/chart/KriScatterChart.tsx" + }, + { + "path": "chart/chart.config.ts", + "type": "registry:component", + "target": "@components/chart/chart.config.ts" + }, + { + "path": "chart/chart.types.ts", + "type": "registry:component", + "target": "@components/chart/chart.types.ts" + }, + { + "path": "chart/chart.utils.ts", + "type": "registry:component", + "target": "@components/chart/chart.utils.ts" + }, + { + "path": "chart/useResizeKey.ts", + "type": "registry:component", + "target": "@components/chart/useResizeKey.ts" + }, + { + "path": "chart/index.ts", + "type": "registry:component", + "target": "@components/chart/index.ts" + } + ] + }, { "name": "kri-select", "type": "registry:component",