您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么在django項目中使用higcharts統計文章點擊量,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
前端引入
<script src="/static/highcharts/highcharts.js"></script> <script src="/static/highcharts/modules/exporting.js"></script> <script src="/static/highcharts/modules/oldie.js"></script> <script src="/static/highcharts/highcharts-zh_CN.js"></script>
定義在頁面中的位置
<div id="container" ></div>
前端js
<script> var chart = Highcharts.chart('container', { chart: { type: 'line' }, title: { text: '日點擊量和點贊量統計' }, subtitle: { text: '數據來源: terroristhouse.com' }, xAxis: { categories: {{ list_week_day|safe }} }, yAxis: { title: { text: '數量 (次)' } }, plotOptions: { line: { dataLabels: { // 開啟數據標簽 enabled: true }, // 關閉鼠標跟蹤,對應的提示框、點擊事件會失效 enableMouseTracking: false } }, series: [{ name: '點擊量', data:{{ clicknum_list|safe }} }, { name: '點贊量', data: {{ praise_num_list|safe }} }] }); </script>
路由
# 點擊量統計 re_path('article/click/', article.click,name='article/click/'),
后臺方法
from blog.utils import function # 點擊量 def click(request): recent_seven_days = function.recent_seven_days() list_week_day = recent_seven_days[::-1] # 進行倒序 clicknum_list = [] praise_num_list = [] # print(list_week_day) for v in list_week_day: click_num_obj = Praise.objects.filter(click_addtime=v,click_sort=1).aggregate(clicknum=Count('click_sort')) praise_num_obj = Praise.objects.filter(click_addtime=v,click_sort=0).aggregate(praise_num=Count('click_sort')) # print(click_num_obj['clicknum'],praise_num_obj['praise_num']) clicknum = int(click_num_obj['clicknum']) if (click_num_obj['clicknum'] is not None) else 0 praise_num = int(praise_num_obj['praise_num']) if (praise_num_obj['praise_num'] is not None) else 0 clicknum_list.append(clicknum) praise_num_list.append(praise_num) # print(clicknum_list) # data=[{ # 'name': '點擊量', # 'data': clicknum_list # }, { # 'name': '點贊量', # 'data': praise_num_list # }] # num= [ '20190624', '20190625', '20190626', '20190627', '20190628', '20190629', '20190630'] return render(request,'article/click.html',locals())
應用目錄下創建untils文件夾,并在其下創建function.py文件,用來獲取最近七天日期
# 七天日期 def recent_seven_days():# 通過for 循環得到天數,如果想得到兩周的時間,只需要把8改成15就可以了。 import datetime d = datetime.datetime.now()#2019-6-28 9:25:43.843164 lists = [] for i in range(1,8):#i:1-7 oneday = datetime.timedelta(days=i) #1 day, 0:00:00 2 days, 0:00:00 ... 7 days, 0:00:00 day = d - oneday#2019-06-27 11:32:10.186535 2019-06-26 11:32:10.186535 ... 2019-06-21 11:32:10.186535 date_to = datetime.datetime(day.year, day.month, day.day)#2019-06-27 00:00:00 2019-06-26 00:00:00 ... 2019-06-21 00:00:00 lists.append(str(date_to)[0:10])#2019-06-27 2019-06-26 ... 2019-06-21 return lists
頁面效果
上述就是小編為大家分享的怎么在django項目中使用higcharts統計文章點擊量了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。