图表-如何解决图形缩放问题? [英] Recharts - How to fix graph scaling issue?
问题描述
在React应用程序中(使用Next.js),我试图使用图表实现图形功能http://recharts.org/en-US/api/LineChart .我的代码如下,
In React application (using Next.js), I am trying to implement graph feature using recharts http://recharts.org/en-US/api/LineChart. My code is as below,
/index.js
const formatDate = (value) => {
return moment.unix(value).format('HH:MM A DD MM, YYYY')
}
const weeklyData = [
{ date: formatDate(1613619000), price: '1200.00' },
{ date: formatDate(1613617200), price: '1300.83' },
{ date: formatDate(1613615400), price: '1250.23' },
{ date: formatDate(1613611800), price: '500.55' },
{ date: formatDate(1613608200), price: '1600.23' },
{ date: formatDate(1613606400), price: '1850.93' },
{ date: formatDate(1613604600), price: '1750.23' },
{ date: formatDate(1613599200), price: '650.23' },
]
<LineChart
width={900}
height={250}
data={data}
margin={{
top: 5,
right: 30,
left: 20,
bottom: 5,
}}
>
<Tooltip content={<CustomTooltip />} cursor={false} />
<Line type="monotone" dataKey="price" stroke="#4ec6f4" label="Shruthi" />
</LineChart>
/tooltip.js
/tooltip.js
const CustomTooltip = ({ active, payload, label }) => {
if (active && payload && payload.length) {
return (
<div className="tooltip">
<p className="tooltipLabel">{`$${payload[0].payload?.price}`}</p>
<p className="tooltipDesc">{`${payload[0]?.payload?.date}`}</p>
</div>
)
}
return null
}
CustomTooltip.propTypes = {
type: PropTypes.string,
payload: PropTypes.array,
label: PropTypes.string,
}
export default CustomTooltip
现在, YAxis
限制为1000,并且图形(缩放比例)开箱即用且不可见.图形应显示在我设置的容器中.这里的价格不是固定的,可以是 123456.88
或更高的价格.在所有情况下,如何解决此问题?
Right now YAxis
is restricted to 1000 and graph(scaling) is displaying out of the box and its invisible.
Graph should be display within the container which I have set.
Here price is not fixed, it can be like 123456.88
or more than this.
In all scenario, how can I fix this issue?
推荐答案
尝试将价格类型转换为数字或使用不带单引号的值.
try to convert price type to number or use values without single quotes.
注意:如果可能,请发布完整的代码以获得更好的响应
Note: if possible, Post complete code for better response
这篇关于图表-如何解决图形缩放问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!