feat: K线添加mock数据
This commit is contained in:
@@ -61,6 +61,20 @@ export const generateDailyData = (indexCode, days = 30) => {
|
||||
return data;
|
||||
};
|
||||
|
||||
/**
|
||||
* 计算简单移动均价(用于分时图均价线)
|
||||
* @param {Array} data - 已有数据
|
||||
* @param {number} currentPrice - 当前价格
|
||||
* @param {number} period - 均线周期(默认5)
|
||||
* @returns {number} 均价
|
||||
*/
|
||||
function calculateAvgPrice(data, currentPrice, period = 5) {
|
||||
const recentPrices = data.slice(-period).map(d => d.price || d.close);
|
||||
recentPrices.push(currentPrice);
|
||||
const sum = recentPrices.reduce((acc, p) => acc + p, 0);
|
||||
return parseFloat((sum / recentPrices.length).toFixed(2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成时间范围内的数据
|
||||
*/
|
||||
@@ -80,6 +94,11 @@ function generateTimeRange(data, startTime, endTime, basePrice, session) {
|
||||
|
||||
// ✅ 修复:为分时图添加完整的 OHLC 字段
|
||||
const closePrice = parseFloat(price.toFixed(2));
|
||||
|
||||
// 计算均价和涨跌幅
|
||||
const avgPrice = calculateAvgPrice(data, closePrice);
|
||||
const changePercent = parseFloat(((closePrice - basePrice) / basePrice * 100).toFixed(2));
|
||||
|
||||
data.push({
|
||||
time: formatTime(current),
|
||||
timestamp: current.getTime(), // ✅ 新增:毫秒时间戳
|
||||
@@ -88,6 +107,8 @@ function generateTimeRange(data, startTime, endTime, basePrice, session) {
|
||||
low: parseFloat((price * 0.9997).toFixed(2)), // ✅ 新增:最低价(略低于收盘)
|
||||
close: closePrice, // ✅ 保留:收盘价
|
||||
price: closePrice, // ✅ 保留:兼容字段(供 MiniTimelineChart 使用)
|
||||
avg_price: avgPrice, // ✅ 新增:均价(供 TimelineChartModal 使用)
|
||||
change_percent: changePercent, // ✅ 新增:涨跌幅(供 TimelineChartModal 使用)
|
||||
volume: volume,
|
||||
prev_close: basePrice
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user