chartData.api.js 2.77 KB
Newer Older
1
2
import { DEBUG, enablePromise } from 'react-native-sqlite-storage';
import getDb from './moneyDB'
Choi Ga Young's avatar
Choi Ga Young committed
3
import { getDateStr } from '../utils/dateFunction'
4
5
6
7

DEBUG(true);
enablePromise(true);

8
9
10
11
12
13
14
const colorArr = [
  "#f06c77", "#ffa07a", "#fafad2", "#e3fad2", "#b0c4de",
  "#49618f", "#dda0dd", "#ff4d6a", "#fcc24c", "#fffacd",
  "#8fbc8f", "#82a6e9", "#2b4069", "#db7093", "#fa8072",
  "#98fb98", "#d6badb"
]

15
const outMoney = async ({ year, month }) => {
Choi Ga Young's avatar
Choi Ga Young committed
16
17
  const thisFirst = new Date(year, month, 1);
  const thisLast = new Date(year, month + 1, 0); 
18
19
20
21

  const db = await getDb();
  return new Promise((res, rej) => {
    db.transaction(async (tx) => {
Choi Ga Young's avatar
Choi Ga Young committed
22
      const [txn, results] = await tx.executeSql(`SELECT category_id, category_name, sum(price) as total from (SELECT money.type_id, price, money.category_id, category_name FROM money inner JOIN categories on money.category_id = categories.category_id where date BETWEEN "${getDateStr(thisFirst)}" and "${getDateStr(thisLast)}") WHERE type_id=2 group by category_id`);
23
24
      const temp = [];
      for (let i = 0; i < results.rows.length; i++) {
25
26
27
28
29
30
31
        temp.push({
          name: results.rows.item(i).category_name,
          total: results.rows.item(i).total,
          color: colorArr[i], legendFontColor: "#7F7F7F",
          legendFontSize: 15
        })
        res(temp)
32
33
34
35
36
      }
    })
  })
}

37
const yearMoney = async ({ year }) => {
Choi Ga Young's avatar
Choi Ga Young committed
38
39
  const yearFirst = new Date(year, 0, 1); 
  const yearLast = new Date(year, 12, 0); 
40
  const db = await getDb();
Choi Ga Young's avatar
Choi Ga Young committed
41

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
68
69
70
71
72
73
74
75
76
77
  return new Promise((res, rej) => {
    db.transaction(async (tx) => {
      const [txn, results] = await tx.executeSql(`SELECT strftime('%m',date) as month, type_id, sum(price) as total from money where strftime('%Y', date)= "${year}" GROUP by strftime('%m', date), type_id `);
      const temp1 = [];
      const temp2 = [];
      let temparr1 = new Array(12).fill(0)
      let temparr2 = new Array(12).fill(0)

      for (let i = 0; i < results.rows.length; i++) {
        if (results.rows.item(i).type_id === 1) {
          let idx = Number(results.rows.item(i).month)
          temparr1[idx - 1] = results.rows.item(i).total

        } else if (results.rows.item(i).type_id === 2) {
          let idx = Number(results.rows.item(i).month)
          temparr2[idx - 1] = results.rows.item(i).total

        }
      }
      temp1.push({
        labels: ['1월', '2월', '3월', '4월', '5월', '6월', '7월', '8월', '9월', '10월', '11월', '12월'],
        datasets: [{
          data: temparr1
        }]
      })
      temp2.push({
        labels: ['1월', '2월', '3월', '4월', '5월', '6월', '7월', '8월', '9월', '10월', '11월', '12월'],
        datasets: [{
          data: temparr2
        }]
      })
      res({ temp1, temp2 })
    })
  })

}
78
const chartApi = {
79
80
  outMoney,
  yearMoney
81
82
83
}

export default chartApi;