mvbhr's picture
Upload components/StockChart.js with huggingface_hub
8eb2fad verified
import { useEffect, useState } from 'react'
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, Area, AreaChart } from 'recharts'
export default function StockChart({ symbol }) {
const [chartData, setChartData] = useState([])
const [loading, setLoading] = useState(true)
useEffect(() => {
fetchChartData()
}, [symbol])
const fetchChartData = async () => {
try {
setLoading(true)
const response = await fetch(`/api/chart?symbol=${symbol}`)
const data = await response.json()
setChartData(data)
setLoading(false)
} catch (error) {
console.error('Error fetching chart data:', error)
setLoading(false)
}
}
if (loading) {
return (
<div className="h-96 flex items-center justify-center">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-primary-600"></div>
</div>
)
}
return (
<div className="h-96">
<ResponsiveContainer width="100%" height="100%">
<AreaChart data={chartData}>
<defs>
<linearGradient id="colorPrice" x1="0" y1="0" x2="0" y2="1">
<stop offset="5%" stopColor="#3b82f6" stopOpacity={0.3}/>
<stop offset="95%" stopColor="#3b82f6" stopOpacity={0}/>
</linearGradient>
</defs>
<CartesianGrid strokeDasharray="3 3" stroke="#e5e7eb" />
<XAxis
dataKey="date"
tick={{ fontSize: 12 }}
tickFormatter={(value) => {
const date = new Date(value)
return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' })
/>
<YAxis
tick={{ fontSize: 12 }}
domain={['dataMin - 5', 'dataMax + 5']}
/>
<Tooltip
contentStyle={{
backgroundColor: 'white',
border: '1px solid #e5e7eb',
borderRadius: '8px'
labelFormatter={(value) => {
const date = new Date(value)
return date.toLocaleDateString('en-US', {
weekday: 'short',
year: 'numeric',
month: 'short',
day: 'numeric'
})
formatter={(value) => [`$${value}`, 'Price']}
/>
<Area
type="monotone"
dataKey="price"
stroke="#3b82f6"
strokeWidth={2}
fillOpacity={1}
fill="url(#colorPrice)"
/>
</AreaChart>
</ResponsiveContainer>
</div>
)
}