N.map is not a function keep on showing

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

N.map is not a function keep on showing

问题

Sure, here's the translated code without the comments:

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);

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

    const fetchElectronicBikes = (districtName) => {
        const url = `http://localhost:8800/station_status/bikes/electric/${districtName}`;
        return fetch(url)
            .then(response => response.json())
            .then(data => {
                return data.map(item => ({
                    id: `Electronic bikes`,
                    label: `Electronic bikes`,
                    value: item.value,
                    color: greenColor
                }));
            });
    };

    const combineFetchedData = async (districtName) => {
        try {
            setIsGeneratingChart(true);
            const mechanicalData = await fetchMechanicalBikes(districtName);
            const electronicData = await fetchElectronicBikes(districtName);

            const combinedData = [...mechanicalData, ...electronicData];

            setTimeout(() => {
                setFinalData(combinedData);
                setIsGeneratingChart(false);
            }, 3000);

        } catch (error) {
            console.error('Error fetching data: ', error);
            setIsGeneratingChart(false);
        }
    };

    useEffect(() => {
        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;
英文:

I want to generate a Pie Chart, but I keep on having this error even if my array is containing all the necessary information to be displayed on the Pie Chart (via the console log). I don't even know where this error come from on my code:

N.map is not a function keep on showing
N.map is not a function keep on showing

here is my Pie component :

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

    const Pie = () =&gt; {
        const [districts, setDistricts] = useState([]);
        const [selectedDistrict, setSelectedDistrict] = useState(&#39;&#39;);
        const [finalData, setFinalData] = useState([]);
        const lightGreenColor = &quot;hsl(154,52%,45%)&quot;;
        const greenColor = &quot;hsl(139,100%,83%)&quot;;
        const [isGeneratingChart, setIsGeneratingChart] = useState(false);

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

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

        // Combine the fetched data for mechanical and electronic bikes
        const combineFetchedData = async (districtName) =&gt; {
            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(() =&gt; {
                    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(&#39;Error fetching data: &#39;, error);
                setIsGeneratingChart(false); // Set the flag to indicate chart generation completed with an error
            }
        };

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

            fetchDistricts();
        }, []);

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

    export default Pie;

here is my PieChart component :

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

        return (
            &lt;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: &quot;color&quot;,
                    modifiers: [[&quot;darker&quot;, 0.2]],
                }}
                arcLinkLabelsSkipAngle={10}
                arcLinkLabelsTextColor={colors.grey[100]}
                arcLinkLabelsThickness={2}
                arcLinkLabelsColor={{ from: &quot;color&quot; }}
                enableArcLabels={false}
                arcLabelsRadiusOffset={0.4}
                arcLabelsSkipAngle={7}
                arcLabelsTextColor={{
                    from: &quot;color&quot;,
                    modifiers: [[&quot;darker&quot;, 2]],
                }}
                defs={[
                    {
                        id: &quot;dots&quot;,
                        type: &quot;patternDots&quot;,
                        background: &quot;inherit&quot;,
                        color: &quot;rgba(255, 255, 255, 0.3)&quot;,
                        size: 4,
                        padding: 1,
                        stagger: true,
                    },
                    {
                        id: &quot;lines&quot;,
                        type: &quot;patternLines&quot;,
                        background: &quot;inherit&quot;,
                        color: &quot;rgba(255, 255, 255, 0.3)&quot;,
                        rotation: -45,
                        lineWidth: 6,
                        spacing: 10,
                    },
                ]}
                legends={[
                    {
                        anchor: &quot;bottom&quot;,
                        direction: &quot;row&quot;,
                        justify: false,
                        translateX: 0,
                        translateY: 56,
                        itemsSpacing: 0,
                        itemWidth: 100,
                        itemHeight: 18,
                        itemTextColor: &quot;#999&quot;,
                        itemDirection: &quot;left-to-right&quot;,
                        itemOpacity: 1,
                        symbolSize: 18,
                        symbolShape: &quot;circle&quot;,
                        effects: [
                            {
                                on: &quot;hover&quot;,
                                style: {
                                    itemTextColor: &quot;#000&quot;,
                                },
                            },
                        ],
                    },
                ]}
            /&gt;
        );
    };

    export default PieChart;

Can somebody help me solve the problem please ?

答案1

得分: 1

Your PieChart component receives props, not a finalData argument.

Suggested fix:

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

Or you could destructure data from props if you prefer:

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

ResponsivePie expects data to be an array and is attempting to call map on it. (I assume.)

英文:

Your PieChart component receives props, not a finalData argument.

&lt;PieChart data={finalData} /&gt;

// finalData here is a props object that has a data field
const PieChart = (finalData) =&gt; {
 ...
 &lt;ResponsivePie
    data={finalData} // finalData isn&#39;t what you appear to think it is.

Suggested fix:

const PieChart = (props) =&gt; {
 ...
&lt;ResponsivePie
  data={props.data}
  ...

Or you could destructure data from props if you prefer:

const PieChart = ({ data }) =&gt; {
 ...
&lt;ResponsivePie
  data={data}
  ...

ResponsivePie expects data to be an array and is attempting to call map on it. (I assume.)

huangapple
  • 本文由 发表于 2023年5月22日 07:08:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302275.html
匿名

发表评论

匿名网友

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

确定