增加 统计信息 页面,集成Echarts

This commit is contained in:
ying-yee
2017-05-16 19:29:16 +08:00
parent 825ce1c721
commit 7ce0462e74
14 changed files with 434 additions and 28 deletions
+1 -1
View File
@@ -82,7 +82,7 @@ DATABASES = {
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-US'
LANGUAGE_CODE = 'zh_Hans'
TIME_ZONE = 'GMT+8'
+248
View File
File diff suppressed because one or more lines are too long
BIN
View File
Binary file not shown.
+43 -13
View File
@@ -10,6 +10,7 @@ import django
django.setup()
import json
import argparse
import random
import datetime
import os.path as op
@@ -34,7 +35,7 @@ def init_reader_data(amount=50):
def init_book_data():
with open(op.join('DoubanBookSpider', 'books.json'), 'r') as f:
with open('books.json', 'r') as f:
books = json.loads(f.read())
for b in books:
@@ -49,27 +50,56 @@ def init_book_data():
def init_borrowing_data(amount=50):
for i in range(amount):
try:
reader = Reader.objects.get(pk=random.randint(1, 50))
except Reader.DoesNotExist:
pass
reader = random.choice(Reader.objects.all())
isbn = random.choice(Book.objects.all())
issued = datetime.date.today() + datetime.timedelta(random.randint(1, 30))
due_to_returned = issued + datetime.timedelta(30)
date_returned = issued + datetime.timedelta(random.randint(1, 40))
returned_flag = True
if random.randint(1, 100) % 2 == 0:
returned_flag = False
if returned_flag:
if (date_returned - issued).days > 30:
fine = ((date_returned - issued).days - 30) * 0.1
else:
fine = 0
if reader.max_borrowing > 0:
b = Borrowing.objects.create(
reader=reader,
ISBN=isbn,
date_issued=issued,
date_due_to_returned=due_to_returned)
reader.max_borrowing -= 1
reader.save()
date_due_to_returned=due_to_returned,
date_returned=date_returned,
amount_of_fine=fine,
)
b.save()
else:
if reader.max_borrowing > 0 and isbn.quantity > 0:
b = Borrowing.objects.create(
reader=reader,
ISBN=isbn,
date_issued=issued,
date_due_to_returned=due_to_returned
)
reader.max_borrowing -= 1
isbn.quantity -= 1
reader.save()
isbn.save()
b.save()
if __name__ == '__main__':
init_reader_data()
init_book_data()
init_borrowing_data()
parser = argparse.ArgumentParser()
parser.add_argument("data", help=u"你要生成的数据")
args = parser.parse_args()
if args.data == 'all':
init_reader_data()
init_book_data()
init_borrowing_data()
else:
init_borrowing_data()
File diff suppressed because one or more lines are too long
+1 -3
View File
@@ -1,8 +1,6 @@
{% extends 'library/base.html' %}
{% block title %}
关于
{% endblock %}
{% block title %} 关于 {% endblock %}
{% block content %}
+2
View File
@@ -8,6 +8,7 @@
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'library/bootstrap/css/bootstrap.min.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'library/custom.css' %}"/>
<script src="{% static 'library/echarts.min.js' %}"></script>
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>{% block title %}{% endblock %}</title>
@@ -39,6 +40,7 @@
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="/search/?keyword=_书目列表">书目列表</a></li>
<li><a href="/statistics/">统计信息</a></li>
<li><a href="/about/">关于</a></li>
</ul>
+1 -3
View File
@@ -1,8 +1,6 @@
{% extends 'library/base.html' %}
{% block title %}
欢迎来到图书馆!
{% endblock %}
{% block title %} 欢迎来到图书馆! {% endblock %}
{% block content %}
+1 -3
View File
@@ -1,8 +1,6 @@
{% extends 'library/base.html' %}
{% block title %}
请登陆
{% endblock %}
{% block title %} 请登陆 {% endblock %}
{% load static %}
<link href="{% static 'library/signin.css' %}" rel="stylesheet">
+1 -3
View File
@@ -1,8 +1,6 @@
{% extends 'library/base.html' %}
{% block title %}
请登陆
{% endblock %}
{% block title %} 请登陆 {% endblock %}
{% load static %}
<link href="{% static 'library/signin.css' %}" rel="stylesheet">
+1 -1
View File
@@ -27,7 +27,7 @@
</form>
</div>
<h2 class="label-success text-center col-md-5 col-md-offset-3">以下是{{ search_by }}匹配'{{ keyword }}'的结果</h2>
<h2 class="label-success text-center col-md-5 col-md-offset-4">以下是{{ search_by }}匹配'{{ keyword }}'的结果</h2>
<table class="table table-hover">
<thead>
<th>#</th>
+64
View File
@@ -0,0 +1,64 @@
{% extends 'library/base.html' %}
{% load staticfiles %}
{% block title %} 统计信息 {% endblock %}
{% block content %}
<div class="text-center">
<h1>统计信息</h1>
</div>
<div id="reader-rank" class="col-md-offset-3" style="width: 1000px;height:400px;"></div>
<div id="book-rank" class="col-md-offset-3 myContainer" style="width: 1000px;height:400px;"></div>
<script type="text/javascript">
// 基于准备好的dom,初始化echarts实例
var readerChart = echarts.init(document.getElementById('reader-rank'));
var bookChart = echarts.init(document.getElementById('book-rank'));
// 指定图表的配置项和数据
var readerOption = {
title: {
text: '借书排行榜'
},
tooltip: {},
legend: {
data: ['借书量']
},
xAxis: {
data: [ {% for x in readerData %} '{{ x }}', {% endfor %} ]
},
yAxis: {},
series: [{
name: '借书量',
type: 'bar',
data: [ {% for x in readerAmountData %} '{{ x }}', {% endfor %} ]
}]
};
var bookOption = {
title: {
text: '最受欢迎的图书'
},
tooltip: {},
legend: {
data: ['借书量'],
},
xAxis: {
data: [ {% for x in bookData %} '《{{ x }}》', {% endfor %} ]
},
yAxis: {},
series: [{
name: '借书量',
type: 'bar',
data: [ {% for x in bookAmountData %} '{{ x }}', {% endfor %} ]
}]
};
readerChart.setOption(readerOption);
bookChart.setOption(bookOption);
</script>
{% endblock %}
+1
View File
@@ -19,5 +19,6 @@ urlpatterns = [
url(r'^book/action$', views.reader_operation, name='reader_operation'),
url(r'^search/', views.book_search, name='book_search'),
url(r'^profile/', views.profile, name='profile'),
url(r'^statistics/', views.statistics, name='statistics'),
url(r'^about/', views.about, name='about'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
+33 -1
View File
@@ -257,7 +257,7 @@ def book_detail(request):
reader.save()
bk = Book.objects.get(pk=ISBN)
bk.quantity-=1
bk.quantity -= 1
bk.save()
issued = datetime.date.today()
@@ -282,5 +282,37 @@ def book_detail(request):
return render(request, 'library/book_detail.html', context)
def statistics(request):
borrowing = Borrowing.objects.all()
readerInfo = {}
for r in borrowing:
if r.reader.name not in readerInfo:
readerInfo[r.reader.name] = 1
else:
readerInfo[r.reader.name] += 1
bookInfo = {}
for r in borrowing:
if r.ISBN.title not in bookInfo:
bookInfo[r.ISBN.title] = 1
else:
bookInfo[r.ISBN.title] += 1
readerData = list(sorted(readerInfo, key=readerInfo.__getitem__, reverse=True))[0:10]
bookData = list(sorted(bookInfo, key=bookInfo.__getitem__, reverse=True))[0:5]
readerAmountData = [readerInfo[x] for x in readerData]
bookAmountData = [bookInfo[x] for x in bookData]
context = {
'readerData': readerData,
'readerAmountData': readerAmountData,
'bookData': bookData,
'bookAmountData': bookAmountData,
}
return render(request, 'library/statistics.html', context)
def about(request):
return render(request, 'library/about.html', {})