当前位置:   article > 正文

基于django的可视化大屏编程_大屏可视化交互动态模板网页前端模板如何引入基于django的平台中

大屏可视化交互动态模板网页前端模板如何引入基于django的平台中

一、创建工程

 1、在项目下创建模板static目录,并建立cssimgjs目录

 

 2、js目录下创建charts目录,其下创建bar.jsbar_vertical.jsheatmap.jsline.jsmap.jspie.jsrose.jssandian.js、wordcloud.js文件

 wordcloud.js的代码

  1. function create_wordcloud(dom_id,data) {
  2. var chart = echarts.init(document.getElementById(dom_id));
  3. var option = {
  4. tooltip: {},
  5. series: [{
  6. type: 'wordCloud',
  7. gridSize: 2,
  8. sizeRange: [12, 50],
  9. rotationRange: [-90, 90],
  10. //shape: 'pentagon',
  11. width:100,
  12. height: 100,
  13. drawOutOfBound: true,
  14. textStyle: {
  15. normal: {
  16. color: function () {
  17. return 'rgb(' + [
  18. Math.round(Math.random() * 160),
  19. Math.round(Math.random() * 160),
  20. Math.round(Math.random() * 160)
  21. ].join(',') + ')';
  22. }
  23. },
  24. emphasis: {
  25. shadowBlur: 10,
  26. shadowColor: '#333'
  27. }
  28. },
  29. data: data
  30. }]
  31. };
  32. if (option && typeof option === "object") {
  33. chart.setOption(option, true);
  34. }
  35. }

 dashboard.html文件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>用户行为分析展示大屏</title>
  8. <link rel="stylesheet" href="/static/css/bootstrap.min.css">
  9. <link rel="stylesheet" href="/static/css/layui.css">
  10. <link rel="stylesheet" href="/static/css/site.css">
  11. <link rel="stylesheet" href="/static/css/dashboard.css">
  12. <script src="/static/js/jquery.min.js"></script>
  13. <script src="/static/js/bootstrap.min.js"></script>
  14. <script src="/static/js/echarts.min.js"></script>
  15. <script src="/static/js/echarts-wordcloud.min.js"></script>
  16. <script src="/static/js/shine.js"></script>
  17. <script src="/static/js/macarons.js"></script>
  18. <script src="/static/js/china.js"></script>
  19. <script src="/static/js/charts/pie.js"></script>
  20. <script src="/static/js/charts/map.js"></script>
  21. <script src="/static/js/charts/bar_vertical.js"></script>
  22. <script src="/static/js/charts/line.js"></script>
  23. <script src="/static/js/charts/rose.js"></script>
  24. <script src="/static/js/charts/heatmap.js"></script>
  25. <script src="/static/js/charts/bar.js"></script>
  26. <script src="/static/js/charts/sandian.js"></script>
  27. <script src="/static/js/charts/wordcloud.js"></script>
  28. <script src="/static/js/dashboard.js"></script>
  29. </head>
  30. <body>
  31. <div class="container">
  32. <div class="row" style="text-align:
  33. center;height:50px;font-weight:bolder;font-size:28px;color:white;background:url(/static/img/title.png);background-repeat:
  34. no-repeat;background-size: 100% 100%">
  35. <span>用户购物行为分析展示大屏</span>
  36. <span id="time"></span>
  37. </div>
  38. <div class="row" >
  39. <div class="col-sm-3">
  40. <div class="row">
  41. <div class="chart m-panel">
  42. <div class="panel-header">
  43. <span style="margin-left:5px;font-weight:
  44. bolder;">用户性别分析</span>
  45. </div>
  46. <div id="user_gender" style="height: 200px"></div>
  47. </div>
  48. </div>
  49. <div class="row">
  50. <div class="chart m-panel">
  51. <div class="panel-header">
  52. <span style="margin-left:5px;font-weight:
  53. bolder;">新用户注册</span>
  54. </div>
  55. <div id="user_registertime" style="height: 200px"></div>
  56. </div>
  57. </div>
  58. <div class="row">
  59. <div class="chart m-panel">
  60. <div class="panel-header">
  61. <span style="margin-left:5px;font-weight:
  62. bolder;">商品推荐排行</span>
  63. </div>
  64. <div id="product_category" style="height: 200px"></div>
  65. </div>
  66. </div>
  67. </div>
  68. <div class="col-sm-6">
  69. <!-- <div class="row" id="summary" style="padding: 5px 0 0 0;">
  70. <div class="col-sm-3 summary">
  71. <div
  72. style="height:100%;width:100%;background:#0D349B;"></div>
  73. </div> <div class="col-sm-3 summary">
  74. <div
  75. style="height:100%;width:100%;background:#0D349B;"></div>
  76. </div> <div class="col-sm-3 summary">
  77. <div
  78. style="height:100%;width:100%;background:#0D349B;"></div>
  79. </div> <div class="col-sm-3 summary">
  80. <div
  81. style="height:100%;width:100%;background:#0D349B;"></div>
  82. </div>
  83. </div> -->
  84. <div class="row">
  85. <div class="m-panel">
  86. <div class="panel-header">
  87. <span style="margin-left:5px;font-weight:
  88. bolder;">用户地域分布</span>
  89. </div>
  90. <div id="user_province" style="height: 388px"></div>
  91. </div>
  92. </div>
  93. <div class="row">
  94. <div class="col-sm-6" style="padding:0 5px 0 0;">
  95. <div class="chart m-panel">
  96. <div class="panel-header">
  97. <span style="margin-left:5px;font-weight:
  98. bolder;">商品热销词云</span>
  99. </div>
  100. <div id="product_name" style="height: 200px"></div>
  101. </div>
  102. </div>
  103. <div class="col-sm-6" style="padding:0 0 0 5px;">
  104. <div class="chart m-panel">
  105. <div class="panel-header">
  106. <span style="margin-left:5px;font-weight:
  107. bolder;">商品销售量分布</span>
  108. </div>
  109. <div id="sandian" style="height: 200px"></div>
  110. </div>
  111. </div>
  112. </div>
  113. </div>
  114. <div class="col-sm-3">
  115. <div class="row">
  116. <div class="chart m-panel">
  117. <div class="panel-header">
  118. <span style="margin-left:5px;font-weight:
  119. bolder;">用户等级分布</span>
  120. </div>
  121. <div id="user_viplevel" style="height: 200px"></div>
  122. </div>
  123. </div>
  124. <div class="row">
  125. <div class="chart m-panel">
  126. <div class="panel-header">
  127. <span style="margin-left:5px;font-weight:
  128. bolder;">用户活跃度热图</span>
  129. </div>
  130. <div id="heatmap" style="height: 200px"></div>
  131. </div>
  132. </div>
  133. <div class="row">
  134. <div class="chart m-panel">
  135. <div class="panel-header">
  136. <span style="margin-left:5px;font-weight:
  137. bolder;">商品品牌排行</span>
  138. </div>
  139. <div id="product_brand" style="height: 200px"></div>
  140. </div>
  141. </div>
  142. <div class="row">
  143. </div>
  144. </div>
  145. </div>
  146. </div>
  147. <script type="text/javascript">
  148. function time(){
  149. var vWeek,vWeek_s,vDay;
  150. vWeek = ["星期天","星期一","星期二","星期三","星期四","星期五","星期六"];
  151. var date = new Date();
  152. year = date.getFullYear();
  153. month = date.getMonth() + 1;
  154. day = date.getDate();
  155. hours = date.getHours();
  156. minutes = date.getMinutes();
  157. seconds = date.getSeconds();
  158. vWeek_s = date.getDay();
  159. document.getElementById("time").innerHTML = year + "年" + month + "月" + day + "日" + "\t" + hours + ":" + minutes +":" + seconds + "\t" + vWeek[vWeek_s] ;
  160. };
  161. setInterval("time()",1000);
  162. </script>
  163. </body>
  164. </html>

 models.py文件

  1. from django.db import models
  2. # Create your models here.
  3. class attribute(models.Model):
  4. id = models.IntegerField(verbose_name="信息ID", default=1, primary_key=True, unique=True, null=False)
  5. type = models.IntegerField(verbose_name="类型", default=1, blank=False, null=False)
  6. attr = models.CharField(verbose_name="属性罗列", max_length=50, null=False, default='')
  7. value = models.CharField(verbose_name="占比罗列", max_length=50, null=False, default='')
  8. operator = models.IntegerField(verbose_name="操作人员ID", default=999999, null=False)
  9. opertime = models.DateField(verbose_name="操作时间", max_length=50, null=False)
  10. class action(models.Model):
  11. id = models.IntegerField(verbose_name="信息ID",default=1, primary_key=True, unique=True, null=False)
  12. action = models.CharField(verbose_name="行为大类", max_length=50, null=False, default='')
  13. quantity = models.CharField(verbose_name="数量占比", max_length=50, null=False, default='')
  14. operator = models.IntegerField(verbose_name="操作人员ID", default=999999, null=False)
  15. opertime = models.DateField(verbose_name="操作时间", max_length=50, null=False)

 创建view.py文件

  1. from django.http import HttpResponse
  2. from django.shortcuts import render
  3. from user.models import attribute as UserAttribute
  4. from user.models import action as UserAction
  5. from product.models import attribute as ProductAttribute
  6. from product.models import action as ProductAction
  7. import json
  8. from enum import Enum
  9. class UserAttrType(Enum):
  10. gender = 1
  11. province = 2
  12. viplevel = 3
  13. registertime = 4
  14. class ProductAttrType(Enum):
  15. category = 1
  16. brand= 2
  17. tradename = 3
  18. def get_user_gender():
  19. user_gender = UserAttribute.objects.filter(type=1).latest('id')
  20. return user_gender
  21. def get_user_province():
  22. user_province = UserAttribute.objects.filter(type=2).latest('id')
  23. return user_province
  24. def get_user_viplevel():
  25. user_viplevel = UserAttribute.objects.filter(type=3).latest('id')
  26. return user_viplevel
  27. def get_user_registertime():
  28. user_registertime = UserAttribute.objects.filter(type=4).latest('id')
  29. return user_registertime
  30. def get_user_action(request):
  31. product_info = UserAction.objects.latest('id')
  32. return product_info
  33. def get_product_attribute(request):
  34. user_info = ProductAttribute.objects.latest('id')
  35. return user_info
  36. def get_product_name():
  37. product_name = ProductAttribute.objects.filter(type=3).latest('id')
  38. return product_name
  39. def get_product_brand():
  40. product_brand = ProductAttribute.objects.filter(type=2).latest('id')
  41. return product_brand
  42. def get_product_category():
  43. product_category = ProductAttribute.objects.filter(type=1).latest('id')
  44. return product_category
  45. def get_prodcut_action(request):
  46. product_info = ProductAction.objects.latest('id')
  47. return product_info
  48. def get_user_gender_json(request):
  49. user_gender=get_user_gender()
  50. resp = {}
  51. resp['errorcode'] = 100
  52. resp['detail'] = "success"
  53. data = []
  54. try:
  55. for i in range(len(user_gender.attr.split( '|'))):
  56. data.append({"name": user_gender.attr.split( '|')[i], "value": user_gender.value.split( '|')[i]})
  57. except Exception as e:
  58. print('Error:', e)
  59. resp['errorcode'] = 404
  60. resp['detail'] = "failure"
  61. data = e
  62. resp['data'] =data
  63. return HttpResponse(json.dumps(resp,ensure_ascii=False), content_type="application/json")
  64. def get_user_province_json(request):
  65. user_province=get_user_province()
  66. resp = {}
  67. resp['errorcode'] = 100
  68. resp['detail'] = "success"
  69. data =[]
  70. try:
  71. for i in range(len(user_province.attr.split( '|'))):
  72. data.append({"name": user_province.attr.split( '|')[i], "value": user_province.value.split( '|')[i]})
  73. except Exception as e:
  74. print('Error:', e)
  75. resp['errorcode'] = 404
  76. resp['detail'] = "failure"
  77. data = e
  78. resp['data'] =data
  79. return HttpResponse(json.dumps(resp,ensure_ascii=False), content_type="application/json")
  80. def get_user_viplevel_json(request):
  81. user_viplevel=get_user_viplevel()
  82. resp = {}
  83. resp['errorcode'] = 100
  84. resp['detail'] = "success"
  85. data =[]
  86. try:
  87. for i in range(len(user_viplevel.attr.split( '|'))):
  88. data.append({"name": user_viplevel.attr.split( '|')[i], "value": user_viplevel.value.split( '|')[i]})
  89. except Exception as e:
  90. print('Error:', e)
  91. resp['errorcode'] = 404
  92. resp['detail'] = "failure"
  93. data = e
  94. resp['data'] =data
  95. return HttpResponse(json.dumps(resp,ensure_ascii=False), content_type="application/json")
  96. def get_user_registertime_json(request):
  97. user_registertime=get_user_registertime()
  98. resp = {}
  99. resp['errorcode'] = 100
  100. resp['detail'] = "success"
  101. data =[]
  102. try:
  103. for i in range(len(user_registertime.attr.split( '|'))):
  104. data.append({"name": user_registertime.attr.split( '|')[i], "value": user_registertime.value.split( '|')[i]})
  105. except Exception as e:
  106. print('Error:', e)
  107. resp['errorcode'] = 404
  108. resp['detail'] = "failure"
  109. data = e
  110. resp['data'] =data
  111. return HttpResponse(json.dumps(resp,ensure_ascii=False), content_type="application/json")
  112. def get_product_category_json(request):
  113. product_category=get_product_category()
  114. resp = {}
  115. resp['errorcode'] = 100
  116. resp['detail'] = "success"
  117. data =[]
  118. try:
  119. for i in range(len(product_category.attr.split( '|'))):
  120. data.append({"name": product_category.attr.split( '|')[i], "value": product_category.value.split( '|')[i]})
  121. except Exception as e:
  122. print('Error:', e)
  123. resp['errorcode'] = 404
  124. resp['detail'] = "failure"
  125. data = e
  126. resp['data'] =data
  127. return HttpResponse(json.dumps(resp,ensure_ascii=False), content_type="application/json")
  128. def get_product_brand_json(request):
  129. product_brand=get_product_brand()
  130. resp = {}
  131. resp['errorcode'] = 100
  132. resp['detail'] = "success"
  133. data =[]
  134. try:
  135. for i in range(len(product_brand.attr.split( '|'))):
  136. data.append({"name": product_brand.attr.split( '|')[i], "value": product_brand.value.split( '|')[i]})
  137. except Exception as e:
  138. print('Error:', e)
  139. resp['errorcode'] = 404
  140. resp['detail'] = "failure"
  141. data = e
  142. resp['data'] =data
  143. return HttpResponse(json.dumps(resp,ensure_ascii=False), content_type="application/json")
  144. def get_product_name_json(request):
  145. product_name=get_product_name()
  146. resp = {}
  147. resp['errorcode'] = 100
  148. resp['detail'] = "success"
  149. data =[]
  150. try:
  151. for i in range(len(product_name.attr.split( '|'))):
  152. data.append({"name": product_name.attr.split( '|')[i], "value": product_name.value.split( '|')[i]})
  153. except Exception as e:
  154. print('Error:', e)
  155. resp['errorcode'] = 404
  156. resp['detail'] = "failure"
  157. data = e
  158. resp['data'] =data
  159. return HttpResponse(json.dumps(resp,ensure_ascii=False), content_type="application/json")
  160. def dashboard(request,):
  161. return render(request,'dashboard.html')

 修改urls.py文件

  1. """ConsumerBuyingBehaviour URL Configuration
  2. The `urlpatterns` list routes URLs to views. For more information please see:
  3. https://docs.djangoproject.com/en/2.2/topics/http/urls/
  4. Examples:
  5. Function views
  6. 1. Add an import: from my_app import views
  7. 2. Add a URL to urlpatterns: path('', views.home, name='home')
  8. Class-based views
  9. 1. Add an import: from other_app.views import Home
  10. 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
  11. Including another URLconf
  12. 1. Import the include() function: from django.urls import include, path
  13. 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
  14. """
  15. from django.contrib import admin
  16. from django.urls import path
  17. from django.conf.urls import url
  18. from . import view
  19. urlpatterns = [
  20. path('',view.dashboard),
  21. path('api/user_gender/', view.get_user_gender_json),
  22. path('api/user_province/', view.get_user_province_json),
  23. path('api/user_viplevel/', view.get_user_viplevel_json),
  24. path('api/user_registertime/', view.get_user_registertime_json),
  25. path('api/product_category/', view.get_product_category_json),
  26. path('api/product_brand/', view.get_product_brand_json),
  27. path('api/product_name/', view.get_product_name_json),
  28. path('dashboard/', view.dashboard),
  29. ]

