javascript - 如何修改 JSON 标签 React JS
问题描述
我正在尝试在 ReactJS 页面中添加图表。在同一页面中,我在表格中获得了数据,我希望这些数据与我的图表相同。我的图表是这样的:
export const myDataSourcePie ={
chart: {
caption: "Title",
subcaption: "Friends' chart",
startingangle: "120",
showlabels: "0",
showlegend: "1",
enablemultislicing: "0",
slicingdistance: "15",
showpercentvalues: "1",
showpercentintooltip: "0",
plottooltext: "Friend's email : $label Total messages : $datavalue",
theme: "ocean"
},
data: [
{
label: 'email',
value: '17',
},
{
label: 'email',
value: '100',
},
{
label: 'email',
value: '17',
},
{
label: 'email',
value: '17',
},
{
label: 'email',
value: '17',
},
{
label: 'email',
value: '17',
},
{
label: 'email',
value: '100',
},
{
label: 'email',
value: '17',
},
]
}
export const chartConfigs = {
type: 'pie3d',
width: 800,
height: 600,
dataFormat: 'json',
dataSource: myDataSourcePie
};
我导入它,然后用
<ReactFC {...chartConfigs} />
现在显然我的 JSON 中的数据不是正确的数据。我有一个“朋友”对象列表
friends:[
{"first_name": "name",
"surname": "surname",
"email": "foo@foo.com",
"date_of_birth": "1982-02-20",
"nationality": "Japan",
"messages": 5
},
{...},
{...},
..
]
我从中获取要放入表中的数据,并且我只想将此对象列表的两个标签放入图表中(电子邮件和消息)。我怎么能把这个值放在我的“myDataSourcePie”中?带道具?谢谢
解决方案
试试这个:
getFriendsChart()
{
let dataChart = [];
this.state.friends.forEach( function( friend ) {
dataChart.push(
{
label: friend.email,
value: friend.messages,
}
);
});
let tempConfig = {...chartConfigs};
tempConfig.dataSource.data = dataChart;
return tempConfig;
}
推荐阅读
- typescript - 带有返回类型注释的 TypeScript 过多属性错误,但在实现接口时没有
- laravel-5 - Laravel 5.4 找不到请求的 URL
- html - CSS过滤器导致定位问题
- javascript - 将数组格式化为对象 javascript
- android - Android音频播放器中的下一首歌曲按钮和上一首歌曲按钮
- javascript - 无法从另一个 javascript 文件的类访问一个 javascript 文件
- javascript - 使用multer从客户端获取详细信息后,如何在nodejs中以base64编码文件
- python - OpenCV - 用霍夫变换找不到垂直线
- visual-studio - 无法在 Visual Studio 2015+opencv3 上加载视频或图像
- magento - 我们如何调用身份验证意味着使用 Magento2 在 react-native 中登录/注册 api