recharts - 防止rechart溢出到保证金?
问题描述
我想显示最近的 N 点,但是当将域设置为小于chartData.length
.
我看到我可以通过添加margin={{left:59}}
to将所有内容都向左移动<ComposedChart>
,但这似乎有点 hacky。有没有更好的方法来隐藏这条溢出的线?
/*
* NOTE: the axis `domain` requires a _function_ if you want to display less than the total number of
* data points (see: https://stackoverflow.com/questions/53751564/can-i-set-the-recharts-axis-domain-max-lower-than-datamax)
*/
const pointsToDisplay = 8;
const xAxisStart = chartData.length - pointsToDisplay;
return (
<ResponsiveContainer height={210}>
<ComposedChart data={chartData}>
<XAxis dataKey="index" tick={true} type="number" domain={[() => xAxisStart, 'dataMax']} />
<YAxis tick={false} domain={['dataMin - 3', 'dataMax + 3']} />
<Line type="monotone" dataKey="score" strokeWidth={5} dot={false} stroke="var(--accentColor)" />
</ComposedChart>
</ResponsiveContainer>
);
解决方案
根据文档 allowDataOverflow必须设置为 true 才能裁剪数据。然后,您还可以从域数组中删除该函数并简单地使用该变量:
/*
* NOTE: the axis `domain` requires a _function_ if you want to display less than the total number of
* data points (see: https://stackoverflow.com/questions/53751564/can-i-set-the-recharts-axis-domain-max-lower-than-datamax)
*/
const pointsToDisplay = 8;
const xAxisStart = chartData.length - pointsToDisplay;
return (
<ResponsiveContainer height={210}>
<ComposedChart data={chartData}>
<XAxis dataKey="index" tick={true} type="number" domain={[xAxisStart, 'dataMax']} allowDataOverflow={true}/>
<YAxis tick={false} domain={['dataMin - 3', 'dataMax + 3']} />
<Line type="monotone" dataKey="score" strokeWidth={5} dot={false} stroke="var(--accentColor)" />
</ComposedChart>
</ResponsiveContainer>
);
推荐阅读
- javascript - 如何从 react.js 中的主组件调用实用程序组件
- reactjs - React 功能组件:如何从外部 useEffect() 访问变量
- css-selectors - 了解第 n 个选择器
- html - 使盒子的高度适应它的弹性盒子内容
- c# - Unity3D 中的错误“已注册名为 XXX 的虚拟轴。” 在 VirtualInput.cs
- typescript - Use typescript interface & abstract class without specifying generic types
- amazon-web-services - 绝对初学者在哪里可以学习将 CloudWatch 警报串到 Lambda,然后到 SSM 中的 AWS-RunRemoteScript?
- c++ - 如何在 VS Code 中链接 boost 库的库路径?
- python - **kwargs 中的 Python 装饰器 TypeError
- html - Why mouse not working with this form (html)