gpt4 book ai didi

javascript - 在图表中 react DataKey

转载 作者:行者123 更新时间:2023-11-29 21:12:58 25 4
gpt4 key购买 nike

我正在使用数据装置来填充用 Recharts 构建的图表

我的数据中有两个对象:FinancesDAU

const charts = [
{
name: 'Finances',
data:[
{name: 'April', Balance: 48530, saving: 0},
{name: 'May', Balance: 50328, saving: 1798},
{name: 'June', Balance: 48573, saving: -1755},
{name: 'July', Balance: 48825, saving: 252}
]
},
{
name: 'DAU',
data: [
{Day: '2016-10-28', Android: 27, iOS: 12},
{Day: '2016-10-29', Android: 20, iOS: 17},
{Day: '2016-10-30', Android: 17, iOS: 13},
{Day: '2016-10-31', Android: 15, iOS: 13},
{Day: '2016-11-01', Android: 14, iOS: 11}
]
}
];

export default charts;

此刻我正在通过 datakey仅针对第一个数据对象(财务)

<XAxis dataKey="name"/>和两行组件 <Line type="monotone" dataKey="Balance"/><Line type="monotone" dataKey="saving"/>

这是我的图表组件

export default class Chart extends React.Component {
render () {
const { chart, i } = this.props;
return (
<div>
<h1>{chart.name}</h1>
<LineChart width={600} height={300} data={chart.data}
margin={{top: 5, right: 30, left: 20, bottom: 5}}>
<XAxis dataKey="name"/>
<YAxis/>
<CartesianGrid strokeDasharray="3 3"/>
<Tooltip/>
<Legend />
<Line type="monotone" dataKey="Balance" stroke="#82ca9d" />
<Line type="monotone" dataKey="saving" stroke="#8884d8" activeDot={{r: 8}}/>
</LineChart>
</div>
);
}
};

我怎样才能通过 dataKey动态地在我的XAxisLines FinancesDAU 这两个对象的组成部分 ?

最佳答案

我会将键添加到图表对象,并将它们用作 Chart 组件内的 Prop 。像这样:

const charts = [
{
name: 'Finances',
xAxisKey: 'name',
line1Key: 'Balance',
line2Key: 'saving',
data:[
{name: 'April', Balance: 48530, saving: 0},
{name: 'May', Balance: 50328, saving: 1798},
{name: 'June', Balance: 48573, saving: -1755},
{name: 'July', Balance: 48825, saving: 252}
]
},
{
name: 'DAU',
xAxisKey: 'Day',
line1Key: 'Android',
line2Key: 'iOS',
data: [
{Day: '2016-10-28', Android: 27, iOS: 12},
{Day: '2016-10-29', Android: 20, iOS: 17},
{Day: '2016-10-30', Android: 17, iOS: 13},
{Day: '2016-10-31', Android: 15, iOS: 13},
{Day: '2016-11-01', Android: 14, iOS: 11}
]
}
];

和组件:

export default class Chart extends React.Component {
render () {
const { chart, i } = this.props;
return (
<div>
<h1>{chart.name}</h1>
<LineChart width={600} height={300} data={chart.data}
margin={{top: 5, right: 30, left: 20, bottom: 5}}>
<XAxis dataKey={chart.xAxisKey} />
<YAxis/>
<CartesianGrid strokeDasharray="3 3"/>
<Tooltip/>
<Legend />
<Line type="monotone" dataKey={chart.line1Key} stroke="#82ca9d" />
<Line type="monotone" dataKey={chart.line2Key} stroke="#8884d8" activeDot={{r: 8}}/>
</LineChart>
</div>
);
}
};

关于javascript - 在图表中 react DataKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40827798/

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