react-google-charts: 表格没有列,无法绘制图表:未指定数据

huangapple go评论68阅读模式
英文:

react-google-charts: Table has no columns and Cannot draw chart: no data specified

问题

我有一个项目,这个项目包含多个接口,其中有一个用于显示统计信息的接口。

为了显示统计信息,我使用了一个库:

react-google-charts

但我遇到了这个问题:

表格没有列
无法绘制图表:未指定数据
如何解决它?

请注意,打印函数返回:

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:

react-google-charts: 表格没有列,无法绘制图表:未指定数据

This file contains pie chart,

import { Button, Card, Col, Row, Space } from &#39;antd&#39;;
import { FunctionComponent, useContext, useEffect, useState } from &#39;react&#39;;
import ReactChart from &#39;../../common/chart&#39;;
import { typeChart } from &#39;../../common/chart/bar-chart/data/enum&#39;;
import &#39;../../../styles/dashboard/index.scss&#39;;
import { AuthContext, IAuthContext } from &#39;../../../contexts/auth-context&#39;;
import colors from &#39;../../../constants/colors&#39;;
import calendar1 from &#39;../../../assets/icons/calendar1-icon.svg&#39;;
import calendar2 from &#39;../../../assets/icons/calendar2-icon.svg&#39;;
import calendar3 from &#39;../../../assets/icons/calendar3-icon.svg&#39;;
import thermometer from &#39;../../../assets/icons/thermometer-icon.svg&#39;;
import waving from &#39;../../../assets/icons/waving-hand-icon.svg&#39;;
import { useNavigate } from &#39;react-router&#39;;
import { FormattedMessage } from &#39;react-intl&#39;;
import { useQuery } from &#39;react-query&#39;;
import statisticCharts from &#39;../../../api/nuclearMedicineApi/services/Statistic&#39;;
import { BsFillSquareFill } from &#39;react-icons/bs&#39;;
import { Chart } from &quot;react-google-charts&quot;;
interface DashboardProps { }
const Dashboard: FunctionComponent&lt;DashboardProps&gt; = () =&gt; {
const auth = useContext&lt;IAuthContext&gt;(AuthContext);
// **************************************************************
const chartsQueryCityData = useQuery([&#39;chartsQueryCityData&#39;], () =&gt;
statisticCharts.GetTopographyStatistic({ FilterType: 2 }),
).data;
var cityTopography: any[] = [];
const [xyCitytotalTopography, setxyCitytotalTopography] = useState&lt;any[]&gt;([])
useEffect(() =&gt; {
// trying for City
chartsQueryCityData?.resultList.map((xy: any, index: any) =&gt; {
console.log(&#39;mmmm: &#39;, xy);
if (xy?.x !== undefined &amp;&amp; xy?.yPercent !== undefined) {
if (xy?.yPercent !== 0) {
let xyData: any[] = [xy?.x, xy?.yPercent];
cityTopography.push(xyData);
setxyCitytotalTopography(cityTopography);
console.log(&#39;xxxx: &#39;, xyCitytotalTopography);
return cityTopography;
}
}
})
}, [chartsQueryTopographyData, chartsQueryCityData, chartsQueryAgeData]);
console.log(&#39;xxxx: &#39;, xyCitytotalTopography);
const data: any[] = xyCitytotalTopography;
console.log(&#39;datadatadata: &#39;, data);
const options = {
is3D: true,
};
// **************************************************************
const current = new Date();
const date = `${current.getFullYear()}/${current.getMonth() + 1
}/${current.getDate()}`;
const navigate = useNavigate();
return (
&lt;&gt;
&lt;div className=&#39;dashdoard-donutData&#39;&gt;
&lt;Row&gt;
&lt;Col className=&#39;mt-8&#39; &gt;
&lt;Chart
chartType=&quot;PieChart&quot;
data={data}
options={options}
width={&quot;100%&quot;}
height={&quot;400px&quot;}
/&gt;
&lt;/Col&gt;
&lt;/Row&gt;
&lt;/Card&gt;
&lt;/Col&gt;
&lt;/Row&gt;
&lt;/div&gt;
&lt;/&gt;
);
};
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 = [
[&quot;Task&quot;, &quot;Hours per Day&quot;],
[&quot;Work&quot;, 11],
[&quot;Eat&quot;, 2],
[&quot;Commute&quot;, 2],
[&quot;Watch TV&quot;, 2],
[&quot;Sleep&quot;, 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&lt;any[]&gt;([[&#39;&#39;, &#39;&#39;]])
// trying for City with ypercent  ****pie chart 
chartsQueryCityData?.resultList.map((xy: any, index: any) =&gt; {
if (xy?.x !== undefined &amp;&amp; xy?.yPercent !== undefined) {
if (xy?.yPercent !== 0) {
let xyData: any[] = [xy?.x, xy?.yPercent];
cityPercent.push(xyData);
setxyCitytotalTopography(cityPercent);
return cityPercent;
}
}
})
&lt;Col className=&#39;mt-4&#39;&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;Chart
chartType=&quot;PieChart&quot;
data={xyTopographyPercent}
options={options}
width={&quot;130%&quot;}
height={&quot;500%&quot;}
/&gt;
&lt;/Col&gt;

huangapple
  • 本文由 发表于 2023年2月8日 14:50:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定