@keneth80/k-chart 1.0.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 ADDED
@@ -0,0 +1,23 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0
4
+
5
+ ### Added
6
+
7
+ - Published KChart as a class-free functional TypeScript chart runtime.
8
+ - Added SVG, Canvas, and WebGL series renderer factories.
9
+ - Added `createCustomSeries` for custom SVG, Canvas, and WebGL visualizations.
10
+ - Added chart options for spec areas, guide lines, cursor lines, legends, and tooltips.
11
+ - Added optional `crossFilter` support for large numeric/time zoom-in ranges.
12
+ - Added dark/light demo styling and chart style options.
13
+
14
+ ### Changed
15
+
16
+ - Refactored chart creation toward functional APIs.
17
+ - Exposed `KChartController` for UI-friendly integration and lifecycle control.
18
+ - Migrated build tooling to modern Babel, Webpack, and TypeScript versions.
19
+
20
+ ### Migration Notes
21
+
22
+ - Use imports from `@keneth80/k-chart`.
23
+ - Use `createKChart(...)` and series factory functions instead of class-based chart construction.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kenneth Kang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,484 @@
1
+ # KChart
2
+
3
+ KChart는 TypeScript 기반 D3 하이브리드 차트 엔진입니다.
4
+
5
+ 축, scale, layout은 차트 코어가 계산하고, 실제 시각 표현은 series renderer 함수가 담당합니다. Renderer는 같은 scale 정보를 받아 SVG, Canvas, WebGL 레이어 중 원하는 방식으로 그릴 수 있습니다. 새 저장소 기준 public API는 class-free 함수형 런타임입니다.
6
+
7
+ ## Core Concept
8
+
9
+ ```txt
10
+ createKChart(config)
11
+ ├─ axes -> scales
12
+ ├─ svg/canvas/webgl layers
13
+ └─ series[]
14
+ ├─ createLineSeries(...)
15
+ ├─ createCanvasLineSeries(...)
16
+ ├─ createCanvasPointSeries(...)
17
+ ├─ createWebglLineSeries(...)
18
+ ├─ createWebglPointSeries(...)
19
+ └─ createCustomSeries({ render(context) })
20
+ ```
21
+
22
+ - `createKChart(...)`는 class 없이 plain runtime state와 controller를 만듭니다.
23
+ - `createLineSeries(...)`는 SVG line renderer를 함수형 series로 생성합니다.
24
+ - `createCanvasLineSeries(...)`, `createCanvasPointSeries(...)`는 Canvas 2D renderer를 함수형 series로 생성합니다.
25
+ - `createWebglLineSeries(...)`는 대용량 line renderer를 WebGL `LINE_STRIP` 기반 함수형 series로 생성합니다.
26
+ - `createWebglPointSeries(...)`는 WebGL point renderer를 함수형 series로 생성합니다.
27
+ - `createCustomSeries(...)`는 renderer 함수를 그대로 series로 사용합니다.
28
+ - `render(context)`는 `group`, `data`, `scales`, `xScale`, `yScale`, `plotSize`, `color`, `getCanvas`, `getWebglCanvas`를 받습니다.
29
+ - Canvas/WebGL 시각화도 renderer 안에서 필요한 layer를 받아 직접 그릴 수 있습니다.
30
+
31
+ ## Install
32
+
33
+ ```bash
34
+ npm install @keneth80/k-chart
35
+ ```
36
+
37
+ ## Local Development
38
+
39
+ ```bash
40
+ npm install
41
+ npm run build
42
+ ```
43
+
44
+ Demo:
45
+
46
+ ```bash
47
+ npm run dev
48
+ ```
49
+
50
+ Then open `http://127.0.0.1:9003/`.
51
+
52
+ ## Quick Start
53
+
54
+ ```ts
55
+ import {
56
+ createKChart,
57
+ createLineSeries,
58
+ KChartController
59
+ } from '@keneth80/k-chart';
60
+
61
+ interface TrafficPoint {
62
+ time: string;
63
+ value: number;
64
+ }
65
+
66
+ const data: TrafficPoint[] = [
67
+ { time: '00:00', value: 12 },
68
+ { time: '01:00', value: 18 },
69
+ { time: '02:00', value: 21 }
70
+ ];
71
+
72
+ const chart: KChartController<TrafficPoint> = createKChart<TrafficPoint>({
73
+ selector: '#chart',
74
+ data,
75
+ axes: [
76
+ { field: 'time', type: 'string', placement: 'bottom' },
77
+ { field: 'value', type: 'number', placement: 'left' }
78
+ ],
79
+ series: [
80
+ createLineSeries({
81
+ selector: 'traffic-line',
82
+ displayName: 'Traffic',
83
+ xField: 'time',
84
+ yField: 'value',
85
+ curve: true,
86
+ dot: true
87
+ })
88
+ ]
89
+ });
90
+
91
+ chart.render();
92
+ ```
93
+
94
+ ## Custom Series
95
+
96
+ 아래 예제는 차트 코어가 계산한 x/y scale을 받아 SVG 원을 그립니다. Class 상속이나 interface 구현 없이 renderer 함수만 넘깁니다.
97
+
98
+ ```ts
99
+ import {
100
+ createKChart,
101
+ createCustomSeries
102
+ } from '@keneth80/k-chart';
103
+
104
+ interface CirclePoint {
105
+ x: number;
106
+ y: number;
107
+ radius: number;
108
+ }
109
+
110
+ const circleSeries = createCustomSeries<CirclePoint>({
111
+ selector: 'custom-circle',
112
+ displayName: 'Custom Circle',
113
+ xField: 'x',
114
+ yField: 'y',
115
+ render({ group, data, xScale, yScale, color }) {
116
+ if (!xScale || !yScale) {
117
+ return;
118
+ }
119
+
120
+ group.selectAll('.custom-circle')
121
+ .data(data)
122
+ .join('circle')
123
+ .attr('class', 'custom-circle')
124
+ .attr('cx', (point) => xScale.scale(point.x))
125
+ .attr('cy', (point) => yScale.scale(point.y))
126
+ .attr('r', (point) => point.radius)
127
+ .style('fill', color)
128
+ .style('fill-opacity', 0.6);
129
+ }
130
+ });
131
+
132
+ createKChart<CirclePoint>({
133
+ selector: '#chart',
134
+ data: [
135
+ { x: 1, y: 12, radius: 8 },
136
+ { x: 2, y: 18, radius: 12 },
137
+ { x: 3, y: 9, radius: 6 }
138
+ ],
139
+ axes: [
140
+ { field: 'x', type: 'number', placement: 'bottom', min: 0, max: 4 },
141
+ { field: 'y', type: 'number', placement: 'left', min: 0, max: 24 }
142
+ ],
143
+ series: [
144
+ circleSeries
145
+ ]
146
+ }).render();
147
+ ```
148
+
149
+ Full example: [examples/custom-circle-series.ts](examples/custom-circle-series.ts)
150
+
151
+ ## Canvas Or WebGL Renderer
152
+
153
+ 기본 Canvas/WebGL renderer는 factory로 바로 사용할 수 있습니다.
154
+
155
+ ```ts
156
+ import {
157
+ createCanvasLineSeries,
158
+ createCanvasPointSeries,
159
+ createWebglLineSeries,
160
+ createWebglPointSeries
161
+ } from '@keneth80/k-chart';
162
+
163
+ const canvasLine = createCanvasLineSeries<Point>({
164
+ selector: 'canvas-line',
165
+ xField: 'x',
166
+ yField: 'y',
167
+ color: '#56d08f',
168
+ lineWidth: 3
169
+ });
170
+
171
+ const canvasPoints = createCanvasPointSeries<Point>({
172
+ selector: 'canvas-points',
173
+ xField: 'x',
174
+ yField: 'y',
175
+ radius: 4,
176
+ color: '#5db8ff'
177
+ });
178
+
179
+ const webglPoints = createWebglPointSeries<Point>({
180
+ selector: 'webgl-points',
181
+ xField: 'x',
182
+ yField: 'y',
183
+ pointSize: 8,
184
+ color: '#f3b45b'
185
+ });
186
+
187
+ const webglLine = createWebglLineSeries<Point>({
188
+ selector: 'webgl-large-line',
189
+ displayName: '120k WebGL Line',
190
+ xField: 'x',
191
+ yField: 'y',
192
+ color: '#5db8ff',
193
+ lineWidth: 1
194
+ });
195
+ ```
196
+
197
+ 대용량 line 예제에서는 축 tick 수를 줄여 라벨 겹침을 피할 수 있습니다.
198
+
199
+ ```ts
200
+ const largeData = Array.from({ length: 120000 }, (_, index) => ({
201
+ x: index,
202
+ y: 52 + Math.sin(index / 120) * 22 + Math.cos(index / 43) * 8
203
+ }));
204
+
205
+ createKChart({
206
+ selector: '#chart',
207
+ data: largeData,
208
+ axes: [
209
+ {
210
+ field: 'x',
211
+ type: 'number',
212
+ placement: 'bottom',
213
+ min: 0,
214
+ max: 119999,
215
+ tickCount: 6,
216
+ tickFormat: (value: number) => Math.round(value / 1000) + 'k'
217
+ },
218
+ { field: 'y', type: 'number', placement: 'left', min: 0, max: 100, tickCount: 5 }
219
+ ],
220
+ series: [webglLine]
221
+ }).render();
222
+ ```
223
+
224
+ 직접 제어가 필요하면 `render(context)` 안에서 Canvas/WebGL layer를 받을 수도 있습니다.
225
+
226
+ ```ts
227
+ const customCanvasSeries = createCustomSeries<Point>({
228
+ selector: 'custom-canvas-points',
229
+ xField: 'x',
230
+ yField: 'y',
231
+ render({ getCanvas, data, xScale, yScale, color }) {
232
+ if (!xScale || !yScale) {
233
+ return;
234
+ }
235
+
236
+ const canvas = getCanvas('points');
237
+ const context = canvas.getContext('2d');
238
+ if (!context) {
239
+ return;
240
+ }
241
+
242
+ context.clearRect(0, 0, canvas.width, canvas.height);
243
+ context.fillStyle = color;
244
+ data.forEach((point) => {
245
+ context.beginPath();
246
+ context.arc(xScale.scale(point.x), yScale.scale(point.y), 3, 0, Math.PI * 2);
247
+ context.fill();
248
+ });
249
+ }
250
+ });
251
+ ```
252
+
253
+ ## Controller API
254
+
255
+ `createKChart(...)` returns `KChartController<T>`.
256
+
257
+ | Method | Description |
258
+ | --- | --- |
259
+ | `render()` | Render the chart. |
260
+ | `updateData(data)` | Replace data and render again. |
261
+ | `updateAxes(axes)` | Replace axes and render again. |
262
+ | `updateSeries(series)` | Replace series and render again. |
263
+ | `resize(size?)` | Recalculate size and render again. |
264
+ | `destroy()` | Remove SVG/Canvas resources. |
265
+ | `getState()` | Read current runtime state. |
266
+
267
+ ## Chart Options
268
+
269
+ `createKChart(...)` supports layout and display options for common playground-style charts.
270
+
271
+ ```ts
272
+ createKChart({
273
+ selector: '#chart',
274
+ data,
275
+ margin: { top: 28, right: 52, bottom: 44, left: 52 },
276
+ title: {
277
+ text: 'Hybrid Revenue And Volume',
278
+ align: 'left'
279
+ },
280
+ grid: {
281
+ visible: true,
282
+ x: false,
283
+ y: true,
284
+ dasharray: '2 6'
285
+ },
286
+ legend: {
287
+ visible: true,
288
+ placement: 'top',
289
+ selectable: true
290
+ },
291
+ tooltip: {
292
+ visible: true
293
+ },
294
+ axes: [
295
+ { field: 'x', type: 'number', placement: 'bottom', title: 'Month Index' },
296
+ { field: 'value', type: 'number', placement: 'left', title: 'Value' },
297
+ { field: 'volume', type: 'number', placement: 'right', title: 'Volume' }
298
+ ],
299
+ series: [
300
+ createLineSeries({ selector: 'value', xField: 'x', yField: 'value' }),
301
+ createCanvasPointSeries({ selector: 'volume', xField: 'x', yField: 'volume' })
302
+ ]
303
+ });
304
+ ```
305
+
306
+ Legend item은 기본적으로 선택 가능합니다. 여러 series를 넣으면 legend checkbox를 클릭해 SVG, Canvas, WebGL series를 개별적으로 숨기거나 다시 표시할 수 있습니다. `selectable: false`를 주면 표시 전용 legend로 동작합니다.
307
+
308
+ ```ts
309
+ series: [
310
+ createLineSeries({ selector: 'value-line', displayName: 'Value', xField: 'x', yField: 'value' }),
311
+ createLineSeries({ selector: 'volume-line', displayName: 'Volume', xField: 'x', yField: 'volume' }),
312
+ createLineSeries({ selector: 'extra-line', displayName: 'Extra', xField: 'x', yField: 'extra' })
313
+ ]
314
+ ```
315
+
316
+ ## Spec Areas, Fixed Guide Lines, And Cursor Guide
317
+
318
+ 대용량 trace/WebGL 차트처럼 공정 단계나 관심 구간을 표시해야 할 때 `specAreas`를 사용할 수 있습니다.
319
+
320
+ 옵션 기능은 series처럼 `options` 배열로 지정합니다. `createSpecAreaOption`, `createGuideLineOption`, `createCursorLineOption`을 조합하면 x/y 기준선, 영역, 마우스 추적선을 필요한 만큼 붙일 수 있습니다.
321
+
322
+ ```ts
323
+ import {
324
+ createCursorLineOption,
325
+ createGuideLineOption,
326
+ createKChart,
327
+ createSpecAreaOption,
328
+ createWebglLineSeries
329
+ } from '@keneth80/k-chart';
330
+
331
+ createKChart({
332
+ selector: '#chart',
333
+ data,
334
+ options: [
335
+ createSpecAreaOption([
336
+ { start: 18000, end: 36000, label: 'STEP 02', color: 'rgba(216, 118, 255, 0.16)' },
337
+ { start: 62000, end: 78000, label: 'STEP 05', color: 'rgba(93, 184, 255, 0.13)' }
338
+ ]),
339
+ createGuideLineOption({
340
+ x: [
341
+ { value: 1200, label: '1' },
342
+ { value: 6200, label: '2' },
343
+ { value: 24000, label: '3' },
344
+ { value: 64000, label: '4' }
345
+ ],
346
+ y: [
347
+ { value: 80, label: 'upper', color: 'rgba(255, 107, 138, 0.55)' }
348
+ ]
349
+ }),
350
+ createCursorLineOption({
351
+ xFormat: (value: number) => `${Math.round(value / 1000)}k`,
352
+ valueFormat: (value: number) => Number(value).toFixed(1)
353
+ })
354
+ ],
355
+ series: [
356
+ createWebglLineSeries({ selector: 'trace-1', displayName: 'Trace 1', xField: 'x', yField: 'signal0' }),
357
+ createWebglLineSeries({ selector: 'trace-2', displayName: 'Trace 2', xField: 'x', yField: 'signal1' })
358
+ ]
359
+ }).render();
360
+ ```
361
+
362
+ `createCursorLineOption`은 마우스 위치를 따라 가장 가까운 x 위치의 series 값을 읽어 보여주는 inspect overlay입니다. 기존 `specAreas`, `guideLines`, `cursorGuide`, `guideLine` 직접 필드는 호환을 위해 남아 있지만, 새 코드에서는 `options` 배열을 권장합니다.
363
+
364
+ `tooltip.formatter` can be used when the default series/x/y text is not enough.
365
+
366
+ ```ts
367
+ tooltip: {
368
+ visible: true,
369
+ formatter: ({ data, series }) => `${series.displayName}: ${data.value}`
370
+ }
371
+ ```
372
+
373
+ Custom renderer가 한 series 안에서 여러 SVG/Canvas/WebGL 요소를 그리는 경우에는 series 단위 `tooltip(context)` hook을 사용할 수 있습니다. 예를 들어 stacked column, range bar, box plot처럼 하나의 datum에서 여러 시각 요소가 나올 때 core tooltip overlay는 이 hook이 돌려준 위치와 HTML을 그대로 사용합니다.
374
+
375
+ ```ts
376
+ const stackedSeries = createCustomSeries<Point>({
377
+ selector: 'stacked-column',
378
+ displayName: 'Stacked Column',
379
+ xField: 'x',
380
+ yField: 'value',
381
+ render({ group, data, xScale, yScale }) {
382
+ // draw stacked rects with the chart scales
383
+ },
384
+ tooltip({ data, scales, mouseX, mouseY }) {
385
+ const xScale = scales.find((scale) => scale.field === 'x');
386
+ const yScale = scales.find((scale) => scale.field === 'value');
387
+ if (!xScale || !yScale) {
388
+ return undefined;
389
+ }
390
+
391
+ const hit = data.find((point) => {
392
+ const x = xScale.scale(point.x);
393
+ return Math.abs(mouseX - x) < 12;
394
+ });
395
+ if (!hit) {
396
+ return undefined;
397
+ }
398
+
399
+ return {
400
+ data: hit,
401
+ x: xScale.scale(hit.x),
402
+ y: yScale.scale(hit.value),
403
+ distance: 0,
404
+ html: `<strong>${hit.label}</strong><br/>A: ${hit.a}<br/>B: ${hit.b}`
405
+ };
406
+ }
407
+ });
408
+ ```
409
+
410
+ ## React / Next.js
411
+
412
+ KChart touches the DOM, so create it in a Client Component after mount.
413
+
414
+ ```tsx
415
+ 'use client';
416
+
417
+ import { useEffect, useRef } from 'react';
418
+ import { createKChart, createLineSeries, KChartController } from '@keneth80/k-chart';
419
+
420
+ export function TrafficChart({ data }) {
421
+ const rootRef = useRef<HTMLDivElement | null>(null);
422
+ const chartRef = useRef<KChartController | null>(null);
423
+
424
+ useEffect(() => {
425
+ if (!rootRef.current) {
426
+ return;
427
+ }
428
+
429
+ const chart = createKChart({
430
+ selector: rootRef.current,
431
+ data,
432
+ axes: [
433
+ { field: 'time', type: 'string', placement: 'bottom' },
434
+ { field: 'value', type: 'number', placement: 'left' }
435
+ ],
436
+ series: [
437
+ createLineSeries({
438
+ selector: 'traffic',
439
+ xField: 'time',
440
+ yField: 'value'
441
+ })
442
+ ]
443
+ });
444
+
445
+ chart.render();
446
+ chartRef.current = chart;
447
+
448
+ return () => {
449
+ chart.destroy();
450
+ chartRef.current = null;
451
+ };
452
+ }, []);
453
+
454
+ useEffect(() => {
455
+ chartRef.current?.updateData(data);
456
+ }, [data]);
457
+
458
+ return <div ref={rootRef} style={{ width: '100%', height: 420 }} />;
459
+ }
460
+ ```
461
+
462
+ ## Migration Status
463
+
464
+ The public runtime is `src/kchart.ts`.
465
+
466
+ The legacy class-based chart source has been removed from the new runtime source tree. New code should use:
467
+
468
+ - `createKChart`
469
+ - `createLineSeries`
470
+ - `createCanvasLineSeries`
471
+ - `createCanvasPointSeries`
472
+ - `createWebglPointSeries`
473
+ - `createCustomSeries`
474
+ - `createSpecAreaOption`
475
+ - `createGuideLineOption`
476
+ - `createCursorLineOption`
477
+ - `KChartController`
478
+
479
+ The next migration target is adding zoom/pan and richer tooltip interaction on top of this class-free runtime.
480
+
481
+ ## Docs
482
+
483
+ - [Functional API](docs/functional-api.md)
484
+ - [Refactor Plan](docs/refactor-plan.md)
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './kchart';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./kchart"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB"}