This document contains code for rendering interactive charts on a dashboard. It includes code to generate monthly and yearly collection charts from donation data, handle date filtering, and display the charts. JavaScript code updates chart titles and applies filters when dropdowns are changed.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views7 pages
Monthly and Yearly Collection Charts
This document contains code for rendering interactive charts on a dashboard. It includes code to generate monthly and yearly collection charts from donation data, handle date filtering, and display the charts. JavaScript code updates chart titles and applies filters when dropdowns are changed.
<script> // JavaScript code for handling dropdown selection and applying filters for month-wise chart [Link]('apply-filter-btn').addEventListener('click', function() { var selectedMonth = [Link]('month-dropdown').value; var selectedYear = [Link]('year-dropdown').value; updateMonthlyCollectionTitle(selectedMonth, selectedYear); // Update monthly collection title [Link] = '/dashboard/admin_chart/?month=' + selectedMonth + '&year=' + selectedYear; });
// JavaScript code for handling dropdown selection and applying filters for year- wise chart [Link]('apply-year-filter-btn').addEventListener('click', function() { var selectedYear = [Link]('year-dropdown-year-chart').value; updateYearlyCollectionTitle(selectedYear); // Update yearly collection title [Link] = '/dashboard/admin_chart/?year_for_year_chart=' + selectedYear; });
// Update monthly collection title
function updateMonthlyCollectionTitle(selectedMonth, selectedYear) { var monthName = getMonthName(selectedMonth); [Link]('monthly-collection-title').innerText = 'Collection Amount for ' + monthName + ' ' + selectedYear; } // Update yearly collection title function updateYearlyCollectionTitle(selectedYear) { [Link]('yearly-collection-title').innerText = 'Yearly Collection for ' + selectedYear; }
// Function to get month name from month number
function getMonthName(monthNumber) { var d = new Date(); [Link](monthNumber - 1); // Months are 0-indexed return [Link]('en-US', { month: 'long' }); } </script> {% endblock %}
from [Link] import login_required, user_passes_test
from [Link] import JsonResponse from [Link] import render from [Link] import Donation import [Link] as opy import plotly.graph_objs as go import pandas as pd from datetime import datetime import calendar from [Link] import timezone
# Prepare data for year-wise chart year_data = { 'dates': [], 'amounts': [] }
# Group data by month and sum the amounts for each month monthly_collection = {} for donation in data: month_year_key = donation.service_date.strftime('%B-%Y') if month_year_key not in monthly_collection: monthly_collection[month_year_key] = 0 monthly_collection[month_year_key] += [Link]
# Convert the grouped data into lists for Plotly
for month_year, amount in monthly_collection.items(): year_data['dates'].append(month_year) year_data['amounts'].append(amount)
# Define the order of months for correct ordering on the X-axis
ordered_months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] # Sort the data based on the order of months sorted_data = sorted(zip(year_data['dates'], year_data['amounts']), key=lambda x: ordered_months.index(x[0].split('-')[0]))
# Add trace for the line chart only if there is data
if sorted_dates and sorted_amounts: fig.add_trace([Link]( x=sorted_dates, y=sorted_amounts, mode='lines+markers', name='Monthly Collection', line=dict(color='#1f77b4', width=2), # Blue line marker=dict(color='#1f77b4', size=8, line=dict(color='#ffffff', width=1)), # Blue markers ))
# Update layout if selected_year is not None: title = f'Monthly Collection for {selected_year}' else: current_year = [Link]().year title = f'Monthly Collection for {current_year}'
fig.update_layout( title=title, xaxis_title='Month', yaxis_title='Amount', template='plotly_white', font=dict(family='Arial, sans-serif', size=12, color='#333'), # Darker font color margin=dict(l=60, r=60, t=80, b=60), # Increased margins plot_bgcolor='#f5f5f5', # Light gray background color paper_bgcolor='#f5f5f5', # Light gray paper background color xaxis=dict( showline=True, showgrid=False, showticklabels=True, linecolor='#333', linewidth=1, ticks='outside', tickfont=dict(family='Arial, sans-serif', size=10, color='#333'), # Darker axis line and ticks categoryorder='array', # Specify ordering for categorical axis categoryarray=ordered_months # Order the months according to the predefined list ), yaxis=dict( showline=True, showgrid=True, showticklabels=True, linecolor='#333', linewidth=1, ticks='outside', tickfont=dict(family='Arial, sans-serif', size=10, color='#333') # Darker axis line and ticks ), )
year_chart_div, years_for_year_chart = render_year_chart(filtered_donations_yearly, selected_year_for_year_chart) # Prepare data for year-wise chart year_title = f'Yearly Collection for {selected_year_for_year_chart}'
context = { 'month_chart_div': month_chart_div, 'year_chart_div': year_chart_div, 'months': [(i, calendar.month_name[i]) for i in range(1, 13)], 'years': sorted(years), 'years_for_year_chart': years_for_year_chart, 'selected_month': selected_month, 'selected_year': selected_year, 'selected_year_for_year_chart': selected_year_for_year_chart,# Provide default value 'month_title': month_title, # Add the month title to the context 'year_title': year_title, # Add the year title to the context