ChartWindSpeed.js 2.84 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
import { Col } from 'react-bootstrap';
Spark's avatar
Spark committed
4
5
6
7
import { Bar, Line } from 'react-chartjs-2'
import { callUserInfo } from '../utils/CheckDB';
import { routesClient } from './../routesClient';
import { isLogined } from './../utils/Auth';
Spark's avatar
chartjs    
Spark committed
8
9
10

function ChartWindSpeed() {

Spark's avatar
Spark committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    const [windSpd, setWindSpd] = useState([])
    const [newLabel, setNewLabel] = useState([])

    useEffect(() => {
        if (isLogined()) {
            callUserInfo().then((res) => {
                const outs = axios.get(routesClient.outsideLoc + res['loc_code'])
                console.log('>>', outs)
            })
        }
        else {
            const locDefault = localStorage.getItem('local-code')
            axios.get(routesClient.outsideLoc + locDefault)
                .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].wind_speed)
                        Array2.push(outWeather[i].collected_at.split('T')[1].split('.')[0])
                    }
                    setWindSpd(Array)
                    setNewLabel(Array2)
                })
        }
    }, [])

Spark's avatar
chartjs    
Spark committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    const options = {
        legend: {
            display: true, // label 보이기 여부
        },
        scales: {
            yAxes: [{
                ticks: {
                    min: 800, // y축 스케일에 대한 최소값 설정
                    stepSize: 1, // y축 그리드 한 칸당 수치
                }
            }]
        },

        // false : 사용자 정의 크기에 따라 그래프 크기가 결정됨.
        // true : 크기가 알아서 결정됨.
        maintainAspectRatio: false
    }
    const data = {
Spark's avatar
Spark committed
56
        labels: newLabel,
Spark's avatar
chartjs    
Spark committed
57
58
59
        datasets: [
            {
                label: '풍속',
Spark's avatar
Spark committed
60
61
                data: windSpd,
                lineTension: 0.1,
Spark's avatar
chartjs    
Spark committed
62
                borderWidth: '2',
Spark's avatar
Spark committed
63
64
                fill: true,
                backgroundColor: 'rgba(75,192,192,0.1)',
Spark's avatar
chartjs    
Spark committed
65
                borderColor: 'rgba(75,192,192,1)',
Spark's avatar
Spark committed
66
67
68
69
70
71
72
73
74
75
                borderCapStyle: 'round',
                pointBorderColor: 'rgba(75,192,192,1)',
                pointBackgroundColor: '#fff',
                pointBorderWidth: 5,
                pointHoverRadius: 5,
                pointHoverBackgroundColor: 'rgba(75,192,192,1)',
                pointHoverBorderColor: 'rgba(220,220,220,1)',
                pointHoverBorderWidth: 2,
                pointRadius: 1,
                pointHitRadius: 10,
Spark's avatar
chartjs    
Spark committed
76
77
78
79
80
81
            }
        ]
    };

    return (
        <Col id='chartTab'>
Spark's avatar
Spark committed
82
            <Line
Spark's avatar
chartjs    
Spark committed
83
84
85
86
87
88
89
90
                data={data}
                options={options}
            />
        </Col>
    )
};

export default ChartWindSpeed;