@keneth80/k-chart 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,262 @@
1
+ # React And Next.js Guide
2
+
3
+ KChart core package(`@keneth80/k-chart`)는 framework-agnostic 함수형 chart runtime입니다. 현재 npm으로 배포된 라이브러리 안에는 공식 React plugin package가 포함되어 있지 않습니다.
4
+
5
+ 현재 React wrapper는 `kchart-next-playground` 저장소의 `packages/react/src`에 로컬 예제로 들어 있습니다. 이 문서는 React/Next.js 프로젝트에서 core API를 직접 쓰는 방법과, wrapper 컴포넌트를 만들어 재사용하는 방법을 설명합니다.
6
+
7
+ 복사해서 사용할 수 있는 wrapper 예제 파일도 제공합니다: [examples/react-wrapper.tsx](../examples/react-wrapper.tsx)
8
+
9
+ ## Client Component에서 사용
10
+
11
+ KChart는 DOM element, SVG, Canvas, WebGL layer를 직접 만들기 때문에 Next.js App Router에서는 반드시 Client Component 안에서 mount 이후 생성해야 합니다.
12
+
13
+ ```tsx
14
+ 'use client';
15
+
16
+ import { useEffect, useRef } from 'react';
17
+ import {
18
+ createKChart,
19
+ createLineSeries,
20
+ type KChartController
21
+ } from '@keneth80/k-chart';
22
+
23
+ type TrafficPoint = {
24
+ time: string;
25
+ value: number;
26
+ };
27
+
28
+ export function TrafficChart({ data }: { data: TrafficPoint[] }) {
29
+ const rootRef = useRef<HTMLDivElement | null>(null);
30
+ const chartRef = useRef<KChartController<TrafficPoint> | null>(null);
31
+
32
+ useEffect(() => {
33
+ if (!rootRef.current) {
34
+ return;
35
+ }
36
+
37
+ const chart = createKChart<TrafficPoint>({
38
+ selector: rootRef.current,
39
+ data,
40
+ axes: [
41
+ { field: 'time', type: 'string', placement: 'bottom' },
42
+ { field: 'value', type: 'number', placement: 'left' }
43
+ ],
44
+ series: [
45
+ createLineSeries({
46
+ selector: 'traffic',
47
+ displayName: 'Traffic',
48
+ xField: 'time',
49
+ yField: 'value',
50
+ curve: true
51
+ })
52
+ ],
53
+ grid: { visible: true, y: true },
54
+ legend: { visible: true, placement: 'top' },
55
+ tooltip: { visible: true }
56
+ }).render();
57
+
58
+ chartRef.current = chart;
59
+
60
+ return () => {
61
+ chart.destroy();
62
+ chartRef.current = null;
63
+ };
64
+ }, []);
65
+
66
+ useEffect(() => {
67
+ chartRef.current?.updateData(data);
68
+ }, [data]);
69
+
70
+ return <div ref={rootRef} style={{ width: '100%', height: 420 }} />;
71
+ }
72
+ ```
73
+
74
+ ## 재사용 가능한 React Wrapper
75
+
76
+ 여러 화면에서 같은 방식으로 차트를 쓴다면 wrapper 컴포넌트를 만들어두는 편이 좋습니다.
77
+
78
+ 전체 파일 예제: [examples/react-wrapper.tsx](../examples/react-wrapper.tsx)
79
+
80
+ ```tsx
81
+ 'use client';
82
+
83
+ import { useEffect, useRef } from 'react';
84
+ import {
85
+ createKChart,
86
+ type KChartAxis,
87
+ type KChartConfiguration,
88
+ type KChartController,
89
+ type KChartOption,
90
+ type KChartSeries,
91
+ type KChartTooltipConfiguration
92
+ } from '@keneth80/k-chart';
93
+
94
+ export type KChartReactProps<T = any> = {
95
+ data: T[];
96
+ axes: KChartAxis<T>[];
97
+ series: KChartSeries<T>[];
98
+ options?: KChartOption[];
99
+ width?: number;
100
+ height?: number;
101
+ margin?: KChartConfiguration<T>['margin'];
102
+ title?: KChartConfiguration<T>['title'];
103
+ grid?: KChartConfiguration<T>['grid'];
104
+ legend?: KChartConfiguration<T>['legend'];
105
+ tooltip?: KChartTooltipConfiguration<T>;
106
+ className?: string;
107
+ };
108
+
109
+ export function KChart<T = any>(props: KChartReactProps<T>) {
110
+ const rootRef = useRef<HTMLDivElement | null>(null);
111
+ const chartRef = useRef<KChartController<T> | null>(null);
112
+
113
+ useEffect(() => {
114
+ if (!rootRef.current) {
115
+ return;
116
+ }
117
+
118
+ chartRef.current?.destroy();
119
+ chartRef.current = createKChart<T>({
120
+ selector: rootRef.current,
121
+ data: props.data,
122
+ axes: props.axes,
123
+ series: props.series,
124
+ options: props.options,
125
+ width: props.width ?? rootRef.current.clientWidth,
126
+ height: props.height ?? rootRef.current.clientHeight,
127
+ margin: props.margin,
128
+ title: props.title,
129
+ grid: props.grid,
130
+ legend: props.legend,
131
+ tooltip: props.tooltip,
132
+ className: props.className
133
+ }).render();
134
+
135
+ return () => {
136
+ chartRef.current?.destroy();
137
+ chartRef.current = null;
138
+ };
139
+ }, [props]);
140
+
141
+ useEffect(() => {
142
+ if (!rootRef.current || !chartRef.current || typeof ResizeObserver === 'undefined') {
143
+ return;
144
+ }
145
+
146
+ const observer = new ResizeObserver((entries) => {
147
+ const rect = entries[0]?.contentRect;
148
+ if (!rect) {
149
+ return;
150
+ }
151
+
152
+ chartRef.current?.resize({
153
+ width: Math.max(rect.width, 320),
154
+ height: Math.max(rect.height, 260)
155
+ });
156
+ });
157
+
158
+ observer.observe(rootRef.current);
159
+
160
+ return () => observer.disconnect();
161
+ }, []);
162
+
163
+ return <div ref={rootRef} className="kchart-react-root" />;
164
+ }
165
+ ```
166
+
167
+ ## Wrapper 사용 예
168
+
169
+ ```tsx
170
+ 'use client';
171
+
172
+ import { useMemo } from 'react';
173
+ import { createLineSeries } from '@keneth80/k-chart';
174
+ import { KChart } from './KChart';
175
+
176
+ type Point = {
177
+ x: number;
178
+ value: number;
179
+ volume: number;
180
+ };
181
+
182
+ export function DashboardChart({ data }: { data: Point[] }) {
183
+ const series = useMemo(() => [
184
+ createLineSeries<Point>({
185
+ selector: 'value',
186
+ displayName: 'Value',
187
+ xField: 'x',
188
+ yField: 'value',
189
+ color: '#5db8ff',
190
+ curve: true
191
+ }),
192
+ createLineSeries<Point>({
193
+ selector: 'volume',
194
+ displayName: 'Volume',
195
+ xField: 'x',
196
+ yField: 'volume',
197
+ color: '#56d08f',
198
+ curve: true
199
+ })
200
+ ], []);
201
+
202
+ return (
203
+ <KChart<Point>
204
+ data={data}
205
+ axes={[
206
+ { field: 'x', type: 'number', placement: 'bottom', title: 'Index' },
207
+ { field: 'value', type: 'number', placement: 'left', title: 'Value' }
208
+ ]}
209
+ series={series}
210
+ grid={{ visible: true, y: true }}
211
+ legend={{ visible: true, placement: 'top', selectable: true }}
212
+ tooltip={{ visible: true }}
213
+ />
214
+ );
215
+ }
216
+ ```
217
+
218
+ ## Next.js 설정
219
+
220
+ Next.js 프로젝트에서 패키지 transpile이 필요하면 `next.config.mjs`에 추가합니다.
221
+
222
+ ```js
223
+ const nextConfig = {
224
+ transpilePackages: ['@keneth80/k-chart']
225
+ };
226
+
227
+ export default nextConfig;
228
+ ```
229
+
230
+ ## OffscreenCanvas Worker 사용
231
+
232
+ `asyncRender`를 쓰는 경우 worker 파일은 client bundle에서 생성해야 합니다.
233
+
234
+ ```ts
235
+ // kchart-render.worker.ts
236
+ import { startKChartRenderWorker } from '@keneth80/k-chart';
237
+
238
+ startKChartRenderWorker();
239
+ ```
240
+
241
+ ```tsx
242
+ createWebglLineSeries<Point>({
243
+ selector: 'trace',
244
+ xField: 'x',
245
+ yField: 'value',
246
+ asyncRender: {
247
+ enabled: true,
248
+ workerFactory: () => new Worker(
249
+ new URL('./kchart-render.worker.ts', import.meta.url),
250
+ { type: 'module' }
251
+ )
252
+ }
253
+ });
254
+ ```
255
+
256
+ OffscreenCanvas나 Worker를 지원하지 않는 브라우저에서는 기존 main-thread renderer로 fallback됩니다.
257
+
258
+ ## React Plugin 상태
259
+
260
+ - `@keneth80/k-chart` npm package에는 아직 공식 React plugin이 포함되어 있지 않습니다.
261
+ - `kchart-next-playground/packages/react/src`에 로컬 wrapper 예제가 있습니다.
262
+ - 공식 plugin으로 분리한다면 `@keneth80/k-chart-react` 같은 별도 package로 배포하는 방향이 적합합니다.
@@ -0,0 +1,61 @@
1
+ # KChart Refactor Plan
2
+
3
+ ## Current Structure
4
+
5
+ ```txt
6
+ src/
7
+ ├─ index.ts public export surface
8
+ ├─ kchart.ts class-free chart runtime
9
+ ├─ main.ts playground demo
10
+ ├─ style.css playground and chart theme
11
+ └─ types/ local asset/type declarations
12
+ ```
13
+
14
+ ## Runtime Responsibility Split
15
+
16
+ ```txt
17
+ createKChart(config)
18
+ ├─ resolve container and size
19
+ ├─ create SVG / Canvas / WebGL layers
20
+ ├─ resolve axes into D3 scales
21
+ ├─ render axes
22
+ └─ call series renderers
23
+ ├─ built-in renderer factory
24
+ └─ custom renderer function
25
+ ```
26
+
27
+ KChart keeps axis, scale, layout, and layer ownership in the runtime. Visual rendering belongs to series functions. A series receives prepared scale and layer context, so the same chart can host SVG, Canvas, and WebGL renderers without requiring class inheritance.
28
+
29
+ ## Public API
30
+
31
+ ```ts
32
+ import {
33
+ createKChart,
34
+ createLineSeries,
35
+ createCustomSeries,
36
+ KChartController,
37
+ KChartSeries
38
+ } from '@keneth80/k-chart';
39
+ ```
40
+
41
+ `src/index.ts` exports only `src/kchart.ts`, so package consumers do not receive the old class-based API.
42
+
43
+ ## Completed Migration
44
+
45
+ 1. Added `src/kchart.ts` as the functional runtime.
46
+ 2. Replaced the public package surface with KChart-only exports.
47
+ 3. Replaced legacy playground entry with KChart examples.
48
+ 4. Removed legacy class-based chart source from `src/component`.
49
+ 5. Reduced package `files` so published tarballs include only KChart runtime output.
50
+ 6. Reduced runtime dependencies to the D3 modules used by `src/kchart.ts`.
51
+ 7. Added built-in Canvas 2D line and point renderer factories.
52
+ 8. Added built-in WebGL point renderer factory.
53
+ 9. Added title, grid, legend, and axis title display options.
54
+ 10. Added basic nearest-point tooltip support.
55
+
56
+ ## Remaining Roadmap
57
+
58
+ 1. Add zoom/pan behavior as a runtime plugin.
59
+ 2. Add richer tooltip modes, fixed tooltip positioning, and crosshair support.
60
+ 3. Add built-in WebGL line renderer.
61
+ 4. Add focused tests for scale resolution, resize, SVG line render, Canvas/WebGL renderers, tooltip, and custom renderer context.
@@ -0,0 +1,81 @@
1
+ # Release Guide
2
+
3
+ KChart is published to npm as `@keneth80/k-chart`.
4
+
5
+ ## Prerequisites
6
+
7
+ - npm account with publish permission for the `@keneth80` scope.
8
+ - GitHub repository remote set to `https://github.com/keneth80/k-chart.git`.
9
+ - Clean git working tree before publishing.
10
+
11
+ ## Local Verification
12
+
13
+ Run these checks before every release:
14
+
15
+ ```bash
16
+ npm install
17
+ npm run typecheck
18
+ npm run build:lib
19
+ npm run pack:dry
20
+ ```
21
+
22
+ `npm run pack:dry` should show only the runtime package files:
23
+
24
+ - `lib/**`
25
+ - `README.md`
26
+ - `LICENSE`
27
+ - `CHANGELOG.md`
28
+ - `package.json`
29
+
30
+ It should not include `node_modules`, `src`, `dist`, demo files, webpack config, or local archives.
31
+
32
+ ## Publish
33
+
34
+ For the first public release:
35
+
36
+ ```bash
37
+ npm login
38
+ npm publish --access public
39
+ ```
40
+
41
+ For later releases, update the version first:
42
+
43
+ ```bash
44
+ npm version patch
45
+ npm publish
46
+ git push origin main --follow-tags
47
+ ```
48
+
49
+ Use semantic versioning:
50
+
51
+ - `patch`: bug fixes and docs-only compatible changes.
52
+ - `minor`: new backwards-compatible API or examples.
53
+ - `major`: breaking API changes.
54
+
55
+ ## GitHub Release
56
+
57
+ After pushing the version tag, create a GitHub Release from that tag.
58
+
59
+ Recommended release note sections:
60
+
61
+ ```md
62
+ ## Added
63
+
64
+ ## Changed
65
+
66
+ ## Fixed
67
+
68
+ ## Migration Notes
69
+ ```
70
+
71
+ Use the same content to update `CHANGELOG.md`.
72
+
73
+ ## Playground
74
+
75
+ After publishing, update the playground project to consume npm instead of a vendored tgz:
76
+
77
+ ```bash
78
+ npm install @keneth80/k-chart@latest
79
+ ```
80
+
81
+ Then remove the local `vendor/kchart-1.0.0.tgz` dependency if it is no longer needed.
@@ -0,0 +1,55 @@
1
+ import {
2
+ createKChart,
3
+ createCustomSeries,
4
+ KChartController
5
+ } from '../src';
6
+
7
+ interface CirclePoint {
8
+ x: number;
9
+ y: number;
10
+ radius: number;
11
+ }
12
+
13
+ const data: CirclePoint[] = [
14
+ { x: 1, y: 12, radius: 8 },
15
+ { x: 2, y: 18, radius: 12 },
16
+ { x: 3, y: 9, radius: 6 }
17
+ ];
18
+
19
+ const circleSeries = createCustomSeries<CirclePoint>({
20
+ selector: 'custom-circle',
21
+ displayName: 'Custom Circle',
22
+ xField: 'x',
23
+ yField: 'y',
24
+ render({ group, data: seriesData, xScale, yScale, color }) {
25
+ if (!xScale || !yScale) {
26
+ return;
27
+ }
28
+
29
+ group.selectAll('.custom-circle')
30
+ .data(seriesData)
31
+ .join('circle')
32
+ .attr('class', 'custom-circle')
33
+ .attr('cx', (point: CirclePoint) => xScale.scale(point.x))
34
+ .attr('cy', (point: CirclePoint) => yScale.scale(point.y))
35
+ .attr('r', (point: CirclePoint) => point.radius)
36
+ .style('fill', color ?? '#58a6ff')
37
+ .style('fill-opacity', 0.6)
38
+ .style('stroke', '#ffffff')
39
+ .style('stroke-width', 1.5);
40
+ }
41
+ });
42
+
43
+ const chart: KChartController<CirclePoint> = createKChart<CirclePoint>({
44
+ selector: '#chart',
45
+ data,
46
+ axes: [
47
+ { field: 'x', type: 'number', placement: 'bottom', min: 0, max: 4 },
48
+ { field: 'y', type: 'number', placement: 'left', min: 0, max: 24 }
49
+ ],
50
+ series: [
51
+ circleSeries
52
+ ]
53
+ });
54
+
55
+ chart.render();
@@ -0,0 +1,44 @@
1
+ import {
2
+ createKChart,
3
+ createLineSeries,
4
+ KChartController
5
+ } from '../src';
6
+
7
+ interface TrafficPoint {
8
+ time: string;
9
+ value: number;
10
+ }
11
+
12
+ const data: TrafficPoint[] = [
13
+ { time: '00:00', value: 12 },
14
+ { time: '01:00', value: 18 }
15
+ ];
16
+
17
+ const chart: KChartController<TrafficPoint> = createKChart<TrafficPoint>({
18
+ selector: '#chart',
19
+ data,
20
+ axes: [
21
+ { field: 'time', type: 'string', placement: 'bottom' },
22
+ { field: 'value', type: 'number', placement: 'left' }
23
+ ],
24
+ series: [
25
+ createLineSeries({
26
+ selector: 'traffic',
27
+ displayName: 'Traffic',
28
+ xField: 'time',
29
+ yField: 'value',
30
+ curve: true,
31
+ dot: true
32
+ })
33
+ ]
34
+ });
35
+
36
+ chart.render();
37
+
38
+ export function updateTraffic(nextData: TrafficPoint[]) {
39
+ chart.updateData(nextData);
40
+ }
41
+
42
+ export function disposeTrafficChart() {
43
+ chart.destroy();
44
+ }
@@ -0,0 +1,84 @@
1
+ 'use client';
2
+
3
+ import { useEffect, useRef } from 'react';
4
+ import {
5
+ createKChart,
6
+ type KChartAxis,
7
+ type KChartConfiguration,
8
+ type KChartController,
9
+ type KChartOption,
10
+ type KChartSeries,
11
+ type KChartTooltipConfiguration
12
+ } from '@keneth80/k-chart';
13
+
14
+ export type KChartReactProps<T = any> = {
15
+ data: T[];
16
+ axes: KChartAxis<T>[];
17
+ series: KChartSeries<T>[];
18
+ options?: KChartOption[];
19
+ width?: number;
20
+ height?: number;
21
+ margin?: KChartConfiguration<T>['margin'];
22
+ title?: KChartConfiguration<T>['title'];
23
+ grid?: KChartConfiguration<T>['grid'];
24
+ legend?: KChartConfiguration<T>['legend'];
25
+ tooltip?: KChartTooltipConfiguration<T>;
26
+ className?: string;
27
+ };
28
+
29
+ export function KChart<T = any>(props: KChartReactProps<T>) {
30
+ const rootRef = useRef<HTMLDivElement | null>(null);
31
+ const chartRef = useRef<KChartController<T> | null>(null);
32
+
33
+ useEffect(() => {
34
+ if (!rootRef.current) {
35
+ return;
36
+ }
37
+
38
+ chartRef.current?.destroy();
39
+ chartRef.current = createKChart<T>({
40
+ selector: rootRef.current,
41
+ data: props.data,
42
+ axes: props.axes,
43
+ series: props.series,
44
+ options: props.options,
45
+ width: props.width ?? rootRef.current.clientWidth,
46
+ height: props.height ?? rootRef.current.clientHeight,
47
+ margin: props.margin,
48
+ title: props.title,
49
+ grid: props.grid,
50
+ legend: props.legend,
51
+ tooltip: props.tooltip,
52
+ className: props.className
53
+ }).render();
54
+
55
+ return () => {
56
+ chartRef.current?.destroy();
57
+ chartRef.current = null;
58
+ };
59
+ }, [props]);
60
+
61
+ useEffect(() => {
62
+ if (!rootRef.current || !chartRef.current || typeof ResizeObserver === 'undefined') {
63
+ return;
64
+ }
65
+
66
+ const observer = new ResizeObserver((entries) => {
67
+ const rect = entries[0]?.contentRect;
68
+ if (!rect) {
69
+ return;
70
+ }
71
+
72
+ chartRef.current?.resize({
73
+ width: Math.max(rect.width, 320),
74
+ height: Math.max(rect.height, 260)
75
+ });
76
+ });
77
+
78
+ observer.observe(rootRef.current);
79
+
80
+ return () => observer.disconnect();
81
+ }, []);
82
+
83
+ return <div ref={rootRef} className="kchart-react-root" />;
84
+ }
package/lib/kchart.d.ts CHANGED
@@ -1,8 +1,11 @@
1
1
  import { Selection } from 'd3-selection';
