背景:
利用echarts来制作dashboard的热力图。
views.py
def count_heatmap_day(classroom_name,query_date='2022-07-13'):
today1 = query_date
today = dt.datetime.strptime(str(today1),"%Y-%m-%d")
print('query_date',query_date)
week_start_date = today - dt.timedelta(days=today.weekday())
week_end_date = today + dt.timedelta(days=6-today.weekday())
week_Monday = week_start_date
week_Tuesday = week_start_date + dt.timedelta(days=1)
week_Wednesday = week_start_date + dt.timedelta(days=2)
week_Thursday = week_start_date + dt.timedelta(days=3)
week_Friday = week_start_date + dt.timedelta(days=4)
week_Saturday = week_start_date + dt.timedelta(days=5)
week_Sunday = week_end_date
source = []
hours_range = ['8:00', '8:30', '9:00', '9:30','10:00', '10:30', '11:00','11:30',
'12:00', '12:30', '13:00', '13:30','14:00', '14:30', '15:00','15:30',
'16:00', '16:30', '17:00', '17:30','18:00', '18:30', '19:00','19:30',
'20:00','20:30',
]
week_range = [week_Sunday, week_Saturday, week_Friday, week_Thursday, week_Wednesday, week_Tuesday, week_Monday, ]
if classroom_name:
for j in range(len(week_range)):
for i in range(len(hours_range)):
fenzi_week_someday = booking_all.filter(Q(booking_classroom_name__contains=classroom_name) & Q(booking_date=week_range[j]) & Q(booking_start_time__lte=hours_range[i]) & Q(booking_end_time__gte=hours_range[i]) & Q(booking_approve_Y_N='Y')).count()
source.append([j,i,fenzi_week_someday])
else:
for j in range(len(week_range)):
for i in range(len(hours_range)):
fenzi_week_someday = booking_all.filter(Q(booking_date=week_range[j]) & Q(booking_start_time__lte=hours_range[i]) & Q(booking_end_time__gte=hours_range[i]) & Q(booking_approve_Y_N='Y')).count()
source.append([j,i,fenzi_week_someday])
return source
@login_required
def dashboard_map_day(request):
hr_permission = request.user.has_perm('myclassroom.add_classpermission')
print('hr_permission',hr_permission)
now_today = dt.date.today()
classroom_name = request.POST.get('classroom_name', '')
paradata = request.POST.get('paradata', now_today)
if paradata=='':
paradata = now_today
else:
pass
print('paradata',paradata)
source = count_heatmap_day(classroom_name=classroom_name, query_date=paradata)
context = {
'paradata':paradata,
'classroom_name':classroom_name,
'value_map_day':json.dumps(source),
}
return render(request, 'dashboard_map_day.html',context=context)
templates:dashboard_map_day.html
{% extends 'dashboard-base.html' %}
{% block main_block %}
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">
<h1 class="h2">Dashboard</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<div class="row m-1">
<form action="" method="post">
{% csrf_token %}
<div class="form-group row" >
<div class="row">
<input class="form-control col-7" type="text" placeholder="教室名称" style="font-size:15px;" id="classroom_name" name="classroom_name" />
</div>
<div class="row">
<input class="form-control col-7" type="date" style="font-size:15px;" id="paradata" name="paradata" />
<input class="btn-small btn-primary col-4" type="submit" value="查询">
</div>
</div>
</form>
</div>
</div>
</div>
<h2>本周教室借订分布图(教室数量)-{{ paradata }} {{ classroom_name }} </h2>
<canvas class="my-4" id="main" width="1200" height="400"></canvas>
<script type="text/javascript">
var value_map_day_js = JSON.parse('{{ value_map_day|safe }}');
console.log('value_map_day_js',value_map_day_js);
var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
var option;
const hours = ['8:00', '8:30', '9:00', '9:30','10:00', '10:30', '11:00','11:30',
'12:00', '12:30', '13:00', '13:30','14:00', '14:30', '15:00','15:30',
'16:00', '16:30', '17:00', '17:30','18:00', '18:30', '19:00','19:30',
'20:00','20:30',
];
const days = ['Sunday','Saturday', 'Friday', 'Thursday','Wednesday', 'Tuesday', 'Monday', ];
const data = value_map_day_js
.map(function (item) {
return [item[1], item[0], item[2] || '-'];
});
option = {
tooltip: {
position: 'top'
},
grid: {
height: '50%',
top: '10%'
},
xAxis: {
type: 'category',
data: hours,
splitArea: {
show: true
}
},
yAxis: {
type: 'category',
data: days,
splitArea: {
show: true
}
},
visualMap: {
min: 0,
max: 10,
calculable: true,
orient: 'horizontal',
left: 'center',
bottom: '15%'
},
series: [
{
name: 'Punch Card',
type: 'heatmap',
data: data,
label: {
show: true
},
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
option && myChart.setOption(option);
</script>
</main>
{% endblock %}
|