Replies: 2 comments
-
如何修改 Area 组件的 fetch 请求频率? 1. 文档说明Ant Design Charts v2.4.0 文档未直接提供 fetch 请求频率调控参数。fetch 类型的 data 用于自动从远程地址获取数据,一般只会在组件挂载时请求一次,除非手动触发重新渲染(如 props 变化)或父组件的数据发生更新。 2. 如果频繁更新(可能原因)
3. 官方推荐做法(自定义轮询)如果你希望自定义请求频率,比如每隔一段时间自动更新数据,文档建议使用自定义数据刷新逻辑: 步骤如下:
标准代码示例:import React, { useEffect, useState } from 'react';
import { Area } from '@ant-design/charts';
const DemoArea = () => {
const [data, setData] = useState([]);
useEffect(() => {
const fetchData = () => {
fetch('https://assets.antv.antgroup.com/g2/aapl.json')
.then(res => res.json())
.then(rawData => {
// 根据你的需求 transform 数据
const newData = rawData.map(d => ({
日期: d.date,
价格: d.close,
}));
setData(newData);
});
};
fetchData(); // 首次请求
const timer = setInterval(fetchData, 5000); // 每5秒请求一次
return () => clearInterval(timer); // 卸载时清除定时器
}, []);
return (
<Area
padding={0}
margin={0}
axis={false}
height={50}
data={data} // 直接传数组
xField={d => new Date(d['日期'])}
yField="价格"
/>
);
};
export default DemoArea;
4. 相关文档链接5. 总结
如有更具体的数据刷新场景,请补充说明! 此回答由AI自动生成,仅供参考。如有疑问,请等待维护者回复。 |
Beta Was this translation helpful? Give feedback.
-
Hi @xu756! I'm Dosu and I’m helping the ant-design-charts team. ant-design-charts 的 如果你需要定时刷新数据,可以在父组件用 const [data, setData] = useState([]);
useEffect(() => {
const fetchData = async () => {
const res = await fetch('https://assets.antv.antgroup.com/g2/aapl.json');
const json = await res.json();
setData(json);
};
fetchData();
const timer = setInterval(fetchData, 5000); // 每5秒刷新一次
return () => clearInterval(timer);
}, []); 然后把 官方文档和源码都没有提供自动轮询或频率控制的配置项,相关说明可参考这里。 To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
你好 想问一下 这个fetch 我看一直在更新 想知道这个请求频率怎么修改
Beta Was this translation helpful? Give feedback.
All reactions