forked from konklone/ohnomymoney
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proof of concept charting working again, with balances displayed on t…
…he left
- Loading branch information
Showing
6 changed files
with
132 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,78 @@ | |||
require 'google_chart' | |||
|
|||
helpers do | |||
|
|||
def chart_for(days) | |||
oldest = days.first | |||
newest = days[days.size-1] | |||
balances = days.map {|day| day.balance} | |||
|
|||
min = balances.min | |||
max = balances.max | |||
disparity = max - min | |||
y = (disparity * 1.66).to_i | |||
buffer = (y - disparity) / 2 | |||
|
|||
y_min = round(min - buffer) | |||
y_max = round(max + buffer) | |||
y = y_max - y_min | |||
y_step = round(y / 10.0, 2) | |||
|
|||
x_step = days.size / 7 | |||
x_step = 1 if x_step < 1 | |||
x_days = [] | |||
days.each_with_index {|b, i| x_days << b if i % x_step == 0} | |||
x_days[0] = days[0] | |||
x_days[-1] = days[-1] | |||
|
|||
y_labels = (0..10).map {|i| format_balance(y_min + (y_step * i))} | |||
x_labels = x_days.map {|day| format_date day.date_of} | |||
|
|||
label = newest.balance > 0 ? 'Money' : 'Debt' | |||
data = balances.map {|balance| (balance - y_min).to_f} | |||
color = newest.balance > 0 ? '008800' : 'cc0000' | |||
|
|||
chart = GoogleChart::LineChart.new '857x350' | |||
chart.data_encoding = :text | |||
chart.max_value y | |||
chart.data label, data, color | |||
chart.axis :x, :labels => x_labels | |||
chart.axis :y, :labels => y_labels | |||
chart.axis :right, :labels => y_labels | |||
|
|||
p chart.to_url(:chdlp => 'b') | |||
chart.to_url :chdlp => 'b' | |||
end | |||
|
|||
def round(n, level = 3) | |||
(n - (n % (10**level))).to_i | |||
end | |||
|
|||
def double_encode(text) | |||
text.gsub("&", "&") | |||
end | |||
|
|||
def format_balance(balance) | |||
number = balance.to_f / 100 | |||
parts = ("%01.2f" % number).split('.') | |||
answer = '$' + number_with_delimiter(parts[0]) + '.' + parts[1].to_s | |||
if number > 0 | |||
answer = '+' + answer | |||
elsif number < 0 | |||
answer = '-' + answer | |||
end | |||
answer | |||
end | |||
|
|||
def number_with_delimiter(number) | |||
number = number.to_i.abs | |||
parts = number.to_s.split('.') | |||
parts[0].gsub! /(\d)(?=(\d\d\d)+(?!\d))/, "\\1," | |||
parts.join '.' | |||
end | |||
|
|||
def format_date(date) | |||
date.strftime("%B ") + date.strftime("%d").gsub(/^0/, "") | |||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,29 @@ | |||
<!doctype html> | |||
<html> | |||
<head> | |||
<title><%= user.name %>'s Money</title> | |||
</head> | |||
<body> | |||
|
|||
<h1><%= user.name %>'s Money</h1> | |||
|
|||
<div class="sidebar"> | |||
<div class="account worth"> | |||
<h2>Net Worth</h2> | |||
<span><%= worth.balance %></span> | |||
</div> | |||
|
|||
<% (assets + debts).each do |account| %> | |||
<div class="account <%= account.account_type %>"> | |||
<h2><%= account.name %></h2> | |||
<span><%= account.balance %></span> | |||
</div> | |||
<% end %> | |||
</div> | |||
|
|||
<div class="main"> | |||
<img src="<%= double_encode chart_for(worth.days.all(:limit => 10)) %>" /> | |||
</div> | |||
|
|||
</body> | |||
</html> |