@keneth80/k-chart 1.2.0 → 1.4.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 +25 -0
- package/README.md +113 -46
- package/docs/code-graph-handoff.md +242 -0
- package/docs/functional-api.md +487 -0
- package/docs/react-nextjs.md +262 -0
- package/docs/refactor-plan.md +61 -0
- package/docs/release.md +155 -0
- package/examples/canvas-candlestick-series.ts +106 -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 +69 -0
- package/lib/kchart.d.ts.map +1 -1
- package/lib/kchart.js +568 -13
- package/lib/kchart.js.map +1 -1
- package/package.json +10 -6
|
@@ -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.
|
package/docs/release.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
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
|
+
## GitFlow
|
|
33
|
+
|
|
34
|
+
KChart uses a lightweight GitFlow model:
|
|
35
|
+
|
|
36
|
+
- `main`: production and npm release branch. Every commit on `main` should represent a releasable state.
|
|
37
|
+
- `develop`: integration branch for the next release.
|
|
38
|
+
- `feature/<name>`: feature work branched from `develop`, merged back into `develop`.
|
|
39
|
+
- `release/<version>`: release stabilization branched from `develop`, merged into `main` and then back into `develop`.
|
|
40
|
+
- `hotfix/<version>`: urgent production fix branched from `main`, merged into both `main` and `develop`.
|
|
41
|
+
|
|
42
|
+
### Feature Flow
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
git switch develop
|
|
46
|
+
git pull origin develop
|
|
47
|
+
git switch -c feature/my-feature
|
|
48
|
+
|
|
49
|
+
# work, commit, verify
|
|
50
|
+
npm run typecheck
|
|
51
|
+
npm run build:lib
|
|
52
|
+
|
|
53
|
+
git push origin feature/my-feature
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Merge the feature into `develop` after review.
|
|
57
|
+
|
|
58
|
+
### Release Flow
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
git switch develop
|
|
62
|
+
git pull origin develop
|
|
63
|
+
git switch -c release/1.4.0
|
|
64
|
+
|
|
65
|
+
npm version minor --no-git-tag-version
|
|
66
|
+
# update CHANGELOG.md and docs
|
|
67
|
+
npm run typecheck
|
|
68
|
+
npm run build:lib
|
|
69
|
+
npm run pack:dry
|
|
70
|
+
|
|
71
|
+
git commit -am "Prepare 1.4.0 release"
|
|
72
|
+
git switch main
|
|
73
|
+
git merge --no-ff release/1.4.0
|
|
74
|
+
git tag v1.4.0
|
|
75
|
+
git push origin main v1.4.0
|
|
76
|
+
|
|
77
|
+
git switch develop
|
|
78
|
+
git merge --no-ff release/1.4.0
|
|
79
|
+
git push origin develop
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Publish to npm from `main` after the release tag is pushed.
|
|
83
|
+
|
|
84
|
+
### Hotfix Flow
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
git switch main
|
|
88
|
+
git pull origin main
|
|
89
|
+
git switch -c hotfix/1.3.1
|
|
90
|
+
|
|
91
|
+
# fix, verify, update patch version and CHANGELOG.md
|
|
92
|
+
npm run typecheck
|
|
93
|
+
npm run build:lib
|
|
94
|
+
npm run pack:dry
|
|
95
|
+
|
|
96
|
+
git switch main
|
|
97
|
+
git merge --no-ff hotfix/1.3.1
|
|
98
|
+
git tag v1.3.1
|
|
99
|
+
git push origin main v1.3.1
|
|
100
|
+
|
|
101
|
+
git switch develop
|
|
102
|
+
git merge --no-ff hotfix/1.3.1
|
|
103
|
+
git push origin develop
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Publish
|
|
107
|
+
|
|
108
|
+
For the first public release:
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
npm login
|
|
112
|
+
npm publish --access public
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
For later releases, update the version first:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
npm version patch
|
|
119
|
+
npm publish
|
|
120
|
+
git push origin main --follow-tags
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Use semantic versioning:
|
|
124
|
+
|
|
125
|
+
- `patch`: bug fixes and docs-only compatible changes.
|
|
126
|
+
- `minor`: new backwards-compatible API or examples.
|
|
127
|
+
- `major`: breaking API changes.
|
|
128
|
+
|
|
129
|
+
## GitHub Release
|
|
130
|
+
|
|
131
|
+
After pushing the version tag, create a GitHub Release from that tag.
|
|
132
|
+
|
|
133
|
+
Recommended release note sections:
|
|
134
|
+
|
|
135
|
+
```md
|
|
136
|
+
## Added
|
|
137
|
+
|
|
138
|
+
## Changed
|
|
139
|
+
|
|
140
|
+
## Fixed
|
|
141
|
+
|
|
142
|
+
## Migration Notes
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Use the same content to update `CHANGELOG.md`.
|
|
146
|
+
|
|
147
|
+
## Playground
|
|
148
|
+
|
|
149
|
+
After publishing, update the playground project to consume npm instead of a vendored tgz:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
npm install @keneth80/k-chart@latest
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Then remove the local `vendor/kchart-1.0.0.tgz` dependency if it is no longer needed.
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createCanvasCandlestickSeries,
|
|
3
|
+
createKChart
|
|
4
|
+
} from '@keneth80/k-chart';
|
|
5
|
+
|
|
6
|
+
interface StockPoint {
|
|
7
|
+
date: string;
|
|
8
|
+
open: number;
|
|
9
|
+
high: number;
|
|
10
|
+
low: number;
|
|
11
|
+
close: number;
|
|
12
|
+
previousClose: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const formatDate = (date: Date): string => date.toISOString().slice(0, 10);
|
|
16
|
+
|
|
17
|
+
const addDays = (date: Date, days: number): Date => {
|
|
18
|
+
const next = new Date(date);
|
|
19
|
+
next.setDate(next.getDate() + days);
|
|
20
|
+
return next;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const createStockData = (length: number): StockPoint[] => {
|
|
24
|
+
const data: StockPoint[] = [];
|
|
25
|
+
let currentDate = new Date('2024-01-02T00:00:00');
|
|
26
|
+
let previousClose = 104;
|
|
27
|
+
let previousAdjustedClose = 104;
|
|
28
|
+
|
|
29
|
+
while (data.length < length) {
|
|
30
|
+
const day = currentDate.getDay();
|
|
31
|
+
if (day !== 0 && day !== 6) {
|
|
32
|
+
const index = data.length;
|
|
33
|
+
const trend = Math.sin(index / 24) * 8 + index * 0.045;
|
|
34
|
+
const swing = Math.sin(index / 3.4) * 2.8 + Math.cos(index / 5.7) * 1.7;
|
|
35
|
+
const open = previousClose + Math.sin(index / 4.8) * 1.4;
|
|
36
|
+
const close = open + swing * 0.72 + Math.sin(index / 2.3) * 0.9;
|
|
37
|
+
const high = Math.max(open, close) + 1.6 + Math.abs(Math.sin(index / 2.8)) * 2.4;
|
|
38
|
+
const low = Math.min(open, close) - 1.5 - Math.abs(Math.cos(index / 3.1)) * 2.1;
|
|
39
|
+
|
|
40
|
+
data.push({
|
|
41
|
+
date: formatDate(currentDate),
|
|
42
|
+
open: Number((open + trend).toFixed(2)),
|
|
43
|
+
high: Number((high + trend).toFixed(2)),
|
|
44
|
+
low: Number((low + trend).toFixed(2)),
|
|
45
|
+
close: Number((close + trend).toFixed(2)),
|
|
46
|
+
previousClose: Number(previousAdjustedClose.toFixed(2))
|
|
47
|
+
});
|
|
48
|
+
previousClose = close;
|
|
49
|
+
previousAdjustedClose = close + trend;
|
|
50
|
+
}
|
|
51
|
+
currentDate = addDays(currentDate, 1);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return data;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const data = createStockData(520);
|
|
58
|
+
const domain = [
|
|
59
|
+
formatDate(addDays(new Date(data[0].date), -8)),
|
|
60
|
+
formatDate(addDays(new Date(data[data.length - 1].date), 8))
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
createKChart<StockPoint>({
|
|
64
|
+
selector: '#chart',
|
|
65
|
+
data,
|
|
66
|
+
axes: [
|
|
67
|
+
{ field: 'date', type: 'time', placement: 'bottom', tickCount: 8, domain },
|
|
68
|
+
{
|
|
69
|
+
field: 'close',
|
|
70
|
+
type: 'number',
|
|
71
|
+
placement: 'left',
|
|
72
|
+
title: 'Price',
|
|
73
|
+
domainFields: ['low', 'high']
|
|
74
|
+
}
|
|
75
|
+
],
|
|
76
|
+
title: {
|
|
77
|
+
text: 'Candlestick Chart'
|
|
78
|
+
},
|
|
79
|
+
tooltip: {
|
|
80
|
+
visible: true
|
|
81
|
+
},
|
|
82
|
+
zoom: {
|
|
83
|
+
enabled: true,
|
|
84
|
+
mode: 'wheel',
|
|
85
|
+
direction: 'x',
|
|
86
|
+
scaleExtent: [1, 120],
|
|
87
|
+
wheelZoom: { enabled: true, devices: 'pc', sensitivity: 0.85 },
|
|
88
|
+
gestureZoom: { enabled: true, devices: 'mobile', minTouches: 1 },
|
|
89
|
+
resetOnDoubleClick: true
|
|
90
|
+
},
|
|
91
|
+
series: [
|
|
92
|
+
createCanvasCandlestickSeries({
|
|
93
|
+
selector: 'price',
|
|
94
|
+
displayName: 'Price',
|
|
95
|
+
xField: 'date',
|
|
96
|
+
openField: 'open',
|
|
97
|
+
highField: 'high',
|
|
98
|
+
lowField: 'low',
|
|
99
|
+
closeField: 'close',
|
|
100
|
+
colorMode: 'previous-close',
|
|
101
|
+
previousCloseField: 'previousClose',
|
|
102
|
+
upColor: '#22c55e',
|
|
103
|
+
downColor: '#ef4444'
|
|
104
|
+
})
|
|
105
|
+
]
|
|
106
|
+
}).render();
|
|
@@ -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
|
+
}
|