fix: k线图正确显示;当前需要继续处理的问题:1、其他技术内容;2、ai判断的方法,传入多条k线数据;3、具体合约代码如何获取

master
Lxy 3 months ago
parent 6d0b554cee
commit c62f044f5d

32
package-lock.json generated

@ -10,6 +10,7 @@
"dependencies": { "dependencies": {
"@reduxjs/toolkit": "^2.11.2", "@reduxjs/toolkit": "^2.11.2",
"antd": "^6.3.0", "antd": "^6.3.0",
"echarts": "^6.0.0",
"lightweight-charts": "^5.1.0", "lightweight-charts": "^5.1.0",
"react": "^19.2.0", "react": "^19.2.0",
"react-dom": "^19.2.0", "react-dom": "^19.2.0",
@ -2211,6 +2212,22 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/echarts": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/echarts/-/echarts-6.0.0.tgz",
"integrity": "sha512-Tte/grDQRiETQP4xz3iZWSvoHrkCQtwqd6hs+mifXcjrCuo2iKWbajFObuLJVBlDIJlOzgQPd1hsaKt/3+OMkQ==",
"license": "Apache-2.0",
"dependencies": {
"tslib": "2.3.0",
"zrender": "6.0.0"
}
},
"node_modules/echarts/node_modules/tslib": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz",
"integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==",
"license": "0BSD"
},
"node_modules/electron-to-chromium": { "node_modules/electron-to-chromium": {
"version": "1.5.286", "version": "1.5.286",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.286.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.286.tgz",
@ -3805,6 +3822,21 @@
"peerDependencies": { "peerDependencies": {
"zod": "^3.25.0 || ^4.0.0" "zod": "^3.25.0 || ^4.0.0"
} }
},
"node_modules/zrender": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/zrender/-/zrender-6.0.0.tgz",
"integrity": "sha512-41dFXEEXuJpNecuUQq6JlbybmnHaqqpGlbH1yxnA5V9MMP4SbohSVZsJIwz+zdjQXSSlR1Vc34EgH1zxyTDvhg==",
"license": "BSD-3-Clause",
"dependencies": {
"tslib": "2.3.0"
}
},
"node_modules/zrender/node_modules/tslib": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz",
"integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==",
"license": "0BSD"
} }
} }
} }

