Skip to content

Commit

Permalink
Finance script updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrrv committed Dec 1, 2024
1 parent 4fc21cf commit 251219b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 42 deletions.
41 changes: 21 additions & 20 deletions Finance Reports/BudgetCumulativeComparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
global model

sql = model.SqlContent('BudgetCumulativeGivingYoY')
model.Title = "Cumulative Giving Comparison"

# Initialize data structure to store percentages by year and week
weeks = range(1, 54) # Assuming there are 52 weeks
weeks = range(1, 54) # Assuming there are 53 weeks
years = []
d = [] # Will store data for each year, where each entry is a list of 52 weeks

Expand All @@ -31,20 +32,20 @@
d[year_col][52] = row.Percentage

# Generate Google Chart script
print """<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>"""
print "<script>"
print "google.charts.load('current', {'packages':['corechart']});"
print "google.charts.setOnLoadCallback(drawChart);"
print "function drawChart() {"
print "var data = new google.visualization.DataTable();"
print("""<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>""")
print("<script>")
print("google.charts.load('current', {'packages':['corechart']});")
print("google.charts.setOnLoadCallback(drawChart);")
print("function drawChart() {")
print("var data = new google.visualization.DataTable();")

# Define columns: the first column is for the weeks, followed by one column per year
print "data.addColumn('number', 'Week');"
print("data.addColumn('number', 'Week');")
for y in years:
print "data.addColumn('number', '{}');".format(y)
print("data.addColumn('number', '{}');".format(y))

# Add rows for each week (1 to 52), with percentages for each year
print "data.addRows("
print("data.addRows(")
rows = []
for week in range(53): # Loop through 52 weeks
row_data = [week + 1] # Start with the week number
Expand All @@ -55,16 +56,16 @@
rows.append(row_data)

# Use json.dumps to correctly serialize Python None as JavaScript null
print json.dumps(rows)
print ");"
print(json.dumps(rows))
print(");")

# Generate colors and widths
colors = ["'#ccc'"] * (len(years) - 2) + ["'#000'", "'#00f'"]
widths = ['1'] * (len(years) - 2) + ['5', '5']


# Set chart options
print """
print("""
var options = {
title: 'Cumulative Giving as Percentage of Budget',
curveType: 'function',
Expand All @@ -74,22 +75,22 @@
viewWindow: {min: 0, max: 1.09},
format: 'percent'
},
series: {"""
series: {""")
for i in range(len(years)-2):
print str(i) + ": { lineWidth: 1 },"
print """},
legend: 'none', // Hide legend
print(str(i) + ": { lineWidth: 1, visibleInLegend: false },")
print("""},
//legend: 'none', // Hide legend
colors: [""" + ", ".join(colors) + """],
lineWidth: 5
};
"""
""")

# Draw the chart
print """
print("""
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart.draw(data, options);
}
</script>
<div id="curve_chart" style="width: 100%; height: 10in"></div>
"""
""")
8 changes: 5 additions & 3 deletions Finance Reports/BudgetSummary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ FROM #contribs c
LEFT JOIN FundSets fs ON fsf.FundSetId = fs.FundSetId
GROUP BY c.Month, c.year, fsf.FundSetId, COALESCE(fs.description, cf.FundName);

