Skip to content

Commit 0243801

Browse files
committed
feat: add different chart types
1 parent e7232f6 commit 0243801

File tree

4 files changed

+87
-28
lines changed

4 files changed

+87
-28
lines changed

examples/src/samples/ChartTypes/ChartTypes.tsx

Whitespace-only changes.

examples/src/samples/ChartTypes/chartTypesStore.ts

Whitespace-only changes.

lib/src/chart/types.ts

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,108 @@
11
import {
2-
type ChartOptions as ChartNativeOptions,
2+
type ChartOptions as TimeChartNativeOptions,
3+
type YieldCurveChartOptions as YieldCurveChartNativeOptions,
4+
type PriceChartOptions as PriceChartNativeOptions,
35
type DeepPartial,
46
type IChartApi,
57
type MouseEventHandler,
68
type Time,
9+
type IYieldCurveChartApi,
10+
// createOptionsChart,
11+
// createYieldCurveChart
712
} from "lightweight-charts";
13+
import type { IChartApiBase } from "lightweight-charts";
814
import type { JSX, ReactNode } from "react";
915

1016
/**
11-
* Chart component props.
17+
* Chart component properties that can be used to create a chart.
1218
*/
13-
export type ChartProps = {
14-
children?: ReactNode;
15-
containerProps?: JSX.IntrinsicElements["div"];
16-
onClick?: MouseEventHandler<Time>;
17-
onCrosshairMove?: MouseEventHandler<Time>;
18-
options?: DeepPartial<ChartNativeOptions>;
19-
onDblClick?: MouseEventHandler<Time>;
20-
};
19+
export type ChartApiType = IYieldCurveChartApi | IChartApiBase<number> | IChartApi;
2120

