gpt4 book ai didi

javascript - 多行 ReCharts

转载 作者:行者123 更新时间:2023-11-29 10:55:37 24 4
gpt4 key购买 nike

我想创建一个包含多行的 ReChart 组件。

到目前为止,我有这个虚拟代码。

<LineChart
data={data}
margin={{
top: 16,
right: 16,
bottom: 0,
left: 24
}}
>
<XAxis dataKey="time" />
<YAxis>
<Label angle={270} position="left" style={{ textAnchor: "middle" }}>
Cyber assesment
</Label>
</YAxis>
<Line type="monotone" dataKey="amount" stroke="#556CD6" dot={false} />
</LineChart>

数据是一个数组,有两个值对象:

function createData(time, amount) {
return { time, amount };
}

const data = [
createData('00:00', 0),
createData('03:00', 300),
createData('06:00', 600),
createData('09:00', 800),
createData('12:00', 1500),
createData('15:00', 2000),
createData('18:00', 2400),
createData('21:00', 2400),
createData('24:00', undefined),
];

我想这样做,这样我就可以绘制多条线,我尝试创建一个不同的函数并将其他数据添加到同一个数组,但没有很好的结果。

如何实现?

我试图创建一个工作示例,但没有任何运气

function createData(amountA, amountB) {
return {a: amountA, b: amountB };
}

const data = [
createData(100, 200),
createData(200, 400),
createData(400, 500),
createData(600, 800),
createData(650, 700),
createData(900, 800)
];

export default function Chart() {
console.log(data)
return (
<React.Fragment>
<Title>Performance indication</Title>
<ResponsiveContainer>
<LineChart
data={data}
margin={{
top: 16,
right: 16,
bottom: 0,
left: 24,
}}
>
<XAxis dataKey="time" />
<YAxis>
<Label angle={270} position="left" style={{ textAnchor: 'middle' }}>
Cyber assesment
</Label>
</YAxis>
<Line type="monotone" dataKey={data.a} stroke="#556CD6" dot={false} />
<Line type="monotone" dataKey={data.b} stroke="#556CD6" dot={false} />
</LineChart>
</ResponsiveContainer>
</React.Fragment>

最佳答案

只需在 LineChart 中声明两个 Line 和两个 YAxis components 并显式声明 yAxisId。这是我们的一个例子

<LineChart data={data} margin={{ left: -10, right: 10 }}>
<XAxis height={40} dataKey="part" tick={{ fontSize: 10 }}>
<Label
value='Período'
position='insideBottom'
fontSize={14}
fill='#676767'
/>
</XAxis>
<YAxis width={80} yAxisId="left" tick={{ fontSize: 10 }}>
<Label
value={selected.A}
angle={-90}
position='outside'
fill='#676767'
fontSize={14}
/>
</YAxis>
<YAxis width={80} yAxisId="right" orientation="right" tick={{ fontSize: 10, }}>
<Label
value={selected.B}
angle={-90}
position='outside'
fill='#676767'
fontSize={14}
/>
</YAxis>
<Tooltip formatter={(value) => floatParser(value)} />
<Line yAxisId="left" type="monotone" dataKey={selected.A} stroke={colors[1]} />
<Line yAxisId="right" type="monotone" dataKey={selected.B} stroke={colors[0]} />
</LineChart>

enter image description here

数据数组:

const data = [
{ part: 'foo', axisA: 21211, axisB: 1232 }
]

关于javascript - 多行 ReCharts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58133726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com