Skip to content

Commit cb1d24b

Browse files
8: added series markers property
1 parent f7d6cde commit cb1d24b

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

packages/demo/src/terminal/controlled-chart.tsx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {ReactNode} from 'react';
1+
import {ReactNode, useMemo} from 'react';
22
import {useControls} from 'leva';
3-
import {Chart} from 'lightweight-charts-react-wrapper';
3+
import {Chart, ChartProps} from 'lightweight-charts-react-wrapper';
44
import {
55
ColorType,
66
CrosshairMode,
@@ -192,10 +192,20 @@ export function ControlledChart(props: ControlledChartProps): JSX.Element {
192192
const kineticScroll = useKineticScrollControls('Kinetic scroll');
193193
const trackingMode = useTrackingModeControls('Tracking mode');
194194

195+
const container = useMemo(
196+
(): ChartProps['container'] => size.autoSize ? {
197+
style: { aspectRatio: '3 / 2', width: size.width, contain: 'size' },
198+
} : {
199+
ref: (r: HTMLDivElement | null) => console.log(r),
200+
},
201+
[size.autoSize, size.width]
202+
);
203+
195204
return (
196205
<Chart
197206
{...props}
198207
{...size}
208+
container={container}
199209
watermark={watermark}
200210
layout={layout}
201211
trackingMode={trackingMode}
@@ -211,8 +221,19 @@ export function ControlledChart(props: ControlledChartProps): JSX.Element {
211221

212222
function useSizeControls(name: string) {
213223
return useControls(name, {
214-
width: 600,
215-
height: 300,
224+
autoSize: {
225+
value: true,
226+
label: 'Autosize',
227+
},
228+
width: {
229+
value: 600,
230+
label: 'Width',
231+
},
232+
height: {
233+
value: 300,
234+
label: 'Height',
235+
render: (get) => !get(`${name}.autoSize`),
236+
},
216237
});
217238
}
218239

@@ -235,7 +256,8 @@ function useWatermarkControls(name: string) {
235256
render: (get) => get(`${name}.visible`),
236257
},
237258
fontSize: {
238-
value: 48, label: '-- font size',
259+
value: 48,
260+
label: '-- font size',
239261
optional: true,
240262
disabled: true,
241263
render: (get) => get(`${name}.visible`),

packages/lib/src/components/chart.tsx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
HTMLAttributes,
23
ForwardedRef,
34
forwardRef,
45
memo,
@@ -18,22 +19,38 @@ import {chart, ChartActionResult} from '../internal/chart.js';
1819

1920
export interface ChartProps extends DeepPartial<ChartOptions> {
2021
children?: ReactNode;
22+
container?: HTMLAttributes<HTMLDivElement> & { ref?: ForwardedRef<HTMLDivElement> };
2123
onClick?: MouseEventHandler;
2224
onCrosshairMove?: MouseEventHandler;
2325
}
2426

2527
export const Chart = memo(forwardRef(function Chart(props: ChartProps, ref: ForwardedRef<IChartApi>) {
28+
const {container = {}, ...rest} = props;
29+
const {ref: containerRef, ...restContainer} = container;
30+
2631
const [element, setElement] = useState<HTMLElement | null>(null);
27-
const handleContainerRef = useCallback((ref: HTMLElement | null) => setElement(ref), []);
32+
const handleContainerRef = useCallback(
33+
(ref: HTMLDivElement | null) => {
34+
setElement(ref);
35+
if (containerRef) {
36+
if (typeof containerRef === 'function') {
37+
containerRef(ref);
38+
} else {
39+
containerRef.current = ref;
40+
}
41+
}
42+
},
43+
[containerRef]
44+
);
2845

2946
return (
30-
<div ref={handleContainerRef}>
31-
{element !== null ? <ChartComponent {...props} ref={ref} container={element}/> : null}
47+
<div ref={handleContainerRef} {...restContainer}>
48+
{element !== null ? <ChartComponent {...rest} ref={ref} container={element}/> : null}
3249
</div>
3350
)
3451
}));
3552

36-
const ChartComponent = memo(forwardRef(function ChartComponent(props: ChartProps & { container: HTMLElement }, ref: ForwardedRef<IChartApi>) {
53+
const ChartComponent = memo(forwardRef(function ChartComponent(props: Omit<ChartProps, 'container'> & { container: HTMLElement }, ref: ForwardedRef<IChartApi>) {
3754
const {children} = props;
3855

3956
const context = useChartAction(props, ref);
@@ -45,7 +62,7 @@ const ChartComponent = memo(forwardRef(function ChartComponent(props: ChartProps
4562
);
4663
}));
4764

48-
function useChartAction(props: ChartProps & { container: HTMLElement }, ref: ForwardedRef<IChartApi>): MutableRefObject<LazyValue<ChartActionResult>> {
65+
function useChartAction(props: Omit<ChartProps, 'container'> & { container: HTMLElement }, ref: ForwardedRef<IChartApi>): MutableRefObject<LazyValue<ChartActionResult>> {
4966
const {children, container, ...rest} = props;
5067

5168
const context = useRef(createLazyValue(

packages/lib/src/internal/chart.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ export function chart(
7474
) {
7575
width = nextOptions.width ?? width;
7676
height = nextOptions.height ?? height;
77-
chart.resize(width, height, true);
77+
if (!nextOptions.autoSize) {
78+
chart.resize(width, height, true);
79+
}
7880
}
7981

8082
options = nextOptions;

0 commit comments

Comments
 (0)