gpt4 book ai didi

javascript - 如何在 Reactjs 中将服务器端 Json 数据转换为 react-google-chart 格式

转载 作者:行者123 更新时间:2023-12-03 20:46:16 25 4
gpt4 key购买 nike

我的后端有以下输出

[
{
"_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,
}}
我将不胜感激任何帮助。

最佳答案

以下是一些示例状态,应如下所示:

const data = [
["Element", "Density", { role: "style" }],
["Copper", 8.94, "#b87333"], // RGB value
["Silver", 10.49, "silver"], // English color name
["Gold", 19.3, "gold"],
["Platinum", 21.45, "color: #e5e4e2"] // CSS-style declaration
];
您需要进行一些数组操作来创建正确的数据状态。工作样本: https://codesandbox.io/s/react-google-charts-column-chart-forked-spjmz?file=/src/index.js:577-845

关于javascript - 如何在 Reactjs 中将服务器端 Json 数据转换为 react-google-chart 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65454699/

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