2221
/**
2322
* Chart API reference type that can be used to access the chart plugin API.
2423
*/
25-
export type ChartApiRef = {
26-
_chart: IChartApi | null;
27-
api: () => IChartApi | null;
28-
init: () => IChartApi | null;
24+
export type ChartApiRef<T extends ChartApiType> = {
25+
_chart: T | null;
26+
api: () => T | null;
27+
init: () => T | null;
2928
clear: () => void;
3029
};
3130

3231
/**
3332
* Chart context that provides access to the chart API and readiness state.
3433
*/
35-
export interface IChartContext {
36-
chartApiRef: ChartApiRef | null;
34+
export interface IChartContext<T extends ChartApiType> {
35+
chartApiRef: ChartApiRef<T> | null;
3736
isReady: boolean;
3837
}
3938

39+
type ChartCommonProps = {
40+
children?: ReactNode;
41+
containerProps?: JSX.IntrinsicElements["div"];
42+
};
43+
44+
type TimeChartOwnProps = {
45+
onClick?: MouseEventHandler<Time>;
46+
onCrosshairMove?: MouseEventHandler<Time>;
47+
onDblClick?: MouseEventHandler<Time>;
48+
options?: DeepPartial<TimeChartNativeOptions>;
49+
};
50+
51+
type YieldCurveChartProps = {
52+
onClick?: MouseEventHandler<number>;
53+
onCrosshairMove?: MouseEventHandler<number>;
54+
onDblClick?: MouseEventHandler<number>;
55+
options?: DeepPartial<YieldCurveChartNativeOptions>;
56+
};
57+
58+
type OptionsChartOwnProps = {
59+
onClick?: MouseEventHandler<number>;
60+
onCrosshairMove?: MouseEventHandler<number>;
61+
onDblClick?: MouseEventHandler<number>;
62+
options?: DeepPartial<PriceChartNativeOptions>;
63+
};
64+
4065
/**
4166
* Chart component properties that can be used to create a chart.
4267
*/
68+
export type ChartOwnProps =
69+
| TimeChartOwnProps
70+
| YieldCurveChartProps
71+
| OptionsChartOwnProps;
72+
73+
/**
74+
* Chart component props.
75+
*/
76+
export type ChartProps = ChartCommonProps & TimeChartOwnProps;
77+
78+
/**
79+
* Yield chart component properties.
80+
*/
81+
export type YieldCurveChartOwnProps = ChartCommonProps & YieldCurveChartProps;
82+
83+
/**
84+
* Options chart component properties.
85+
*/
86+
export type OptionsChartProps = ChartCommonProps & OptionsChartOwnProps;
87+
88+
/**
89+
* Chart component properties.
90+
*/
4391
export type ChartComponentProps = {
4492
container: HTMLElement;
4593
} & Omit<ChartProps, "containerProps">;
4694

4795
/**
4896
* Options for the useChart hook.
4997
*/
50-
export type UseChartOptions = {
98+
export type UseChartOptions<T extends ChartOwnProps> = {
5199
container: HTMLElement;
52-
} & Omit<ChartProps, "children" | "containerProps">;
100+
createChartHandler: (
101+
container: string | HTMLElement,
102+
options?: DeepPartial<T["options"]>
103+
) => IChartApi;
104+
} & T;
105+
106+
// so the chart api for different charts is also diferent - different type
107+
// can i handle it in 1 hook?
108+
// need to explore here more

lib/src/chart/useChart.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import { createChart } from "lightweight-charts";
1+
/* eslint-disable */
22
import { useLayoutEffect, useRef, useState } from "react";
3+
import type { ChartApiRef, ChartOwnProps, UseChartOptions } from "./types";
34
import { defaultChartOptions } from "./defaultChartOptions";
4-
import type { ChartApiRef, UseChartOptions } from "./types";
5+
import { createChart } from "lightweight-charts";
56

6-
export const useChart = ({
7+
export const useChart = <T extends ChartOwnProps>({
78
container,
89
onClick,
910
onCrosshairMove,
1011
options = {},
1112
onDblClick,
12-
}: UseChartOptions) => {
13+
createChartHandler,
14+
}: UseChartOptions<T>) => {
1315
const [isReady, setIsReady] = useState(false);
1416

1517
const chartApiRef = useRef<ChartApiRef>({
@@ -24,6 +26,7 @@ export const useChart = ({
2426
...options,
2527
});
2628
this._chart = chart;
29+
this._chart = createChartHandler(container, options);
2730
}
2831

2932
if (!isReady) {
@@ -53,12 +56,12 @@ export const useChart = ({
5356
if (!container) return;
5457

5558
if (onClick) {
56-
chartApiRef.current.api()?.subscribeClick(onClick);
59+
chartApiRef.current.api()?.subscribeClick(onClick as any);
5760
}
5861

5962
return () => {
6063
if (onClick) {
61-
chartApiRef.current.api()?.unsubscribeClick(onClick);
64+
chartApiRef.current.api()?.unsubscribeClick(onClick as any);
6265
}
6366
};
6467
}, [onClick]);
@@ -67,12 +70,12 @@ export const useChart = ({
6770
if (!container) return;
6871

6972
if (onCrosshairMove) {
70-
chartApiRef.current.api()?.subscribeCrosshairMove(onCrosshairMove);
73+
chartApiRef.current.api()?.subscribeCrosshairMove(onCrosshairMove as any);
7174
}
7275

7376
return () => {
7477
if (onCrosshairMove) {
75-
chartApiRef.current.api()?.unsubscribeCrosshairMove(onCrosshairMove);
78+
chartApiRef.current.api()?.unsubscribeCrosshairMove(onCrosshairMove as any);
7679
}
7780
};
7881
}, [onCrosshairMove]);
@@ -81,12 +84,12 @@ export const useChart = ({
8184
if (!container) return;
8285

8386
if (onDblClick) {
84-
chartApiRef.current.api()?.subscribeDblClick(onDblClick);
87+
chartApiRef.current.api()?.subscribeDblClick(onDblClick as any);
8588
}
8689

8790
return () => {
8891
if (onDblClick) {
89-
chartApiRef.current.api()?.unsubscribeDblClick(onDblClick);
92+
chartApiRef.current.api()?.unsubscribeDblClick(onDblClick as any);
9093
}
9194
};
9295
}, [onDblClick]);

0 commit comments

Comments
 (0)