RM Chart Correct Code
RM Chart Correct Code
py
fig = go.Figure()
return plot_div
# 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] += donation['amount']
fig = go.Figure()
# Update layout
if selected_year is not None:
title = f'Monthly Collection for {selected_year}'
else:
current_year = datetime.now().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
),
)
return plot_div
def month_name(month):
# Function to get the name of the month from its number
return datetime.strptime(str(month), '%m').strftime('%B')
@login_required
def chart_view(request):
# Get referral code associated with the logged-in user
referral_code = request.user.referral_code
{% extends 'referral/base.html' %}
{% block content %}
<style>
/* Custom CSS */
.chart-container {
border: 1px solid #dee2e6; /* Gray border */
border-radius: 5px; /* Rounded corners */
padding: 20px; /* Padding around the chart */
background-color: #fff; /* White background color */
margin-top: 20px; /* Add margin to separate from title */
}
.chart-container h2 {
color: #343a40; /* Dark text color */
margin-bottom: 20px; /* Bottom margin for heading */
}
<hr>
<div class="container my-4">
<h2 class="text-center">{{ year_chart_title }}</h2> <!-- Display year-wise chart
title -->
<div id="year_chart_div" class="chart-container">
{{ year_chart_div|safe }}
</div>
</div>
<script>
// JavaScript code for handling dropdown selection and applying filters for
month-wise chart
document.getElementById('apply-filter-btn').addEventListener('click', function()
{
var selectedMonth = document.getElementById('month-dropdown').value;
var selectedYear = document.getElementById('year-dropdown').value;
window.location.href = '/referral/chart/?month=' + selectedMonth + '&year=' +
selectedYear;
});
{% endblock %}