ChartTemp.js 2.88 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
5
6
7
import { callUserInfo } from '../utils/CheckDB';
import { useEffect } from 'react';
import axios from 'axios';
import { routesClient } from './../routesClient';
Spark's avatar
chartjs    
Spark committed
8
9

function ChartTemp() {
Spark's avatar
Spark committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

    const [temp, setTemp] = useState([])

    useEffect(() => {
        callUserInfo().then((res) => {
            const outs = axios.get(routesClient.outsideLoc + res['loc_code'])
            return outs
                .then((res) => {
                    const outWeather = res.data.contents.weather_out
                    console.log(res.data.contents.weather_out)
                    let i = 0;
                    // setTemp(res.data.contents.weather_out[0].temp)
                    const tempArray = []
                    for (i; i < 3; i++) {
                        console.log(i)
                        console.log(outWeather[i])
                        tempArray.push(outWeather[i].temp)
                    }
                    setTemp(tempArray)
                })
        })
    }, [])

    console.log(temp)

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

        // false : 사용자 정의 크기에 따라 그래프 크기가 결정됨.
        // true : 크기가 알아서 결정됨.
        maintainAspectRatio: false
    }
    const data = {
        labels: ['1', '2', '3', '4', '5', '6', '77', '88', '99'],
        datasets: [
            {
                label: '온도',
                borderWidth: '2',
Spark's avatar
Spark committed
58
                data: temp,
Spark's avatar
chartjs    
Spark committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
                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)'],
                backgroundColor: [
                    'rgba(75,192,192,0.4)',
                    'rgba(75,192,192,0.4)',
                    'rgba(75,192,192,0.4)',
                    'rgba(75,192,192,0.4)',
                    'rgba(75,192,192,0.4)',
                    'rgba(75,192,192,0.4)',
Spark's avatar
Spark committed
77

Spark's avatar
chartjs    
Spark committed
78
79
80
81
82
83
84
                    'rgba(191,191,191,0.4)',
                    'rgba(191,191,191,0.4)',
                    'rgba(191,191,191,0.4)']
            }
        ]
    };

Spark's avatar
Spark committed
85
86
87
88
89
90
91
92
93
94


    return (
        <Col id='chartTab'>
            <Bar
                data={data}
                options={options}
            />
        </Col>
    )
Spark's avatar
chartjs    
Spark committed
95
96
97
};

export default ChartTemp;