-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
232 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// components/sentimentchart/ChartComponent.tsx | ||
import { useEffect, useRef } from 'react'; | ||
import { createChart, ISeriesApi, IChartApi } from 'lightweight-charts'; | ||
|
||
|
||
interface ChartData { | ||
datetime: string; | ||
stock_code: string; | ||
exchange_code: string; | ||
product_type: string; | ||
expiry_date: string; | ||
right: string; | ||
strike_price: string; | ||
open: string; | ||
high: string; | ||
low: string; | ||
close: string; | ||
volume: string; | ||
open_interest: string | null; | ||
} | ||
|
||
interface ChartComponentProps { | ||
data: ChartData[]; | ||
} | ||
|
||
const ChartComponent: React.FC<ChartComponentProps> = ({ data }) => { | ||
const chartContainerRef = useRef(null); | ||
let chart: IChartApi | undefined; | ||
let candleSeries: ISeriesApi<'Candlestick'> | undefined; | ||
|
||
|
||
|
||
|
||
|
||
useEffect(() => { | ||
if (chartContainerRef.current) { | ||
chart = createChart(chartContainerRef.current, { width: window.innerWidth, height: 300 }); | ||
candleSeries = chart.addCandlestickSeries(); | ||
window.addEventListener('resize', () => { | ||
if (chart) { | ||
chart.resize(window.innerWidth, 300); | ||
} | ||
}); | ||
} | ||
const chartData = data.map(item => { | ||
const date = new Date(item.datetime); | ||
return { | ||
time: { year: date.getFullYear(), month: date.getMonth() + 1, day: date.getDate() }, | ||
open: parseFloat(item.open), | ||
high: parseFloat(item.high), | ||
low: parseFloat(item.low), | ||
close: parseFloat(item.close), | ||
}; | ||
}); | ||
|
||
const sortedData = chartData.sort((a, b) => { | ||
const aDate = new Date(a.time.year, a.time.month - 1, a.time.day); | ||
const bDate = new Date(b.time.year, b.time.month - 1, b.time.day); | ||
return aDate.getTime() - bDate.getTime(); | ||
}); | ||
|
||
const uniqueData = sortedData.filter((item, index, self) => | ||
index === self.findIndex((t) => ( | ||
t.time.year === item.time.year && t.time.month === item.time.month && t.time.day === item.time.day | ||
)) | ||
); | ||
|
||
if (candleSeries) { | ||
candleSeries.setData(uniqueData); | ||
} | ||
}, [data]); | ||
|
||
return <div ref={chartContainerRef} />; | ||
}; | ||
|
||
export default ChartComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,56 @@ | ||
import React, { useEffect } from 'react'; | ||
import SentimentChart from '../../app/components/SentimentChart/SentimentChart'; | ||
import { ChartStore } from '../../stores/ChartStore'; | ||
import { NseFetchStore } from '../../stores/NseFetchStore'; | ||
import { toJS } from 'mobx'; | ||
import { DefaultStore, initializeDefaultStore } from '../../stores/DefaultStore'; | ||
import { initializeExpiryDateStore } from '../../stores/ExpiryDateStore'; | ||
|
||
const SentimentComponent = () => { | ||
// pages/chart.tsx | ||
import { MongoClient } from 'mongodb'; | ||
import { GetServerSideProps } from 'next'; | ||
import ChartComponent from '../../app/components/SentimentChart/ChartComponent'; | ||
|
||
interface ChartData { | ||
datetime: string; | ||
stock_code: string; | ||
exchange_code: string; | ||
product_type: string; | ||
expiry_date: string; | ||
right: string; | ||
strike_price: string; | ||
open: string; | ||
high: string; | ||
low: string; | ||
close: string; | ||
volume: string; | ||
open_interest: string | null; | ||
} | ||
|
||
interface ChartPageProps { | ||
data: ChartData[]; | ||
} | ||
|
||
const ChartPage: React.FC<ChartPageProps> = ({ data }) => { | ||
return <ChartComponent data={data} />; | ||
}; | ||
|
||
// Create an instance of DefaultStore | ||
const defaultStore = initializeDefaultStore(); | ||
|
||
// Create an instance of ExpiryDateStore | ||
const expiryDateStore = initializeExpiryDateStore(); | ||
export const getServerSideProps: GetServerSideProps = async () => { | ||
const client = new MongoClient('mongodb://thestonepot:5PZAXjH03P4IT7pDuzBn3A7w1mzHwt0nIphn1shsO936Fj60clpMjdFig7BVrCsQabGfOaefWunWACDbuoUNRw%3D%[email protected]:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@thestonepot@'); | ||
|
||
try { | ||
await client.connect(); | ||
console.log('Connected to the database'); | ||
|
||
// Create an instance of NseFetchStore | ||
const nseFetchStore = new NseFetchStore(defaultStore, expiryDateStore); | ||
|
||
// Create an instance of the ChartStore | ||
const chartStore = new ChartStore(nseFetchStore); | ||
|
||
useEffect(() => { | ||
console.log('chartStore state', toJS(chartStore)); | ||
console.log('Index Graph Data', toJS(chartStore.chartData)); | ||
}, [chartStore]); | ||
|
||
useEffect(() => { | ||
console.log('Index Graph Data', toJS(chartStore.chartData)); | ||
}, [chartStore.chartData]); | ||
|
||
return ( | ||
<div> | ||
{/* Render the SentimentChart component and pass the chartStore prop */} | ||
<SentimentChart chartStore={chartStore} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SentimentComponent; | ||
const collection = client.db('tradepod').collection('HisoricalDaily'); | ||
const data = await collection.find({}).toArray(); | ||
console.log('Fetched data:', data); | ||
|
||
return { | ||
props: { | ||
data: JSON.parse(JSON.stringify(data)), // Next.js requires the data to be serializable | ||
}, | ||
}; | ||
} catch (error) { | ||
console.error('An error occurred:', error); | ||
return { props: {} }; // Return an empty props object in case of error | ||
} finally { | ||
client.close(); | ||
console.log('Connection closed'); | ||
} | ||
} | ||
|
||
export default ChartPage; |
Oops, something went wrong.