Skip to content

Commit d1cd50f

Browse files
committed
Adding Cumulative giving chart.
1 parent b8cf352 commit d1cd50f

5 files changed

Lines changed: 104 additions & 3 deletions

File tree

71.9 KB
Loading
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import json
2+
3+
global model
4+
5+
sql = model.SqlContent('BudgetCumulativeGivingYoY')
6+
7+
# Initialize data structure to store percentages by year and week
8+
weeks = range(1, 54) # Assuming there are 52 weeks
9+
years = []
10+
d = [] # Will store data for each year, where each entry is a list of 52 weeks
11+
12+
# Retrieve the data from the database
13+
dbd = model.SqlListDynamicData(sql)
14+
15+
# Process the data and dynamically build the year-week-percentage matrix
16+
year_index_map = {}
17+
for row in dbd:
18+
# If it's a new year, initialize a new entry in the data matrix
19+
if row.Year not in year_index_map:
20+
year_index_map[row.Year] = len(years) # Assign a new column index for this year
21+
years.append(row.Year) # Track the year
22+
d.append([None] * 53) # Initialize 52 weeks with 'None' for this year
23+
24+
# Get the column index for the current year
25+
year_col = year_index_map[row.Year]
26+
27+
# Assign the percentage value for the corresponding week (row.Week - 1)
28+
if 1 <= row.Week <= 53: # Ensure the week is within a valid range (1-52)
29+
d[year_col][row.Week - 1] = row.Percentage
30+
if row.Week > 53:
31+
d[year_col][52] = row.Percentage
32+
33+
# Generate Google Chart script
34+
print """<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>"""
35+
print "<script>"
36+
print "google.charts.load('current', {'packages':['corechart']});"
37+
print "google.charts.setOnLoadCallback(drawChart);"
38+
print "function drawChart() {"
39+
print "var data = new google.visualization.DataTable();"
40+
41+
# Define columns: the first column is for the weeks, followed by one column per year
42+
print "data.addColumn('number', 'Week');"
43+
for y in years:
44+
print "data.addColumn('number', '{}');".format(y)
45+
46+
# Add rows for each week (1 to 52), with percentages for each year
47+
print "data.addRows("
48+
rows = []
49+
for week in range(53): # Loop through 52 weeks
50+
row_data = [week + 1] # Start with the week number
51+
for year_index in range(len(years)):
52+
# Ensure the data is JSON serializable: float or None
53+
value = d[year_index][week] if d[year_index][week] is not None else None
54+
row_data.append(float(value/100) if value is not None else None) # Force float if it's not None
55+
rows.append(row_data)
56+
57+
# Use json.dumps to correctly serialize Python None as JavaScript null
58+
print json.dumps(rows)
59+
print ");"
60+
61+
# Generate colors and widths
62+
colors = ["'#ccc'"] * (len(years) - 2) + ["'#000'", "'#00f'"]
63+
widths = ['1'] * (len(years) - 2) + ['5', '5']
64+
65+
66+
# Set chart options
67+
print """
68+
var options = {
69+
title: 'Cumulative Giving as Percentage of Budget',
70+
curveType: 'function',
71+
hAxis: {title: 'Week'},
72+
vAxis: {
73+
title: 'Percentage of Budget',
74+
viewWindow: {min: 0, max: 1.09},
75+
format: 'percent'
76+
},
77+
series: {"""
78+
for i in range(len(years)-2):
79+
print str(i) + ": { lineWidth: 1 },"
80+
print """},
81+
legend: 'none', // Hide legend
82+
colors: [""" + ", ".join(colors) + """],
83+
lineWidth: 5
84+
};
85+
"""
86+
87+
# Draw the chart
88+
print """
89+
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
90+
chart.draw(data, options);
91+
}
92+
</script>
93+
94+
<div id="curve_chart" style="width: 100%; height: 10in"></div>
95+
"""

Finance Reports/BudgetCumulativeGivingYoY.sql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
DECLARE @BudgetJson nvarchar(MAX);
22

3-
-- Indicate the relevant funds by choosing a fund set.
4-
53
SELECT *
64
INTO #relevantFunds
75
FROM FundSetFunds
86
WHERE FundSetId = 1;
97

108

11-
-- Load the budget JSON
9+
-- Load the budget JSON (assuming it holds data for multiple years and funds)
1210
SET @BudgetJson = (SELECT TOP 1 Body FROM Content WHERE Name = 'Budgets.json');
1311

1412

Finance Reports/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ This is a collection of simple reports used to report on giving.
2121
</div>
2222

2323
- [**Cumulative Giving YoY (SQL)**](BudgetCumulativeGivingYoY.sql) - Compare giving year-over-year as a percentage of budget, week by week.
24+
25+
- [**Cumulative Giving Comparison to Budget**](BudgetCumulativeComparison.py) - A Python script that takes the Cumulative Giving YoY SQL above and
26+
renders it into a chart that allows for comparison to previous years.
27+
28+
![Cumulative Giving Comparison to Budget Screenshot](https://github.com/TenthPres/TouchPointScripts/blob/master/.documentation/CumulativeGivingYoY.png?raw=true)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ their attendnance together.
4242

4343
- [**Finance > Cumulative Giving YoY (SQL)**](Finance%20Reports) - Compare giving year-over-year as a percentage of budget, week by week.
4444

45+
- [**Finance > Cumulative Giving Comparison to Budget YoY**](Finance%20Reports) - Compare giving year-over-year as a percentage of budget,
46+
week by week, and see if this year is trending ahead or behind previous years.
47+
4548
- [**Global > Birthdays & Anniversaries**](Global%20Reports) - Generate a list of partners who have birthdays or
4649
anniversaries coming up, and email it to the appropriate people. We use this to send cards to partners each month.
4750

0 commit comments

Comments
 (0)