修改settings.py文件

  1. """
  2. Django settings for ConsumerBuyingBehaviour project.
  3. Generated by 'django-admin startproject' using Django 2.2.4.
  4. For more information on this file, see
  5. https://docs.djangoproject.com/en/2.2/topics/settings/
  6. For the full list of settings and their values, see
  7. https://docs.djangoproject.com/en/2.2/ref/settings/
  8. """
  9. import os
  10. # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
  11. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  12. # Quick-start development settings - unsuitable for production
  13. # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
  14. # SECURITY WARNING: keep the secret key used in production secret!
  15. SECRET_KEY = '0=$&m8he4kttcw8xzw534olj-m%-vud%%cdtz)_#vo+l_oumht'
  16. # SECURITY WARNING: don't run with debug turned on in production!
  17. DEBUG = True
  18. ALLOWED_HOSTS = ['*']
  19. # Application definition
  20. INSTALLED_APPS = [
  21. 'django.contrib.admin',
  22. 'django.contrib.auth',
  23. 'django.contrib.contenttypes',
  24. 'django.contrib.sessions',
  25. 'django.contrib.messages',
  26. 'django.contrib.staticfiles',
  27. 'user',
  28. 'product',
  29. ]
  30. MIDDLEWARE = [
  31. 'django.middleware.security.SecurityMiddleware',
  32. 'django.contrib.sessions.middleware.SessionMiddleware',
  33. 'django.middleware.common.CommonMiddleware',
  34. 'django.middleware.csrf.CsrfViewMiddleware',
  35. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  36. 'django.contrib.messages.middleware.MessageMiddleware',
  37. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  38. ]
  39. ROOT_URLCONF = 'ConsumerBuyingBehaviour.urls'
  40. TEMPLATES = [
  41. {
  42. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  43. 'DIRS': [BASE_DIR+"/templates"],
  44. 'APP_DIRS': True,
  45. 'OPTIONS': {
  46. 'context_processors': [
  47. 'django.template.context_processors.debug',
  48. 'django.template.context_processors.request',
  49. 'django.contrib.auth.context_processors.auth',
  50. 'django.contrib.messages.context_processors.messages',
  51. ],
  52. },
  53. },
  54. ]
  55. WSGI_APPLICATION = 'ConsumerBuyingBehaviour.wsgi.application'
  56. # Database
  57. # https://docs.djangoproject.com/en/2.2/ref/settings/#databases
  58. DATABASES = {
  59. 'default': {
  60. #'ENGINE': 'django.db.backends.sqlite3',
  61. #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  62. 'ENGINE': 'django.db.backends.mysql',
  63. #'NAME': 'consumer_buying_behaviour',
  64. #'USER': 'user01',
  65. #'PASSWORD': 'sugon123!',
  66. #'HOST': 'localhost',
  67. #'PORT': '3306',,
  68. 'NAME': 'user_actions_db',
  69. 'USER': 'root',
  70. 'PASSWORD': '123456',
  71. 'HOST': '211.84.112.23',
  72. 'PORT': '8046',
  73. #'HOST': '172.16.0.11',
  74. #'PORT': '3306',
  75. }
  76. }
  77. # Password validation
  78. # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
  79. AUTH_PASSWORD_VALIDATORS = [
  80. {
  81. 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  82. },
  83. {
  84. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  85. },
  86. {
  87. 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  88. },
  89. {
  90. 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  91. },
  92. ]
  93. # Internationalization
  94. # https://docs.djangoproject.com/en/2.2/topics/i18n/
  95. LANGUAGE_CODE = 'en-us'
  96. TIME_ZONE = 'UTC'
  97. USE_I18N = True
  98. USE_L10N = True
  99. USE_TZ = True
  100. # Static files (CSS, JavaScript, Images)
  101. # https://docs.djangoproject.com/en/2.2/howto/static-files/
  102. STATIC_URL = '/static/'
  103. STATICFILES_DIRS=[os.path.join(BASE_DIR,'static'),]

3、发布工程

python.exe manage.py runserver 127.0.0.1:8000

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号