ChartY.js 2.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
import React from 'react';
import { View, Text, StyleSheet, Dimensions } from 'react-native';
import { BarChart } from "react-native-chart-kit";
import { Button } from 'react-native-elements';
import Ionicons from 'react-native-vector-icons/Ionicons';

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

const ChartY = ({ resDataY, year, setYear }) => {
Choi Ga Young's avatar
Choi Ga Young committed
11

12
13
14
15
16
17
18
19
20
  const chartConfig = {
    backgroundGradientFrom: "#ffffff", //좌측 색
    backgroundGradientFromOpacity: 1,
    backgroundGradientTo: "#ffffff", // 우측 색
    backgroundGradientToOpacity: 1,
    color: (opacity = 1) => `rgba(48, 48, 48, ${opacity})`,
    strokeWidth: 2, // optional, default 3
    barPercentage: 0.5, // 그래프 width
    useShadowColorFromDataset: false,// optional, default is false
21
    fillShadowGradientOpacity: 1,
22
23
24
25
26
27
28
29
  };
  const prevBtn = () => {
    setYear(year - 1)
  };
  const nextBtn = () => {
    setYear(year + 1)
  }

Choi Ga Young's avatar
Choi Ga Young committed
30
31
32
33
34
35
36
37
38
39
40
41
  const convertY = (y) => {
    let res = '';
    if (y >= 1000000000) {
      res = (y / 1000000000).toFixed(2) + "B"
    } else if (y >= 1000000) {
      res = (y / 1000000).toFixed(2) + "M"
    } else if (y >= 1000) {
      res = (y / 1000).toFixed(2) + "K"
    }
    return res
  }

42
43
  let totalYI = (resDataY.temp1[0].datasets[0].data).reduce((acc, curr) => acc + curr, 0);
  let totalYO = (resDataY.temp2[0].datasets[0].data).reduce((acc, curr) => acc + curr, 0);
Choi Ga Young's avatar
Choi Ga Young committed
44

45
46
47
48
49
50
  return (
    <>
      <View style={{ flexDirection: 'row' }}>
        <Button icon={
          <Ionicons name='chevron-back-sharp' onPress={prevBtn} size={15} color='black' />
        } type='clear' />
Choi Ga Young's avatar
Choi Ga Young committed
51
        <Text style={{ fontFamily: 'GowunDodum-Regular' }}>{year}</Text>
52
53
54
55
        <Button icon={
          <Ionicons name='chevron-forward-sharp' onPress={nextBtn} size={15} color='black' />
        } type='clear' />
      </View>
Choi Ga Young's avatar
Choi Ga Young committed
56
57
58
59
      <View style={{ flexDirection: 'row', justifyContent: "space-around" }}>
        <Text style={[{ color: '#1E90FF' }, style.Font]}>수입:  {(totalYI).toLocaleString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</Text>
        <Text style={[{ color: '#dc143c' }, style.Font]}>지출:  {(totalYO).toLocaleString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")} </Text>
      </View>
60
61
62
63
64
65
      <View>
        <BarChart
          style={{ marginBottom: "2%", marginTop: "2%" }}
          data={resDataY.temp1[0]}
          width={screenWidth}
          height={screenHeight / 3}
Choi Ga Young's avatar
Choi Ga Young committed
66
          chartConfig={{ ...chartConfig, fillShadowGradient: '#1E90FF', formatYLabel: convertY }}
67
68
69
70
71
72
73
          fromZero={true}
        />
        <BarChart
          style={{ marginTop: "2%" }}
          data={resDataY.temp2[0]}
          width={screenWidth}
          height={screenHeight / 3}
Choi Ga Young's avatar
Choi Ga Young committed
74
          chartConfig={{ ...chartConfig, fillShadowGradient: '#DC143C', formatYLabel: convertY }}
75
76
77
78
79
80
81
82
83
84
          fromZero={true}
        />
      </View>
    </>
  )
}
const style = StyleSheet.create({
  Font: {
    textAlign: "center",
    fontSize: 20,
Choi Ga Young's avatar
Choi Ga Young committed
85
    fontFamily: 'GowunDodum-Regular'
86
87
88
89
  },
});

export default ChartY;