@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.
- package/CHANGELOG.md +16 -0
- package/README.md +40 -46
- package/docs/functional-api.md +432 -0
- package/docs/react-nextjs.md +262 -0
- package/docs/refactor-plan.md +61 -0
- package/docs/release.md +81 -0
- package/examples/custom-circle-series.ts +55 -0
- package/examples/functional-ui-usage.ts +44 -0
- package/examples/react-wrapper.tsx +84 -0
- package/lib/kchart.d.ts +28 -0
- package/lib/kchart.d.ts.map +1 -1
- package/lib/kchart.js +262 -4
- package/lib/kchart.js.map +1 -1
- package/package.json +10 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.3.0
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Added chart-level `zoom` configuration for number and time axes.
|
|
8
|
+
- Added `zoom.mode` support for `wheel`, `select`, and `both`.
|
|
9
|
+
- Added wheel/trackpad zoom, drag pan, double-click reset, and drag-selection zoom interactions.
|
|
10
|
+
- Added zoom callback context with visible x/y domains and active transform state.
|
|
11
|
+
- Enabled zoom examples for Canvas BigData and WebGL BigData demos.
|
|
12
|
+
- Added `d3-zoom` as a runtime dependency and direct `@types/d3-zoom` typing support.
|
|
13
|
+
|
|
14
|
+
### Changed
|
|
15
|
+
|
|
16
|
+
- Updated the functional API guide and README with zoom usage examples.
|
|
17
|
+
- Preserved initial axes separately from active zoom axes so reset returns to the original domain.
|
|
18
|
+
|
|
3
19
|
## 1.2.0
|
|
4
20
|
|
|
5
21
|
### Added
|
package/README.md
CHANGED
|
@@ -49,6 +49,13 @@ npm run dev
|
|
|
49
49
|
|
|
50
50
|
Then open `http://127.0.0.1:9003/`.
|
|
51
51
|
|
|
52
|
+
## Guides
|
|
53
|
+
|
|
54
|
+
- [Functional API Guide](docs/functional-api.md)
|
|
55
|
+
- [React And Next.js Guide](docs/react-nextjs.md)
|
|
56
|
+
- [Release Guide](docs/release.md)
|
|
57
|
+
- [React Wrapper Example](examples/react-wrapper.tsx)
|
|
58
|
+
|
|
52
59
|
## Quick Start
|
|
53
60
|
|
|
54
61
|
```ts
|
|
@@ -225,6 +232,37 @@ createKChart({
|
|
|
225
232
|
}).render();
|
|
226
233
|
```
|
|
227
234
|
|
|
235
|
+
## Zoom
|
|
236
|
+
|
|
237
|
+
`zoom` 옵션을 켜면 number/time 축의 domain을 런타임에 갱신하면서 SVG, Canvas, WebGL series를 다시 렌더링합니다. 기본적으로 wheel/trackpad 확대, 드래그 pan, 더블클릭 reset을 지원합니다.
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
createKChart({
|
|
241
|
+
selector: '#chart',
|
|
242
|
+
data: largeData,
|
|
243
|
+
axes: [
|
|
244
|
+
{ field: 'x', type: 'number', placement: 'bottom', min: 0, max: 119999 },
|
|
245
|
+
{ field: 'y', type: 'number', placement: 'left', min: 0, max: 100 }
|
|
246
|
+
],
|
|
247
|
+
series: [webglLine],
|
|
248
|
+
zoom: {
|
|
249
|
+
enabled: true,
|
|
250
|
+
mode: 'both',
|
|
251
|
+
direction: 'x',
|
|
252
|
+
scaleExtent: [1, 80],
|
|
253
|
+
resetOnDoubleClick: true,
|
|
254
|
+
onZoom: ({ xDomain }) => {
|
|
255
|
+
console.log('visible x domain', xDomain);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}).render();
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
- `direction`: `'x'`, `'y'`, `'xy'` 중 하나입니다. 대용량 line 차트는 보통 `'x'`가 가장 자연스럽습니다.
|
|
262
|
+
- `mode`: `'wheel'`은 wheel/trackpad zoom과 drag pan, `'select'`는 드래그 영역 선택 zoom, `'both'`는 wheel/trackpad zoom과 드래그 영역 선택 zoom을 함께 사용합니다.
|
|
263
|
+
- `scaleExtent`: 최소/최대 확대 배율입니다.
|
|
264
|
+
- `resetOnDoubleClick`: `false`로 지정하면 더블클릭 reset을 끌 수 있습니다.
|
|
265
|
+
|
|
228
266
|
## LTTB Downsampling
|
|
229
267
|
|
|
230
268
|
Line 계열 series는 `downsample` 옵션으로 LTTB(Largest Triangle Three Buckets) 다운샘플링을 사용할 수 있습니다. 원본 `data`와 축 domain은 그대로 유지하고, SVG/Canvas/WebGL renderer에 넘기는 series 데이터만 그리기 직전에 줄입니다.
|
|
@@ -488,53 +526,9 @@ const stackedSeries = createCustomSeries<Point>({
|
|
|
488
526
|
|
|
489
527
|
KChart touches the DOM, so create it in a Client Component after mount.
|
|
490
528
|
|
|
491
|
-
|
|
492
|
-
'use client';
|
|
493
|
-
|
|
494
|
-
import { useEffect, useRef } from 'react';
|
|
495
|
-
import { createKChart, createLineSeries, KChartController } from '@keneth80/k-chart';
|
|
496
|
-
|
|
497
|
-
export function TrafficChart({ data }) {
|
|
498
|
-
const rootRef = useRef<HTMLDivElement | null>(null);
|
|
499
|
-
const chartRef = useRef<KChartController | null>(null);
|
|
500
|
-
|
|
501
|
-
useEffect(() => {
|
|
502
|
-
if (!rootRef.current) {
|
|
503
|
-
return;
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
const chart = createKChart({
|
|
507
|
-
selector: rootRef.current,
|
|
508
|
-
data,
|
|
509
|
-
axes: [
|
|
510
|
-
{ field: 'time', type: 'string', placement: 'bottom' },
|
|
511
|
-
{ field: 'value', type: 'number', placement: 'left' }
|
|
512
|
-
],
|
|
513
|
-
series: [
|
|
514
|
-
createLineSeries({
|
|
515
|
-
selector: 'traffic',
|
|
516
|
-
xField: 'time',
|
|
517
|
-
yField: 'value'
|
|
518
|
-
})
|
|
519
|
-
]
|
|
520
|
-
});
|
|
521
|
-
|
|
522
|
-
chart.render();
|
|
523
|
-
chartRef.current = chart;
|
|
524
|
-
|
|
525
|
-
return () => {
|
|
526
|
-
chart.destroy();
|
|
527
|
-
chartRef.current = null;
|
|
528
|
-
};
|
|
529
|
-
}, []);
|
|
530
|
-
|
|
531
|
-
useEffect(() => {
|
|
532
|
-
chartRef.current?.updateData(data);
|
|
533
|
-
}, [data]);
|
|
529
|
+
See the full guide: [React And Next.js Guide](docs/react-nextjs.md).
|
|
534
530
|
|
|
535
|
-
|
|
536
|
-
}
|
|
537
|
-
```
|
|
531
|
+
Note: the current `@keneth80/k-chart` package does not include an official React plugin package yet. The Next playground has a local wrapper example under `packages/react/src`.
|
|
538
532
|
|
|
539
533
|
## Migration Status
|
|
540
534
|
|
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
# KChart Functional API
|
|
2
|
+
|
|
3
|
+
## Goal
|
|
4
|
+
|
|
5
|
+
KChart의 새 public API는 class adapter가 아니라 class-free runtime입니다. UI에서는 `new BasicChart(...)` 같은 인스턴스를 직접 만들지 않고 `createKChart(...)`로 controller를 생성합니다.
|
|
6
|
+
|
|
7
|
+
차트 코어는 축, scale, layout, SVG/Canvas/WebGL layer를 준비합니다. Series는 `render(context)` 함수만 제공하고, 같은 scale 정보를 받아 SVG, Canvas, WebGL 중 원하는 방식으로 그립니다.
|
|
8
|
+
|
|
9
|
+
## Import Surface
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import {
|
|
13
|
+
createKChart,
|
|
14
|
+
createCursorLineOption,
|
|
15
|
+
createGuideLineOption,
|
|
16
|
+
createLineSeries,
|
|
17
|
+
createCanvasLineSeries,
|
|
18
|
+
createCanvasPointSeries,
|
|
19
|
+
createSpecAreaOption,
|
|
20
|
+
createWebglPointSeries,
|
|
21
|
+
createCustomSeries,
|
|
22
|
+
KChartController,
|
|
23
|
+
KChartSeries
|
|
24
|
+
} from '@keneth80/k-chart';
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
새 저장소 기준 public export는 `src/kchart.ts`입니다. 기존 class 기반 구현은 마이그레이션 참고용 source로 남아 있어도 패키지 API로는 내보내지 않습니다.
|
|
28
|
+
|
|
29
|
+
## Controller
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
const chart = createKChart({
|
|
33
|
+
selector: '#chart',
|
|
34
|
+
data,
|
|
35
|
+
axes,
|
|
36
|
+
series
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
chart.render();
|
|
40
|
+
chart.updateData(nextData);
|
|
41
|
+
chart.updateAxes(nextAxes);
|
|
42
|
+
chart.updateSeries(nextSeries);
|
|
43
|
+
chart.resize({ width: 960, height: 420 });
|
|
44
|
+
chart.destroy();
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Controller Contract
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
type KChartController<T> = {
|
|
51
|
+
render(): KChartController<T>;
|
|
52
|
+
updateData(data: T[]): KChartController<T>;
|
|
53
|
+
updateAxes(axes: KChartAxis<T>[]): KChartController<T>;
|
|
54
|
+
updateSeries(series: KChartSeries<T>[]): KChartController<T>;
|
|
55
|
+
resize(size?: Partial<KChartSize>): KChartController<T>;
|
|
56
|
+
destroy(): void;
|
|
57
|
+
getState(): KChartState<T>;
|
|
58
|
+
};
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
반환값이 다시 controller이기 때문에 `render().updateData(...)`처럼 chaining할 수 있습니다.
|
|
62
|
+
|
|
63
|
+
## Built-In Line Series
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const chart = createKChart<TrafficPoint>({
|
|
67
|
+
selector: '#chart',
|
|
68
|
+
data,
|
|
69
|
+
axes: [
|
|
70
|
+
{ field: 'time', type: 'string', placement: 'bottom' },
|
|
71
|
+
{ field: 'value', type: 'number', placement: 'left' }
|
|
72
|
+
],
|
|
73
|
+
series: [
|
|
74
|
+
createLineSeries({
|
|
75
|
+
selector: 'traffic',
|
|
76
|
+
displayName: 'Traffic',
|
|
77
|
+
xField: 'time',
|
|
78
|
+
yField: 'value',
|
|
79
|
+
curve: true,
|
|
80
|
+
dot: true
|
|
81
|
+
})
|
|
82
|
+
]
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
chart.render();
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Built-In Canvas Series
|
|
89
|
+
|
|
90
|
+
Canvas 2D line/point renderer는 별도 class 없이 factory로 생성합니다.
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
const chart = createKChart<Point>({
|
|
94
|
+
selector: '#chart',
|
|
95
|
+
data,
|
|
96
|
+
axes: [
|
|
97
|
+
{ field: 'x', type: 'number', placement: 'bottom' },
|
|
98
|
+
{ field: 'y', type: 'number', placement: 'left' }
|
|
99
|
+
],
|
|
100
|
+
series: [
|
|
101
|
+
createCanvasLineSeries({
|
|
102
|
+
selector: 'canvas-line',
|
|
103
|
+
xField: 'x',
|
|
104
|
+
yField: 'y',
|
|
105
|
+
color: '#56d08f',
|
|
106
|
+
lineWidth: 3
|
|
107
|
+
}),
|
|
108
|
+
createCanvasPointSeries({
|
|
109
|
+
selector: 'canvas-points',
|
|
110
|
+
xField: 'x',
|
|
111
|
+
yField: 'y',
|
|
112
|
+
radius: 4,
|
|
113
|
+
color: '#5db8ff'
|
|
114
|
+
})
|
|
115
|
+
]
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Built-In WebGL Series
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
const chart = createKChart<Point>({
|
|
123
|
+
selector: '#chart',
|
|
124
|
+
data,
|
|
125
|
+
axes: [
|
|
126
|
+
{ field: 'x', type: 'number', placement: 'bottom' },
|
|
127
|
+
{ field: 'y', type: 'number', placement: 'left' }
|
|
128
|
+
],
|
|
129
|
+
series: [
|
|
130
|
+
createWebglLineSeries({
|
|
131
|
+
selector: 'webgl-large-line',
|
|
132
|
+
displayName: '120k WebGL Line',
|
|
133
|
+
xField: 'x',
|
|
134
|
+
yField: 'y',
|
|
135
|
+
color: '#5db8ff',
|
|
136
|
+
lineWidth: 1,
|
|
137
|
+
downsample: {
|
|
138
|
+
enabled: true,
|
|
139
|
+
threshold: ({ plotSize }) => Math.floor(plotSize.width)
|
|
140
|
+
}
|
|
141
|
+
}),
|
|
142
|
+
createWebglPointSeries({
|
|
143
|
+
selector: 'webgl-points',
|
|
144
|
+
xField: 'x',
|
|
145
|
+
yField: 'y',
|
|
146
|
+
pointSize: 8,
|
|
147
|
+
color: '#f3b45b'
|
|
148
|
+
})
|
|
149
|
+
]
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Downsampling
|
|
154
|
+
|
|
155
|
+
`createLineSeries`, `createCanvasLineSeries`, `createWebglLineSeries`는 `downsample` 옵션을 지원합니다. `true`를 넘기면 현재 plot width를 기준으로 LTTB가 적용되고, 객체를 넘기면 threshold와 x/y accessor를 직접 지정할 수 있습니다.
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
createWebglLineSeries<Point>({
|
|
159
|
+
selector: 'trace',
|
|
160
|
+
xField: 'x',
|
|
161
|
+
yField: 'signal',
|
|
162
|
+
downsample: {
|
|
163
|
+
enabled: true,
|
|
164
|
+
threshold: ({ plotSize }) => Math.floor(plotSize.width * 1.5)
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
알고리즘 자체도 export됩니다.
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
import { downsampleLTTB } from '@keneth80/k-chart';
|
|
173
|
+
|
|
174
|
+
const sampled = downsampleLTTB(data, 1000, (point) => point.x, (point) => point.signal);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Zoom
|
|
178
|
+
|
|
179
|
+
차트 레벨의 `zoom` 옵션은 number/time 축의 domain을 갱신하고 기존 렌더 파이프라인을 다시 실행합니다. 별도 series API 없이 SVG, Canvas, WebGL renderer가 같은 확대 상태를 공유합니다.
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
createKChart<Point>({
|
|
183
|
+
selector: '#chart',
|
|
184
|
+
data,
|
|
185
|
+
axes: [
|
|
186
|
+
{ field: 'x', type: 'number', placement: 'bottom' },
|
|
187
|
+
{ field: 'signal', type: 'number', placement: 'left' }
|
|
188
|
+
],
|
|
189
|
+
series: [
|
|
190
|
+
createWebglLineSeries({
|
|
191
|
+
selector: 'trace',
|
|
192
|
+
xField: 'x',
|
|
193
|
+
yField: 'signal'
|
|
194
|
+
})
|
|
195
|
+
],
|
|
196
|
+
zoom: {
|
|
197
|
+
enabled: true,
|
|
198
|
+
mode: 'both',
|
|
199
|
+
direction: 'x',
|
|
200
|
+
scaleExtent: [1, 80],
|
|
201
|
+
resetOnDoubleClick: true
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
`mode`는 `'wheel'`, `'select'`, `'both'`를 지원합니다. `'wheel'`은 wheel/trackpad zoom과 drag pan, `'select'`는 드래그 영역 선택 zoom, `'both'`는 wheel/trackpad zoom과 드래그 영역 선택 zoom을 함께 사용합니다. `direction`은 `'x'`, `'y'`, `'xy'`를 지원합니다. string/point 축은 순서형 축이라 현재 zoom 대상에서 제외됩니다.
|
|
207
|
+
|
|
208
|
+
### OffscreenCanvas Worker Rendering
|
|
209
|
+
|
|
210
|
+
`createCanvasLineSeries`와 `createWebglLineSeries`는 `asyncRender` 옵션을 지원합니다. worker 파일에서는 `startKChartRenderWorker()`를 호출하고, series에는 worker를 만드는 factory를 넘깁니다.
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
// kchart-render.worker.ts
|
|
214
|
+
import { startKChartRenderWorker } from '@keneth80/k-chart';
|
|
215
|
+
|
|
216
|
+
startKChartRenderWorker();
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
createWebglLineSeries<Point>({
|
|
221
|
+
selector: 'trace',
|
|
222
|
+
xField: 'x',
|
|
223
|
+
yField: 'signal',
|
|
224
|
+
asyncRender: {
|
|
225
|
+
enabled: true,
|
|
226
|
+
workerFactory: () => new Worker(
|
|
227
|
+
new URL('./kchart-render.worker.ts', import.meta.url),
|
|
228
|
+
{ type: 'module' }
|
|
229
|
+
)
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
OffscreenCanvas를 지원하지 않거나 worker 생성에 실패하면 기존 메인 스레드 렌더러로 fallback됩니다.
|
|
235
|
+
|
|
236
|
+
## Display Options
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
createKChart({
|
|
240
|
+
selector: '#chart',
|
|
241
|
+
data,
|
|
242
|
+
title: { text: 'Hybrid Revenue And Volume', align: 'left' },
|
|
243
|
+
grid: { visible: true, y: true, x: false, dasharray: '2 6' },
|
|
244
|
+
legend: { visible: true, placement: 'top' },
|
|
245
|
+
tooltip: { visible: true },
|
|
246
|
+
axes: [
|
|
247
|
+
{ field: 'x', type: 'number', placement: 'bottom', title: 'Month Index' },
|
|
248
|
+
{ field: 'value', type: 'number', placement: 'left', title: 'Value' },
|
|
249
|
+
{ field: 'volume', type: 'number', placement: 'right', title: 'Volume' }
|
|
250
|
+
],
|
|
251
|
+
series: [
|
|
252
|
+
createLineSeries({ selector: 'value', xField: 'x', yField: 'value' }),
|
|
253
|
+
createCanvasPointSeries({ selector: 'volume', xField: 'x', yField: 'volume' })
|
|
254
|
+
]
|
|
255
|
+
});
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
The default tooltip shows the active series name and nearest x/y values. Provide `tooltip.formatter` for custom HTML/text.
|
|
259
|
+
|
|
260
|
+
## Option Factories
|
|
261
|
+
|
|
262
|
+
Series는 `series: [...]`로 받고, chart 부가 기능은 `options: [...]`로 받습니다.
|
|
263
|
+
|
|
264
|
+
```ts
|
|
265
|
+
createKChart({
|
|
266
|
+
selector: '#chart',
|
|
267
|
+
data,
|
|
268
|
+
axes,
|
|
269
|
+
series,
|
|
270
|
+
options: [
|
|
271
|
+
createSpecAreaOption([
|
|
272
|
+
{ start: 2, end: 3.5, label: 'STEP 02' }
|
|
273
|
+
]),
|
|
274
|
+
createGuideLineOption({
|
|
275
|
+
x: [{ value: 4, label: 'A' }],
|
|
276
|
+
y: [{ value: 58, label: 'HIGH' }]
|
|
277
|
+
}),
|
|
278
|
+
createCursorLineOption({
|
|
279
|
+
valueFormat: (value: number) => Number(value).toFixed(1)
|
|
280
|
+
})
|
|
281
|
+
]
|
|
282
|
+
});
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
기존 `specAreas`, `guideLines`, `cursorGuide`, `guideLine` 직접 필드도 호환됩니다. 새 코드에서는 option factory 사용을 권장합니다.
|
|
286
|
+
|
|
287
|
+
## Custom Series
|
|
288
|
+
|
|
289
|
+
기본 series가 부족하면 `createCustomSeries(...)`에 renderer 함수를 넘깁니다. Class 상속이나 interface 구현 없이 같은 확장 지점을 사용할 수 있습니다.
|
|
290
|
+
|
|
291
|
+
Renderer context 주요 필드:
|
|
292
|
+
|
|
293
|
+
- `group`: 이 series 전용 SVG group
|
|
294
|
+
- `data`: 현재 차트 데이터
|
|
295
|
+
- `scales`, `xScale`, `yScale`: 코어가 계산한 scale
|
|
296
|
+
- `size`, `plotSize`, `margin`: 전체 크기와 plot 영역
|
|
297
|
+
- `color`: series 기본 색상
|
|
298
|
+
- `getCanvas(name)`: Canvas 2D layer
|
|
299
|
+
- `getWebglCanvas(name)`: WebGL canvas layer
|
|
300
|
+
|
|
301
|
+
```ts
|
|
302
|
+
const circleSeries = createCustomSeries<CirclePoint>({
|
|
303
|
+
selector: 'custom-circle',
|
|
304
|
+
displayName: 'Custom Circle',
|
|
305
|
+
xField: 'x',
|
|
306
|
+
yField: 'y',
|
|
307
|
+
render({ group, data, xScale, yScale, color }) {
|
|
308
|
+
if (!xScale || !yScale) {
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
group.selectAll('.custom-circle')
|
|
313
|
+
.data(data)
|
|
314
|
+
.join('circle')
|
|
315
|
+
.attr('class', 'custom-circle')
|
|
316
|
+
.attr('cx', (point) => xScale.scale(point.x))
|
|
317
|
+
.attr('cy', (point) => yScale.scale(point.y))
|
|
318
|
+
.attr('r', (point) => point.radius)
|
|
319
|
+
.style('fill', color);
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
간단한 SVG 원 series 예제는 [examples/custom-circle-series.ts](../examples/custom-circle-series.ts)를 참고하세요.
|
|
325
|
+
|
|
326
|
+
## Canvas/WebGL Pattern
|
|
327
|
+
|
|
328
|
+
```ts
|
|
329
|
+
const canvasSeries = createCustomSeries<Point>({
|
|
330
|
+
selector: 'canvas-points',
|
|
331
|
+
xField: 'x',
|
|
332
|
+
yField: 'y',
|
|
333
|
+
render({ getCanvas, data, xScale, yScale, color }) {
|
|
334
|
+
if (!xScale || !yScale) {
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const canvas = getCanvas('points');
|
|
339
|
+
const context = canvas.getContext('2d');
|
|
340
|
+
if (!context) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
345
|
+
context.fillStyle = color;
|
|
346
|
+
|
|
347
|
+
data.forEach((point) => {
|
|
348
|
+
context.beginPath();
|
|
349
|
+
context.arc(xScale.scale(point.x), yScale.scale(point.y), 3, 0, Math.PI * 2);
|
|
350
|
+
context.fill();
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
## React / Next.js Pattern
|
|
357
|
+
|
|
358
|
+
DOM을 직접 사용하므로 Client Component에서 mount 이후 생성합니다. 자세한 wrapper 예제와 Next.js 설정은 [React And Next.js Guide](react-nextjs.md)를 참고합니다.
|
|
359
|
+
|
|
360
|
+
```tsx
|
|
361
|
+
'use client';
|
|
362
|
+
|
|
363
|
+
import { useEffect, useRef } from 'react';
|
|
364
|
+
import { createKChart, createLineSeries, KChartController } from '@keneth80/k-chart';
|
|
365
|
+
|
|
366
|
+
export function TrafficChart({ data }) {
|
|
367
|
+
const rootRef = useRef<HTMLDivElement | null>(null);
|
|
368
|
+
const chartRef = useRef<KChartController | null>(null);
|
|
369
|
+
|
|
370
|
+
useEffect(() => {
|
|
371
|
+
if (!rootRef.current) {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const chart = createKChart({
|
|
376
|
+
selector: rootRef.current,
|
|
377
|
+
data,
|
|
378
|
+
axes: [
|
|
379
|
+
{ field: 'time', type: 'string', placement: 'bottom' },
|
|
380
|
+
{ field: 'value', type: 'number', placement: 'left' }
|
|
381
|
+
],
|
|
382
|
+
series: [
|
|
383
|
+
createLineSeries({
|
|
384
|
+
selector: 'traffic',
|
|
385
|
+
xField: 'time',
|
|
386
|
+
yField: 'value'
|
|
387
|
+
})
|
|
388
|
+
]
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
chart.render();
|
|
392
|
+
chartRef.current = chart;
|
|
393
|
+
|
|
394
|
+
return () => {
|
|
395
|
+
chart.destroy();
|
|
396
|
+
chartRef.current = null;
|
|
397
|
+
};
|
|
398
|
+
}, []);
|
|
399
|
+
|
|
400
|
+
useEffect(() => {
|
|
401
|
+
chartRef.current?.updateData(data);
|
|
402
|
+
}, [data]);
|
|
403
|
+
|
|
404
|
+
return <div ref={rootRef} style={{ width: '100%', height: 420 }} />;
|
|
405
|
+
}
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
## Migration Checklist
|
|
409
|
+
|
|
410
|
+
1. `new BasicChart(...)` 호출을 `createKChart(...)`로 교체합니다.
|
|
411
|
+
2. class 상속 series를 `createCustomSeries({ render(context) { ... } })`로 옮깁니다.
|
|
412
|
+
3. 축/scale 계산은 runtime에 맡기고, renderer는 전달받은 `xScale`, `yScale`, `getCanvas`, `getWebglCanvas`만 사용합니다.
|
|
413
|
+
4. UI unmount 시 `chart.destroy()`를 호출합니다.
|
|
414
|
+
5. 새 패키지 public import는 `@keneth80/k-chart`의 함수형 API만 사용합니다.
|
|
415
|
+
|
|
416
|
+
## Refactor Status
|
|
417
|
+
|
|
418
|
+
완료:
|
|
419
|
+
|
|
420
|
+
- `src/kchart.ts` class-free runtime 추가
|
|
421
|
+
- SVG line series renderer 함수화
|
|
422
|
+
- Canvas line/point series renderer 함수화
|
|
423
|
+
- WebGL point series renderer 함수화
|
|
424
|
+
- title/grid/legend/axis title 옵션 추가
|
|
425
|
+
- 기본 nearest-point tooltip 옵션 추가
|
|
426
|
+
- Custom series renderer API 추가
|
|
427
|
+
- 새 public entry를 KChart runtime으로 축소
|
|
428
|
+
|
|
429
|
+
다음 단계:
|
|
430
|
+
|
|
431
|
+
- zoom을 runtime plugin 형태로 분리
|
|
432
|
+
- legacy demo를 새 runtime 예제로 교체
|