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

function ChartTemp() {
Spark's avatar
Spark committed
11
12

    const [temp, setTemp] = useState([])
Spark's avatar
Spark committed
13
    const [newLabel, setNewLabel] = useState([])
Spark's avatar
Spark committed
14
15

    useEffect(() => {
Spark's avatar
Spark committed
16
17
18
19
20
21
22
23
24
        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)
Spark's avatar
Spark committed
25
26
                .then((res) => {
                    const outWeather = res.data.contents.weather_out
Spark's avatar
Spark committed
27
28
29
30
31
32
33
                    const Array = []
                    const Array2 = []
                    console.log(outWeather)
                    let i = outWeather.length - 9
                    for (i; i < outWeather.length; i++) {
                        Array.push(outWeather[i].temp)
                        Array2.push(outWeather[i].collected_at.split('T')[1].split('.')[0])
Spark's avatar
Spark committed
34
                    }
Spark's avatar
Spark committed
35
36
                    setTemp(Array)
                    setNewLabel(Array2)
Spark's avatar
Spark committed
37
                })
Spark's avatar
Spark committed
38
        }
Spark's avatar
Spark committed
39
40
    }, [])

Spark's avatar
Spark committed
41
42
43



Spark's avatar
Spark committed
44

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

        // false : 사용자 정의 크기에 따라 그래프 크기가 결정됨.
        // true : 크기가 알아서 결정됨.
        maintainAspectRatio: false
    }
    const data = {
Spark's avatar
Spark committed
63
        labels: newLabel,
Spark's avatar
chartjs    
Spark committed
64
65
66
67
        datasets: [
            {
                label: '온도',
                borderWidth: '2',
Spark's avatar
Spark committed
68
                data: temp,
Spark's avatar
chartjs    
Spark committed
69
70
71
72
73
74
75
76
77
78
79
                borderColor: [
                    'rgba(75,192,192,1)',
                    'rgba(75,192,192,1)',
                    'rgba(75,192,192,1)',
                    'rgba(75,192,192,1)',
                    'rgba(75,192,192,1)',
                    'rgba(75,192,192,1)',

                    'rgba(191,191,191,1)',
                    'rgba(191,191,191,1)',
                    'rgba(191,191,191,1)'],
Spark's avatar
Spark committed
80
                
Spark's avatar
chartjs    
Spark committed
81
                backgroundColor: [
Spark's avatar
Spark committed
82
83
84
85
86
87
                    'rgba(75,192,192,0.1)',
                    'rgba(75,192,192,0.1)',
                    'rgba(75,192,192,0.1)',
                    'rgba(75,192,192,0.1)',
                    'rgba(75,192,192,0.1)',
                    'rgba(75,192,192,0.1)',
Spark's avatar
Spark committed
88

Spark's avatar
Spark committed
89
90
91
                    'rgba(191,191,191,0.1)',
                    'rgba(191,191,191,0.1)',
                    'rgba(191,191,191,0.1)']
Spark's avatar
chartjs    
Spark committed
92
93
94
95
            }
        ]
    };

Spark's avatar
Spark committed
96
97
98
99
100
101
102
103
104
105


    return (
        <Col id='chartTab'>
            <Bar
                data={data}
                options={options}
            />
        </Col>
    )
Spark's avatar
chartjs    
Spark committed
106
107
108
};

export default ChartTemp;