N.map不是一个继续显示的函数

问题描述 投票:0回答:1

我想生成一个饼图,但即使我的数组包含要在饼图上显示的所有必要信息(通过控制台日志),我仍然会出现此错误。我什至不知道我的代码中这个错误来自哪里:

这是我的饼图组件:

    import { Box } from "@mui/material";
    import Header from "../components/Header";
    import PieChart from "../components/PieChart";
    import { useEffect, useState } from "react";
    import { mockDistrictData } from "../data/MockData";

    const Pie = () => {
        const [districts, setDistricts] = useState([]);
        const [selectedDistrict, setSelectedDistrict] = useState('');
        const [finalData, setFinalData] = useState([]);
        const lightGreenColor = "hsl(154,52%,45%)";
        const greenColor = "hsl(139,100%,83%)";
        const [isGeneratingChart, setIsGeneratingChart] = useState(false);

        // Fetch data for mechanical bikes
        const fetchMechanicalBikes = (districtName) => {
            const url = `http://localhost:8800/station_status/bikes/mechanical/${districtName}`;
            return fetch(url)
                .then(response => response.json())
                .then(data => {
                    // Transform the fetched data and assign random colors
                    return data.map(i => ({
                        id: `Mechanical bikes`,
                        label: `Mechanical bikes`,
                        value: i.value,
                        color: lightGreenColor
                    }));
                });
        };

        // Fetch data for electronic bikes
        const fetchElectronicBikes = (districtName) => {
            const url = `http://localhost:8800/station_status/bikes/electric/${districtName}`;
            return fetch(url)
                .then(response => response.json())
                .then(data => {
                    // Transform the fetched data and assign random colors
                    return data.map(item => ({
                        id: `Electronic bikes`,
                        label: `Electronic bikes`,
                        value: item.value,
                        color: greenColor
                    }));
                });
        };

        // Combine the fetched data for mechanical and electronic bikes
        const combineFetchedData = async (districtName) => {
            try {
                setIsGeneratingChart(true); // Set the flag to indicate chart generation in progress
                const mechanicalData = await fetchMechanicalBikes(districtName);
                const electronicData = await fetchElectronicBikes(districtName);

                // Combine the two arrays into a single array
                const combinedData = [...mechanicalData, ...electronicData];

                setTimeout(() => {
                    setFinalData(combinedData);
                    console.log(combinedData); // Print the combined data
                    setIsGeneratingChart(false); // Set the flag to indicate chart generation completed
                }, 3000); // 3 seconds delay

                // You can use the combined data for further processing or visualization

            } catch (error) {
                console.error('Error fetching data: ', error);
                setIsGeneratingChart(false); // Set the flag to indicate chart generation completed with an error
            }
        };

        useEffect(() => {
            // Fetch districts
            const fetchDistricts = () => {
                setDistricts(mockDistrictData.map(district => district.district_name));
            };

            fetchDistricts();
        }, []);

        return (
            <Box m="20px">
                <Header title="Pie Chart" subtitle="Electric Bikes & Mechanical Bikes" />
                <Box height="75vh">
                    <div>
                        <select value={selectedDistrict} onChange={e => setSelectedDistrict(e.target.value)}>
                            <option value="">Select a district</option>
                            {districts.map(district => (
                                <option key={district} value={district}>{district}</option>
                            ))}
                        </select>
                        <button disabled={isGeneratingChart} onClick={() => combineFetchedData(selectedDistrict)}>
                            {isGeneratingChart ? 'Generating...' : 'Generate Chart'}
                        </button>
                    </div>
                    {finalData.length > 0 && <PieChart data={finalData} />}
                </Box>
            </Box>
        );
    };

    export default Pie;

这是我的饼图组件:

    import { ResponsivePie } from "@nivo/pie";
    import { tokens } from "../theme";
    import { useTheme } from "@mui/material";
    const PieChart = (finalData) => {
        const theme = useTheme();
        const colors = tokens(theme.palette.mode);

        return (
            <ResponsivePie
                data={finalData}
                theme={{
                    axis: {
                        domain: {
                            line: {
                                stroke: colors.grey[100],
                            },
                        },
                        legend: {
                            text: {
                                fill: colors.grey[100],
                            },
                        },
                        ticks: {
                            line: {
                                stroke: colors.grey[100],
                                strokeWidth: 1,
                            },
                            text: {
                                fill: colors.grey[100],
                            },
                        },
                    },
                    legends: {
                        text: {
                            fill: colors.grey[100],
                        },
                    },
                }}
                margin={{ top: 40, right: 80, bottom: 80, left: 80 }}
                innerRadius={0.5}
                padAngle={0.7}
                cornerRadius={3}
                activeOuterRadiusOffset={8}
                borderColor={{
                    from: "color",
                    modifiers: [["darker", 0.2]],
                }}
                arcLinkLabelsSkipAngle={10}
                arcLinkLabelsTextColor={colors.grey[100]}
                arcLinkLabelsThickness={2}
                arcLinkLabelsColor={{ from: "color" }}
                enableArcLabels={false}
                arcLabelsRadiusOffset={0.4}
                arcLabelsSkipAngle={7}
                arcLabelsTextColor={{
                    from: "color",
                    modifiers: [["darker", 2]],
                }}
                defs={[
                    {
                        id: "dots",
                        type: "patternDots",
                        background: "inherit",
                        color: "rgba(255, 255, 255, 0.3)",
                        size: 4,
                        padding: 1,
                        stagger: true,
                    },
                    {
                        id: "lines",
                        type: "patternLines",
                        background: "inherit",
                        color: "rgba(255, 255, 255, 0.3)",
                        rotation: -45,
                        lineWidth: 6,
                        spacing: 10,
                    },
                ]}
                legends={[
                    {
                        anchor: "bottom",
                        direction: "row",
                        justify: false,
                        translateX: 0,
                        translateY: 56,
                        itemsSpacing: 0,
                        itemWidth: 100,
                        itemHeight: 18,
                        itemTextColor: "#999",
                        itemDirection: "left-to-right",
                        itemOpacity: 1,
                        symbolSize: 18,
                        symbolShape: "circle",
                        effects: [
                            {
                                on: "hover",
                                style: {
                                    itemTextColor: "#000",
                                },
                            },
                        ],
                    },
                ]}
            />
        );
    };

    export default PieChart;

有人可以帮我解决这个问题吗?

reactjs material-ui fetch-api pie-chart
1个回答
1
投票

您的 PieChart 组件接收 props,而不是

finalData
参数。

<PieChart data={finalData} />

// finalData here is a props object that has a data field
const PieChart = (finalData) => {
 ...
 <ResponsivePie
    data={finalData} // finalData isn't what you appear to think it is.

建议修复:

const PieChart = (props) => {
 ...
<ResponsivePie
  data={props.data}
  ...

或者如果您愿意,您可以从道具中解构

data

const PieChart = ({ data }) => {
 ...
<ResponsivePie
  data={data}
  ...

ResponsivePie
期望数据是一个数组,并尝试在其上调用映射。 (我假设。)

© www.soinside.com 2019 - 2024. All rights reserved.