"use client" import React, { useEffect, useRef, useState, useCallback } from 'react' import { createChart, ColorType, CrosshairMode } from 'lightweight-charts' const ChartComponent = ({ data, chartType, theme, drawings, onAddDrawing, activeTool, onResetTools, currentSymbol }) => { const chartContainerRef = useRef(null) const chartRef = useRef(null) const seriesRef = useRef(null) const [chartReady, setChartReady] = useState(false) const [priceScaleWidth, setPriceScaleWidth] = useState(50) const chartColors = { background: theme === 'dark' ? '#1a1d29' : '#ffffff', grid: theme === 'dark' ? '#2a2e39' : '#f0f3fa', text: theme === 'dark' ? '#d1d4dc' : '#131722', border: theme === 'dark' ? '#2a2e39' : '#e0e3eb', } const applyThemeToChart = useCallback(() => { const chart = chartRef.current if (!chart) return chart.applyOptions({ layout: { background: { type: ColorType.Solid, color: chartColors.background, }, textColor: chartColors.text, fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif', }, grid: { vertLines: { color: chartColors.grid }, horzLines: { color: chartColors.grid }, }, crosshair: { mode: CrosshairMode.Normal, vertLine: { color: theme === 'dark' ? '#758696' : '#9598a1', width: 1, style: 2, labelBackgroundColor: theme === 'dark' ? '#758696' : '#9598a1', }, horzLine: { color: theme === 'dark' ? '#758696' : '#9598a1', width: 1, style: 2, labelBackgroundColor: theme === 'dark' ? '#758696' : '#9598a1', }, }, rightPriceScale: { borderColor: chartColors.border, scaleMargins: { top: 0.1, bottom: 0.1, }, }, timeScale: { borderColor: chartColors.border, timeVisible: true, secondsVisible: false, }, }) }, [theme, chartColors]) useEffect(() => { if (chartContainerRef.current) { const chart = createChart(chartContainerRef.current, { width: chartContainerRef.current.clientWidth, height: chartContainerRef.current.clientHeight, layout: { background: { type: ColorType.Solid, color: chartColors.background, }, textColor: chartColors.text, fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif', }, grid: { vertLines: { color: chartColors.grid }, horzLines: { color: chartColors.grid }, }, crosshair: { mode: CrosshairMode.Normal, vertLine: { color: theme === 'dark' ? '#758696' : '#9598a1', width: 1, style: 2, labelBackgroundColor: theme === 'dark' ? '#758696' : '#9598a1', }, horzLine: { color: theme === 'dark' ? '#758696' : '#9598a1', width: 1, style: 2, labelBackgroundColor: theme === 'dark' ? '#758696' : '#9598a1', }, }, rightPriceScale: { borderColor: chartColors.border, scaleMargins: { top: 0.1, bottom: 0.1, }, }, timeScale: { borderColor: chartColors.border, timeVisible: true, secondsVisible: false, }, handleScroll: { vertTouchDrag: false, }, }) chartRef.current = chart setChartReady(true) const handleResize = () => { if (chartContainerRef.current && chartRef.current) { chartRef.current.applyOptions({ width: chartContainerRef.current.clientWidth, height: chartContainerRef.current.clientHeight, }) } } window.addEventListener('resize', handleResize) return () => { window.removeEventListener('resize', handleResize) chart.remove() } } }, []) useEffect(() => { if (!chartRef.current) return applyThemeToChart() }, [applyThemeToChart]) useEffect(() => { const chart = chartRef.current if (!chart) return if (seriesRef.current) { chart.removeSeries(seriesRef.current) seriesRef.current = null } if (!data || data.length === 0) return let series if (chartType === 'line') { series = chart.addLineSeries({ color: '#2962FF', lineWidth: 2, lastValueVisible: true, title: currentSymbol || 'Price', }) const lineData = data.map(d => ({ time: d.time, value: d.close, })) series.setData(lineData) } else if (chartType === 'area') { series = chart.addAreaSeries({ topColor: 'rgba(41, 98, 255, 0.4)', bottomColor: 'rgba(41, 98, 255, 0.01)', lineColor: '#2962FF', lineWidth: 2, lastValueVisible: true, title: currentSymbol || 'Price', }) const areaData = data.map(d => ({ time: d.time, value: d.close, })) series.setData(areaData) } else { series = chart.addCandlestickSeries({ upColor: theme === 'dark' ? '#089981' : '#089981', downColor: theme === 'dark' ? '#F23645' : '#F23645', borderUpColor: theme === 'dark' ? '#089981' : '#089981', borderDownColor: theme === 'dark' ? '#F23645' : '#F23645', wickUpColor: theme === 'dark' ? '#089981' : '#089981', wickDownColor: theme === 'dark' ? '#F23645' : '#F23645', lastValueVisible: true, title: currentSymbol || 'Price', }) series.setData(data) } seriesRef.current = series chart.timeScale().fitContent() chart.timeScale().subscribeVisibleTimeRangeChange(() => { const margins = chart.priceScale('right').width() setPriceScaleWidth(margins || 50) }) }, [data, chartType, theme, currentSymbol]) useEffect(() => { const chart = chartRef.current if (!chart) return chart.clearMarkers() const pricelines = [] drawings.forEach(drawing => { if (drawing.type === 'horizontal') { const line = seriesRef.current.createPriceLine({ price: drawing.price, color: drawing.color || '#2962FF', lineWidth: 1, lineStyle: 2, axisLabelVisible: true, title: drawing.price.toFixed(2), }) pricelines.push(line) } }) return () => { if (seriesRef.current) { pricelines.forEach(line => { try { seriesRef.current.removePriceLine(line) } catch (e) {} }) } } }, [drawings]) const handleChartClick = useCallback((param) => { if (!activeTool || activeTool === 'cursor' || !chartRef.current || !seriesRef.current) return if (!param.point || !param.time) return const chart = chartRef.current const coordinate = chart.timeScale().timeToCoordinate(param.time) const price = seriesRef.current.coordinateToPrice(param.point.y) if (price === null || coordinate === null) return let newDrawing = null if (activeTool === 'horizontal') { newDrawing = { id: Date.now(), type: 'horizontal', price: price, time: param.time, color: '#2962FF', } } else if (activeTool === 'vertical') { newDrawing = { id: Date.now(), type: 'vertical', time: param.time, color: '#2962FF', } } else if (activeTool === 'rectangle') { if (!drawingStart.current) { drawingStart.current = { time: param.time, price: price } } else { const start = drawingStart.current newDrawing = { id: Date.now(), type: 'rectangle', startTime: Math.min(start.time, param.time), endTime: Math.max(start.time, param.time), topPrice: Math.max(start.price, price), bottomPrice: Math.min(start.price, price), color: 'rgba(41, 98, 255, 0.2)', borderColor: '#2962FF', } drawingStart.current = null onResetTools() } } else if (activeTool === 'fibonacci') { if (!drawingStart.current) { drawingStart.current = { time: param.time, price: price } } else { const start = drawingStart.current const topPrice = Math.max(start.price, price) const bottomPrice = Math.min(start.price, price) const diff = topPrice - bottomPrice const startTime = Math.min(start.time, param.time) const endTime = Math.max(start.time, param.time) const levels = [ { price: topPrice, percent: '0%' }, { price: topPrice - diff * 0.236, percent: '23.6%' }, { price: topPrice - diff * 0.382, percent: '38.2%' }, { price: topPrice - diff * 0.5, percent: '50%' }, { price: topPrice - diff * 0.618, percent: '61.8%' }, { price: bottomPrice, percent: '100%' }, ] newDrawing = { id: Date.now(), type: 'fibonacci', startTime: startTime, endTime: endTime, topPrice: topPrice, bottomPrice: bottomPrice, levels: levels, color: '#089981', } drawingStart.current = null onResetTools() } } if (newDrawing) { onAddDrawing(newDrawing) if (activeTool !== 'rectangle' && activeTool !== 'fibonacci') { onResetTools() } } }, [activeTool, onAddDrawing, onResetTools]) const drawingStart = useRef(null) useEffect(() => { const chart = chartRef.current if (!chart) return chart.subscribeClick(handleChartClick) return () => { chart.unsubscribeClick(handleChartClick) } }, [handleChartClick]) const removeDrawing = (id) => { onAddDrawing(null, id) } return (
{drawings.length > 0 && (
{drawings.map((drawing, index) => (
{drawing.type === 'horizontal' && `H: ${drawing.price.toFixed(2)}`} {drawing.type === 'vertical' && `V`} {drawing.type === 'rectangle' && `Rect`} {drawing.type === 'fibonacci' && `Fibo`}
))}
)}
{(!data || data.length === 0) && (

Завантажте CSV файл з OHLCV даними

або використовуйте тестові дані

)}
) } export default ChartComponent