@ -12,6 +12,7 @@
"dependencies": { "dependencies": {
"@reduxjs/toolkit": "^2.11.2", "@reduxjs/toolkit": "^2.11.2",
"antd": "^6.3.0", "antd": "^6.3.0",
"echarts": "^6.0.0",
"lightweight-charts": "^5.1.0", "lightweight-charts": "^5.1.0",
"react": "^19.2.0", "react": "^19.2.0",
"react-dom": "^19.2.0", "react-dom": "^19.2.0",

@ -1,9 +1,9 @@
import React, { useState, useEffect, useRef } from 'react'; import React, { useState, useEffect, useRef } from 'react';
import { Card, Button, Row, Col, Select, Tabs, Tag, Statistic, Alert, Spin } from 'antd'; import { Card, Button, Row, Col, Select, Tabs, Tag, Statistic, Alert, Spin } from 'antd';
import { useParams, useNavigate } from 'react-router-dom'; import { useParams, useNavigate } from 'react-router-dom';
import { LineChartOutlined, BarChartOutlined, AreaChartOutlined, ArrowUpOutlined, AlertOutlined, RobotOutlined, SafetyOutlined } from '@ant-design/icons'; import { LineChartOutlined, BarChartOutlined, AreaChartOutlined, ArrowUpOutlined, AlertOutlined, RobotOutlined, SafetyOutlined } from '@ant-design/icons';
import { generateFutureData, generateKlineData } from '../../utils/mockData'; import { generateFutureData, generateKlineData } from '../../utils/mockData';
import { createChart } from 'lightweight-charts'; import * as echarts from 'echarts';
import './Detail.css'; import './Detail.css';
const { Option } = Select; const { Option } = Select;
@ -52,7 +52,7 @@ const Detail = () => {
// //
if (chartInstance.current) { if (chartInstance.current) {
chartInstance.current.destroy(); chartInstance.current.dispose();
} }
// APIK线 // APIK线
@ -85,93 +85,106 @@ const Detail = () => {
// chartRef.current // chartRef.current
if (!chartRef.current) return; if (!chartRef.current) return;
// // ECharts
const chart = createChart(chartRef.current, { const chart = echarts.init(chartRef.current);
width: chartRef.current.clientWidth, console.log('ECharts instance created:', chart);
height: 400,
timeScale: { // K线
timeVisible: true, const candlestickData = klineData.map(item => [
secondsVisible: false, new Date(item.timestamp * 1000).getTime(), //
item.open, //
item.close, //
item.low, //
item.high //
]);
console.log('Candlestick data for ECharts:', candlestickData);
// MA5
const ma5Data = [];
for (let i = 4; i < klineData.length; i++) {
const sum = klineData.slice(i - 4, i + 1).reduce((acc, item) => acc + item.close, 0);
ma5Data.push([
new Date(klineData[i].timestamp * 1000).getTime(),
sum / 5
]);
}
// MA10
const ma10Data = [];
for (let i = 9; i < klineData.length; i++) {
const sum = klineData.slice(i - 9, i + 1).reduce((acc, item) => acc + item.close, 0);
ma10Data.push([
new Date(klineData[i].timestamp * 1000).getTime(),
sum / 10
]);
}
//
const option = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross'
}
},
legend: {
data: ['K线', 'MA5', 'MA10']
}, },
grid: { grid: {
vertLines: { left: '3%',
color: 'rgba(42, 46, 57, 0.1)', right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'time',
boundaryGap: false
},
yAxis: {
type: 'value',
scale: true
},
series: [
{
name: 'K线',
type: 'candlestick',
data: candlestickData,
itemStyle: {
color: '#52c41a',
color0: '#ff4d4f',
borderColor: '#52c41a',
borderColor0: '#ff4d4f'
}
}, },
horzLines: { {
color: 'rgba(42, 46, 57, 0.1)', name: 'MA5',
type: 'line',
data: ma5Data,
smooth: true,
lineStyle: {
width: 1,
color: '#1890ff'
},
showSymbol: false
}, },
}, {
}); name: 'MA10',
type: 'line',
// chart data: ma10Data,
console.log('Chart object:', chart); smooth: true,
console.log('Chart methods:', Object.keys(chart)); lineStyle: {
width: 1,
// K线 color: '#faad14'
try { },
const candlestickSeries = chart.addCandlestickSeries({ showSymbol: false
upColor: '#52c41a',
downColor: '#ff4d4f',
borderVisible: false,
wickUpColor: '#52c41a',
wickDownColor: '#ff4d4f',
});
// K线
candlestickSeries.setData(klineData.map(item => ({
time: new Date(item.timestamp * 1000).toISOString().split('T')[0],
open: item.open,
high: item.high,
low: item.low,
close: item.close,
})));
//
if (currentIndicator === 'MA') {
// MA5
const ma5Series = chart.addLineSeries({
color: '#1890ff',
lineWidth: 1,
});
// MA5
const ma5Data = [];
for (let i = 4; i < klineData.length; i++) {
const sum = klineData.slice(i - 4, i + 1).reduce((acc, item) => acc + item.close, 0);
ma5Data.push({
time: new Date(klineData[i].timestamp * 1000).toISOString().split('T')[0],
value: sum / 5,
});
} }
ma5Series.setData(ma5Data); ]
};
// MA10
const ma10Series = chart.addLineSeries({ //
color: '#faad14', chart.setOption(option);
lineWidth: 1, console.log('ECharts option set successfully');
});
// MA10
const ma10Data = [];
for (let i = 9; i < klineData.length; i++) {
const sum = klineData.slice(i - 9, i + 1).reduce((acc, item) => acc + item.close, 0);
ma10Data.push({
time: new Date(klineData[i].timestamp * 1000).toISOString().split('T')[0],
value: sum / 10,
});
}
ma10Series.setData(ma10Data);
}
} catch (error) {
console.error('Error creating chart series:', error);
// 使线
const lineSeries = chart.addLineSeries({
color: '#1890ff',
lineWidth: 1,
});
lineSeries.setData(klineData.map(item => ({
time: new Date(item.timestamp * 1000).toISOString().split('T')[0],
value: item.close,
})));
}
// //
chartInstance.current = chart; chartInstance.current = chart;
@ -179,7 +192,7 @@ const Detail = () => {
// //
const handleResize = () => { const handleResize = () => {
if (chartInstance.current) { if (chartInstance.current) {
chartInstance.current.resize(chartRef.current.clientWidth, 400); chartInstance.current.resize();
} }
}; };
@ -189,7 +202,7 @@ const Detail = () => {
return () => { return () => {
window.removeEventListener('resize', handleResize); window.removeEventListener('resize', handleResize);
if (chartInstance.current) { if (chartInstance.current) {
chartInstance.current.destroy(); chartInstance.current.dispose();
} }
}; };
}); });

Loading…
Cancel
Save