英文:
react-google-charts: Table has no columns and Cannot draw chart: no data specified
问题
我有一个项目,这个项目包含多个接口,其中有一个用于显示统计信息的接口。
为了显示统计信息,我使用了一个库:
react-google-charts
但我遇到了这个问题:
表格没有列
无法绘制图表:未指定数据
如何解决它?
请注意,打印函数返回:
这个文件包含饼图,
import { Button, Card, Col, Row, Space } from 'antd';
import { FunctionComponent, useContext, useEffect, useState } from 'react';
import ReactChart from '../../common/chart';
import { typeChart } from '../../common/chart/bar-chart/data/enum';
import '../../../styles/dashboard/index.scss';
import { AuthContext, IAuthContext } from '../../../contexts/auth-context';
import colors from '../../../constants/colors';
import calendar1 from '../../../assets/icons/calendar1-icon.svg';
import calendar2 from '../../../assets/icons/calendar2-icon.svg';
import calendar3 from '../../../assets/icons/calendar3-icon.svg';
import thermometer from '../../../assets/icons/thermometer-icon.svg';
import waving from '../../../assets/icons/waving-hand-icon.svg';
import { useNavigate } from 'react-router';
import { FormattedMessage } from 'react-intl';
import { useQuery } from 'react-query';
import statisticCharts from '../../../api/nuclearMedicineApi/services/Statistic';
import { BsFillSquareFill } from 'react-icons/bs';
import { Chart } from "react-google-charts";
interface DashboardProps { }
const Dashboard: FunctionComponent<DashboardProps> = () => {
const auth = useContext<IAuthContext>(AuthContext);
// **************************************************************
const chartsQueryCityData = useQuery(['chartsQueryCityData'], () =>
statisticCharts.GetTopographyStatistic({ FilterType: 2 }),
).data;
var cityTopography: any[] = [];
const [xyCitytotalTopography, setxyCitytotalTopography] = useState<any[]>([])
useEffect(() => {
// trying for City
chartsQueryCityData?.resultList.map((xy: any, index: any) => {
console.log('mmmm: ', xy);
if (xy?.x !== undefined && xy?.yPercent !== undefined) {
if (xy?.yPercent !== 0) {
let xyData: any[] = [xy?.x, xy?.yPercent];
cityTopography.push(xyData);
setxyCitytotalTopography(cityTopography);
console.log('xxxx: ', xyCitytotalTopography);
return cityTopography;
}
}
})
}, [chartsQueryTopographyData, chartsQueryCityData, chartsQueryAgeData]);
console.log('xxxx: ', xyCitytotalTopography);
const data: any[] = xyCitytotalTopography;
console.log('datadatadata: ', data);
const options = {
is3D: true,
};
// **************************************************************
const current = new Date();
const date = `${current.getFullYear()}/${current.getMonth() + 1
}/${current.getDate()}`;
const navigate = useNavigate();
return (
<>
<div className='dashdoard-donutData'>
<Row>
<Col className='mt-8' >
<Chart
chartType="PieChart"
data={data}
options={options}
width="100%"
height="400px"
/>
</Col>
</Row>
</div>
</>
);
};
export default Dashboard;
英文:
I have a project, and this project contains several interfaces, and among these interfaces, I have an interface for displaying statistics.
In order to display the statistics, I used a library:
react-google-charts
But I got this problem:
Table has no columns
Cannot draw chart: no data specified
how can i solve it?,
Note that the print function returns:
This file contains pie chart,
import { Button, Card, Col, Row, Space } from 'antd';
import { FunctionComponent, useContext, useEffect, useState } from 'react';
import ReactChart from '../../common/chart';
import { typeChart } from '../../common/chart/bar-chart/data/enum';
import '../../../styles/dashboard/index.scss';
import { AuthContext, IAuthContext } from '../../../contexts/auth-context';
import colors from '../../../constants/colors';
import calendar1 from '../../../assets/icons/calendar1-icon.svg';
import calendar2 from '../../../assets/icons/calendar2-icon.svg';
import calendar3 from '../../../assets/icons/calendar3-icon.svg';
import thermometer from '../../../assets/icons/thermometer-icon.svg';
import waving from '../../../assets/icons/waving-hand-icon.svg';
import { useNavigate } from 'react-router';
import { FormattedMessage } from 'react-intl';
import { useQuery } from 'react-query';
import statisticCharts from '../../../api/nuclearMedicineApi/services/Statistic';
import { BsFillSquareFill } from 'react-icons/bs';
import { Chart } from "react-google-charts";
interface DashboardProps { }
const Dashboard: FunctionComponent<DashboardProps> = () => {
const auth = useContext<IAuthContext>(AuthContext);
// **************************************************************
const chartsQueryCityData = useQuery(['chartsQueryCityData'], () =>
statisticCharts.GetTopographyStatistic({ FilterType: 2 }),
).data;
var cityTopography: any[] = [];
const [xyCitytotalTopography, setxyCitytotalTopography] = useState<any[]>([])
useEffect(() => {
// trying for City
chartsQueryCityData?.resultList.map((xy: any, index: any) => {
console.log('mmmm: ', xy);
if (xy?.x !== undefined && xy?.yPercent !== undefined) {
if (xy?.yPercent !== 0) {
let xyData: any[] = [xy?.x, xy?.yPercent];
cityTopography.push(xyData);
setxyCitytotalTopography(cityTopography);
console.log('xxxx: ', xyCitytotalTopography);
return cityTopography;
}
}
})
}, [chartsQueryTopographyData, chartsQueryCityData, chartsQueryAgeData]);
console.log('xxxx: ', xyCitytotalTopography);
const data: any[] = xyCitytotalTopography;
console.log('datadatadata: ', data);
const options = {
is3D: true,
};
// **************************************************************
const current = new Date();
const date = `${current.getFullYear()}/${current.getMonth() + 1
}/${current.getDate()}`;
const navigate = useNavigate();
return (
<>
<div className='dashdoard-donutData'>
<Row>
<Col className='mt-8' >
<Chart
chartType="PieChart"
data={data}
options={options}
width={"100%"}
height={"400px"}
/>
</Col>
</Row>
</Card>
</Col>
</Row>
</div>
</>
);
};
export default Dashboard;
答案1
得分: 1
以下是翻译好的代码部分:
数据必须以以下形式提供:
export const data = [
["任务", "每天小时数"],
["工作", 11],
["吃饭", 2],
["通勤", 2],
["看电视", 2],
["睡觉", 7],
];
这意味着“任务”和“每天小时数”的 **标题** 必须是字符串并且必须存在。
为此,添加一个空数组,其中包含空字符串 ['','']。
我所做的更新:
const [xyCitytotalTopography, setxyCitytotalTopography] = useState<any[]>([['', '']]);
// 尝试使用 ypercent 为 City 创建饼图
chartsQueryCityData?.resultList.map((xy: any, index: any) => {
if (xy?.x !== undefined && xy?.yPercent !== undefined) {
if (xy?.yPercent !== 0) {
let xyData: any[] = [xy?.x, xy?.yPercent];
cityPercent.push(xyData);
setxyCitytotalTopography(cityPercent);
return cityPercent;
}
}
})
<Col className='mt-4'>
<br />
<br />
<Chart
chartType="PieChart"
data={xyTopographyPercent}
options={options}
width={"130%"}
height={"500%"}
/>
</Col>
英文:
the data must be as this form:
export const data = [
["Task", "Hours per Day"],
["Work", 11],
["Eat", 2],
["Commute", 2],
["Watch TV", 2],
["Sleep", 7],
];
that means the header ["Task", "Hours per Day"] must be string and must be found,
for that add an empty array, with empty string ['', ''],
the updates that i do:
const [xyCitytotalTopography, setxyCitytotalTopography] = useState<any[]>([['', '']])
// trying for City with ypercent ****pie chart
chartsQueryCityData?.resultList.map((xy: any, index: any) => {
if (xy?.x !== undefined && xy?.yPercent !== undefined) {
if (xy?.yPercent !== 0) {
let xyData: any[] = [xy?.x, xy?.yPercent];
cityPercent.push(xyData);
setxyCitytotalTopography(cityPercent);
return cityPercent;
}
}
})
<Col className='mt-4'>
<br />
<br />
<Chart
chartType="PieChart"
data={xyTopographyPercent}
options={options}
width={"130%"}
height={"500%"}
/>
</Col>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论