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

function ChartTemp() {
KangMin An's avatar
KangMin An committed
11
  const [temp, setTemp] = useState([]);
KangMin An's avatar
KangMin An committed
12
  const [newLabel, setNewLabel] = useState([]);
Spark's avatar
Spark committed
13

KangMin An's avatar
KangMin An committed
14
  useEffect(() => {
KangMin An's avatar
KangMin An committed
15
16
17
18
19
20
21
22
    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) => {
KangMin An's avatar
KangMin An committed
23
        const outWeather = res.data.contents.weather_out;
KangMin An's avatar
KangMin An committed
24
25
26
27
28
29
30
        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]);
KangMin An's avatar
KangMin An committed
31
        }
KangMin An's avatar
KangMin An committed
32
33
        setTemp(Array);
        setNewLabel(Array2);
KangMin An's avatar
KangMin An committed
34
      });
KangMin An's avatar
KangMin An committed
35
    }
KangMin An's avatar
KangMin An committed
36
  }, []);
Spark's avatar
Spark committed
37

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

KangMin An's avatar
KangMin An committed
53
54
55
56
57
    // false : 사용자 정의 크기에 따라 그래프 크기가 결정됨.
    // true : 크기가 알아서 결정됨.
    maintainAspectRatio: false,
  };
  const data = {
KangMin An's avatar
KangMin An committed
58
    labels: newLabel,
KangMin An's avatar
KangMin An committed
59
60
61
62
63
64
65
66
67
68
69
70
    datasets: [
      {
        label: "온도",
        borderWidth: "2",
        data: temp,
        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)",
Spark's avatar
chartjs    
Spark committed
71

KangMin An's avatar
KangMin An committed
72
73
74
75
          "rgba(191,191,191,1)",
          "rgba(191,191,191,1)",
          "rgba(191,191,191,1)",
        ],
Spark's avatar
chartjs    
Spark committed
76

KangMin An's avatar
KangMin An committed
77
        backgroundColor: [
KangMin An's avatar
KangMin An committed
78
79
80
81
82
83
          "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
84

KangMin An's avatar
KangMin An committed
85
86
87
          "rgba(191,191,191,0.1)",
          "rgba(191,191,191,0.1)",
          "rgba(191,191,191,0.1)",
KangMin An's avatar
KangMin An committed
88
89
90
91
        ],
      },
    ],
  };
Spark's avatar
Spark committed
92

KangMin An's avatar
KangMin An committed
93
94
95
96
97
98
  return (
    <Col id="chartTab">
      <Bar data={data} options={options} />
    </Col>
  );
}
Spark's avatar
chartjs    
Spark committed
99

KangMin An's avatar
KangMin An committed
100
export default ChartTemp;