Attendance Year over Year Widget

The Attendance Year over Year widget displays a line graph showing historical attendance for selected divisions. Attendance for for the current and previous years are shown via color-coded lines with a color legend at the top of the graph.

The widget utilizes an HTML file, SQL script and Python script as shown below. Since the attendance data is the same for all users, Caching should be set to all users.

To customize the widget, you will need to make a few changes in the Python script.

  • Replace lines 6 - 8 with the names and IDs of the division(s) for which to display attendance. The sample shows three divisions, but you can use more or fewer. Enter one line per division in the format ['division name', id],. The names are informational within the script and do not display on the graph.
  • Modify line 12 set startyear with the first year to include. Only three years can be displayed, so you can select the past year (to compare only last year and this year) or the year before that (to compare the previous two years and the current year).

HTML Code

Below is the HTML code for the Attendance Year over Year widget. As supplied by TouchPoint, the file name is WidgetAttendanceYearOverYearHTML.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<div class="box">
    <div class="box-title hidden-xs">
        <h5>{{WidgetName}}</h5>
    </div>
    <a class="visible-xs-block" id="giving-fc-collapse" data-toggle="collapse" href="#{{WidgetId}}-section" aria-expanded="true" aria-controls="{{WidgetId}}-section">
        <div class="box-title">
            <h5>
                <i class="fa fa-chevron-circle-right"></i>&nbsp;&nbsp;{{WidgetName}}
            </h5>
        </div>
    </a>
    <div class="collapse in" id="{{WidgetId}}-section">
        <div class="box-content center">
        <div id="tester"></div>
            <div class="chart">
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var inputList = [];
    var outputList = [];
    var year1 = [];
    var year2 = [];
    var year3 = [];
    var dates = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var {{WidgetId}} = function() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Name');
        {{{addcolumns}}}
        inputList = {{{rowdata}}};

        let intervalYear = {{{startingyear}}};
        for(let i = 1; i <= {{{numyears}}}; i++)
        {
            for(let j = 1; j <= 12; j++)
            {
                if(intervalYear == {{{currentyear}}} && j > {{{currentmonth}}})
                {
                    break;
                }
                else
                {
                    //count++;
                    outputList.push([0, j, intervalYear]);
                }
            }
            intervalYear++;
        }

        for(let i = 0; i < outputList.length; i++)
        {
            var match = false;
            for(let j = 0; j < inputList.length; j++)
            {
                if(outputList[i][1] == inputList[j][1] && outputList[i][2] == inputList[j][2])
                {
                    outputList[i][0] += inputList[j][0];
                    break;
                }
            }
        }

        for(let i = 0; i < outputList.length; i++)
        {
            if(outputList[i][2] == {{{startingyear}}})
            {
                year1.push(outputList[i]);
            }
            if(outputList[i][2] == {{{startingyear}}} +1)
            {
                year2.push(outputList[i]);
            }
            if(outputList[i][2] == {{{startingyear}}} +2)
            {
                year3.push(outputList[i]);
            }
        }

        for(let i = 0; i < year1.length; i++)
        {
            switch(year1[i][1])
            {
                case 1:
                    year1[i][1] = 'Jan';
                    break;
                case 2:
                    year1[i][1] = 'Feb';
                    break;
                case 3:
                    year1[i][1] = 'Mar';
                    break;
                case 4:
                    year1[i][1] = 'Apr';
                    break;
                case 5:
                    year1[i][1] = 'May';
                    break;
                case 6:
                    year1[i][1] = 'Jun';
                    break;
                case 7:
                    year1[i][1] = 'Jul';
                    break;
                case 8:
                    year1[i][1] = 'Aug';
                    break;
                case 9:
                    year1[i][1] = 'Sep';
                    break;
                case 10:
                    year1[i][1] = 'Oct';
                    break;
                case 11:
                    year1[i][1] = 'Nov';
                    break;
                case 12:
                    year1[i][1] = 'Dec';
                    break;
                default:
                    break;
            }
        }
        for(let i = 0; i < year2.length; i++)
        {
            switch(year2[i][1])
            {
                case 1:
                    year2[i][1] = 'Jan';
                    break;
                case 2:
                    year2[i][1] = 'Feb';
                    break;
                case 3:
                    year2[i][1] = 'Mar';
                    break;
                case 4:
                    year2[i][1] = 'Apr';
                    break;
                case 5:
                    year2[i][1] = 'May';
                    break;
                case 6:
                    year2[i][1] = 'Jun';
                    break;
                case 7:
                    year2[i][1] = 'Jul';
                    break;
                case 8:
                    year2[i][1] = 'Aug';
                    break;
                case 9:
                    year2[i][1] = 'Sep';
                    break;
                case 10:
                    year2[i][1] = 'Oct';
                    break;
                case 11:
                    year2[i][1] = 'Nov';
                    break;
                case 12:
                    year2[i][1] = 'Dec';
                    break;
                default:
                    break;
            }
        }
        for(let i = 0; i < year3.length; i++)
        {
            switch(year3[i][1])
            {
                case 1:
                    year3[i][1] = 'Jan';
                    break;
                case 2:
                    year3[i][1] = 'Feb';
                    break;
                case 3:
                    year3[i][1] = 'Mar';
                    break;
                case 4:
                    year3[i][1] = 'Apr';
                    break;
                case 5:
                    year3[i][1] = 'May';
                    break;
                case 6:
                    year3[i][1] = 'Jun';
                    break;
                case 7:
                    year3[i][1] = 'Jul';
                    break;
                case 8:
                    year3[i][1] = 'Aug';
                    break;
                case 9:
                    year3[i][1] = 'Sep';
                    break;
                case 10:
                    year3[i][1] = 'Oct';
                    break;
                case 11:
                    year3[i][1] = 'Nov';
                    break;
                case 12:
                    year3[i][1] = 'Dec';
                    break;
                default:
                    break;
            }
        }

        switch({{{numyears}}})
        {
            case 1:
                for (let i = 0; i < 12; i++)
                {
                    if(year1[i] == null)
                    {
                        data.addRow([dates[i], null]);
                    }
                    else
                    {
                        data.addRow([dates[i], year1[i][0]]);
                    }
                }
                break;
            case 2:
                for (let i = 0; i < 12; i++)
                {
                    if(year2[i] == null)
                    {
                        data.addRow([dates[i], year1[i][0], null]);
                    }
                    else
                    {
                        data.addRow([dates[i], year1[i][0], year2[i][0]]);
                    }
                }
                break;
            case 3:
                for (let i = 0; i < 12; i++)
                {
                    if(year3[i] == null)
                    {
                        data.addRow([dates[i], year1[i][0], year2[i][0], null]);
                    }
                    else
                    {
                        data.addRow([dates[i], year1[i][0], year2[i][0], year3[i][0]]);
                    }
                }
                break;
            default:
            break;
        }


        var options = {
            hAxis: {
            title: 'Dates'
            },
            colors: ['#CC0300', '#5CA12B', '#345CAD', '#F9B710', '#9E4EBC', '#74746D'],
            hAxis: {
                slantedText: true,
                slantedTextAngle: 30,
                textStyle: {
                    fontSize: 8
                },
                viewWindow: {
                    min: 0,
                    max: 12
                }
            },
            legend: {
                position: "top",
                textStyle: {fontSize: 8},
                alignment: 'center'
            }
        };

        var chart = new google.visualization.LineChart(document.querySelector('#{{WidgetId}}-section .chart'));
        chart.draw(data, options);
    }
    // load and register the chart
    google.charts.load("current", {packages:["corechart"]});
    google.charts.setOnLoadCallback({{WidgetId}});
    WidgetCharts.{{WidgetId}} = {{WidgetId}};
