Analy.js 3.83 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
import React, { useState, useEffect } from 'react';
2
3
import { SafeAreaView, FlatList, View, Text, StyleSheet, Dimensions } from 'react-native';
import chartApi from './db/chartData.api';
Choi Ga Young's avatar
Choi Ga Young committed
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {
  LineChart,
  BarChart,
  PieChart,
  ProgressChart,
  ContributionGraph,
  StackedBarChart
} from "react-native-chart-kit";
const screenWidth = Dimensions.get("window").width;
const screenHeight = Dimensions.get("window").height;
const data1 = [
  {
    name: "Seoul",
    population: 21500000,
    color: "rgba(131, 167, 234, 1)",
    legendFontColor: "#7F7F7F",
    legendFontSize: 15
  },
  {
    name: "Toronto",
    population: 2800000,
    color: "#d8bfd8",
    legendFontColor: "#7F7F7F",
    legendFontSize: 15
  },
  {
    name: "Beijing",
    population: 1276120,
    color: "#87ceeb",
    legendFontColor: "#7F7F7F",
    legendFontSize: 15
  },
  {
    name: "New York",
    population: 8538000,
    color: "#fff5ee",
    legendFontColor: "#7F7F7F",
    legendFontSize: 15
  },
  {
    name: "Moscow",
    population: 11920000,
    color: "#b0c4de",
    legendFontColor: "#7F7F7F",
    legendFontSize: 15
  }
];
const data2 = {
  labels: ["Jan", "Feb", "Mar", "April", "May", "June"],
  datasets: [
    {
      data: [20, 45, 28, 80, 99, 43]
    }
  ]
};

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const ChartItem = ({ item }) => {
  return (
    <>
      <View style={{
        flexDirection: "row", padding: "5%", borderColor: '#d3d3d3', //light grey
        borderWidth: 1, borderTopWidth: 0,
      }}>
        <Text style={[style.itemText, style.Font]}>{item.name}</Text>
        <Text style={[style.itemText, style.Font]}>{item.total}</Text>
        <Text></Text>
      </View>
    </>
  );
};

Choi Ga Young's avatar
Choi Ga Young committed
75
76
77
const Analy = () => {
  const chartConfig = {
    backgroundGradientFrom: "#abbcd6", //좌측 색
78
    backgroundGradientFromOpacity: 0.5,
Choi Ga Young's avatar
Choi Ga Young committed
79
80
81
82
83
84
85
86
87
88
    backgroundGradientTo: "#E6DDC5", // 우측 색
    backgroundGradientToOpacity: 0.2,
    backgroundColor: "#ffffff", // 어디에 적용된건지 잘 모르겠음
    color: (opacity = 1) => `rgba(0, 93, 232, ${opacity})`, // data의 색상 계산할 때 사용하는 함수
    //color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
    strokeWidth: 2, // optional, default 3
    barPercentage: 0.5, // 그래프 width
    useShadowColorFromDataset: false,// optional, default is false
    //fillShadowGradient: 'blue',
    fillShadowGradientOpacity: 1, // 좀 더 진하게만 할 뿐 단색 설정은 못하는 것 같음
Choi Ga Young's avatar
Choi Ga Young committed
89

Choi Ga Young's avatar
Choi Ga Young committed
90
  };
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  const [resData, setResData] = useState([])
  const getData = async () => {
    try {
      const resdata = await chartApi.outMoney({ year: 2021, month: 6 })
      console.log('db res', resdata)
      setResData(resdata)
    } catch (error) {
      console.log('error in getData', error)
    }
  }
  const renderChart = ({ item }) => {
    return (
      <ChartItem item={item} />
    )
  }
  useEffect(() => {
    getData()
  }, [])
Choi Ga Young's avatar
Choi Ga Young committed
109
  return (
Choi Ga Young's avatar
Choi Ga Young committed
110
    <View>
Choi Ga Young's avatar
Choi Ga Young committed
111
112
      <Text style={style.Font}>여기는 분석 페이지/ 차트 테스트</Text>
      <PieChart
113
        data={resData}
Choi Ga Young's avatar
Choi Ga Young committed
114
115
116
        width={screenWidth}
        height={screenHeight / 3} //그래프의 높이가 커지기만
        chartConfig={chartConfig}
117
        accessor={"total"}
Choi Ga Young's avatar
Choi Ga Young committed
118
119
120
121
122
        backgroundColor={"#ffffff"}
        // paddingLeft={"15"}
        // center={[10, 50]}
        absolute
      />
123
124
125
126
127
128
129
      <FlatList
        style={{ backgroundColor: "#FFFFFF" }}
        data={resData}
        renderItem={renderChart}
        keyExtractor={item => item.category}
      />
      {/* <BarChart
Choi Ga Young's avatar
Choi Ga Young committed
130
131
132
133
134
135
136
        //style={graphStyle}
        data={data2}
        width={screenWidth}
        height={screenHeight / 3}
        //yAxisLabel="$"
        chartConfig={chartConfig}
      //verticalLabelRotation={30}
137
      /> */}
Choi Ga Young's avatar
Choi Ga Young committed
138
139
140
141
142
    </View>
  )
}
const style = StyleSheet.create({
  Font: {
143
144
145
146
147
148
    fontSize: 20
  },
  itemText: {
    paddingRight: "10%",
    // paddingLeft: "20%",
    // alignItems:"center"
Choi Ga Young's avatar
Choi Ga Young committed
149
150
151
152
  }
});

export default Analy;