@keneth80/k-chart 1.5.0 → 1.7.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.
Files changed (112) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/README.md +119 -1
  3. package/docs/code-graph-handoff.md +67 -3
  4. package/docs/functional-api.md +112 -3
  5. package/docs/react-nextjs.md +0 -6
  6. package/examples/svg-globe-map-series.ts +11 -0
  7. package/examples/three-constellation-series.ts +375 -0
  8. package/lib/core/contracts.d.ts +452 -0
  9. package/lib/core/contracts.d.ts.map +1 -0
  10. package/lib/core/contracts.js +3 -0
  11. package/lib/core/contracts.js.map +1 -0
  12. package/lib/core/create-kchart.d.ts +4 -0
  13. package/lib/core/create-kchart.d.ts.map +1 -0
  14. package/lib/core/create-kchart.js +1344 -0
  15. package/lib/core/create-kchart.js.map +1 -0
  16. package/lib/core/index.d.ts +3 -0
  17. package/lib/core/index.d.ts.map +1 -0
  18. package/lib/core/index.js +19 -0
  19. package/lib/core/index.js.map +1 -0
  20. package/lib/index.d.ts +5 -1
  21. package/lib/index.d.ts.map +1 -1
  22. package/lib/index.js +5 -1
  23. package/lib/index.js.map +1 -1
  24. package/lib/kchart.d.ts +11 -414
  25. package/lib/kchart.d.ts.map +1 -1
  26. package/lib/kchart.js +24 -2660
  27. package/lib/kchart.js.map +1 -1
  28. package/lib/options/cursor-line.d.ts +4 -0
  29. package/lib/options/cursor-line.d.ts.map +1 -0
  30. package/lib/options/cursor-line.js +36 -0
  31. package/lib/options/cursor-line.js.map +1 -0
  32. package/lib/options/guide-line.d.ts +5 -0
  33. package/lib/options/guide-line.d.ts.map +1 -0
  34. package/lib/options/guide-line.js +129 -0
  35. package/lib/options/guide-line.js.map +1 -0
  36. package/lib/options/index.d.ts +5 -0
  37. package/lib/options/index.d.ts.map +1 -0
  38. package/lib/options/index.js +21 -0
  39. package/lib/options/index.js.map +1 -0
  40. package/lib/options/spec-area.d.ts +5 -0
  41. package/lib/options/spec-area.d.ts.map +1 -0
  42. package/lib/options/spec-area.js +112 -0
  43. package/lib/options/spec-area.js.map +1 -0
  44. package/lib/options/tooltip-note.d.ts +15 -0
  45. package/lib/options/tooltip-note.d.ts.map +1 -0
  46. package/lib/options/tooltip-note.js +265 -0
  47. package/lib/options/tooltip-note.js.map +1 -0
  48. package/lib/series/canvas-candlestick.d.ts +3 -0
  49. package/lib/series/canvas-candlestick.d.ts.map +1 -0
  50. package/lib/series/canvas-candlestick.js +204 -0
  51. package/lib/series/canvas-candlestick.js.map +1 -0
  52. package/lib/series/canvas-line.d.ts +3 -0
  53. package/lib/series/canvas-line.d.ts.map +1 -0
  54. package/lib/series/canvas-line.js +67 -0
  55. package/lib/series/canvas-line.js.map +1 -0
  56. package/lib/series/canvas-point.d.ts +3 -0
  57. package/lib/series/canvas-point.d.ts.map +1 -0
  58. package/lib/series/canvas-point.js +52 -0
  59. package/lib/series/canvas-point.js.map +1 -0
  60. package/lib/series/custom.d.ts +3 -0
  61. package/lib/series/custom.d.ts.map +1 -0
  62. package/lib/series/custom.js +6 -0
  63. package/lib/series/custom.js.map +1 -0
  64. package/lib/series/index.d.ts +9 -0
  65. package/lib/series/index.d.ts.map +1 -0
  66. package/lib/series/index.js +25 -0
  67. package/lib/series/index.js.map +1 -0
  68. package/lib/series/support/canvas.d.ts +19 -0
  69. package/lib/series/support/canvas.d.ts.map +1 -0
  70. package/lib/series/support/canvas.js +130 -0
  71. package/lib/series/support/canvas.js.map +1 -0
  72. package/lib/series/support/scale.d.ts +3 -0
  73. package/lib/series/support/scale.d.ts.map +1 -0
  74. package/lib/series/support/scale.js +14 -0
  75. package/lib/series/support/scale.js.map +1 -0
  76. package/lib/series/support/webgl.d.ts +9 -0
  77. package/lib/series/support/webgl.d.ts.map +1 -0
  78. package/lib/series/support/webgl.js +92 -0
  79. package/lib/series/support/webgl.js.map +1 -0
  80. package/lib/series/svg-globe.d.ts +3 -0
  81. package/lib/series/svg-globe.d.ts.map +1 -0
  82. package/lib/series/svg-globe.js +964 -0
  83. package/lib/series/svg-globe.js.map +1 -0
  84. package/lib/series/svg-line.d.ts +3 -0
  85. package/lib/series/svg-line.d.ts.map +1 -0
  86. package/lib/series/svg-line.js +66 -0
  87. package/lib/series/svg-line.js.map +1 -0
  88. package/lib/series/webgl-line.d.ts +3 -0
  89. package/lib/series/webgl-line.d.ts.map +1 -0
  90. package/lib/series/webgl-line.js +67 -0
  91. package/lib/series/webgl-line.js.map +1 -0
  92. package/lib/series/webgl-point.d.ts +3 -0
  93. package/lib/series/webgl-point.d.ts.map +1 -0
  94. package/lib/series/webgl-point.js +63 -0
  95. package/lib/series/webgl-point.js.map +1 -0
  96. package/lib/utils/downsample-lttb.d.ts +2 -0
  97. package/lib/utils/downsample-lttb.d.ts.map +1 -0
  98. package/lib/utils/downsample-lttb.js +60 -0
  99. package/lib/utils/downsample-lttb.js.map +1 -0
  100. package/lib/utils/index.d.ts +2 -0
  101. package/lib/utils/index.d.ts.map +1 -0
  102. package/lib/utils/index.js +18 -0
  103. package/lib/utils/index.js.map +1 -0
  104. package/lib/worker/index.d.ts +2 -0
  105. package/lib/worker/index.d.ts.map +1 -0
  106. package/lib/worker/index.js +18 -0
  107. package/lib/worker/index.js.map +1 -0
  108. package/lib/worker/render-worker.d.ts +2 -0
  109. package/lib/worker/render-worker.d.ts.map +1 -0
  110. package/lib/worker/render-worker.js +116 -0
  111. package/lib/worker/render-worker.js.map +1 -0
  112. package/package.json +24 -9
