Analy.js 2.16 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
import React, { useState, useEffect } from 'react';
2
3
import ChartM from './ChartM';
import ChartY from './ChartY';
4
import chartApi from './db/chartData.api';
5
6
import ButtonsForm from './components/ButtonsForm';
import { View, Dimensions } from 'react-native';
Choi Ga Young's avatar
Choi Ga Young committed
7
const screenWidth = Dimensions.get("window").width;
8

Choi Ga Young's avatar
Choi Ga Young committed
9
const Analy = () => {
10
11
12
13
14
15
  const date = new Date();
  const [year, setYear] = useState(date.getFullYear());
  const [month, setMonth] = useState(date.getMonth());
  const [resDataM, setResDataM] = useState([]);
  const [resDataY, setResDataY] = useState([]);
  const [selectedIndex, setSelectedIndex] = useState(0);
Choi Ga Young's avatar
Choi Ga Young committed
16

17
  const getDataM = async () => {
18
    try {
19
20
21
      const resdata = await chartApi.outMoney({ year: year, month: month })
      console.log('db res M', resdata)
      return resdata
22
    } catch (error) {
23
      console.log('error in getDataM', error)
24
25
    }
  }
26
27
28
29
30
31
32
33
34
  const getDataY = async () => {
    try {
      const resdata = await chartApi.yearMoney({ year: year })
      console.log('db res Y', resdata)
      setResDataY(resdata)

    } catch (error) {
      console.log('error in getDataY', error)
    }
35
  }
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

  const calPercentage = async () => {
    const resPrice = await getDataM()
    //console.log('어떻게 생겼니', resPrice)
    let totalP = 0;

    for (let i = 0; i < resPrice.length; i++) {
      totalP = totalP + resPrice[i].total;
    }
    for (let j = 0; j < resPrice.length; j++) {
      resPrice[j].percentage = Math.round((resPrice[j].total / totalP) * 100);
    }
    setResDataM(resPrice)
  }

51
  useEffect(() => {
52
53
    getDataY()
    calPercentage()
54
  }, [])
55
56
57
58
59
60
61
62
  useEffect(() => {
    setResDataM([])
    calPercentage()
  }, [month])

  useEffect(() => {
    getDataY()
  }, [year])
Choi Ga Young's avatar
Choi Ga Young committed
63
  return (
64
65
66
67
68
69
70
71
72
73
74
75
76
    <>
      <View>
        <ButtonsForm
          onPress={(index) => setSelectedIndex(index)}
          selectedIndex={selectedIndex}
          group={["월간", "연간"]} />
      </View>
      <View>
        {
          selectedIndex === 0 ? <ChartM resDataM={resDataM} year={year} setYear={setYear} month={month} setMonth={setMonth} /> : <ChartY resDataY={resDataY} year={year} setYear={setYear} />
        }
      </View>
    </>
Choi Ga Young's avatar
Choi Ga Young committed
77
78
79
  )
}

80
export default Analy;