SELECT TOP 100 *
SELECT *
INTO #summed
FROM (
SELECT
Expand Down Expand Up @@ -93,7 +93,8 @@ SELECT c.Fund,
p.Giving AS 'Giving PYTD',
c.Giving - p.Giving AS 'PY Difference',
(c.Giving - p.Giving) * 100 / p.Giving AS 'PY Pct',
CAST(@Today AS DATE) AS 'As Of'
CAST(@Today AS DATE) AS 'As Of',
@year as 'Year'
FROM #summed c
LEFT JOIN #summed p ON c.Fund = p.Fund AND c.Year = p.Year + 1
WHERE c.Budget > 0 AND c.year = @year
Expand All @@ -109,7 +110,8 @@ SELECT c.Fund,
p.Giving AS 'Giving PYTD',
c.Giving - p.Giving AS 'PY Difference',
(c.Giving - p.Giving) * 100 / p.Giving AS 'PY Pct',
CAST(@Today AS DATE) AS 'As Of'
CAST(@Today AS DATE) AS 'As Of',
@year as 'Year'
FROM #summed c
LEFT JOIN #summed p ON c.Fund = p.Fund AND c.Year = p.Year + 1
WHERE c.Budget IS NULL AND c.year = @year
Expand Down
43 changes: 24 additions & 19 deletions Finance Reports/BudgetSummaryChart.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
global model
global Data

sql = model.SqlContent('BudgetSummary').replace('@p1', '1')
y = Data.p1 or '1'

sql = model.SqlContent('BudgetSummary').replace('@p1', y)
d = model.SqlListDynamicData(sql)

def formatMoney(num):
Expand All @@ -13,8 +16,8 @@ def formatMoney(num):

labelPositions = [[]]
def determineLine(x):
margin = 80
margin = 85

line = 1
while True:
c = False
Expand All @@ -23,7 +26,7 @@ def determineLine(x):
for lp in labelPositions[line]:
if lp is None:
continue

if lp > x-margin and lp < x+margin:
line += 1
c = True
Expand All @@ -38,12 +41,12 @@ def getMarkedLabel(x, y, label1, label2, textColor="#000", lineStyle="stroke:#99
line = determineLine(x)
yBase = y + 10 # top of bar
y += line * 50 + 10 # top of the indicator (for line 1, bottom of the bar).

yTop = yBase - (10 if extendTop else 0)
r = "<line x1=\"{0}\" y1=\"{2}\" x2=\"{0}\" y2=\"{3}\" style=\"{1}\" />".format(x, lineStyle, yTop, yBase+50)

r += "<line x1=\"{0}\" y1=\"{2}\" x2=\"{0}\" y2=\"{3}\" style=\"{1}\" />".format(x, lineStyle, y, y+10)

r += "<text x=\"{0}\" y=\"{1}\" fill=\"{2}\" style=\"text-anchor: middle\" font-family=\"Lato, sans-serif\">".format(x, y+25, textColor)
r += "<tspan x=\"{0}\" dy=\"0\">{1}</tspan>".format(x, label1)
r += "<tspan x=\"{0}\" dy=\"15\">{1}</tspan>".format(x, label2)
Expand All @@ -57,43 +60,45 @@ def getMarkedLabel(x, y, label1, label2, textColor="#000", lineStyle="stroke:#99
for f in d:
if f['Budget FY'] is not None and f['Budget FY'] > maxBudget:
maxBudget = f['Budget FY']

if f["Giving YTD"] is not None and f["Giving YTD"] > maxBudget:
maxBudget = f["Giving YTD"]

yCum = 0

for f in d:
givingPos = 100 + (500 * f["Giving YTD"] / maxBudget)
budgetPos = 100 + (500 * f["Budget YTD"] / maxBudget) if f["Budget YTD"] is not None else None
prevPos = 100 + (500 * f["Giving PYTD"] / maxBudget) if f["Giving PYTD"] is not None else None
barWidth = (500 * f["Budget FY"] / maxBudget) if f["Budget FY"] is not None else None

chart += "<!-- {} -->".format(f['Fund'])

# fund label
# chart += "<text x=\"{0}\" y=\"{1}\" fill=\"#000\">".format(3, yCum+35)
# chart += "<tspan x=\"{0}\" dy=\"0\">{1}</tspan>".format(3, f['Fund'])
# chart += "</text>"

# base bar
if barWidth is not None:
chart += """<rect width="{0}" height="50" x="100" y="{1}" style="fill:#ccc;" />""".format(barWidth, yCum+10)

# giving bar
chart += """<rect width="{0}" height="50" x="100" y="{1}" style="fill:#49917b;" />""".format(givingPos-100, yCum+10)

chart += getMarkedLabel(givingPos, yCum, "Giving YTD", formatMoney(f["Giving YTD"]))
if f["Budget YTD"] is not None and f["Budget FY"] is not None and f["Budget YTD"] / f["Budget FY"] < 10.0/12:

if f["Budget YTD"] is not None and f["Budget FY"] is not None and f["Budget YTD"] / f["Budget FY"] < 11.2/12:
chart += getMarkedLabel(budgetPos, yCum, "Budget YTD", formatMoney(f["Budget YTD"]), "#000", "stroke:#000;stroke-width:3", True)

if f["Budget FY"] is not None:
chart += getMarkedLabel(barWidth+100, yCum, "Budget FY", formatMoney(f["Budget FY"]))

chart += getMarkedLabel(prevPos, yCum, "Giving Last Year", formatMoney(f["Giving PYTD"]), "#999")

height = len(labelPositions) * 50 + 10
yCum += height
labelPositions = [None]

break

print("""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
Expand Down

0 comments on commit 251219b

Please sign in to comment.