Guest User

ledger -> gnuplot with monthly cumulatives

a guest
Jan 13th, 2021
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. ---
  2.  
  3. food.gnuplot
  4.  
  5. # == Setup ==
  6. set terminal qt
  7. set xdata time
  8. set timefmt "%Y/%m/%d"
  9.  
  10. set label "COVID" at "2020/03/20",850
  11.  
  12. set key on left top vertical enhanced title "Categories"
  13. set title "Food Expenses"
  14.  
  15. set offsets -328725, 0, 0, 0
  16.  
  17. # == Monthly-Bucketed Expenses ==
  18. plot \
  19. "monthly_expenses/food_groceries.txt" using 1:2 title "Groceries" with fillsteps, \
  20. "monthly_expenses/food_in.txt" using 1:2 title "Take-out" with fillsteps, \
  21. "monthly_expenses/food_out.txt" using 1:2 title "Eat-out" with fillsteps, \
  22. "monthly_expenses/food_all.txt" using 1:2 title "Total" with fillsteps
  23.  
  24. ---
  25.  
  26. get_viz-data.sh
  27.  
  28. # manufacture zeros at the beginning of each month between START_DATE and END_DATE
  29. BLANKS=mktemp
  30. start_date=$1
  31. end_date=$2
  32. current_date=$(gdate -d "$start_date" "+%Y/%m/01")
  33. while [[ "$current_date" < "$end_date" ]]; do
  34. echo "$current_date 0.0" >>$BLANKS
  35. current_date=$(gdate -d "$current_date 1 month" "+%Y/%m/01")
  36. done
  37. echo "$current_date 0.0" >>$BLANKS
  38.  
  39. ledger --effective -f ledgers/main.ldg reg --sort date -F '%D %t\n' \
  40. Expenses:Food:Groceries |
  41. tr -d "$," |
  42. gawk -F " " -f viz-bucket.awk \
  43. >$MONTHLY_DIR/food_groceries.txt
  44.  
  45. ledger --effective -f ledgers/main.ldg reg --sort date -F '%D %t\n' \
  46. Expenses:Food:Out |
  47. tr -d "$," |
  48. awk -F " " -f viz-bucket.awk \
  49. >$MONTHLY_DIR/food_out.txt
  50.  
  51. ledger --effective -f ledgers/main.ldg reg --sort date -F '%D %t\n' \
  52. Expenses:Food:In |
  53. tr -d "$," |
  54. awk -F " " -f viz-bucket.awk \
  55. >$MONTHLY_DIR/food_in.txt
  56.  
  57. ledger --effective -f ledgers/main.ldg reg --sort date -F '%D %t\n' \
  58. Expenses:Food |
  59. tr -d "$," |
  60. awk -F " " -f viz-bucket.awk \
  61. >$MONTHLY_DIR/food_all.txt
  62.  
  63. # merge blanks into monthly expense files (tricky!)
  64. ls $MONTHLY_DIR | xargs -R 2 -I % \
  65. gsort \
  66. -k1,1 \
  67. -s \
  68. $MONTHLY_DIR/% $BLANKS \
  69. -o $MONTHLY_DIR/%
  70.  
  71. # clean up blanks
  72. rm $BLANKS
  73.  
  74. ---
  75.  
  76. viz-bucket.awk
  77.  
  78. BEGIN {
  79. sum=0
  80. bucket_month=""
  81. } {
  82. line_date = substr($1, 0, 10)
  83. line_year = substr(line_date, 0, 4)
  84. line_month = substr(line_date, 6, 2)
  85. line_amt = $2
  86.  
  87. if (bucket_month == line_month) {
  88. sum += line_amt
  89. printf "%s/%s/%s %s\n", line_year, line_month, substr(line_date, 9, 2), sum
  90. } else {
  91. sum = line_amt
  92. bucket_month=line_month
  93. printf "%s/%s/%s %s\n", line_year, line_month, substr(line_date, 9, 2), sum
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment