ChartM.js 3.25 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
import React from 'react';
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { FlatList, View, Text, StyleSheet, Dimensions } from 'react-native';
import { Button } from 'react-native-elements';
import Ionicons from 'react-native-vector-icons/Ionicons';
import { PieChart } from "react-native-chart-kit";

const screenWidth = Dimensions.get("window").width;
const screenHeight = Dimensions.get("window").height;

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>
Choi Ga Young's avatar
Choi Ga Young committed
18
        <Text style={[style.itemTextNum, style.Font]}>{(item.total).toLocaleString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</Text>
Choi Ga Young's avatar
Choi Ga Young committed
19
        <Text style={[style.itemTextNum, style.Font]}>{item.percentage}%</Text>
20
21
22
23
24
25
26
27
28
29
30
31
32
      </View>
    </>
  );
};

const ChartM = ({
  resDataM,
  year,
  setYear,
  month,
  setMonth
}) => {
  const chartConfig = {
Choi Ga Young's avatar
Choi Ga Young committed
33
    backgroundGradientFrom: "#abbcd6",
34
    backgroundGradientFromOpacity: 0.5,
Choi Ga Young's avatar
Choi Ga Young committed
35
    backgroundGradientTo: "#E6DDC5",
36
    backgroundGradientToOpacity: 0.2,
Choi Ga Young's avatar
Choi Ga Young committed
37
    color: (opacity = 1) => `rgba(0, 93, 232, ${opacity})`,
38
39
40
    strokeWidth: 2, // optional, default 3
    barPercentage: 0.5, // 그래프 width
    useShadowColorFromDataset: false,// optional, default is false
Choi Ga Young's avatar
Choi Ga Young committed
41
    fillShadowGradientOpacity: 1,
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

  };

  const renderChart = ({ item }) => {
    return (
      <ChartItem item={item} />
    )
  }
  const prevBtn = () => {
    if (month < 1) {
      setYear(year - 1)
      setMonth(11)
    } else {
      setMonth(month - 1)
    }
  };
  const nextBtn = () => {
    if (month > 10) {
      setYear(year + 1)
      setMonth(0)
    } else {
      setMonth(month + 1)
    }
  }
  return (
    <>
Choi Ga Young's avatar
Choi Ga Young committed
68
      <View style={{ flexDirection: 'row', justifyContent: "center", alignItems: "center" }}>
69
        <Button icon={
Choi Ga Young's avatar
Choi Ga Young committed
70
          <Ionicons name='chevron-back-sharp' onPress={prevBtn} size={20} color='black' />
71
        } type='clear' />
Choi Ga Young's avatar
Choi Ga Young committed
72
        <Text style={{ fontFamily: 'GowunDodum-Regular', fontSize: 20 }}>{year}.{month + 1}</Text>
73
        <Button icon={
Choi Ga Young's avatar
Choi Ga Young committed
74
          <Ionicons name='chevron-forward-sharp' onPress={nextBtn} size={20} color='black' />
75
76
77
78
        } type='clear' />
      </View>
      <View>
        {
Choi Ga Young's avatar
Choi Ga Young committed
79
          resDataM.length !== 0 ?
80
81
82
            < PieChart
              data={resDataM}
              width={screenWidth}
Choi Ga Young's avatar
Choi Ga Young committed
83
              height={screenHeight / 3}
84
85
86
              chartConfig={chartConfig}
              accessor={"total"}
              backgroundColor={"#ffffff"}
Choi Ga Young's avatar
Choi Ga Young committed
87
            /> : <Text style={style.infoText}> 지출 내역이 없습니다.</Text>
88
89
        }
        <FlatList
Choi Ga Young's avatar
Choi Ga Young committed
90
          style={{ backgroundColor: "#FFFFFF", height: screenHeight*0.42 }}
91
92
93
94
95
96
97
98
99
100
          data={resDataM}
          renderItem={renderChart}
          keyExtractor={item => item.color}
        />
      </View>
    </>
  )
}
const style = StyleSheet.create({
  Font: {
Choi Ga Young's avatar
Choi Ga Young committed
101
102
103
104
105
106
107
    fontSize: 20,
    fontFamily: 'GowunDodum-Regular'
  },
  itemTextNum: {
    flex: 1,
    textAlign: "center",
    fontFamily: 'GowunDodum-Regular'
108
109
  },
  itemText: {
Choi Ga Young's avatar
Choi Ga Young committed
110
111
112
113
114
115
116
117
    flex: 1,
    fontFamily: 'GowunDodum-Regular'
  },
  infoText: {
    fontSize: 30,
    textAlign: "center",
    marginTop: "20%",
    fontFamily: 'GowunDodum-Regular'
118
119
120
121
  }
});

export default ChartM;