@@ -0,0 +1,375 @@
1
+ import * as THREE from 'three';
2
+ import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
3
+ import {
4
+ createCustomSeries,
5
+ KChartSeries
6
+ } from '../src';
7
+
8
+ export interface ConstellationNode {
9
+ id: string;
10
+ label: string;
11
+ x: number;
12
+ y: number;
13
+ z: number;
14
+ size: number;
15
+ color?: string;
16
+ }
17
+
18
+ export interface ConstellationLink {
19
+ source: string;
20
+ target: string;
21
+ }
22
+
23
+ export interface ThreeConstellationSeriesConfiguration<T> {
24
+ selector: string;
25
+ displayName?: string;
26
+ idField: keyof T & string;
27
+ labelField: keyof T & string;
28
+ xField: keyof T & string;
29
+ yField: keyof T & string;
30
+ zField: keyof T & string;
31
+ sizeField?: keyof T & string;
32
+ colorField?: keyof T & string;
33
+ links: ConstellationLink[];
34
+ canvasName?: string;
35
+ lineColor?: string;
36
+ backgroundColor?: string;
37
+ autoRotate?: boolean;
38
+ onNodeClick?: (node: T) => void;
39
+ }
40
+
41
+ const disposeObject = (object: THREE.Object3D): void => {
42
+ object.traverse((child) => {
43
+ const mesh = child as THREE.Mesh;
44
+ mesh.geometry?.dispose();
45
+ const materials = Array.isArray(mesh.material) ? mesh.material : [mesh.material];
46
+ materials.filter(Boolean).forEach((material) => {
47
+ (material as THREE.MeshBasicMaterial).map?.dispose();
48
+ material.dispose();
49
+ });
50
+ });
51
+ };
52
+
53
+ const createGlowTexture = (): THREE.CanvasTexture => {
54
+ const glowCanvas = document.createElement('canvas');
55
+ glowCanvas.width = 128;
56
+ glowCanvas.height = 128;
57
+ const context = glowCanvas.getContext('2d');
58
+ if (context) {
59
+ const gradient = context.createRadialGradient(64, 64, 4, 64, 64, 62);
60
+ gradient.addColorStop(0, 'rgba(255, 255, 255, 1)');
61
+ gradient.addColorStop(0.16, 'rgba(226, 243, 255, 0.92)');
62
+ gradient.addColorStop(0.42, 'rgba(134, 217, 255, 0.42)');
63
+ gradient.addColorStop(1, 'rgba(134, 217, 255, 0)');
64
+ context.fillStyle = gradient;
65
+ context.fillRect(0, 0, 128, 128);
66
+ }
67
+ const texture = new THREE.CanvasTexture(glowCanvas);
68
+ texture.colorSpace = THREE.SRGBColorSpace;
69
+ return texture;
70
+ };
71
+
72
+ export const createThreeConstellationSeries = <T = ConstellationNode>(
73
+ configuration: ThreeConstellationSeriesConfiguration<T>
74
+ ): KChartSeries<T> => {
75
+ let canvas: HTMLCanvasElement | undefined;
76
+ let renderer: THREE.WebGLRenderer | undefined;
77
+ let scene: THREE.Scene | undefined;
78
+ let camera: THREE.PerspectiveCamera | undefined;
79
+ let controls: OrbitControls | undefined;
80
+ let constellation: THREE.Group | undefined;
81
+ let stars: THREE.InstancedMesh | undefined;
82
+ let nodes: T[] = [];
83
+ let selectedIndex = -1;
84
+ let hoveredIndex = -1;
85
+ let animationFrame = 0;
86
+ let infoLabel: HTMLDivElement | undefined;
87
+ const pointer = new THREE.Vector2();
88
+ const raycaster = new THREE.Raycaster();
89
+
90
+ const setNodeColors = (): void => {
91
+ if (!stars) {
92
+ return;
93
+ }
94
+ nodes.forEach((node, index) => {
95
+ const configuredColor = configuration.colorField
96
+ ? node[configuration.colorField]
97
+ : undefined;
98
+ const baseColor = new THREE.Color(
99
+ String(configuredColor ?? '#e8f5ff')
100
+ );
101
+ const color = index === selectedIndex
102
+ ? new THREE.Color('#9edcff')
103
+ : index === hoveredIndex
104
+ ? new THREE.Color('#ffffff')
105
+ : baseColor;
106
+ stars?.setColorAt(index, color);
107
+ });
108
+ if (stars.instanceColor) {
109
+ stars.instanceColor.needsUpdate = true;
110
+ }
111
+ };
112
+
113
+ const resolvePointerIndex = (event: PointerEvent): number => {
114
+ if (!canvas || !camera || !stars) {
115
+ return -1;
116
+ }
117
+ const bounds = canvas.getBoundingClientRect();
118
+ pointer.set(
119
+ ((event.clientX - bounds.left) / Math.max(bounds.width, 1)) * 2 - 1,
120
+ -((event.clientY - bounds.top) / Math.max(bounds.height, 1)) * 2 + 1
121
+ );
122
+ raycaster.setFromCamera(pointer, camera);
123
+ return raycaster.intersectObject(stars, false)[0]?.instanceId ?? -1;
124
+ };
125
+
126
+ const handlePointerMove = (event: PointerEvent): void => {
127
+ const nextIndex = resolvePointerIndex(event);
128
+ if (nextIndex === hoveredIndex) {
129
+ return;
130
+ }
131
+ hoveredIndex = nextIndex;
132
+ if (canvas) {
133
+ canvas.style.cursor = hoveredIndex >= 0 ? 'pointer' : 'grab';
134
+ }
135
+ if (infoLabel) {
136
+ const node = nodes[hoveredIndex];
137
+ infoLabel.textContent = node
138
+ ? String(node[configuration.labelField])
139
+ : 'Drag to rotate · Wheel to zoom';
140
+ }
141
+ setNodeColors();
142
+ };
143
+
144
+ const handleClick = (event: MouseEvent): void => {
145
+ const nextIndex = resolvePointerIndex(event as PointerEvent);
146
+ if (nextIndex < 0) {
147
+ return;
148
+ }
149
+ selectedIndex = nextIndex;
150
+ setNodeColors();
151
+ configuration.onNodeClick?.(nodes[nextIndex]);
152
+ };
153
+
154
+ const initialize = (nextCanvas: HTMLCanvasElement): void => {
155
+ canvas = nextCanvas;
156
+ canvas.style.pointerEvents = 'auto';
157
+ canvas.style.zIndex = '4';
158
+ canvas.style.cursor = 'grab';
159
+
160
+ renderer = new THREE.WebGLRenderer({
161
+ canvas,
162
+ antialias: true,
163
+ alpha: true
164
+ });
165
+ renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
166
+ renderer.outputColorSpace = THREE.SRGBColorSpace;
167
+ renderer.setClearColor(configuration.backgroundColor ?? '#08111f', 1);
168
+
169
+ scene = new THREE.Scene();
170
+ scene.fog = new THREE.Fog(configuration.backgroundColor ?? '#08111f', 9, 18);
171
+ camera = new THREE.PerspectiveCamera(45, 1, 0.1, 100);
172
+ camera.position.set(0, 0.6, 8.5);
173
+
174
+ controls = new OrbitControls(camera, canvas);
175
+ controls.enableDamping = true;
176
+ controls.dampingFactor = 0.06;
177
+ controls.enablePan = false;
178
+ controls.minDistance = 4;
179
+ controls.maxDistance = 14;
180
+ controls.autoRotate = configuration.autoRotate ?? true;
181
+ controls.autoRotateSpeed = 0.28;
182
+
183
+ canvas.addEventListener('pointermove', handlePointerMove);
184
+ canvas.addEventListener('click', handleClick);
185
+
186
+ infoLabel = document.createElement('div');
187
+ infoLabel.className = 'kchart-three-constellation-label';
188
+ infoLabel.textContent = 'Drag to rotate · Wheel to zoom';
189
+ canvas.parentElement?.appendChild(infoLabel);
190
+
191
+ const animate = (): void => {
192
+ controls?.update();
193
+ if (scene && camera) {
194
+ renderer?.render(scene, camera);
195
+ }
196
+ animationFrame = window.requestAnimationFrame(animate);
197
+ };
198
+ animate();
199
+ };
200
+
201
+ const rebuildConstellation = (data: T[]): void => {
202
+ if (!scene) {
203
+ return;
204
+ }
205
+ if (constellation) {
206
+ scene.remove(constellation);
207
+ disposeObject(constellation);
208
+ }
209
+
210
+ nodes = data;
211
+ selectedIndex = Math.min(selectedIndex, nodes.length - 1);
212
+ hoveredIndex = -1;
213
+ constellation = new THREE.Group();
214
+ scene.add(constellation);
215
+
216
+ const positions = new Map<string, THREE.Vector3>();
217
+ const matrix = new THREE.Matrix4();
218
+ const starGeometry = new THREE.SphereGeometry(0.09, 16, 12);
219
+ const starMaterial = new THREE.MeshBasicMaterial({
220
+ color: '#ffffff',
221
+ vertexColors: true,
222
+ toneMapped: false
223
+ });
224
+ stars = new THREE.InstancedMesh(starGeometry, starMaterial, nodes.length);
225
+ const glowTexture = createGlowTexture();
226
+ const glowGroup = new THREE.Group();
227
+
228
+ nodes.forEach((node, index) => {
229
+ const position = new THREE.Vector3(
230
+ Number(node[configuration.xField]),
231
+ Number(node[configuration.yField]),
232
+ Number(node[configuration.zField])
233
+ );
234
+ const size = configuration.sizeField
235
+ ? Math.max(Number(node[configuration.sizeField]) || 1, 0.4)
236
+ : 1;
237
+ positions.set(String(node[configuration.idField]), position);
238
+ matrix.makeScale(size, size, size);
239
+ matrix.setPosition(position);
240
+ stars?.setMatrixAt(index, matrix);
241
+
242
+ const configuredColor = configuration.colorField
243
+ ? node[configuration.colorField]
244
+ : undefined;
245
+ const glow = new THREE.Sprite(new THREE.SpriteMaterial({
246
+ map: glowTexture.clone(),
247
+ color: String(configuredColor ?? '#e8f5ff'),
248
+ transparent: true,
249
+ opacity: 0.9,
250
+ depthWrite: false,
251
+ blending: THREE.AdditiveBlending,
252
+ toneMapped: false
253
+ }));
254
+ glow.position.copy(position);
255
+ glow.scale.setScalar(size * 0.72);
256
+ glowGroup.add(glow);
257
+ });
258
+ stars.instanceMatrix.needsUpdate = true;
259
+ setNodeColors();
260
+ constellation.add(stars);
261
+ constellation.add(glowGroup);
262
+ glowTexture.dispose();
263
+
264
+ const linePositions: number[] = [];
265
+ configuration.links.forEach((link) => {
266
+ const source = positions.get(link.source);
267
+ const target = positions.get(link.target);
268
+ if (source && target) {
269
+ linePositions.push(source.x, source.y, source.z, target.x, target.y, target.z);
270
+ }
271
+ });
272
+ const lineGeometry = new THREE.BufferGeometry();
273
+ lineGeometry.setAttribute(
274
+ 'position',
275
+ new THREE.Float32BufferAttribute(linePositions, 3)
276
+ );
277
+ const lines = new THREE.LineSegments(
278
+ lineGeometry,
279
+ new THREE.LineBasicMaterial({
280
+ color: configuration.lineColor ?? '#5db8ff',
281
+ transparent: true,
282
+ opacity: 0.58
283
+ })
284
+ );
285
+ constellation.add(lines);
286
+
287
+ const backgroundGeometry = new THREE.BufferGeometry();
288
+ const backgroundPositions: number[] = [];
289
+ let seed = 17;
290
+ for (let index = 0; index < 280; index += 1) {
291
+ seed = (seed * 9301 + 49297) % 233280;
292
+ const radius = 6 + (seed / 233280) * 7;
293
+ const phi = ((index * 137.5) % 360) * Math.PI / 180;
294
+ const theta = ((index * 61.7) % 180) * Math.PI / 180;
295
+ backgroundPositions.push(
296
+ radius * Math.sin(theta) * Math.cos(phi),
297
+ radius * Math.cos(theta),
298
+ radius * Math.sin(theta) * Math.sin(phi)
299
+ );
300
+ }
301
+ backgroundGeometry.setAttribute(
302
+ 'position',
303
+ new THREE.Float32BufferAttribute(backgroundPositions, 3)
304
+ );
305
+ constellation.add(new THREE.Points(
306
+ backgroundGeometry,
307
+ new THREE.PointsMaterial({
308
+ color: '#d7efff',
309
+ size: 0.025,
310
+ transparent: true,
311
+ opacity: 0.62
312
+ })
313
+ ));
314
+ };
315
+
316
+ const destroy = (): void => {
317
+ if (animationFrame) {
318
+ window.cancelAnimationFrame(animationFrame);
319
+ animationFrame = 0;
320
+ }
321
+ canvas?.removeEventListener('pointermove', handlePointerMove);
322
+ canvas?.removeEventListener('click', handleClick);
323
+ controls?.dispose();
324
+ if (constellation) {
325
+ disposeObject(constellation);
326
+ }
327
+ renderer?.dispose();
328
+ infoLabel?.remove();
329
+ canvas = undefined;
330
+ renderer = undefined;
331
+ scene = undefined;
332
+ camera = undefined;
333
+ controls = undefined;
334
+ constellation = undefined;
335
+ stars = undefined;
336
+ infoLabel = undefined;
337
+ };
338
+
339
+ return createCustomSeries<T>({
340
+ selector: configuration.selector,
341
+ displayName: configuration.displayName,
342
+ render({ getWebglCanvas, data, plotSize }) {
343
+ const nextCanvas = getWebglCanvas(
344
+ configuration.canvasName ?? configuration.selector
345
+ );
346
+ if (canvas !== nextCanvas || !renderer) {
347
+ destroy();
348
+ initialize(nextCanvas);
349
+ }
350
+ renderer?.setSize(plotSize.width, plotSize.height, false);
351
+ if (camera) {
352
+ camera.aspect = plotSize.width / Math.max(plotSize.height, 1);
353
+ camera.updateProjectionMatrix();
354
+ }
355
+ rebuildConstellation(data);
356
+ },
357
+ destroy
358
+ });
359
+ };
360
+
361
+ // Relative placement follows the J2000 right ascension/declination pattern.
362
+ export const ariesNodes: ConstellationNode[] = [
363
+ { id: 'mesarthim', label: 'Mesarthim · γ Ari', x: -2.25, y: -0.78, z: 0.08, size: 0.82, color: '#f8fcff' },
364
+ { id: 'sheratan', label: 'Sheratan · β Ari', x: -2.08, y: -0.34, z: -0.06, size: 1.15, color: '#e8f5ff' },
365
+ { id: 'hamal', label: 'Hamal · α Ari', x: -1.05, y: 0.62, z: 0.1, size: 1.55, color: '#fff7e8' },
366
+ { id: 'botein', label: 'Botein · δ Ari', x: 1.95, y: -0.62, z: 0.04, size: 0.78, color: '#f2f8ff' },
367
+ { id: 'bharani', label: 'Bharani · 41 Ari', x: 0.82, y: 1.78, z: -0.12, size: 0.9, color: '#e1f2ff' }
368
+ ];
369
+
370
+ export const ariesLinks: ConstellationLink[] = [
371
+ { source: 'mesarthim', target: 'sheratan' },
372
+ { source: 'sheratan', target: 'hamal' },
373
+ { source: 'hamal', target: 'botein' },
374
+ { source: 'hamal', target: 'bharani' }
375
+ ];