如何从RESTAPI绑定Google图表中的数据

2024-05-23 20:17:42 发布

您现在位置:Python中文网/ 问答频道 /正文

最近我尝试使用谷歌图表。在后端,我使用python&;数据库postgres。对于绑定数据,我编写了一个RESTAPI。并称之为我的前端(react)

getLeadSourceList = () => {
    axios.get( this.state.apiUrl+'/api/v1/SalesLead/getLeadsByLeadSource', {})
        .then((response) => {
            console.log("response",response.data.data);
            this.setState({ leadSourceList: response.data.data  });
        }).catch((error)=>{  console.log("error",error); this.setState({ leadSourceList: []  });   });
};

render() {
    const pieData = this.state.leadSourceList.map((lead, key) => {
         return <p>{lead.lead_source},
            {lead.count}</p>
     })
}

价值来自:

PBazar 9
Rashed 3
websource 4
Messenger 2
Fahme bhai 2

但是google chart期望这种类型的价值:

data={[
       ['Task', 'Hours per Day'],
       ['Work', 11],
       ['Eat', 2],
       ['Commute', 2],
       ]}

图表代码的其余部分:

<Chart
     width={'500px'}
     height={'300px'}
     chartType="PieChart"
     loader={<div>Loading Chart</div>}
     data={[
         ['Task', 'Hours per Day'],
         ['Work', 11],
         ['Eat', 2],
         ['Commute', 2],
         ]}
     options={{
     title: 'My Daily Activities',
             }}
     rootProps={{ 'data-testid': '1' }}
     />

所以我的问题是如何绑定谷歌图表所期望的数据?提前谢谢


Tags: 数据logtaskdataresponse图表errorthis