</script>

SQL Script

Below is the SQL script for the Attendance Year over Year widget. As supplied by TouchPoint, the file name is WidgetAttendanceYearOverYearSQL.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
select MaxCount, dd.DivId DivisionId, m.MeetingDate into #meetingdata
from dbo.Meetings m
join dbo.Organizations o on o.OrganizationId = m.OrganizationId
join dbo.DivOrg dd on dd.orgid = o.OrganizationId
join dbo.Division d on d.Id = dd.DivId
where dd.DivId in (@divs) and YEAR(MeetingDate) >= YEAR('@startyear');
with data as ( select MaxCount, MeetingDate from #meetingdata )
select
SUM(MaxCount) as MaxCount,
Month(MeetingDate) as MyMonth,
Year(MeetingDate) as MyYear
from #meetingdata
group by Month(MeetingDate), Year(MeetingDate)
order by Year(MeetingDate), Month(MeetingDate)
drop table #meetingdata

Python Script

Below is the Python script for the Attendance Year over Year widget. As supplied by TouchPoint, the file name is WidgetAttendanceYearOverYearPython.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Users can add divisions to include in the line chart for this widget inside this divisions array.
# This array is made up of objects. Each object contains a name and a division id number.
# The first item in the object is the name you wish to display in the widget for the selected division.
# The second item in the object is the division id you wish to display in the widget.
divisions = [
    ['Young Couples', 210],
    ['Young Marrieds', 211],
    ['Adults 1', 213]
]
# You can change startyear to the date you wish to start the chart for your widget.
# Notice, the furthest back you can select is 3 years.
startyear = '2018'

import datetime

currentDate = datetime.datetime.now()


def GetData(divisions, startyear):
    sql = model.Content('WidgetAttendanceYearOverYearSQL')
    divids = [row[1] for row in divisions]
    sql = sql.replace('@startyear', startyear)
    sql = sql.replace('@divs', str(divids)[1:-1])
    return q.QuerySqlJsonArray(sql)


def Get():
    sql = Data.SQLContent
    template = Data.HTMLContent
    Data.rowdata = GetData(divisions, startyear)
    startingYear = int(startyear)
    maxYears = currentDate.year - 2
    if startingYear < maxYears:
        startingYear = maxYears
    numYears = currentDate.year - startingYear + 1
    selectedStartingYear = startingYear
    selectedYears = []
    while selectedStartingYear <= currentDate.year:
        selectedYears.append(selectedStartingYear)
        selectedStartingYear += 1
    addcolumn = "data.addColumn('number', '{}');"
    alist = [addcolumn.format(x) for x in selectedYears]
    Data.addcolumns = '\n'.join(alist)
    Data.startingyear = startingYear
    Data.numyears = numYears
    Data.currentyear = currentDate.year
    Data.currentmonth = currentDate.month
    print model.RenderTemplate(template)


Get()


Latest Update 05/28/2020

Added this article.

© 2020 TouchPoint Software, LLC.  All rights reserved.