对比图完成

This commit is contained in:
cheney
2026-04-26 11:41:39 +08:00
parent 342042577d
commit becf0364ad
6 changed files with 216 additions and 34 deletions
+12 -15
View File
@@ -22,30 +22,27 @@ async function handler(req, res) {
const params = [];
const conditions = [];
if (startDate) {
conditions.push('date >= ?');
params.push(startDate);
}
if (endDate) {
conditions.push('date <= ?');
params.push(endDate);
if (startDate && endDate) {
conditions.push('date BETWEEN ? AND ?');
params.push(startDate, endDate);
}
if (conditions.length > 0) {
query += ' WHERE ' + conditions.join(' AND ');
}
query += ' ORDER BY date ASC';
if (limit) {
query += ' LIMIT ?';
params.push(parseInt(limit));
}
query += ' ORDER BY date DESC';
const result = await pool.query(query, params);
const data = result.map(row => {
// 限制返回数量,默认为30
const limitValue = limit ? parseInt(limit) : 30;
const limitedResult = result.slice(0, limitValue);
// 反转顺序,使日期从早到晚
const sortedResult = limitedResult.reverse();
const data = sortedResult.map(row => {
let dateStr;
if (row.date instanceof Date) {
dateStr = row.date.toISOString().split('T')[0];
+16 -8
View File
@@ -50,12 +50,6 @@ export default function Strategy() {
}
]
useEffect(() => {
if (selectedStrategy === 'sh300-exchange-rate') {
fetchSh300ExchangeData()
}
}, [selectedStrategy, timeRange, startDate, endDate])
const handleTimeRangeChange = (range, start, end) => {
setTimeRange(range)
setStartDate(start)
@@ -63,6 +57,7 @@ export default function Strategy() {
}
const fetchSh300ExchangeData = async () => {
console.log('fetchSh300ExchangeData called')
setSh300Loading(true)
try {
let sh300Url = '/api/sh300-index'
@@ -85,13 +80,19 @@ export default function Strategy() {
exchangeUrl += '?' + params.join('&')
}
console.log('Fetching from:', sh300Url, exchangeUrl)
const [sh300Res, exchangeRes] = await Promise.all([
fetch(sh300Url),
fetch(exchangeUrl)
])
console.log('Response status:', sh300Res.status, exchangeRes.status)
const sh300Json = await sh300Res.json()
const exchangeJson = await exchangeRes.json()
console.log('Response data:', sh300Json, exchangeJson)
if (sh300Json.success && exchangeJson.success) {
const sh300Map = new Map(sh300Json.data.map(item => [item.date, item.close]))
const exchangeMap = new Map(exchangeJson.data.map(item => [item.date, item.rate]))
@@ -104,6 +105,7 @@ export default function Strategy() {
exchangeRate: exchangeMap.get(date) || null
})).filter(item => item.sh300 !== null && item.exchangeRate !== null)
console.log('Merged data:', mergedData)
setSh300ExchangeData(mergedData)
}
} catch (error) {
@@ -113,6 +115,12 @@ export default function Strategy() {
}
}
useEffect(() => {
if (selectedStrategy === 'sh300-exchange-rate') {
fetchSh300ExchangeData()
}
}, [selectedStrategy, timeRange, startDate, endDate])
return (
<div className="container">
<Head>
@@ -371,8 +379,8 @@ export default function Strategy() {
<LineChart data={sh300ExchangeData} margin={{ top: 20, right: 60, left: 20, bottom: 5 }}>
<CartesianGrid strokeDasharray="3 3" />
<XAxis dataKey="date" />
<YAxis yAxisId="left" domain={['dataMin - 100', 'dataMax + 100']} />
<YAxis yAxisId="right" orientation="right" domain={['dataMin - 0.05', 'dataMax + 0.05']} tickFormatter={(value) => value.toFixed(4)} />
<YAxis yAxisId="left" domain={['auto', 'auto']} allowDataOverflow={true} />
<YAxis yAxisId="right" orientation="right" domain={['auto', 'auto']} allowDataOverflow={true} tickFormatter={(value) => value.toFixed(4)} />
<Tooltip />
<Legend />
<Line yAxisId="left" type="monotone" dataKey="sh300" name="沪深300收盘价" stroke="#8884d8" strokeWidth={2} activeDot={{ r: 8 }} />