ChartHumidity.js 3.43 KB
Newer Older
Spark's avatar
Spark committed
1
2
import axios from 'axios';
import React, { useState, useEffect } from 'react'
Spark's avatar
chartjs    
Spark committed
3
4
import { Col } from 'react-bootstrap';
import { Bar, Line } from 'react-chartjs-2'
Spark's avatar
Spark committed
5
6
7
import { callUserInfo } from '../utils/CheckDB';
import { routesClient } from './../routesClient';
import { isLogined } from './../utils/Auth';
Spark's avatar
chartjs    
Spark committed
8
9

function ChartHumidity() {
Spark's avatar
layout    
Spark committed
10

Spark's avatar
Spark committed
11
12
13
14
15
    const [humi, setHumi] = useState([])
    const [newLabel, setNewLabel] = useState([])

    useEffect(() => {
        if (isLogined()) {
Spark's avatar
Spark committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
            axios.get(routesClient.userWeather, { withCredentials: true })
                .then((res) => {
                    console.log('humi', res.data.contents.weather_in)
                    const userWeather = res.data.contents.weather_in
                    const Array = []
                    const Array2 = []
                    for (let i = 0; i < userWeather.length; i++) {
                        Array.push(userWeather[i].humi)
                        Array2.push(userWeather[i].collected_at.split('T')[1].split('.')[0])
                    }
                    setHumi(Array)
                    setNewLabel(Array2)

                })
Spark's avatar
Spark committed
30
31
        }
        else {
Spark's avatar
Spark committed
32
            axios.get(routesClient.outsideLoc + `3743011`)
Spark's avatar
Spark committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
                .then((res) => {
                    const outWeather = res.data.contents.weather_out
                    const Array = []
                    const Array2 = []
                    for (let i = 0; i < outWeather.length; i++) {
                        Array.push(outWeather[i].humi)
                        Array2.push(outWeather[i].collected_at.split('T')[1].split('.')[0])
                        // const colHour = outWeather[i].collected_at.split('T')[1].split('.')[0].split(':')[0]
                        // const colMin = outWeather[i].collected_at.split('T')[1].split('.')[0].split(':')[1]
                    }
                    setHumi(Array)
                    setNewLabel(Array2)
                })
        }
    }, [])

Spark's avatar
Spark committed
49

Spark's avatar
layout    
Spark committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    const options = {
        legend: {
            display: true, // label 보이기 여부
        },
        scales: {
            yAxes: [{
                ticks: {
                    min: 0, // y축 스케일에 대한 최소값 설정
                    stepSize: 1, // y축 그리드 한 칸당 수치
                }
            }]
        },

        // false : 사용자 정의 크기에 따라 그래프 크기가 결정됨.
        // true : 크기가 알아서 결정됨.
        maintainAspectRatio: false
    }
    const data = {
Spark's avatar
Spark committed
68
        labels: newLabel,
Spark's avatar
layout    
Spark committed
69
        datasets: [
70
            {
Spark's avatar
chartjs    
Spark committed
71
                label: '습도',
Spark's avatar
Spark committed
72
                data: humi,
73
                lineTension: 0.1,
Spark's avatar
Spark committed
74
75
76
                borderWidth: '2',
                fill: true,
                backgroundColor: 'rgba(75,192,192,0.1)',
77
                borderColor: 'rgba(75,192,192,1)',
Spark's avatar
Spark committed
78
                borderCapStyle: 'round',
79
80
                pointBorderColor: 'rgba(75,192,192,1)',
                pointBackgroundColor: '#fff',
Spark's avatar
Spark committed
81
                pointBorderWidth: 5,
82
83
84
85
86
87
88
                pointHoverRadius: 5,
                pointHoverBackgroundColor: 'rgba(75,192,192,1)',
                pointHoverBorderColor: 'rgba(220,220,220,1)',
                pointHoverBorderWidth: 2,
                pointRadius: 1,
                pointHitRadius: 10,
            }
Spark's avatar
layout    
Spark committed
89
        ]
90
    };
Spark's avatar
layout    
Spark committed
91
92

    return (
Spark's avatar
chartjs    
Spark committed
93
94
95
96
97
98
        <Col id='chartTab'>
            <Line
                data={data}
                options={options}
            />
        </Col>
Spark's avatar
layout    
Spark committed
99
    )
Spark's avatar
chartjs    
Spark committed
100
};
Spark's avatar
layout    
Spark committed
101

Spark's avatar
chartjs    
Spark committed
102
export default ChartHumidity;