2
+ import { ZoomTransform } from 'd3-zoom';
2
3
  export type KChartScaleType = 'number' | 'time' | 'string' | 'point';
3
4
  export type KChartPlacement = 'top' | 'right' | 'bottom' | 'left';
4
5
  export type KChartTextAlign = 'left' | 'center' | 'right';
5
6
  export type KChartLegendPlacement = 'top' | 'right' | 'bottom';
7
+ export type KChartZoomDirection = 'x' | 'y' | 'xy';
8
+ export type KChartZoomMode = 'wheel' | 'select' | 'both';
6
9
  export interface KChartAxis<T = any> {
7
10
  field: keyof T & string;
8
11
  type: KChartScaleType;
@@ -186,6 +189,20 @@ export interface KChartTooltipConfiguration<T = any> {
186
189
  color: string;
187
190
  }) => string;
188
191
  }
192
+ export interface KChartZoomContext<T = any> {
193
+ axes: KChartAxis<T>[];
194
+ xDomain?: Array<number | Date>;
195
+ yDomain?: Array<number | Date>;
196
+ transform: ZoomTransform;
197
+ }
198
+ export interface KChartZoomConfiguration<T = any> {
199
+ enabled?: boolean;
200
+ mode?: KChartZoomMode;
201
+ direction?: KChartZoomDirection;
202
+ scaleExtent?: [number, number];
203
+ resetOnDoubleClick?: boolean;
204
+ onZoom?: (context: KChartZoomContext<T>) => void;
205
+ }
189
206
  export interface KChartSpecAreaConfiguration {
190
207
  visible?: boolean;
191
208
  start: number | Date | string;
@@ -247,6 +264,7 @@ export interface KChartConfiguration<T = any> {
247
264
  grid?: KChartGridConfiguration;
248
265
  legend?: KChartLegendConfiguration;
249
266
  tooltip?: KChartTooltipConfiguration<T>;
267
+ zoom?: KChartZoomConfiguration<T>;
250
268
  specAreas?: KChartSpecAreaConfiguration[];
251
269
  cursorGuide?: KChartCursorGuideConfiguration;
252
270
  guideLine?: KChartCursorGuideConfiguration;
@@ -258,6 +276,8 @@ export interface KChartState<T = any> {
258
276
  container: Selection<HTMLElement, unknown, any, any>;
259
277
  data: T[];
260
278
  axes: KChartAxis<T>[];
279
+ initialAxes: KChartAxis<T>[];
280
+ baseAxes: KChartAxis<T>[];
261
281
  series: KChartSeries<T>[];
262
282
  size: KChartSize;
263
283
  plotSize: KChartSize;
@@ -266,6 +286,14 @@ export interface KChartState<T = any> {
266
286
  scales: KChartResolvedScale<T>[];
267
287
  layers: KChartLayerContext;
268
288
  hiddenSeries: Set<string>;
289
+ zoomTransform: ZoomTransform;
290
+ zoomSelection?: {
291
+ active: boolean;
292
+ startX: number;
293
+ startY: number;
294
+ currentX: number;
295
+ currentY: number;
296
+ };
269
297
  }
270
298
  export interface KChartController<T = any> {
271
299
  render(): KChartController<T>;