Recent Attendance Trends Widget¶
The Recent Attendance Trends widget displays a line graph that shows attendance for selected divisions. Attendance for one or more divisions 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 divisions 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],
.Modify begindate with the beginning date of your choice (line 11 in the sample code). This will be the first date for which attendance data is shown. The graph will extend from the begindate to the current date.
It is recommended that you leave graphPoints (the number of data points per interval) at 15, but it can be set to fewer is desired.
HTML Code¶
Below is the HTML code for the Recent Attendance Trends widget. As supplied by TouchPoint, the file name is WidgetRecentAttendanceTrendsHTML.
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 | <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> {{title}} </h5> </div> </a> <div class="collapse in" id="{{WidgetId}}-section"> <div class="box-content center"> <h4 class="text-center">{{title}}</h4> <div class="chart"> </div> </div> </div> </div> <script type="text/javascript"> var {{WidgetId}} = function() { var data = new google.visualization.DataTable(); data.addColumn('string', 'Sunday'); {{{addcolumns}}} data.addRows({{{rowdata}}}); var options = { hAxis: { title: 'Dates' }, colors: ['#CC0300', '#5CA12B', '#345CAD', '#F9B710', '#9E4EBC', '#74746D'], hAxis: { slantedText: true, slantedTextAngle: 30, textStyle: { fontSize: 8 }, viewWindow: { max: {{{xAxisInterval}}} }, }, legend: { position: "top", textStyle: {fontSize: 8}, alignment: 'center' }, lineWidth: 2 }; 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 Recent Attendance Trends widget. As supplied by TouchPoint, the file name is WidgetRecentAttendanceTrendsSQL.
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 | select MaxCount, dd.DivId DivisionId, iif(datepart(dw, m.MeetingDate) = 1, CAST(m.MeetingDate AS DATE), DATEADD(DAY, (DATEDIFF(DAY, ((1 + 5) % 7), m.MeetingDate) / 7) * 7 + 7, ((1 + 5) % 7))) ss 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 m.MeetingDate >= '@begindate'; --and MaxCount > 0; declare @aux int = 1; declare @auxDate datetime = iif(datepart(dw, '@begindate') = 1, CAST('@begindate' AS DATE), DATEADD(DAY, (DATEDIFF(DAY, ((1 + 5) % 7), '@begindate') / 7) * 7 + 7, ((1 + 5) % 7))); declare @resultRows int = (select COUNT(*) from #meetingdata); declare @auxDiv int = (select Top(1)Id from Division where Id in (@divs)); while (@aux < @resultRows or (select COUNT(*) from #meetingdata) <= @graphPoints) begin if not exists(select 1 from #meetingdata where ss = @auxDate) begin insert into #meetingdata values(0,@auxDiv,@auxDate) end select @auxDate = dateadd(day, 7,@auxDate); select @aux = @aux + 1; end; with data as ( select MaxCount, DivisionId, ss from #meetingdata where ss >= '@begindate' ) select CONVERT(varchar, ss, 111) [Sunday], @rowcounts from data dd group by dd.ss drop table #meetingdata |
Python Script¶
Below is the Python script for the Recent Attendance Trends widget. As supplied by TouchPoint, the file name is WidgetRecentAttendanceTrendsPython.
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 | # 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 begindate to the date you wish to start the chart for your widget. begindate = '09/01/2020' # You can change Interval to change the horizontal axis of your chart. graphPoints = '15' def GetData(divisions, begindate): sql = model.Content('WidgetRecentAttendanceTrendsSQL') divids = [row[1] for row in divisions] sql = sql.replace('@begindate', begindate) sql = sql.replace('@graphPoints', graphPoints) sql = sql.replace('@divs', str(divids)[1:-1]) rowcountsql = 'isnull((select sum(MaxCount) from data d where d.DivisionId = {0} and d.ss = dd.ss group by d.ss), 0) d{0}' rlist = [rowcountsql.format(d) for d in divids] s = ',\n'.join(rlist) sql = sql.replace('@rowcounts', s) return q.QuerySqlJsonArray(sql) def Get(): sql = Data.SQLContent template = Data.HTMLContent Data.results = GetData(divisions, begindate) Data.rowdata = Data.results Data.xAxisInterval = graphPoints addcolumn = "data.addColumn('number', '{}');" alist = [addcolumn.format(s[0]) for s in divisions] Data.addcolumns = '\n'.join(alist) # for x in Data.rowdata: # print(x) print model.RenderTemplate(template) Get() |
Latest Update |
11/19/2020 |
Updated scripts and customization note regarding graphPoints.