我的后端有以下输出
[
{
"_id": null,
"counts": [
{
"department": "Cleaning",
"number": 1
},
{
"department": "Engineering",
"number": 2
},
{
"department": "Transportation",
"number": 1
}
]
}
]
我想将以下数据转换为谷歌图表格式,然后将其显示在 reactjs 中的饼图上。
这是我第一次使用 google-charts,我真的不知道如何进行转换。根据我在网上阅读的内容,我实际上可以做这样的事情
class ChartPie extends React.Component {
state={
data:[]
}
componentDidMount(){
axios.get(`/api/v1/employee/departCount`)
.then(({data}) => { // destructure data from response object
// {department,number} from data
const restructuredData = data.map(({department, number}) =>
[department,number])
this.setState({data: restructuredData});
})
.catch(function (error) {
console.log(error);
})
}
如果是这种情况,那么我的另一个挑战将是如何利用谷歌图表 API 中的新状态
render() {
return (
<div>
<Chart
width={'500px'}
height={'300px'}
chartType="PieChart"
loader={<div>Loading Chart</div>}
data={[
// how do I add the new state ?
]}
options={{
title: 'Work data',
is3D: true,
}}
我将不胜感激任何帮助。