ChartTabs.js 4.89 KB
Newer Older
Spark's avatar
Spark committed
1
import { useEffect, useState } from "react";
Spark's avatar
chartjs    
Spark committed
2
3
4
5
6
7
import { Card, Row, Tab, Tabs } from "react-bootstrap";
import ChartTemp from './ChartTemp';
import ChartHumidity from './ChartHumidity';
import ChartWindSpeed from './ChartWindSpeed';
import ChartPressure from './ChartPressure';
import '../App.css'
Spark's avatar
Spark committed
8
import { isLogined } from './../utils/Auth';
Spark's avatar
Spark committed
9
10
import axios from "axios";
import { routesClient } from './../routesClient';
Spark's avatar
chartjs    
Spark committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

function ChartTabs() {

    const cardstyled = {
        margin: 'auto',
        padding: '1em',
        display: 'flex',
        justifyContent: 'center',
        width: '100%',
        borderWidth: '3px',
        borderRadius: '20px',
        borderColor: 'rgb(110, 189, 142)',
        color: '#04AB70'
    }


Spark's avatar
Spark committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    const [temp, setTemp] = useState([]);
    const [press, setPress] = useState([]);
    const [humi, setHumi] = useState([])
    const [windSpd, setWindSpd] = useState([])

    const [newLabel, setNewLabel] = useState([]);
    const [tempNewLabel, setTempNewLabel] = useState([]);

    useEffect(() => {
        if (isLogined()) {
            axios.get(routesClient.userWeather, { withCredentials: true })
                .then((res) => {
                    console.log(res.data.contents.weather_in)
                    const userWeather = res.data.contents.weather_in
                    const userWeatherPredict = res.data.contents.weather_predict

                    const tempArray = []
                    const humiArray = []
                    const pressArray = []
                    const windspeedArray = []

                    const tempLabelArray = []
                    const labelArray = []

                    for (let i = 3; i < 9; i++) {
                        tempArray.push(userWeather[i].temp)
                        humiArray.push(userWeather[i].humi)
                        pressArray.push(userWeather[i].press)
                        windspeedArray.push(userWeather[i].wind_speed)

                        tempLabelArray.push(userWeather[i].collected_at.split('T')[1].split('.')[0])
                        labelArray.push(userWeather[i].collected_at.split('T')[1].split('.')[0])
                    }
                    for (let j = 0; j < 3; j++) {
                        tempArray.push(userWeatherPredict[j].temp)
                        
                        tempLabelArray.push(userWeatherPredict[j].collected_at.split('T')[1].split('.')[0])
                    }

                    setTemp(tempArray)
                    setHumi(humiArray)
                    setPress(pressArray)
                    setWindSpd(windspeedArray)

                    setTempNewLabel(tempLabelArray)
                    setNewLabel(labelArray)
                })
        }
        else {
            axios.get(routesClient.outsideLoc + `3743011`).then((res) => {
                const outWeather = res.data.contents.weather_out;

                const tempArray = []
                const humiArray = []
                const pressArray = []
                const windspeedArray = []

                const tempLabelArray = []
                const labelArray = []

                let i = outWeather.length - 9;
                for (i; i < outWeather.length; i++) {
                    tempArray.push(outWeather[i].temp)
                    humiArray.push(outWeather[i].humi)
                    pressArray.push(outWeather[i].press)
                    windspeedArray.push(outWeather[i].wind_speed)

                    tempLabelArray.push(outWeather[i].collected_at.split("T")[1].split(".")[0]);
                    labelArray.push(outWeather[i].collected_at.split("T")[1].split(".")[0]);
                }

                setTemp(tempArray)
                setHumi(humiArray)
                setPress(pressArray)
                setWindSpd(windspeedArray)

                setTempNewLabel(tempLabelArray);
                setNewLabel(labelArray);
            });
        }
    }, []);
Spark's avatar
Spark committed
108
109
    //3743011 default

Spark's avatar
Spark committed
110
111
112

    const [key, setKey] = useState('temp');

Spark's avatar
chartjs    
Spark committed
113
114
115
116
117
118
119
120
    return (
        <Row className='text-center w-100 my-2'>
            <Card style={cardstyled}>
                <Tabs
                    activeKey={key}
                    onSelect={(k) => setKey(k)}
                    className="mb-3" >
                    <Tab eventKey="temp" title="온도">
Spark's avatar
Spark committed
121
                        <ChartTemp temp={temp} newLabel={tempNewLabel} />
Spark's avatar
chartjs    
Spark committed
122
123
                    </Tab>
                    <Tab eventKey="humidity" title="습도">
Spark's avatar
Spark committed
124
                        <ChartHumidity humi={humi} newLabel={newLabel} />
Spark's avatar
chartjs    
Spark committed
125
126
                    </Tab>
                    <Tab eventKey="windspeed" title="풍속">
Spark's avatar
Spark committed
127
                        <ChartWindSpeed windSpd={windSpd} newLabel={newLabel} />
Spark's avatar
chartjs    
Spark committed
128
129
                    </Tab>
                    <Tab eventKey="pressure" title="기압">
Spark's avatar
Spark committed
130
                        <ChartPressure press={press} newLabel={newLabel} />
Spark's avatar
chartjs    
Spark committed
131
132
133
134
135
136
137
138
                    </Tab>
                </Tabs>
            </Card>
        </Row>
    )
}

export default ChartTabs;