前言

这是个项目是对淘宝的数据进行:数据爬取,数据分析,可视化大屏,机器学习进行销量预测。

视频:商品数据可视化大屏

技术:

数据获取:爬虫的selenium自动化技术

数据存储:MySQL数据库

后端:python的Django框架

前端:vue框架

图表:echarts

需要的工具:

后端与数据库:

  1. python,pycharm专业版
  2. MySQL,Navicat
  3. Chrome,ChromeDriver

前端:

  1. node.js
1
2
># 镜像源:
>npm config set registry https://registry.npmmirror.com
  1. vscode

项目展示:

001

1.项目初始化

1.创建项目

001

2.安装依赖

  1. 创建一个requirements.txt文件,requirements.txt的内容是:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    asgiref==3.7.2
    async-generator==1.10
    attrs==22.1.0
    backports.zoneinfo==0.2.1
    blinker==1.6.2
    certifi==2022.6.15
    cffi==1.15.1
    charset-normalizer==3.2.0
    click==8.1.7
    colorama==0.4.6
    cryptography==37.0.4
    cycler==0.11.0
    distlib==0.3.7
    Django==4.2.5
    django-cors-headers==4.2.0
    exceptiongroup==1.1.2
    filelock==3.12.2
    Flask==2.3.3
    Flask-Cors==4.0.0
    Flask-SQLAlchemy==3.0.5
    fonttools==4.34.4
    greenlet==2.0.2
    h11==0.13.0
    idna==3.3
    importlib-metadata==6.8.0
    itsdangerous==2.1.2
    jieba==0.42.1
    Jinja2==3.1.2
    joblib==1.3.2
    kiwisolver==1.4.4
    lxml==4.9.3
    MarkupSafe==2.1.3
    matplotlib==3.5.2
    mysqlclient==2.2.0
    numpy==1.23.1
    outcome==1.2.0
    packaging==21.3
    pandas==1.5.3
    Pillow==9.2.0
    platformdirs==3.10.0
    pycparser==2.21
    PyMySQL==1.0.2
    pyOpenSSL==22.0.0
    pyparsing==3.0.9
    PySocks==1.7.1
    python-dateutil==2.8.2
    pytz==2022.1
    requests==2.31.0
    scikit-learn==1.3.2
    scipy==1.10.1
    selenium==4.3.0
    six==1.16.0
    sniffio==1.2.0
    sortedcontainers==2.4.0
    SQLAlchemy==1.4.45
    sqlparse==0.4.2
    threadpoolctl==3.2.0
    trio==0.21.0
    trio-websocket==0.9.2
    typing_extensions==4.7.1
    tzdata==2022.1
    urllib3==1.26.11
    urllib3-secure-extra==0.1.0
    virtualenv==20.24.3
    virtualenvwrapper-win==1.2.7
    werkzeug==2.3.7
    wordcloud==1.8.2.2
    wsproto==1.1.0
    zipp==3.8.0
  2. 将requirements.txt文件复制到同名目录下:

    001

  3. 打开终端

    001

  4. 安装依赖,在终端输入

    1
    pip install -r ..\..\requirements.txt
  5. 查看安装的依赖

    001

3.基础配置

  1. 修改语言,时区

    1
    2
    3
    LANGUAGE_CODE = 'zh-Hans'

    TIME_ZONE = 'Asia/Shanghai'

    001

  2. 配置数据库

    • 在Navicat中创建数据库goodsData:

      001

    • 回到settings.py修改:

      001

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      DATABASES = {
      'default': {
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'goodsData',
      'USER': 'root',
      'PASSWORD': 'root',
      'HOST': 'localhost',
      'POST': '3306'
      }
      }

2.爬虫

新建一个spiderMain文件夹,并且在spiderMain文件夹创建一个spider.py文件,如:
001

1.软件安装

需要下载chrome浏览器和chromedriver.exe驱动。

  1. 查看Chrome的版本号

  2. 下载ChromeDriver

    1
    2
    3
    ChromeDriver下载地址:
    版本在114及以下:http://chromedriver.storage.googleapis.com/index.html
    版本在127:https://googlechromelabs.github.io/chrome-for-testing/#stable
  3. 将chromedriver.exe放在spiderMain文件夹下

  4. 将chrome的目录添加在环境变量中

    1
    C:\Program Files\Google\Chrome\Application

    001

  5. chrome浏览器设置

    可以防止浏览器打开新的页面,被爬取数据的网页系统监测到

    001

  6. chrome浏览器端口设置

    管理员身份运行cmd输入

    1
    chrome.exe --remote-debugging-port=9223

    注:出现报错是没有进行第4步的配置,只需要配置第4步后,重新执行第6步即可

2.爬虫编写

注意:运行代码时,一定要点开浏览器,并且是对应的端口号

*1.导入所需的依赖

1
2
3
4
5
6
7
8
9
10
11
from selenium import webdriver
from pymysql import *
import pandas as pd
from sqlalchemy import create_mock_engine
import time
import re
import csv
import os
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

*2.创建浏览器对象

目标:使用chromedriver.exe打开需要爬取数据的网页

1
2
3
4
5
6
7
8
9
10
if __name__ == '__main__':
service = Service('./chromedriver.exe')
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress","localhost:9223") # 不打开新页面进行数据爬取
brower = webdriver.Chrome(service=service, options=options) # 创建浏览器对象
brower.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',{
'source': 'Object.defineProperty(navigator,"webdriver",{get: () => undefined})'}) # 通过反爬措施:通过网站的测验,如二维码检测等

url = "https://www.taobao.com/"
brower.get(url)

*3.数据初始化(init)

目标:用来创建存储数据的文件,并且初始化文件的内容

1
2
3
4
5
6
7
8
9
# 创建存取数据的文件data.csv
def init():
# 删除文件后重新创建文件夹
if os.path.exists('./data.csv'):
os.remove('./data.csv')
with open('./data.csv','a',encoding='utf-8',newline='') as f:
myWrite = csv.writer(f)
myWrite.writerow(['product_type','title','price','buy_len',
'img_src','name','address','isFreeDelivery','href','nameHref'])

*4.获取指定的数据类型

目标:通过key参数来传递数据,能够获取对应key值得搜索

例如:获取购物网站上衣物,食品,电器等不同类型的数据得搜索

1
2
3
4
5
# 通过key参数,传递搜索的关键字
def search_product(key):
brower.find_element(By.ID,"q").send_keys(key) # find_element找到参数中的元素By.ID,"q"
brower.find_element(By.XPATH,'//*[@id="J_TSearchForm"]/div[1]/button').click()
time.sleep(5)

*5.获取对应的产品信息

目标:获取到当页的需要的数据信息,并且实现下一页的翻页

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 获取对应的产品信息
def get_product(count):
items = brower.find_elements(By.XPATH,'//div[@class="Content--contentInner--QVTcU0M"]/a')
for div in items:
try:
count += 1
# 类型
product_type = key
# 商品名
title = div.find_element(By.XPATH, './/div[@class="Title--title--jCOPvpf "]/span').text
# 价格
price = div.find_element(By.XPATH,
'.//div[@style="margin-right: 8px;"]/span[@class="Price--priceInt--ZlsSi_M"]').text + \
div.find_element(By.XPATH,
'.//div[@style="margin-right: 8px;"]/span[@class="Price--priceFloat--h2RR0RK"]').text
# 销量
buy_len = div.find_element(By.XPATH,
'.//div[@class="Price--priceWrapper--Q0Dn7pN "]/span[@class="Price--realSales--FhTZc7U"]').text
buy_len = re.search("\d+", buy_len).group() # d+为匹配数字的正则表达式
# 图片
img_src = div.find_element(By.XPATH, './/img[@class="MainPic--mainPic--rcLNaCv"]').get_attribute("src") # 获取属性使用get_attribute函数
# 店铺名
name = div.find_element(By.XPATH, './/div[@class="ShopInfo--TextAndPic--yH0AZfx"]/a').text
# 地址
address = div.find_element(By.XPATH, './/div[@class="Price--procity--_7Vt3mX"]/span').text
# 包邮情况
isFreeDelivery = '不包邮' # 默认不包邮
try:
isFreeDeliveryList = div.find_elements(By.XPATH,
'.//div[@class="SalesPoint--subIconWrapper--s6vanNY "]/div/span')
isFreeDeliveryList = [element.text for element in isFreeDeliveryList] # 获取所有的文本元素
if "包邮" in isFreeDeliveryList:
isFreeDelivery = "包邮"
except:
isFreeDelivery = "不包邮"
# 详情页面
href = div.get_attribute("href") # 此时的div变量就是a标签
# 店铺详情
namehref = div.find_element(By.XPATH, './/a[@class="ShopInfo--shopName--rg6mGmy"]').get_attribute("href")
# 写入数据
save_to_csv(product_type, title, price, buy_len, img_src, name, address, isFreeDelivery, href, namehref)

# print(product_type, title, price, buy_len, img_src, name, address, isFreeDelivery, href, namehref)
except:
count -= 1
continue

if count % 10 == 0:
print("已爬取%d条数据"%count)
else:
# 实现翻页,获取下一页的数据
brower.find_element(By.XPATH,'.//button[@class="next-btn next-medium next-btn-normal next-pagination-item next-next"]').click()
print("正在翻页")
time.sleep(10)
get_product(count)

*6.保存数据

1
2
3
4
5
6
# 数据保存到CSV文件夹中
def save_to_csv(product_type, title, price, buy_len, img_src, name, address, isFreeDelivery, href, namehref):
with open('./data.csv', 'a', encoding="UTF-8", newline='') as f:
myWriter = csv.writer(f, dialect='excel', delimiter=',') # dialect='excle'使它兼容excle; delimiter=','逗号分隔
myWriter.writerow([product_type, title, price, buy_len, img_src, name, address, isFreeDelivery, href, namehref]) # 按顺序写入,注意写入的数据类型是列表格式

7.创建数据库的表

  1. 打开命令行

    001

  2. 创建一个app,输入:

    1
    python manage.py startapp myApp
  3. 配置settings.py,添加:

    1
    'myApp'

    001

  4. 编写models.py

    001

    编写代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    from django.db import models

    # Create your models here.
    from django.db import models

    # 商品表
    class Products(models.Model):
    id = models.AutoField('id',primary_key=True)
    type = models.CharField('商品类型', max_length=255,default='')
    title = models.CharField('商品名称', max_length=255, default='')
    price = models.CharField('商品价格', max_length=255, default='')
    buy_len = models.CharField('商品销量', max_length=255, default='')
    img_src = models.CharField('图片', max_length=2555, default='')
    name = models.CharField('店铺名', max_length=255, default='')
    address = models.CharField('地址', max_length=255, default='')
    isFreeDelivery = models.CharField('是否包邮', max_length=255, default='')
    href = models.CharField('详情链接', max_length=2555, default='')
    nameHref = models.CharField('店铺详情链接', max_length=2555, default='')

    class Meta:
    db_table = "products"

    # 用户表
    class User(models.Model):
    id = models.AutoField('id', primary_key=True)
    username = models.CharField('用户名', max_length=255, default='')
    password = models.CharField('密码', max_length=255, default='')
    createTime = models.DateTimeField("创建时间",auto_now_add=True)


    class Meta:
    db_table = "user"
  5. 创建数据库

    在终端输入:

    1
    python manage.py makemigrations

    接着输入,即可将表创建成功(可在navicat中查看):

    1
    python manage.py migrate

*8.数据清洗

目标:对数据进行清洗

1
2
3
4
5
6
# 数据清洗
def df_clean(df):
df['address'] = df['address'].str.split('省')
df['address'] = df['address'].str.get(0)
df.fillna('暂无',inplace=True)
return df

*9.数据保存到数据库

1
2
3
4
5
6
7
def save_to_sql():
products = pd.read_csv('./data.csv')
df = pd.DataFrame(products)
conn = create_engine('mysql+pymysql://root:root@localhost:3306/goodsdata?charset=utf8')
df = df_clean(df)
df.to_sql('products',con=conn,index=False,if_exists='append')
print("导入数据库成功")

10.爬虫优化

目标:自动完成页面滚动,脱离人工

  1. 查看整个网页的高度

    001

  2. 实现浏览器滚动

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # 爬虫的优化
    scroll_position = 0
    scroll_amount = 200 # 一次滚动的距离
    max_scroll = 7000


    # 实现浏览器滚动
    if scroll_position < max_scroll:
    scroll_script = f"window.scrollBy(0,{scroll_amount});"
    brower.execute_script(scroll_script)
    scroll_position += scroll_amount

11.爬虫完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from selenium import webdriver
from pymysql import *
import pandas as pd
from sqlalchemy import create_mock_engine, create_engine
import time
import re
import csv
import os
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


# 创建爬虫函数
def spider_fn(key):
# 创建存取数据的文件data.csv
def init():
# 删除文件后重新创建文件夹
if os.path.exists('./data.csv'):
os.remove('./data.csv')
with open('./data.csv','a',encoding='utf-8',newline='') as f:
myWrite = csv.writer(f)
myWrite.writerow(['type','title','price','buy_len',
'img_src','name','address','isFreeDelivery','href','nameHref'])

# 通过key参数,传递搜索的关键字
def search_product(key):
brower.find_element(By.ID,"q").send_keys(key) # find_element找到参数中的元素By.ID,"q"
brower.find_element(By.XPATH,'//*[@id="J_TSearchForm"]/div[1]/button').click()
time.sleep(5)

# 获取对应的产品信息
def get_product(count):
# 爬虫的优化
scroll_position = 0
scroll_amount = 300 # 一次滚动的距离
max_scroll = 6900

items = brower.find_elements(By.XPATH,'//div[@class="Content--contentInner--QVTcU0M"]/a')
for div in items:
try:
count += 1
# 类型
type = key
# 商品名
title = div.find_element(By.XPATH, './/div[@class="Title--title--jCOPvpf "]/span').text
# 价格
price = div.find_element(By.XPATH,
'.//div[@style="margin-right: 8px;"]/span[@class="Price--priceInt--ZlsSi_M"]').text + \
div.find_element(By.XPATH,
'.//div[@style="margin-right: 8px;"]/span[@class="Price--priceFloat--h2RR0RK"]').text
# 销量
buy_len = div.find_element(By.XPATH,
'.//div[@class="Price--priceWrapper--Q0Dn7pN "]/span[@class="Price--realSales--FhTZc7U"]').text
buy_len = re.search("\d+", buy_len).group() # d+为匹配数字的正则表达式
# 图片
img_src = div.find_element(By.XPATH, './/img[@class="MainPic--mainPic--rcLNaCv"]').get_attribute("src") # 获取属性使用get_attribute函数
# 店铺名
name = div.find_element(By.XPATH, './/div[@class="ShopInfo--TextAndPic--yH0AZfx"]/a').text
# 地址
address = div.find_element(By.XPATH, './/div[@class="Price--procity--_7Vt3mX"]/span').text
# 包邮情况
isFreeDelivery = '不包邮' # 默认不包邮
try:
isFreeDeliveryList = div.find_elements(By.XPATH,
'.//div[@class="SalesPoint--subIconWrapper--s6vanNY "]/div/span')
isFreeDeliveryList = [element.text for element in isFreeDeliveryList] # 获取所有的文本元素
if "包邮" in isFreeDeliveryList:
isFreeDelivery = "包邮"
except:
isFreeDelivery = "不包邮"
# 详情页面
href = div.get_attribute("href") # 此时的div变量就是a标签
# 店铺详情
nameHref = div.find_element(By.XPATH, './/a[@class="ShopInfo--shopName--rg6mGmy"]').get_attribute("href")
# 写入数据
save_to_csv(type, title, price, buy_len, img_src, name, address, isFreeDelivery, href, nameHref)

# print(type, title, price, buy_len, img_src, name, address, isFreeDelivery, href, nameHref)
except:
count -= 1
continue

# 实现浏览器滚动
if scroll_position < max_scroll:
scroll_script = f"window.scrollBy(0,{scroll_amount});"
brower.execute_script(scroll_script)
scroll_position += scroll_amount

if count % 10 == 0:
print("已爬取%d条数据"%count)
else:
# 实现翻页,获取下一页的数据
brower.find_element(By.XPATH,'.//button[@class="next-btn next-medium next-btn-normal next-pagination-item next-next"]').click()
print("正在翻页")
time.sleep(10)
get_product(count)

# 数据保存到CSV文件夹中
def save_to_csv(type, title, price, buy_len, img_src, name, address, isFreeDelivery, href, nameHref):
with open('./data.csv', 'a', encoding="UTF-8", newline='') as f:
myWriter = csv.writer(f, dialect='excel', delimiter=',') # dialect='excle'使它兼容excle; delimiter=','逗号分隔
myWriter.writerow([type, title, price, buy_len, img_src, name, address, isFreeDelivery, href, nameHref]) # 按顺序写入,注意写入的数据类型是列表格式

# 数据清洗
def df_clean(df):
df['address'] = df['address'].str.split('省')
df['address'] = df['address'].str.get(0)
df.fillna('暂无',inplace=True)
return df

def save_to_sql():
products = pd.read_csv('./data.csv')
df = pd.DataFrame(products)
conn = create_engine('mysql+pymysql://root:root@localhost:3306/goodsdata?charset=utf8')
df = df_clean(df)
df.to_sql('products',con=conn,index=False,if_exists='append')
print("导入数据库成功")



def main():
count = 0 # 获取爬取数据信息的条数
init()
search_product(key)
get_product(count)
# save_to_sql()
main()


if __name__ == '__main__':
service = Service('./chromedriver.exe')
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress","localhost:9223") # 不打开新页面进行数据爬取
brower = webdriver.Chrome(service=service, options=options) # 创建浏览器对象
brower.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',{
'source': 'Object.defineProperty(navigator,"webdriver",{get: () => undefined})'}) # 通过反爬措施:通过网站的测验,如二维码检测等

url = "https://www.taobao.com/"
brower.get(url)

spider_fn('食品')

3.前端

为了更加注重后端的编写,所以此项目的前端使用的是模板。

vscode格式化快捷键:shift + alt + f

1.前端–初始化

  1. 模板网页链接: 轻量级后台管理系统基础模板

    001

    解压到项目文件夹下

    001

  2. 在vscode中打开解压后的文件夹

  3. 在vscode的终端中下载package.json,并且一定要删除package-lock.json文件

    001

  4. 在终端输入(若node.js安装在C盘,右键管理员身份运行vscode):

    1
    npm install
  5. 启动项目

    1
    2
    3
    npm run serve

    # 若出现报错,则输入:npm run lint -- --fix

    可在浏览器输入,查看:

    1
    http://localhost:8080

    001

  6. eslin注释(或删除)

    1
    2
    3
    "lint": "vue-cli-service lint",

    "@vue/cli-plugin-babel": "^4.1.0",
  7. ctrl+c停止项目,然后重启项目

2.前端–大屏样式

  1. 注释app.vue中的内容(快捷键ctrl + /)

    作用:让网页能够向下滑动

    001

  2. 网页布局

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    <style scoped>
    /*设置整体*/
    .layout {
    height: 100vh;
    width: 100%;
    background: url('../assets/imgs/bg03.jpg');
    }
    .header {
    display: flex;
    align-items: center;
    justify-content: center;
    }
    .header div {
    width: 50%;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    }
    .header div span {
    color: steelblue;
    font-weight: bold;
    font-size: 23px;
    padding: 10px;
    }
    .content {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: row;
    }
    .content .left {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 80%;
    width: 350px;
    }
    .content .left div {
    flex-grow: 1;
    height: 300px;
    width: 300px;
    padding: 5px;
    }
    .content .right {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 80%;
    width: 350px;
    }
    .content .right div {
    flex-grow: 1;
    height: 300px;
    width: 300px;
    padding: 5px;
    }
    .content .center {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 80%;
    width: 600px;
    }
    </style>
  3. 布局元素:DataV (jiaminghi.com)

    • 找到安装依赖

    001

    • 在vscode中新建一个终端,输入:

      1
      2
      3
      npm install @jiaminghi/data-view

      # 注:若安装失败,需要使用管理员身份运行vscode
  4. 在main.js中引入datav,输入:

    注意导入的依赖一定要在import App from ‘./App’之前

    1
    2
    3
    import { borderBox1 } from '@jiaminghi/data-view'

    Vue.use(borderBox1)

    001

  5. 样式的home.vue文件代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    <template>
    <div class="layout">
    <!-- 头部 -->
    <div class="header">
    <dv-decoration-8 style="width:300px;height:50px;" />
    <div>
    <span class="title-text">大数据可视化平台</span>
    <dv-decoration-5 style="width:300px;height:40px;" />
    </div>
    <dv-decoration-8 :reverse="true" style="width:300px;height:50px;" />
    </div>

    <!-- 内容 -->
    <div class="content">
    <div class="left">
    <dv-border-box-13>
    <dv-decoration-1 style="width:200px;height:10px;" />
    <div></div>
    </dv-border-box-13>
    <dv-border-box-8><div></div></dv-border-box-8>
    </div>
    <div class="center">
    <div></div>
    </div>
    <div class="right">
    <dv-border-box-12>
    <dv-decoration-3 style="width:250px;height:10px;" />
    <div></div>
    </dv-border-box-12>
    <dv-border-box-1><div></div></dv-border-box-1>
    </div>
    </div>
    </div>
    </template>

    <script>
    // 注释
    /*
    注释
    */
    export default {

    }
    </script>

    <style scoped>
    /*设置整体*/
    .layout {
    height: 100vh;
    width: 100%;
    background: url('../assets/imgs/bg03.jpg');
    }
    .header {
    display: flex;
    align-items: center;
    justify-content: center;
    }
    .header div {
    width: 50%;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    }
    .header div span {
    color: steelblue;
    font-weight: bold;
    font-size: 23px;
    padding: 10px;
    }
    .content {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: row;
    }
    .content .left {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 80%;
    width: 350px;
    }
    .content .left div {
    flex-grow: 1;
    height: 300px;
    width: 300px;
    padding: 5px;
    }
    .content .right {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 80%;
    width: 350px;
    }
    .content .right div {
    flex-grow: 1;
    height: 300px;
    width: 300px;
    padding: 5px;
    }
    .content .center {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 80%;
    width: 600px;
    }
    </style>

4.图形绘制(前端 + 后端接口)

1.柱状图

1.柱状图前端

  1. script基础配置(一定要有注释和空格)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <script>
    // 注释
    export default {
    data() {
    // 函数体
    },
    methods: {
    // 方法
    },
    async mounted() {
    // 生命周期钩子
    },
    }
    </script>
  2. 柱状图:使用echarts图表

    • 在终端输入安装echarts:

      1
      npm install --save echarts
    • 在main.js中引入echarts

      1
      2
      3
      import * as echarts from 'echarts'

      Vue.prototype.$echarts = echarts

      001

    • 柱状图

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      <script>
      // 注释
      export default {
      data() {
      // 函数体
      },
      methods: {
      // 方法
      drawLeftTop() {
      let myChart = this.$echarts.init(this.$refs.firstMain)
      let option = {
      xAxis: {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      },
      yAxis: {
      type: 'value',
      },
      series: [
      {
      data: [120, 200, 150, 80, 70, 110, 130],
      type: 'bar',
      },
      ],
      }
      if (option) {
      myChart.setOption(option)
      }
      },
      },
      async mounted() {
      // 生命周期
      this.drawLeftTop()
      },
      }
      </script>

      如果报错,在终端输入:

      1
      2
      3
      npm install -g eslint

      npx eslint --fix src/views/Home.vue
  3. 最终的Home.vue的代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    <template>
    <div class="layout">
    <!-- 头部 -->
    <div class="header">
    <dv-decoration-8 style="width:300px;height:50px;" />
    <div>
    <span class="title-text">大数据可视化平台</span>
    <dv-decoration-5 style="width:300px;height:40px;" />
    </div>
    <dv-decoration-8 :reverse="true" style="width:300px;height:50px;" />
    </div>

    <!-- 内容 -->
    <div class="content">
    <div class="left">
    <dv-border-box-13>
    <dv-decoration-1 style="width:200px;height:10px;" />
    <div ref="firstMain" style="width:300px;height:270px;"></div>
    </dv-border-box-13>
    <dv-border-box-8>
    <div></div>
    </dv-border-box-8>
    </div>
    <div class="center">
    <div></div>
    </div>
    <div class="right">
    <dv-border-box-12>
    <dv-decoration-3 style="width:250px;height:10px;" />
    <div></div>
    </dv-border-box-12>
    <dv-border-box-1>
    <div></div>
    </dv-border-box-1>
    </div>
    </div>
    </div>
    </template>

    <script>
    // 注释
    export default {
    data() {
    // 函数体
    },
    methods: {
    // 方法
    drawLeftTop() {
    let myChart = this.$echarts.init(this.$refs.firstMain)
    let option = {
    xAxis: {
    type: 'category',
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
    },
    yAxis: {
    type: 'value',
    },
    series: [
    {
    data: [120, 200, 150, 80, 70, 110, 130],
    type: 'bar',
    },
    ],
    }
    if (option) {
    myChart.setOption(option)
    }
    },
    },
    async mounted() {
    // 生命周期
    this.drawLeftTop()
    },
    }
    </script>

    <style scoped>
    /*设置整体*/
    .layout {
    height: 100vh;
    width: 100%;
    background: url('../assets/imgs/bg03.jpg');
    }

    .header {
    display: flex;
    align-items: center;
    justify-content: center;
    }

    .header div {
    width: 50%;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    }

    .header div span {
    color: steelblue;
    font-weight: bold;
    font-size: 23px;
    padding: 10px;
    }

    .content {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: row;
    }

    .content .left {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 80%;
    width: 350px;
    margin-left: 30px;
    }

    .content .left div {
    flex-grow: 1;
    height: 300px;
    width: 300px;
    padding: 5px;
    }

    .content .right {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 80%;
    width: 350px;
    }

    .content .right div {
    flex-grow: 1;
    height: 300px;
    width: 300px;
    padding: 5px;
    }

    .content .center {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 80%;
    width: 600px;
    }
    </style>

2.柱状图后端接口

1.流程

创建好下图的几个文件

001

1
2
3
4
5
1.商品/urls.py:配置路由
2.myApp/urls.py
3.myApp/views.py:接受请求,返回数据
4.myApp/utils/getScreenData.py:实现获取数据的的方法
5.myApp/utils/getBaseData.py:引入数据库的数据

2.实现

  1. 商品/urls.py

    1
    2
    3
    4
    5
    6
    7
    8
    from django.contrib import admin
    from django.urls import path,include

    urlpatterns = [
    path('admin/', admin.site.urls),
    path('myApp/',include('myApp.urls'))
    ]

  2. myApp/urls.py

    1
    2
    3
    4
    5
    6
    from django.urls import path
    from myApp import views

    urlpatterns = [
    path("screenData/", views.screenData,name="screenData"),
    ]
  3. myApp/views.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    from django.shortcuts import render
    from django.http import JsonResponse
    from .utils import getScreenData

    # Create your views here.

    def screenData(request):
    if request.method == "GET":
    cityList,volumnList = getScreenData.getSquareData()
    return JsonResponse({
    'cityList' : cityList,
    'volumnList' : volumnList
    })
  4. myApp/utils/getScreenData.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    from .getBaseData import *

    def getSquareData():
    goods = list(getAllGoods())
    goodVolumn = {}
    for i in goods:
    if goodVolumn.get(i.address,-1): # 未获取到键,则赋值给销量
    goodVolumn[i.address] = int(i.buy_len)
    else:
    goodVolumn[i.address] += int(i.buy_len)
    goodsSortVolumn = sorted(goodVolumn.items(),key=lambda x:x[1],reverse=True) # 对销量排序
    cityList = []
    volumnList = []
    for i in goodsSortVolumn:
    cityList.append(i[0])
    volumnList.append(i[1])
    return cityList[:7],volumnList[:7]
  5. myApp/utils/getBaseData.py

    1
    2
    3
    4
    5
    6
    from myApp.models import *

    def getAllGoods():
    # 引入Products表中的所有数据
    return Products.objects.all()

3.实现前后端关联

1.跨域的实现

为了防止前后端的端口号不同,出现跨域问题。需在pycharm项目的settings.py中设置:

001

1
2
3
4
5
6
7
8
9
10
11
'corsheaders',


'corsheaders.middleware.CorsMiddleware',


CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_ORIGINS = 'http://localhost:8080'
CORS_ALLOW_HEADERS = ('*')
CORS_ORIGIN_ALLOW_ALL = True

2.前端发送请求与接受数据

  1. 流程:

    1
    前端 -(发送请求)-> 后端 -(数据接收)-> 前端
  2. 前端编写流程

    1
    2
    3
    1.src/api/index.js:封装HTTP请求
    2.src/main.js:全局注册HTTP客户端
    3.src/views/Home.vue:发送HTTP请求

3.前端编写

  1. src/api/index.js

    1
    2
    3
    4
    5
    6
    7
    8
    import axios from 'axios'

    const service = axios.create({
    baseURL: 'http://127.0.0.1:8000/',
    timeout: 40000,
    })

    export default service
  2. src/main.js

    1
    2
    3
    import $http from '@/api/index.js'

    Vue.prototype.$http = $http
  3. src/views/Home.vue

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    // 发送请求
    <script>
    // 注释
    export default {
    data() {
    // 函数体
    return {
    realData: {
    cityList: [1],
    volumnList: [1],
    },
    }
    },
    methods: {
    // 方法
    drawLeftTop() {
    let myChart = this.$echarts.init(this.$refs.firstMain)
    let option = {
    xAxis: {
    type: 'category',
    data: this.realData.cityList,
    },
    yAxis: {
    type: 'value',
    },
    series: [
    {
    data: this.realData.volumnList,
    type: 'bar',
    },
    ],
    }
    if (option) {
    myChart.setOption(option)
    }
    },
    },
    async mounted() {
    // 生命周期
    // 异步请求,使用await
    const res = await this.$http.get('myApp/screenData/')
    // 接收数据
    this.$set(this.realData, 'cityList', res.data.cityList)
    this.$set(this.realData, 'volumnList', res.data.volumnList)
    this.drawLeftTop()
    },
    }
    </script>

    以及修改表的x,y轴的数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    let option = {
    xAxis: {
    type: 'category',
    data: this.realData.cityList,
    },
    yAxis: {
    type: 'value',
    },
    series: [
    {
    data: this.realData.volumnList,
    type: 'bar',
    },
    ],
    }
  4. 美化图形

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    <script>
    import { color } from 'echarts'
    import * as echarts from 'echarts'
    import { readonly } from 'vue'

    // 注释
    export default {
    data() {
    // 函数体
    return {
    isHovered: true,
    realData: {
    cityList: [1],
    volumnList: [1],
    },
    }
    },
    methods: {
    // 方法
    drawLeftTop() {
    let myChart = this.$echarts.init(this.$refs.firstMain)
    let option = {
    xAxis: {
    type: 'category',
    data: this.realData.cityList,
    axisLabel: {
    textStyle: {
    color: '#C0C0C0',
    },
    },
    },
    dataZoom: [
    {
    type: 'slider',
    start: 0,
    end: 85,
    show: false,
    },
    ],
    title: {
    text: '各地区销售数据',
    textStyle: {
    color: '#C0C0C0',
    },
    },
    toolbox: {
    show: true,
    feature: {
    // dataView: { show: true, realData: true },
    magicType: { show: true, type: ['line', 'bar'] },
    restore: { show: true },
    SaveAsImage: { show: true },
    },
    },
    legend: {
    orient: 'vertical',
    right: 'right',
    data: ['销售数据'],
    textStyle: {
    color: 'grey',
    },
    top: '8%',
    left: '70%',
    },
    tooltip: {
    trigger: 'item',
    formatter: '',
    },
    yAxis: {
    type: 'value',
    axisLabel: {
    textStyle: {
    color: '#C0C0C0',
    },
    },
    },
    series: [
    {
    name: '销售数据',
    data: this.realData.volumnList,
    type: 'bar',
    label: {
    show: true,
    position: 'top',
    textStyle: {
    color: '#C0C0C0',
    },
    },
    },
    ],
    }
    if (option) {
    myChart.setOption(option)
    }
    },
    changeData(x) {
    let st = x[0]
    for (let i = 0; i < x.length - 1; i++) {
    x[i] = x[i + 1]
    }
    x[x.length - 1] = st
    },
    updataBarChart() {
    if (this.isHovered == true) {
    this.changeData(this.realData.cityList)
    this.changeData(this.realData.volumnList)
    let myChart = this.$echarts.init(this.$refs.firstMain)
    myChart.setOption({
    xAxis: {
    data: this.realData.cityList,
    },
    series: [
    {
    data: this.realData.volumnList,
    },
    ],
    })
    } else {
    clearInterval(this.timer)
    }
    },
    startDataupdataInterval() {
    const interval = 2000
    clearInterval(this.timer)
    setInterval(this.updataBarChart, interval)
    },
    startAction() {
    this.isHovered = false
    },
    cancelAction() {
    this.isHovered = true
    },
    },
    async mounted() {
    // 生命周期
    // 异步请求,使用await
    const res = await this.$http.get('myApp/screenData/')
    // 接收数据
    this.$set(this.realData, 'cityList', res.data.cityList)
    this.$set(this.realData, 'volumnList', res.data.volumnList)
    },
    async updated() {
    this.drawLeftTop()
    this.startDataupdataInterval()
    },
    }
    </script>

4.Home.vue的完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<template>
<div class="layout">
<!-- 头部 -->
<div class="header">
<dv-decoration-8 style="width:300px;height:50px;" />
<div>
<span class="title-text">大数据可视化平台</span>
<dv-decoration-5 style="width:300px;height:40px;" />
</div>
<dv-decoration-8 :reverse="true" style="width:300px;height:50px;" />
</div>

<!-- 内容 -->
<div class="content">
<div class="left">
<dv-border-box-13>
<dv-decoration-1 style="width:200px;height:10px;" />
<div
ref="firstMain"
style="width:350px;height:270px;"
v-bind:key="realData.volumnList[0]"
@mouseenter="startAction"
@mouseleave="cancelAction"
></div>
</dv-border-box-13>
<dv-border-box-8>
<div></div>
</dv-border-box-8>
</div>
<div class="center">
<div></div>
</div>
<div class="right">
<dv-border-box-12>
<dv-decoration-3 style="width:250px;height:10px;" />
<div></div>
</dv-border-box-12>
<dv-border-box-1>
<div></div>
</dv-border-box-1>
</div>
</div>
</div>
</template>

<script>
import { color } from 'echarts'
import * as echarts from 'echarts'
import { readonly } from 'vue'

// 注释
export default {
data() {
// 函数体
return {
isHovered: true,
realData: {
cityList: [1],
volumnList: [1],
},
}
},
methods: {
// 方法
drawLeftTop() {
let myChart = this.$echarts.init(this.$refs.firstMain)
let option = {
xAxis: {
type: 'category',
data: this.realData.cityList,
axisLabel: {
textStyle: {
color: '#C0C0C0',
},
},
},
dataZoom: [
{
type: 'slider',
start: 0,
end: 85,
show: false,
},
],
title: {
text: '各地区销售数据',
textStyle: {
color: '#C0C0C0',
},
},
toolbox: {
show: true,
feature: {
// dataView: { show: true, realData: true },
magicType: { show: true, type: ['line', 'bar'] },
restore: { show: true },
SaveAsImage: { show: true },
},
},
legend: {
orient: 'vertical',
right: 'right',
data: ['销售数据'],
textStyle: {
color: 'grey',
},
top: '8%',
left: '70%',
},
tooltip: {
trigger: 'item',
formatter: '',
},
yAxis: {
type: 'value',
axisLabel: {
textStyle: {
color: '#C0C0C0',
},
},
},
series: [
{
name: '销售数据',
data: this.realData.volumnList,
type: 'bar',
label: {
show: true,
position: 'top',
textStyle: {
color: '#C0C0C0',
},
},
},
],
}
if (option) {
myChart.setOption(option)
}
},
changeData(x) {
let st = x[0]
for (let i = 0; i < x.length - 1; i++) {
x[i] = x[i + 1]
}
x[x.length - 1] = st
},
updataBarChart() {
if (this.isHovered == true) {
this.changeData(this.realData.cityList)
this.changeData(this.realData.volumnList)
let myChart = this.$echarts.init(this.$refs.firstMain)
myChart.setOption({
xAxis: {
data: this.realData.cityList,
},
series: [
{
data: this.realData.volumnList,
},
],
})
} else {
clearInterval(this.timer)
}
},
startDataupdataInterval() {
const interval = 2000
clearInterval(this.timer)
setInterval(this.updataBarChart, interval)
},
startAction() {
this.isHovered = false
},
cancelAction() {
this.isHovered = true
},
},
async mounted() {
// 生命周期
// 异步请求,使用await
const res = await this.$http.get('myApp/screenData/')
// 接收数据
this.$set(this.realData, 'cityList', res.data.cityList)
this.$set(this.realData, 'volumnList', res.data.volumnList)
},
async updated() {
this.drawLeftTop()
this.startDataupdataInterval()
},
}
</script>

<style scoped>
/*设置整体*/
.layout {
height: 100vh;
width: 100%;
background: url('../assets/imgs/bg03.jpg');
}

.header {
display: flex;
align-items: center;
justify-content: center;
}

.header div {
width: 50%;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}

.header div span {
color: steelblue;
font-weight: bold;
font-size: 23px;
padding: 10px;
}

.content {
width: 100%;
height: 100%;
display: flex;
flex-direction: row;
}

.content .left {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 350px;
margin-left: 30px;
}

.content .left div {
flex-grow: 1;
height: 300px;
width: 380px;
padding: 5px;
}

.content .right {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 350px;
}

.content .right div {
flex-grow: 1;
height: 300px;
width: 350px;
padding: 5px;
}

.content .center {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 600px;
}
</style>

2.饼状图

1.饼状图前端

  1. 饼状图:使用echarts图表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    drawLeftBottom() {
    let myChart = this.$echarts.init(this.$refs.secondMain)
    let option = {
    title: {
    text: 'Referer of a Website',
    subtext: 'Fake Data',
    left: 'center',
    },
    tooltip: {
    trigger: 'item',
    },
    legend: {
    orient: 'vertical',
    left: 'left',
    },
    series: [
    {
    name: 'Access From',
    type: 'pie',
    radius: '50%',
    data: [
    { value: 1048, name: 'Search Engine' },
    { value: 735, name: 'Direct' },
    { value: 580, name: 'Email' },
    { value: 484, name: 'Union Ads' },
    { value: 300, name: 'Video Ads' },
    ],
    emphasis: {
    itemStyle: {
    shadowBlur: 10,
    shadowOffsetX: 0,
    shadowColor: 'rgba(0, 0, 0, 0.5)',
    },
    },
    },
    ],
    }
    if (option) {
    myChart.setOption(option)
    }
    },
  2. 最终的Home.vue的代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    <template>
    <div class="layout">
    <!-- 头部 -->
    <div class="header">
    <dv-decoration-8 style="width:300px;height:50px;" />
    <div>
    <span class="title-text">大数据可视化平台</span>
    <dv-decoration-5 style="width:300px;height:40px;" />
    </div>
    <dv-decoration-8 :reverse="true" style="width:300px;height:50px;" />
    </div>

    <!-- 内容 -->
    <div class="content">
    <div class="left">
    <dv-border-box-13>
    <dv-decoration-1 style="width:200px;height:10px;" />
    <div ref="firstMain" style="width:350px;height:270px;" v-bind:key="realData.volumnList[0]"
    @mouseenter="startAction" @mouseleave="cancelAction"></div>
    </dv-border-box-13>
    <dv-border-box-8>
    <div ref="secondMain" style="width:350px;height:270px;"></div>
    </dv-border-box-8>
    </div>
    <div class="center">
    <div></div>
    </div>
    <div class="right">
    <dv-border-box-12>
    <dv-decoration-3 style="width:250px;height:10px;" />
    <div></div>
    </dv-border-box-12>
    <dv-border-box-1>
    <div></div>
    </dv-border-box-1>
    </div>
    </div>
    </div>
    </template>

    <script>
    import { color } from 'echarts'
    import * as echarts from 'echarts'
    import { readonly } from 'vue'

    // 注释
    export default {
    data() {
    // 函数体
    return {
    isHovered: true,
    realData: {
    cityList: [1],
    volumnList: [1],
    },
    }
    },
    methods: {
    // 方法
    drawLeftTop() {
    let myChart = this.$echarts.init(this.$refs.firstMain)
    let option = {
    xAxis: {
    type: 'category',
    data: this.realData.cityList,
    axisLabel: {
    textStyle: {
    color: '#C0C0C0',
    },
    },
    },
    dataZoom: [
    {
    type: 'slider',
    start: 0,
    end: 85,
    show: false,
    },
    ],
    title: {
    text: '各地区销售数据',
    textStyle: {
    color: '#C0C0C0',
    },
    },
    toolbox: {
    show: true,
    feature: {
    // dataView: { show: true, realData: true },
    magicType: { show: true, type: ['line', 'bar'] },
    restore: { show: true },
    SaveAsImage: { show: true },
    },
    },
    legend: {
    orient: 'vertical',
    right: 'right',
    data: ['销售数据'],
    textStyle: {
    color: 'grey',
    },
    top: '8%',
    left: '70%',
    },
    tooltip: {
    trigger: 'item',
    formatter: '',
    },
    yAxis: {
    type: 'value',
    data: this.realData.volumnList,
    axisLabel: {
    textStyle: {
    color: '#C0C0C0',
    },
    },
    },
    series: [
    {
    name: '销售数据',
    data: this.realData.volumnList,
    type: 'bar',
    label: {
    show: true,
    position: 'top',
    textStyle: {
    color: '#C0C0C0',
    },
    },
    },
    ],
    }
    if (option) {
    myChart.setOption(option)
    }
    },
    drawLeftBottom() {
    let myChart = this.$echarts.init(this.$refs.secondMain)
    let option = {
    title: {
    text: 'Referer of a Website',
    subtext: 'Fake Data',
    left: 'center',
    },
    tooltip: {
    trigger: 'item',
    },
    legend: {
    orient: 'vertical',
    left: 'left',
    },
    series: [
    {
    name: 'Access From',
    type: 'pie',
    radius: '50%',
    data: [
    { value: 1048, name: 'Search Engine' },
    { value: 735, name: 'Direct' },
    { value: 580, name: 'Email' },
    { value: 484, name: 'Union Ads' },
    { value: 300, name: 'Video Ads' },
    ],
    emphasis: {
    itemStyle: {
    shadowBlur: 10,
    shadowOffsetX: 0,
    shadowColor: 'rgba(0, 0, 0, 0.5)',
    },
    },
    },
    ],
    }
    if (option) {
    myChart.setOption(option)
    }
    },
    changeData(x) {
    let st = x[0]
    for (let i = 0; i < x.length - 1; i++) {
    x[i] = x[i + 1]
    }
    x[x.length - 1] = st
    },
    updataBarChart() {
    if (this.isHovered == true) {
    this.changeData(this.realData.cityList)
    this.changeData(this.realData.volumnList)
    let myChart = this.$echarts.init(this.$refs.firstMain)
    myChart.setOption({
    xAxis: {
    data: this.realData.cityList,
    },
    series: [
    {
    data: this.realData.volumnList,
    },
    ],
    })
    } else {
    clearInterval(this.timer)
    }
    },
    startDataupdataInterval() {
    const interval = 2000
    clearInterval(this.timer)
    setInterval(this.updataBarChart, interval)
    },
    startAction() {
    this.isHovered = false
    },
    cancelAction() {
    this.isHovered = true
    },
    },
    async mounted() {
    // 生命周期
    // 异步请求,使用await
    const res = await this.$http.get('myApp/screenData/')
    // 接收数据
    this.$set(this.realData, 'cityList', res.data.cityList)
    this.$set(this.realData, 'volumnList', res.data.volumnList)
    this.drawLeftBottom()
    },
    async updated() {
    this.drawLeftTop()
    this.startDataupdataInterval()
    },
    }
    </script>

    <style scoped>
    /*设置整体*/
    .layout {
    height: 100vh;
    width: 100%;
    background: url('../assets/imgs/bg03.jpg');
    }

    .header {
    display: flex;
    align-items: center;
    justify-content: center;
    }

    .header div {
    width: 50%;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    }

    .header div span {
    color: steelblue;
    font-weight: bold;
    font-size: 23px;
    padding: 10px;
    }

    .content {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: row;
    }

    .content .left {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 80%;
    width: 350px;
    margin-left: 30px;
    }

    .content .left div {
    flex-grow: 1;
    height: 300px;
    width: 380px;
    padding: 5px;
    }

    .content .right {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 80%;
    width: 350px;
    }

    .content .right div {
    flex-grow: 1;
    height: 300px;
    width: 350px;
    padding: 5px;
    }

    .content .center {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 80%;
    width: 600px;
    }
    </style>

2.饼状图后端

  1. myApp/utils/getScreenData.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    from .getBaseData import *

    def getSquareData():
    goods = list(getAllGoods())
    goodVolumn = {}
    for i in goods:
    if goodVolumn.get(i.address,-1): # 未获取到键,则赋值给销量
    goodVolumn[i.address] = int(i.buy_len)
    else:
    goodVolumn[i.address] += int(i.buy_len)
    goodsSortVolumn = sorted(goodVolumn.items(),key=lambda x:x[1],reverse=True) # 对销量排序
    cityList = []
    volumnList = []
    for i in goodsSortVolumn:
    cityList.append(i[0])
    volumnList.append(i[1])
    return cityList[:7],volumnList[:7]

    def getPieData():
    goods = list(getAllGoods())
    goodsNumber = {}
    for i in goods:
    if goodsNumber.get(i.type,-1) == -1:
    goodsNumber[i.type] = 1
    else:
    goodsNumber[i.type] += 1
    pieList = []
    for k,v in goodsNumber.items():
    pieList.append({
    'name': k,
    'value': v
    })
    return pieList
  2. myApp/views.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    from django.shortcuts import render
    from django.http import JsonResponse
    from .utils import getScreenData

    # Create your views here.

    def screenData(request):
    if request.method == "GET":
    cityList,volumnList = getScreenData.getSquareData()
    pieList = getScreenData.getPieData()
    return JsonResponse({
    'cityList' : cityList,
    'volumnList' : volumnList,
    'pieList' : pieList
    })

3.前后端关联

Home.vue的全部代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<template>
<div class="layout">
<!-- 头部 -->
<div class="header">
<dv-decoration-8 style="width:300px;height:50px;" />
<div>
<span class="title-text">大数据可视化平台</span>
<dv-decoration-5 style="width:300px;height:40px;" />
</div>
<dv-decoration-8 :reverse="true" style="width:300px;height:50px;" />
</div>

<!-- 内容 -->
<div class="content">
<div class="left">
<dv-border-box-13>
<dv-decoration-1 style="width:200px;height:10px;" />
<div ref="firstMain" style="width:350px;height:270px;" v-bind:key="realData.volumnList[0]"
@mouseenter="startAction" @mouseleave="cancelAction"></div>
</dv-border-box-13>
<dv-border-box-8>
<div ref="secondMain" style="width:350px;height:270px;" v-bind:key="realData.pieList[0][1]"></div>
</dv-border-box-8>
</div>
<div class="center">
<div></div>
</div>
<div class="right">
<dv-border-box-12>
<dv-decoration-3 style="width:250px;height:10px;" />
<div></div>
</dv-border-box-12>
<dv-border-box-1>
<div></div>
</dv-border-box-1>
</div>
</div>
</div>
</template>

<script>
import { color } from 'echarts'
import * as echarts from 'echarts'
import { readonly } from 'vue'

// 注释
export default {
data() {
// 函数体
return {
isHovered: true,
realData: {
cityList: [1],
volumnList: [1],
pieList: [{ name: 'data1', value: 10 }],
},
}
},
methods: {
// 方法
drawLeftTop() {
let myChart = this.$echarts.init(this.$refs.firstMain)
let option = {
xAxis: {
type: 'category',
data: this.realData.cityList,
axisLabel: {
textStyle: {
color: '#C0C0C0',
},
},
},
dataZoom: [
{
type: 'slider',
start: 0,
end: 85,
show: false,
},
],
title: {
text: '各地区销售数据',
textStyle: {
color: '#C0C0C0',
},
},
toolbox: {
show: true,
feature: {
// dataView: { show: true, realData: true },
magicType: { show: true, type: ['line', 'bar'] },
restore: { show: true },
SaveAsImage: { show: true },
},
},
legend: {
orient: 'vertical',
right: 'right',
data: ['销售数据'],
textStyle: {
color: 'grey',
},
top: '8%',
left: '70%',
},
tooltip: {
trigger: 'item',
formatter: '',
},
yAxis: {
type: 'value',
data: this.realData.volumnList,
axisLabel: {
textStyle: {
color: '#C0C0C0',
},
},
},
series: [
{
name: '销售数据',
data: this.realData.volumnList,
type: 'bar',
label: {
show: true,
position: 'top',
textStyle: {
color: '#C0C0C0',
},
},
},
],
}
if (option) {
myChart.setOption(option)
}
},
drawLeftBottom() {
let myChart = this.$echarts.init(this.$refs.secondMain)
let option = {
title: {
text: '各类型产品占比',
subtext: '',
left: 'center',
textStyle: {
color: '#C0C0C0',
},
},
tooltip: {
trigger: 'item',
},
legend: {
orient: 'vertical',
left: 'left',
textStyle: {
color: '#C0C0C0',
},
},
series: [
{
name: '数量',
type: 'pie',
radius: '50%',
data: this.realData.pieList,
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)',
},
},
},
],
label: {
textStyle: {
color: '#C0C0C0',
},
},
}
if (option) {
myChart.setOption(option)
}
},
changeData(x) {
let st = x[0]
for (let i = 0; i < x.length - 1; i++) {
x[i] = x[i + 1]
}
x[x.length - 1] = st
},
updataBarChart() {
if (this.isHovered == true) {
this.changeData(this.realData.cityList)
this.changeData(this.realData.volumnList)
let myChart = this.$echarts.init(this.$refs.firstMain)
myChart.setOption({
xAxis: {
data: this.realData.cityList,
},
series: [
{
data: this.realData.volumnList,
},
],
})
} else {
clearInterval(this.timer)
}
},
startDataupdataInterval() {
const interval = 2000
clearInterval(this.timer)
setInterval(this.updataBarChart, interval)
},
startAction() {
this.isHovered = false
},
cancelAction() {
this.isHovered = true
},
},
async mounted() {
// 生命周期
// 异步请求,使用await
const res = await this.$http.get('myApp/screenData/')
// 接收数据
this.$set(this.realData, 'cityList', res.data.cityList)
this.$set(this.realData, 'volumnList', res.data.volumnList)
this.$set(this.realData, 'pieList', res.data.pieList)
this.drawLeftBottom()
console.log(this.realData.pieList)
},
async updated() {
this.drawLeftTop()
this.startDataupdataInterval()
},
}
</script>

<style scoped>
/*设置整体*/
.layout {
height: 100vh;
width: 100%;
background: url('../assets/imgs/bg03.jpg');
}

.header {
display: flex;
align-items: center;
justify-content: center;
}

.header div {
width: 50%;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}

.header div span {
color: steelblue;
font-weight: bold;
font-size: 23px;
padding: 10px;
}

.content {
width: 100%;
height: 100%;
display: flex;
flex-direction: row;
}

.content .left {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 350px;
margin-left: 30px;
}

.content .left div {
flex-grow: 1;
height: 300px;
width: 380px;
padding: 5px;
}

.content .right {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 350px;
}

.content .right div {
flex-grow: 1;
height: 300px;
width: 350px;
padding: 5px;
}

.content .center {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 600px;
}
</style>

3.地图的配置

1.地图api配置

  1. src/api下创建一个getMap.js文件

    001

  2. 设置api,输入:

    1
    2
    3
    import axios from 'axios'

    export default axios.get('https://geo.datav.aliyun.com/areas_v3/bound/100000_full.json')

2.地图后端

  1. myApp/utils/getScreenData.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    from .getBaseData import *

    def getSquareData():
    goods = list(getAllGoods())
    goodVolumn = {}
    for i in goods:
    if goodVolumn.get(i.address,-1): # 未获取到键,则赋值给销量
    goodVolumn[i.address] = int(i.buy_len)
    else:
    goodVolumn[i.address] += int(i.buy_len)
    goodsSortVolumn = sorted(goodVolumn.items(),key=lambda x:x[1],reverse=True) # 对销量排序
    cityList = []
    volumnList = []
    for i in goodsSortVolumn:
    cityList.append(i[0])
    volumnList.append(i[1])
    return cityList[:7],volumnList[:7]

    def getPieData():
    goods = list(getAllGoods())
    goodsNumber = {}
    for i in goods:
    if goodsNumber.get(i.type,-1) == -1:
    goodsNumber[i.type] = 1
    else:
    goodsNumber[i.type] += 1
    pieList = []
    for k,v in goodsNumber.items():
    pieList.append({
    'name': k,
    'value': v
    })
    return pieList

    def getMapData():
    goods = list(getAllGoods())
    cityVolume = {}
    for i in goods:
    if cityVolume.get(i.address,-1) == -1:
    cityVolume[i.address] = 1
    else:
    cityVolume[i.address] += 1
    mapData = []
    for k,v in cityVolume.items():
    mapData.append({
    'name': k,
    'value': v
    })
    return mapData
  2. myApp/views.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    from django.shortcuts import render
    from django.http import JsonResponse
    from .utils import getScreenData

    # Create your views here.

    def screenData(request):
    if request.method == "GET":
    cityList,volumnList = getScreenData.getSquareData()
    pieList = getScreenData.getPieData()
    mapData = getScreenData.getMapData()
    return JsonResponse({
    'cityList': cityList,
    'volumnList': volumnList,
    'pieList': pieList,
    'mapData': mapData
    })

3.前后端关联

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
<template>
<div class="layout">
<!-- 头部 -->
<div class="header">
<dv-decoration-8 style="width:300px;height:50px;" />
<div>
<span class="title-text">大数据可视化平台</span>
<dv-decoration-5 style="width:300px;height:40px;" />
</div>
<dv-decoration-8 :reverse="true" style="width:300px;height:50px;" />
</div>

<!-- 内容 -->
<div class="content">
<div class="left">
<dv-border-box-13>
<dv-decoration-1 style="width:200px;height:10px;" />
<div ref="firstMain" style="width:350px;height:270px;" v-bind:key="realData.volumnList[0]"
@mouseenter="startAction" @mouseleave="cancelAction"></div>
</dv-border-box-13>
<dv-border-box-8>
<div ref="secondMain" style="width:350px;height:270px;" v-bind:key="realData.pieList[0][1]"></div>
</dv-border-box-8>
</div>
<div class="center">
<div ref="thirdMain" style="width: 450px;height: 450px;"></div>
</div>
<div class="right">
<dv-border-box-12>
<dv-decoration-3 style="width:250px;height:10px;" />
<div></div>
</dv-border-box-12>
<dv-border-box-1>
<div></div>
</dv-border-box-1>
</div>
</div>
</div>
</template>

<script>
import getMap from '@/api/getMap.js'
// import { color } from 'echarts'
// import { scale } from 'echarts/types/src/scale/helper.js'
// 注释
export default {
data() {
// 函数体
return {
isHovered: true,
realData: {
cityList: [1],
volumnList: [1],
pieList: [{ name: 'data1', value: 10 }],
mapData: [{ name: 'data2', value: 10 }],
},
}
},
methods: {
// 方法
drawLeftTop() {
let myChart = this.$echarts.init(this.$refs.firstMain)
let option = {
xAxis: {
type: 'category',
data: this.realData.cityList,
axisLabel: {
textStyle: {
color: '#C0C0C0',
},
},
},
dataZoom: [
{
type: 'slider',
start: 0,
end: 85,
show: false,
},
],
title: {
text: '各地区销售数据',
textStyle: {
color: '#C0C0C0',
},
},
toolbox: {
show: true,
feature: {
// dataView: { show: true, realData: true },
magicType: { show: true, type: ['line', 'bar'] },
restore: { show: true },
SaveAsImage: { show: true },
},
},
legend: {
orient: 'vertical',
right: 'right',
data: ['销售数据'],
textStyle: {
color: 'grey',
},
top: '8%',
left: '70%',
},
tooltip: {
trigger: 'item',
formatter: '',
},
yAxis: {
type: 'value',
data: this.realData.volumnList,
axisLabel: {
textStyle: {
color: '#C0C0C0',
},
},
},
series: [
{
name: '销售数据',
data: this.realData.volumnList,
type: 'bar',
label: {
show: true,
position: 'top',
textStyle: {
color: '#C0C0C0',
},
},
},
],
}
if (option) {
myChart.setOption(option)
}
},
drawLeftBottom() {
let myChart = this.$echarts.init(this.$refs.secondMain)
let option = {
title: {
text: '各类型产品占比',
subtext: '',
left: 'center',
textStyle: {
color: '#C0C0C0',
},
},
tooltip: {
trigger: 'item',
},
legend: {
orient: 'vertical',
left: 'left',
textStyle: {
color: '#C0C0C0',
},
},
series: [
{
name: '数量',
type: 'pie',
radius: '50%',
data: this.realData.pieList,
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)',
},
},
},
],
label: {
textStyle: {
color: '#C0C0C0',
},
},
}
if (option) {
myChart.setOption(option)
}
},
async drawCenterMap() {
let myChart = this.$echarts.init(this.$refs.thirdMain)
const res = await getMap
this.$echarts.registerMap('china', res.data)
let data = this.realData.mapData
let geoCoordMap = {
海门: [121.15, 31.89],
鄂尔多斯: [109.781327, 39.608266],
招远: [120.38, 37.35],
舟山: [122.207216, 29.985295],
齐齐哈尔: [123.97, 47.33],
盐城: [120.13, 33.38],
赤峰: [118.87, 42.28],
青岛: [120.33, 36.07],
乳山: [121.52, 36.89],
金昌: [102.188043, 38.520089],
泉州: [118.58, 24.93],
莱西: [120.53, 36.86],
日照: [119.46, 35.42],
胶南: [119.97, 35.88],
南通: [121.05, 32.08],
拉萨: [91.11, 29.97],
云浮: [112.02, 22.93],
梅州: [116.1, 24.55],
文登: [122.05, 37.2],
上海: [121.48, 31.22],
攀枝花: [101.718637, 26.582347],
威海: [122.1, 37.5],
承德: [117.93, 40.97],
厦门: [118.1, 24.46],
汕尾: [115.375279, 22.786211],
潮州: [116.63, 23.68],
丹东: [124.37, 40.13],
太仓: [121.1, 31.45],
曲靖: [103.79, 25.51],
烟台: [121.39, 37.52],
福州: [119.3, 26.08],
瓦房店: [121.979603, 39.627114],
即墨: [120.45, 36.38],
抚顺: [123.97, 41.97],
玉溪: [102.52, 24.35],
张家口: [114.87, 40.82],
阳泉: [113.57, 37.85],
莱州: [119.942327, 37.177017],
湖州: [120.1, 30.86],
汕头: [116.69, 23.39],
昆山: [120.95, 31.39],
宁波: [121.56, 29.86],
湛江: [110.359377, 21.270708],
揭阳: [116.35, 23.55],
荣成: [122.41, 37.16],
连云港: [119.16, 34.59],
葫芦岛: [120.836932, 40.711052],
常熟: [120.74, 31.64],
东莞: [113.75, 23.04],
河源: [114.68, 23.73],
淮安: [119.15, 33.5],
泰州: [119.9, 32.49],
南宁: [108.33, 22.84],
营口: [122.18, 40.65],
惠州: [114.4, 23.09],
江阴: [120.26, 31.91],
蓬莱: [120.75, 37.8],
韶关: [113.62, 24.84],
嘉峪关: [98.289152, 39.77313],
广州: [113.23, 23.16],
延安: [109.47, 36.6],
太原: [112.53, 37.87],
清远: [113.01, 23.7],
中山: [113.38, 22.52],
昆明: [102.73, 25.04],
寿光: [118.73, 36.86],
盘锦: [122.070714, 41.119997],
长治: [113.08, 36.18],
深圳: [114.07, 22.62],
珠海: [113.52, 22.3],
宿迁: [118.3, 33.96],
咸阳: [108.72, 34.36],
铜川: [109.11, 35.09],
平度: [119.97, 36.77],
佛山: [113.11, 23.05],
海口: [110.35, 20.02],
江门: [113.06, 22.61],
章丘: [117.53, 36.72],
肇庆: [112.44, 23.05],
大连: [121.62, 38.92],
临汾: [111.5, 36.08],
吴江: [120.63, 31.16],
石嘴山: [106.39, 39.04],
沈阳: [123.38, 41.8],
苏州: [120.62, 31.32],
茂名: [110.88, 21.68],
嘉兴: [120.76, 30.77],
长春: [125.35, 43.88],
胶州: [120.03336, 36.264622],
银川: [106.27, 38.47],
张家港: [120.555821, 31.875428],
三门峡: [111.19, 34.76],
锦州: [121.15, 41.13],
南昌: [115.89, 28.68],
柳州: [109.4, 24.33],
三亚: [109.511909, 18.252847],
自贡: [104.778442, 29.33903],
吉林: [126.57, 43.87],
阳江: [111.95, 21.85],
泸州: [105.39, 28.91],
西宁: [101.74, 36.56],
宜宾: [104.56, 29.77],
呼和浩特: [111.65, 40.82],
成都: [104.06, 30.67],
大同: [113.3, 40.12],
镇江: [119.44, 32.2],
桂林: [110.28, 25.29],
张家界: [110.479191, 29.117096],
宜兴: [119.82, 31.36],
北海: [109.12, 21.49],
西安: [108.95, 34.27],
金坛: [119.56, 31.74],
东营: [118.49, 37.46],
牡丹江: [129.58, 44.6],
遵义: [106.9, 27.7],
绍兴: [120.58, 30.01],
扬州: [119.42, 32.39],
常州: [119.95, 31.79],
潍坊: [119.1, 36.62],
重庆: [106.54, 29.59],
台州: [121.420757, 28.656386],
南京: [118.78, 32.04],
滨州: [118.03, 37.36],
贵阳: [106.71, 26.57],
无锡: [120.29, 31.59],
本溪: [123.73, 41.3],
克拉玛依: [84.77, 45.59],
渭南: [109.5, 34.52],
马鞍山: [118.48, 31.56],
宝鸡: [107.15, 34.38],
焦作: [113.21, 35.24],
句容: [119.16, 31.95],
北京: [116.46, 39.92],
徐州: [117.2, 34.26],
衡水: [115.72, 37.72],
包头: [110, 40.58],
绵阳: [104.73, 31.48],
乌鲁木齐: [87.68, 43.77],
枣庄: [117.57, 34.86],
杭州: [120.19, 30.26],
淄博: [118.05, 36.78],
鞍山: [122.85, 41.12],
溧阳: [119.48, 31.43],
库尔勒: [86.06, 41.68],
安阳: [114.35, 36.1],
开封: [114.35, 34.79],
济南: [117, 36.65],
德阳: [104.37, 31.13],
温州: [120.65, 28.01],
九江: [115.97, 29.71],
邯郸: [114.47, 36.6],
临安: [119.72, 30.23],
兰州: [103.73, 36.03],
沧州: [116.83, 38.33],
临沂: [118.35, 35.05],
南充: [106.110698, 30.837793],
天津: [117.2, 39.13],
富阳: [119.95, 30.07],
泰安: [117.13, 36.18],
诸暨: [120.23, 29.71],
郑州: [113.65, 34.76],
哈尔滨: [126.63, 45.75],
聊城: [115.97, 36.45],
芜湖: [118.38, 31.33],
唐山: [118.02, 39.63],
平顶山: [113.29, 33.75],
邢台: [114.48, 37.05],
德州: [116.29, 37.45],
济宁: [116.59, 35.38],
荆州: [112.239741, 30.335165],
宜昌: [111.3, 30.7],
义乌: [120.06, 29.32],
丽水: [119.92, 28.45],
洛阳: [112.44, 34.7],
秦皇岛: [119.57, 39.95],
株洲: [113.16, 27.83],
石家庄: [114.48, 38.03],
莱芜: [117.67, 36.19],
常德: [111.69, 29.05],
保定: [115.48, 38.85],
湘潭: [112.91, 27.87],
金华: [119.64, 29.12],
岳阳: [113.09, 29.37],
长沙: [113, 28.21],
衢州: [118.88, 28.97],
廊坊: [116.7, 39.53],
菏泽: [115.480656, 35.23375],
合肥: [117.27, 31.86],
武汉: [114.31, 30.52],
大庆: [125.03, 46.58],
}
let convertData = function (data) {
let res = []
for (let i = 0; i < data.length; i++) {
let geoCoord = geoCoordMap[data[i].name]
if (geoCoord) {
res.push({
name: data[i].name,
value: geoCoord.concat(data[i].value),
})
}
}
return res
}
let option = {
scale: 0.1,
backgroundColor: 'transparent',
title: {
text: '全国省市产品数据',
subtext: '数据来自淘宝',
// sublink: 'http://www.pm25.in',
left: 'center',
textStyle: {
color: '#fff',
},
},
// 这个属性用于实现光圈效果
geo: {
show: true,
map: 'china',
zoom: 1.25,
label: {
normal: {
show: true,
textStyle: {
// 鼠标移入省份字体颜色变化
color: 'white',
// 鼠标移入省份字体大小变化
fontSize: '10px',
},
},
},
},
roam: true,
itemStyle: {
normal: {
areacolor: 'skyblue',
borderColor: '#fff',
},
emphasis: {
areaColor: '#2B91B7',
},
},
series: [
{
name: '销量',
type: 'effectScatter',
coordinateSystem: 'geo',
data: convertData(data),
symbolSize(val) {
return val[2] / 10
},
showEffectOn: 'render',
rippleEffect: {
brushType: 'stroke',
},
hoverAnimation: true,
label: {
formatter: '{b}',
position: 'right',
show: true,
},
itemStyle: {
color: '#ddb926',
},
emphasis: {
label: {
show: true,
},
},
},
],
}
if (option) {
myChart.setOption(option)
}
},
changeData(x) {
let st = x[0]
for (let i = 0; i < x.length - 1; i++) {
x[i] = x[i + 1]
}
x[x.length - 1] = st
},
updataBarChart() {
if (this.isHovered == true) {
this.changeData(this.realData.cityList)
this.changeData(this.realData.volumnList)
let myChart = this.$echarts.init(this.$refs.firstMain)
myChart.setOption({
xAxis: {
data: this.realData.cityList,
},
series: [
{
data: this.realData.volumnList,
},
],
})
} else {
clearInterval(this.timer)
}
},
startDataupdataInterval() {
const interval = 2000
clearInterval(this.timer)
setInterval(this.updataBarChart, interval)
},
startAction() {
this.isHovered = false
},
cancelAction() {
this.isHovered = true
},
},
async mounted() {
// 生命周期
// 异步请求,使用await
const res = await this.$http.get('myApp/screenData/')
// 接收数据
this.$set(this.realData, 'cityList', res.data.cityList)
this.$set(this.realData, 'volumnList', res.data.volumnList)
this.$set(this.realData, 'pieList', res.data.pieList)
this.$set(this.realData, 'mapData', res.data.mapData)
// console.log(this.realData.mapData)
},
async updated() {
this.drawLeftTop()
this.drawLeftBottom()
this.drawCenterMap()
this.startDataupdataInterval()
},
}
</script>

<style scoped>
/*设置整体*/
.layout {
height: 100vh;
width: 100%;
background: url('../assets/imgs/bg03.jpg');
}

.header {
display: flex;
align-items: center;
justify-content: center;
}

.header div {
width: 50%;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}

.header div span {
color: steelblue;
font-weight: bold;
font-size: 23px;
padding: 10px;
}

.content {
width: 100%;
height: 100%;
display: flex;
flex-direction: row;
}

.content .left {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 350px;
margin-left: 30px;
}

.content .left div {
flex-grow: 1;
height: 300px;
width: 380px;
padding: 5px;
}

.content .right {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 350px;
}

.content .right div {
flex-grow: 1;
height: 300px;
width: 350px;
padding: 5px;
}

.content .center {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 600px;
}
</style>

4.折线图

1.折线图后端

  1. myApp/utils/getScreenData.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    from .getBaseData import *

    def getSquareData():
    goods = list(getAllGoods())
    goodVolumn = {}
    for i in goods:
    if goodVolumn.get(i.address,-1): # 未获取到键,则赋值给销量
    goodVolumn[i.address] = int(i.buy_len)
    else:
    goodVolumn[i.address] += int(i.buy_len)
    goodsSortVolumn = sorted(goodVolumn.items(),key=lambda x:x[1],reverse=True) # 对销量排序
    cityList = []
    volumnList = []
    for i in goodsSortVolumn:
    cityList.append(i[0])
    volumnList.append(i[1])
    return cityList[:7],volumnList[:7]

    def getPieData():
    goods = list(getAllGoods())
    goodsNumber = {}
    for i in goods:
    if goodsNumber.get(i.type,-1) == -1:
    goodsNumber[i.type] = 1
    else:
    goodsNumber[i.type] += 1
    pieList = []
    for k,v in goodsNumber.items():
    pieList.append({
    'name': k,
    'value': v
    })
    return pieList

    def getMapData():
    goods = list(getAllGoods())
    cityVolume = {}
    for i in goods:
    if cityVolume.get(i.address,-1) == -1:
    cityVolume[i.address] = 1
    else:
    cityVolume[i.address] += 1
    mapData = []
    for k,v in cityVolume.items():
    mapData.append({
    'name': k,
    'value': v
    })
    return mapData

    def getLineData():
    goods = list(getAllGoods())
    priceDict = {'0-100': 0, '100-200': 0, '200-500': 0, '500-1000': 0, '千元以上': 0}
    for i in goods:
    p = float(i.price)
    if p < 100:
    priceDict['0-100'] += 1
    elif p >= 100 and p < 200:
    priceDict['100-200'] += 1
    elif p >= 200 and p < 500:
    priceDict['200-500'] += 1
    elif p >= 500 and p < 1000:
    priceDict['500-1000'] += 1
    else:
    priceDict['千元以上'] += 1
    LineRowData = list(priceDict.keys())
    LineColData = list(priceDict.values())
    return LineRowData,LineColData
  2. myApp/views.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    from django.shortcuts import render
    from django.http import JsonResponse
    from .utils import getScreenData

    # Create your views here.

    def screenData(request):
    if request.method == "GET":
    cityList,volumnList = getScreenData.getSquareData()
    pieList = getScreenData.getPieData()
    mapData = getScreenData.getMapData()
    LineRowData,LineColData = getScreenData.getLineData()
    return JsonResponse({
    'cityList': cityList,
    'volumnList': volumnList,
    'pieList': pieList,
    'mapData': mapData,
    'LineRowData': LineRowData,
    'LineColData': LineColData
    })

2.前后端关联

Home.vue的全部代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
<template>
<div class="layout">
<!-- 头部 -->
<div class="header">
<dv-decoration-8 style="width:300px;height:50px;" />
<div>
<span class="title-text">大数据可视化平台</span>
<dv-decoration-5 style="width:300px;height:40px;" />
</div>
<dv-decoration-8 :reverse="true" style="width:300px;height:50px;" />
</div>

<!-- 内容 -->
<div class="content">
<div class="left">
<dv-border-box-13>
<dv-decoration-1 style="width:200px;height:10px;" />
<div ref="firstMain" style="width:350px;height:270px;" v-bind:key="realData.volumnList[0]"
@mouseenter="startAction" @mouseleave="cancelAction"></div>
</dv-border-box-13>
<dv-border-box-8>
<div ref="secondMain" style="width:350px;height:270px;" v-bind:key="realData.pieList[0][1]"></div>
</dv-border-box-8>
</div>
<div class="center">
<div ref="thirdMain" style="width: 450px;height: 450px;" v-bind:key="realData.mapData[0][1]"></div>
</div>
<div class="right">
<dv-border-box-12>
<dv-decoration-3 style="width:250px;height:10px;" />
<div ref="fourthMain" style="width: 350px;height: 270px;" v-bind:key="realData.LineColData[1]"></div>
</dv-border-box-12>
<dv-border-box-1>
<div></div>
</dv-border-box-1>
</div>
</div>
</div>
</template>

<script>
import getMap from '@/api/getMap.js'

// 注释
export default {
data() {
// 函数体
return {
isHovered: true,
realData: {
cityList: [1],
volumnList: [1],
pieList: [{ name: 'data1', value: 10 }],
mapData: [{ name: 'data2', value: 10 }],
LineRowData: [],
LineColData: [1],
},
}
},
methods: {
// 方法
drawLeftTop() {
let myChart = this.$echarts.init(this.$refs.firstMain)
let option = {
xAxis: {
type: 'category',
data: this.realData.cityList,
axisLabel: {
textStyle: {
color: '#C0C0C0',
},
},
},
dataZoom: [
{
type: 'slider',
start: 0,
end: 85,
show: false,
},
],
title: {
text: '各地区销售数据',
textStyle: {
color: '#C0C0C0',
},
},
toolbox: {
show: true,
feature: {
// dataView: { show: true, realData: true },
magicType: { show: true, type: ['line', 'bar'] },
restore: { show: true },
SaveAsImage: { show: true },
},
},
legend: {
orient: 'vertical',
right: 'right',
data: ['销售数据'],
textStyle: {
color: 'grey',
},
top: '8%',
left: '70%',
},
tooltip: {
trigger: 'item',
formatter: '',
},
yAxis: {
type: 'value',
data: this.realData.volumnList,
axisLabel: {
textStyle: {
color: '#C0C0C0',
},
},
},
series: [
{
name: '销售数据',
data: this.realData.volumnList,
type: 'bar',
label: {
show: true,
position: 'top',
textStyle: {
color: '#C0C0C0',
},
},
},
],
}
if (option) {
myChart.setOption(option)
}
},
drawLeftBottom() {
let myChart = this.$echarts.init(this.$refs.secondMain)
let option = {
title: {
text: '各类型产品占比',
subtext: '',
left: 'center',
textStyle: {
color: '#C0C0C0',
},
},
tooltip: {
trigger: 'item',
},
legend: {
orient: 'vertical',
left: 'left',
textStyle: {
color: '#C0C0C0',
},
},
series: [
{
name: '数量',
type: 'pie',
radius: '50%',
data: this.realData.pieList,
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)',
},
},
},
],
label: {
textStyle: {
color: '#C0C0C0',
},
},
}
if (option) {
myChart.setOption(option)
}
},
async drawCenterMap() {
let myChart = this.$echarts.init(this.$refs.thirdMain)
const res = await getMap
this.$echarts.registerMap('china', res.data)
let data = this.realData.mapData
let geoCoordMap = {
海门: [121.15, 31.89],
鄂尔多斯: [109.781327, 39.608266],
招远: [120.38, 37.35],
舟山: [122.207216, 29.985295],
齐齐哈尔: [123.97, 47.33],
盐城: [120.13, 33.38],
赤峰: [118.87, 42.28],
青岛: [120.33, 36.07],
乳山: [121.52, 36.89],
金昌: [102.188043, 38.520089],
泉州: [118.58, 24.93],
莱西: [120.53, 36.86],
日照: [119.46, 35.42],
胶南: [119.97, 35.88],
南通: [121.05, 32.08],
拉萨: [91.11, 29.97],
云浮: [112.02, 22.93],
梅州: [116.1, 24.55],
文登: [122.05, 37.2],
上海: [121.48, 31.22],
攀枝花: [101.718637, 26.582347],
威海: [122.1, 37.5],
承德: [117.93, 40.97],
厦门: [118.1, 24.46],
汕尾: [115.375279, 22.786211],
潮州: [116.63, 23.68],
丹东: [124.37, 40.13],
太仓: [121.1, 31.45],
曲靖: [103.79, 25.51],
烟台: [121.39, 37.52],
福州: [119.3, 26.08],
瓦房店: [121.979603, 39.627114],
即墨: [120.45, 36.38],
抚顺: [123.97, 41.97],
玉溪: [102.52, 24.35],
张家口: [114.87, 40.82],
阳泉: [113.57, 37.85],
莱州: [119.942327, 37.177017],
湖州: [120.1, 30.86],
汕头: [116.69, 23.39],
昆山: [120.95, 31.39],
宁波: [121.56, 29.86],
湛江: [110.359377, 21.270708],
揭阳: [116.35, 23.55],
荣成: [122.41, 37.16],
连云港: [119.16, 34.59],
葫芦岛: [120.836932, 40.711052],
常熟: [120.74, 31.64],
东莞: [113.75, 23.04],
河源: [114.68, 23.73],
淮安: [119.15, 33.5],
泰州: [119.9, 32.49],
南宁: [108.33, 22.84],
营口: [122.18, 40.65],
惠州: [114.4, 23.09],
江阴: [120.26, 31.91],
蓬莱: [120.75, 37.8],
韶关: [113.62, 24.84],
嘉峪关: [98.289152, 39.77313],
广州: [113.23, 23.16],
延安: [109.47, 36.6],
太原: [112.53, 37.87],
清远: [113.01, 23.7],
中山: [113.38, 22.52],
昆明: [102.73, 25.04],
寿光: [118.73, 36.86],
盘锦: [122.070714, 41.119997],
长治: [113.08, 36.18],
深圳: [114.07, 22.62],
珠海: [113.52, 22.3],
宿迁: [118.3, 33.96],
咸阳: [108.72, 34.36],
铜川: [109.11, 35.09],
平度: [119.97, 36.77],
佛山: [113.11, 23.05],
海口: [110.35, 20.02],
江门: [113.06, 22.61],
章丘: [117.53, 36.72],
肇庆: [112.44, 23.05],
大连: [121.62, 38.92],
临汾: [111.5, 36.08],
吴江: [120.63, 31.16],
石嘴山: [106.39, 39.04],
沈阳: [123.38, 41.8],
苏州: [120.62, 31.32],
茂名: [110.88, 21.68],
嘉兴: [120.76, 30.77],
长春: [125.35, 43.88],
胶州: [120.03336, 36.264622],
银川: [106.27, 38.47],
张家港: [120.555821, 31.875428],
三门峡: [111.19, 34.76],
锦州: [121.15, 41.13],
南昌: [115.89, 28.68],
柳州: [109.4, 24.33],
三亚: [109.511909, 18.252847],
自贡: [104.778442, 29.33903],
吉林: [126.57, 43.87],
阳江: [111.95, 21.85],
泸州: [105.39, 28.91],
西宁: [101.74, 36.56],
宜宾: [104.56, 29.77],
呼和浩特: [111.65, 40.82],
成都: [104.06, 30.67],
大同: [113.3, 40.12],
镇江: [119.44, 32.2],
桂林: [110.28, 25.29],
张家界: [110.479191, 29.117096],
宜兴: [119.82, 31.36],
北海: [109.12, 21.49],
西安: [108.95, 34.27],
金坛: [119.56, 31.74],
东营: [118.49, 37.46],
牡丹江: [129.58, 44.6],
遵义: [106.9, 27.7],
绍兴: [120.58, 30.01],
扬州: [119.42, 32.39],
常州: [119.95, 31.79],
潍坊: [119.1, 36.62],
重庆: [106.54, 29.59],
台州: [121.420757, 28.656386],
南京: [118.78, 32.04],
滨州: [118.03, 37.36],
贵阳: [106.71, 26.57],
无锡: [120.29, 31.59],
本溪: [123.73, 41.3],
克拉玛依: [84.77, 45.59],
渭南: [109.5, 34.52],
马鞍山: [118.48, 31.56],
宝鸡: [107.15, 34.38],
焦作: [113.21, 35.24],
句容: [119.16, 31.95],
北京: [116.46, 39.92],
徐州: [117.2, 34.26],
衡水: [115.72, 37.72],
包头: [110, 40.58],
绵阳: [104.73, 31.48],
乌鲁木齐: [87.68, 43.77],
枣庄: [117.57, 34.86],
杭州: [120.19, 30.26],
淄博: [118.05, 36.78],
鞍山: [122.85, 41.12],
溧阳: [119.48, 31.43],
库尔勒: [86.06, 41.68],
安阳: [114.35, 36.1],
开封: [114.35, 34.79],
济南: [117, 36.65],
德阳: [104.37, 31.13],
温州: [120.65, 28.01],
九江: [115.97, 29.71],
邯郸: [114.47, 36.6],
临安: [119.72, 30.23],
兰州: [103.73, 36.03],
沧州: [116.83, 38.33],
临沂: [118.35, 35.05],
南充: [106.110698, 30.837793],
天津: [117.2, 39.13],
富阳: [119.95, 30.07],
泰安: [117.13, 36.18],
诸暨: [120.23, 29.71],
郑州: [113.65, 34.76],
哈尔滨: [126.63, 45.75],
聊城: [115.97, 36.45],
芜湖: [118.38, 31.33],
唐山: [118.02, 39.63],
平顶山: [113.29, 33.75],
邢台: [114.48, 37.05],
德州: [116.29, 37.45],
济宁: [116.59, 35.38],
荆州: [112.239741, 30.335165],
宜昌: [111.3, 30.7],
义乌: [120.06, 29.32],
丽水: [119.92, 28.45],
洛阳: [112.44, 34.7],
秦皇岛: [119.57, 39.95],
株洲: [113.16, 27.83],
石家庄: [114.48, 38.03],
莱芜: [117.67, 36.19],
常德: [111.69, 29.05],
保定: [115.48, 38.85],
湘潭: [112.91, 27.87],
金华: [119.64, 29.12],
岳阳: [113.09, 29.37],
长沙: [113, 28.21],
衢州: [118.88, 28.97],
廊坊: [116.7, 39.53],
菏泽: [115.480656, 35.23375],
合肥: [117.27, 31.86],
武汉: [114.31, 30.52],
大庆: [125.03, 46.58],
}
let convertData = function (data) {
let res = []
for (let i = 0; i < data.length; i++) {
let geoCoord = geoCoordMap[data[i].name]
if (geoCoord) {
res.push({
name: data[i].name,
value: geoCoord.concat(data[i].value),
})
}
}
return res
}
let option = {
scale: 0.1,
backgroundColor: 'transparent',
title: {
text: '全国省市产品数据',
subtext: '数据来自淘宝',
// sublink: 'http://www.pm25.in',
left: 'center',
textStyle: {
color: '#fff',
},
},
// 这个属性用于实现光圈效果
geo: {
show: true,
map: 'china',
zoom: 1.25,
label: {
normal: {
show: true,
textStyle: {
// 鼠标移入省份字体颜色变化
color: 'white',
// 鼠标移入省份字体大小变化
fontSize: '10px',
},
},
},
},
roam: true,
itemStyle: {
normal: {
areacolor: 'skyblue',
borderColor: '#fff',
},
emphasis: {
areaColor: '#2B91B7',
},
},
series: [
{
name: '销量',
type: 'effectScatter',
coordinateSystem: 'geo',
data: convertData(data),
symbolSize(val) {
return val[2] / 10
},
showEffectOn: 'render',
rippleEffect: {
brushType: 'stroke',
},
hoverAnimation: true,
label: {
formatter: '{b}',
position: 'right',
show: true,
},
itemStyle: {
color: '#ddb926',
},
emphasis: {
label: {
show: true,
},
},
},
],
}
if (option) {
myChart.setOption(option)
}
},
drawRightTop() {
let myChart = this.$echarts.init(this.$refs.fourthMain)
let option = {
xAxis: {
type: 'category',
data: this.realData.LineRowData,
axisLabel: {
interval: 0, // 强制显示所有标签
textStyle: {
color: '#C0C0C0',
},
},
name: '元',
},
yAxis: {
type: 'value',
name: '个',
axisLabel: {
interval: 0, // 强制显示所有标签
textStyle: {
color: '#C0C0C0',
},
},
},
title: {
text: '商品价格占比',
left: 'center',
textStyle: {
color: '#C0C0C0',
},
},
tooltip: {
trigger: 'item',
},
legend: {
data: ['占比情况'],
textStyle: {
color: '#C0C0C0',
},
top: '8%',
right: '10%',
},
series: [
{
name: '占比情况',
data: this.realData.LineColData,
type: 'line',
},
],
}
if (option) {
myChart.setOption(option)
}
},
changeData(x) {
let st = x[0]
for (let i = 0; i < x.length - 1; i++) {
x[i] = x[i + 1]
}
x[x.length - 1] = st
},
updataBarChart() {
if (this.isHovered == true) {
this.changeData(this.realData.cityList)
this.changeData(this.realData.volumnList)
let myChart = this.$echarts.init(this.$refs.firstMain)
myChart.setOption({
xAxis: {
data: this.realData.cityList,
},
series: [
{
data: this.realData.volumnList,
},
],
})
} else {
clearInterval(this.timer)
}
},
startDataupdataInterval() {
const interval = 2000
clearInterval(this.timer)
setInterval(this.updataBarChart, interval)
},
startAction() {
this.isHovered = false
},
cancelAction() {
this.isHovered = true
},
},
async mounted() {
// 生命周期
// 异步请求,使用await
const res = await this.$http.get('myApp/screenData/')
// 接收数据
this.$set(this.realData, 'cityList', res.data.cityList)
this.$set(this.realData, 'volumnList', res.data.volumnList)
this.$set(this.realData, 'pieList', res.data.pieList)
this.$set(this.realData, 'mapData', res.data.mapData)
this.$set(this.realData, 'LineRowData', res.data.LineRowData)
this.$set(this.realData, 'LineColData', res.data.LineColData)
// console.log(this.realData.LineRowData, this.realData.LineColData)
},
async updated() {
this.drawLeftTop()
this.drawLeftBottom()
this.drawCenterMap()
this.startDataupdataInterval()
this.drawRightTop()
},
}
</script>

<style scoped>
/*设置整体*/
.layout {
height: 100vh;
width: 100%;
background: url('../assets/imgs/bg03.jpg');
}

.header {
display: flex;
align-items: center;
justify-content: center;
}

.header div {
width: 50%;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}

.header div span {
color: steelblue;
font-weight: bold;
font-size: 23px;
padding: 10px;
}

.content {
width: 100%;
height: 100%;
display: flex;
flex-direction: row;
}

.content .left {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 350px;
margin-left: 30px;
}

.content .left div {
flex-grow: 1;
height: 300px;
width: 380px;
padding: 5px;
}

.content .right {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 350px;
}

.content .right div {
flex-grow: 1;
height: 300px;
width: 350px;
padding: 5px;
}

.content .center {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 600px;
}
</style>

5.动态环图

1.动态环图后端

  1. myApp/utils/getScreenData.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    from .getBaseData import *

    def getSquareData():
    goods = list(getAllGoods())
    goodVolumn = {}
    for i in goods:
    if goodVolumn.get(i.address,-1): # 未获取到键,则赋值给销量
    goodVolumn[i.address] = int(i.buy_len)
    else:
    goodVolumn[i.address] += int(i.buy_len)
    goodsSortVolumn = sorted(goodVolumn.items(),key=lambda x:x[1],reverse=True) # 对销量排序
    cityList = []
    volumnList = []
    for i in goodsSortVolumn:
    cityList.append(i[0])
    volumnList.append(i[1])
    return cityList[:7],volumnList[:7]

    def getPieData():
    goods = list(getAllGoods())
    goodsNumber = {}
    for i in goods:
    if goodsNumber.get(i.type,-1) == -1:
    goodsNumber[i.type] = 1
    else:
    goodsNumber[i.type] += 1
    pieList = []
    for k,v in goodsNumber.items():
    pieList.append({
    'name': k,
    'value': v
    })
    return pieList

    def getMapData():
    goods = list(getAllGoods())
    cityVolume = {}
    for i in goods:
    if cityVolume.get(i.address,-1) == -1:
    cityVolume[i.address] = 1
    else:
    cityVolume[i.address] += 1
    mapData = []
    for k,v in cityVolume.items():
    mapData.append({
    'name': k,
    'value': v
    })
    return mapData

    def getLineData():
    goods = list(getAllGoods())
    priceDict = {'0-100': 0, '100-200': 0, '200-500': 0, '500-1000': 0, '千元以上': 0}
    for i in goods:
    p = float(i.price)
    if p < 100:
    priceDict['0-100'] += 1
    elif p >= 100 and p < 200:
    priceDict['100-200'] += 1
    elif p >= 200 and p < 500:
    priceDict['200-500'] += 1
    elif p >= 500 and p < 1000:
    priceDict['500-1000'] += 1
    else:
    priceDict['千元以上'] += 1
    LineRowData = list(priceDict.keys())
    LineColData = list(priceDict.values())
    return LineRowData,LineColData

    def getCircleData():
    goods = list(getAllGoods())
    sumDict = {}
    sumPice = 0
    for i in goods:
    p = float(i.price)
    sumPice += p
    if sumDict.get(i.type,-1) == -1:
    sumDict[i.type] = p
    else:
    sumDict[i.type] += p
    for key in sumDict:
    sumDict[key] = round(sumDict[key] / sumPice * 100, 0)
    circlieList = []
    for k,v in sumDict.items():
    circlieList.append({
    'name': k,
    "value": v
    })
    return circlieList

  2. myApp/views.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    from .getBaseData import *

    def getSquareData():
    goods = list(getAllGoods())
    goodVolumn = {}
    for i in goods:
    if goodVolumn.get(i.address,-1): # 未获取到键,则赋值给销量
    goodVolumn[i.address] = int(i.buy_len)
    else:
    goodVolumn[i.address] += int(i.buy_len)
    goodsSortVolumn = sorted(goodVolumn.items(),key=lambda x:x[1],reverse=True) # 对销量排序
    cityList = []
    volumnList = []
    for i in goodsSortVolumn:
    cityList.append(i[0])
    volumnList.append(i[1])
    return cityList[:7],volumnList[:7]

    def getPieData():
    goods = list(getAllGoods())
    goodsNumber = {}
    for i in goods:
    if goodsNumber.get(i.type,-1) == -1:
    goodsNumber[i.type] = 1
    else:
    goodsNumber[i.type] += 1
    pieList = []
    for k,v in goodsNumber.items():
    pieList.append({
    'name': k,
    'value': v
    })
    return pieList

    def getMapData():
    goods = list(getAllGoods())
    cityVolume = {}
    for i in goods:
    if cityVolume.get(i.address,-1) == -1:
    cityVolume[i.address] = 1
    else:
    cityVolume[i.address] += 1
    mapData = []
    for k,v in cityVolume.items():
    mapData.append({
    'name': k,
    'value': v
    })
    return mapData

    def getLineData():
    goods = list(getAllGoods())
    priceDict = {'0-100': 0, '100-200': 0, '200-500': 0, '500-1000': 0, '千元以上': 0}
    for i in goods:
    p = float(i.price)
    if p < 100:
    priceDict['0-100'] += 1
    elif p >= 100 and p < 200:
    priceDict['100-200'] += 1
    elif p >= 200 and p < 500:
    priceDict['200-500'] += 1
    elif p >= 500 and p < 1000:
    priceDict['500-1000'] += 1
    else:
    priceDict['千元以上'] += 1
    LineRowData = list(priceDict.keys())
    LineColData = list(priceDict.values())
    return LineRowData,LineColData

    def getCircleData():
    goods = list(getAllGoods())
    sumDict = {}
    sumPice = 0
    for i in goods:
    p = float(i.price)
    sumPice += p
    if sumDict.get(i.type,-1) == -1:
    sumDict[i.type] = p
    else:
    sumDict[i.type] += p
    for key in sumDict:
    sumDict[key] = round(sumDict[key] / sumPice * 100, 0)
    circlieList = []
    for k,v in sumDict.items():
    circlieList.append({
    'name': k,
    "value": v
    })
    return circlieList

2.前后端关联

Home.vue的全部代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<template>
<div class="layout">
<!-- 头部 -->
<div class="header">
<dv-decoration-8 style="width:300px;height:50px;" />
<div>
<span class="title-text">大数据可视化平台</span>
<dv-decoration-5 style="width:300px;height:40px;" />
</div>
<dv-decoration-8 :reverse="true" style="width:300px;height:50px;" />
</div>

<!-- 内容 -->
<div class="content">
<div class="left">
<dv-border-box-13>
<dv-decoration-1 style="width:200px;height:10px;" />
<div ref="firstMain" style="width:350px;height:270px;" v-bind:key="realData.volumnList[0]"
@mouseenter="startAction" @mouseleave="cancelAction"></div>
</dv-border-box-13>
<dv-border-box-8>
<div ref="secondMain" style="width:350px;height:270px;" v-bind:key="realData.pieList[0][1]"></div>
</dv-border-box-8>
</div>
<div class="center">
<div ref="thirdMain" style="width: 450px;height: 450px;" v-bind:key="realData.mapData[0][1]"></div>
</div>
<div class="right">
<dv-border-box-12>
<dv-decoration-3 style="width:250px;height:10px;" />
<div ref="fourthMain" style="width: 350px;height: 270px;" v-bind:key="realData.LineColData[1]">
</div>
</dv-border-box-12>
<dv-border-box-1>
<h1 style="font-size: 18px;text-align: center;color: #C0C0C0;margin-top: 12px;">各类型销售额占比</h1>
<dv-active-ring-chart :config="config" style="width:300px;height:210px;margin: 12px auto"
v-bind:key="config.data[0][0]"/>
</dv-border-box-1>
</div>
</div>
</div>
</template>

<script>
import getMap from '@/api/getMap.js'
// import { config } from 'vue/types/umd';

// 注释
export default {
data() {
// 函数体
return {
isHovered: true,
realData: {
cityList: [1],
volumnList: [1],
pieList: [{ name: 'data1', value: 10 }],
mapData: [{ name: 'data2', value: 10 }],
LineRowData: [],
LineColData: [1],
},
config: {
data: [0][0],
},
}
},
methods: {
// 方法
drawLeftTop() {
let myChart = this.$echarts.init(this.$refs.firstMain)
let option = {
xAxis: {
type: 'category',
data: this.realData.cityList,
axisLabel: {
textStyle: {
color: '#C0C0C0',
},
},
},
dataZoom: [
{
type: 'slider',
start: 0,
end: 85,
show: false,
},
],
title: {
text: '各地区销售数据',
textStyle: {
color: '#C0C0C0',
},
},
toolbox: {
show: true,
feature: {
// dataView: { show: true, realData: true },
magicType: { show: true, type: ['line', 'bar'] },
restore: { show: true },
SaveAsImage: { show: true },
},
},
legend: {
orient: 'vertical',
right: 'right',
data: ['销售数据'],
textStyle: {
color: 'grey',
},
top: '8%',
left: '70%',
},
tooltip: {
trigger: 'item',
formatter: '',
},
yAxis: {
type: 'value',
data: this.realData.volumnList,
axisLabel: {
textStyle: {
color: '#C0C0C0',
},
},
},
series: [
{
name: '销售数据',
data: this.realData.volumnList,
type: 'bar',
label: {
show: true,
position: 'top',
textStyle: {
color: '#C0C0C0',
},
},
},
],
}
if (option) {
myChart.setOption(option)
}
},
drawLeftBottom() {
let myChart = this.$echarts.init(this.$refs.secondMain)
let option = {
title: {
text: '各类型产品占比',
subtext: '',
left: 'center',
textStyle: {
color: '#C0C0C0',
},
},
tooltip: {
trigger: 'item',
},
legend: {
orient: 'vertical',
left: 'left',
textStyle: {
color: '#C0C0C0',
},
},
series: [
{
name: '数量',
type: 'pie',
radius: '50%',
data: this.realData.pieList,
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)',
},
},
},
],
label: {
textStyle: {
color: '#C0C0C0',
},
},
}
if (option) {
myChart.setOption(option)
}
},
async drawCenterMap() {
let myChart = this.$echarts.init(this.$refs.thirdMain)
const res = await getMap
this.$echarts.registerMap('china', res.data)
let data = this.realData.mapData
let geoCoordMap = {
海门: [121.15, 31.89],
鄂尔多斯: [109.781327, 39.608266],
招远: [120.38, 37.35],
舟山: [122.207216, 29.985295],
齐齐哈尔: [123.97, 47.33],
盐城: [120.13, 33.38],
赤峰: [118.87, 42.28],
青岛: [120.33, 36.07],
乳山: [121.52, 36.89],
金昌: [102.188043, 38.520089],
泉州: [118.58, 24.93],
莱西: [120.53, 36.86],
日照: [119.46, 35.42],
胶南: [119.97, 35.88],
南通: [121.05, 32.08],
拉萨: [91.11, 29.97],
云浮: [112.02, 22.93],
梅州: [116.1, 24.55],
文登: [122.05, 37.2],
上海: [121.48, 31.22],
攀枝花: [101.718637, 26.582347],
威海: [122.1, 37.5],
承德: [117.93, 40.97],
厦门: [118.1, 24.46],
汕尾: [115.375279, 22.786211],
潮州: [116.63, 23.68],
丹东: [124.37, 40.13],
太仓: [121.1, 31.45],
曲靖: [103.79, 25.51],
烟台: [121.39, 37.52],
福州: [119.3, 26.08],
瓦房店: [121.979603, 39.627114],
即墨: [120.45, 36.38],
抚顺: [123.97, 41.97],
玉溪: [102.52, 24.35],
张家口: [114.87, 40.82],
阳泉: [113.57, 37.85],
莱州: [119.942327, 37.177017],
湖州: [120.1, 30.86],
汕头: [116.69, 23.39],
昆山: [120.95, 31.39],
宁波: [121.56, 29.86],
湛江: [110.359377, 21.270708],
揭阳: [116.35, 23.55],
荣成: [122.41, 37.16],
连云港: [119.16, 34.59],
葫芦岛: [120.836932, 40.711052],
常熟: [120.74, 31.64],
东莞: [113.75, 23.04],
河源: [114.68, 23.73],
淮安: [119.15, 33.5],
泰州: [119.9, 32.49],
南宁: [108.33, 22.84],
营口: [122.18, 40.65],
惠州: [114.4, 23.09],
江阴: [120.26, 31.91],
蓬莱: [120.75, 37.8],
韶关: [113.62, 24.84],
嘉峪关: [98.289152, 39.77313],
广州: [113.23, 23.16],
延安: [109.47, 36.6],
太原: [112.53, 37.87],
清远: [113.01, 23.7],
中山: [113.38, 22.52],
昆明: [102.73, 25.04],
寿光: [118.73, 36.86],
盘锦: [122.070714, 41.119997],
长治: [113.08, 36.18],
深圳: [114.07, 22.62],
珠海: [113.52, 22.3],
宿迁: [118.3, 33.96],
咸阳: [108.72, 34.36],
铜川: [109.11, 35.09],
平度: [119.97, 36.77],
佛山: [113.11, 23.05],
海口: [110.35, 20.02],
江门: [113.06, 22.61],
章丘: [117.53, 36.72],
肇庆: [112.44, 23.05],
大连: [121.62, 38.92],
临汾: [111.5, 36.08],
吴江: [120.63, 31.16],
石嘴山: [106.39, 39.04],
沈阳: [123.38, 41.8],
苏州: [120.62, 31.32],
茂名: [110.88, 21.68],
嘉兴: [120.76, 30.77],
长春: [125.35, 43.88],
胶州: [120.03336, 36.264622],
银川: [106.27, 38.47],
张家港: [120.555821, 31.875428],
三门峡: [111.19, 34.76],
锦州: [121.15, 41.13],
南昌: [115.89, 28.68],
柳州: [109.4, 24.33],
三亚: [109.511909, 18.252847],
自贡: [104.778442, 29.33903],
吉林: [126.57, 43.87],
阳江: [111.95, 21.85],
泸州: [105.39, 28.91],
西宁: [101.74, 36.56],
宜宾: [104.56, 29.77],
呼和浩特: [111.65, 40.82],
成都: [104.06, 30.67],
大同: [113.3, 40.12],
镇江: [119.44, 32.2],
桂林: [110.28, 25.29],
张家界: [110.479191, 29.117096],
宜兴: [119.82, 31.36],
北海: [109.12, 21.49],
西安: [108.95, 34.27],
金坛: [119.56, 31.74],
东营: [118.49, 37.46],
牡丹江: [129.58, 44.6],
遵义: [106.9, 27.7],
绍兴: [120.58, 30.01],
扬州: [119.42, 32.39],
常州: [119.95, 31.79],
潍坊: [119.1, 36.62],
重庆: [106.54, 29.59],
台州: [121.420757, 28.656386],
南京: [118.78, 32.04],
滨州: [118.03, 37.36],
贵阳: [106.71, 26.57],
无锡: [120.29, 31.59],
本溪: [123.73, 41.3],
克拉玛依: [84.77, 45.59],
渭南: [109.5, 34.52],
马鞍山: [118.48, 31.56],
宝鸡: [107.15, 34.38],
焦作: [113.21, 35.24],
句容: [119.16, 31.95],
北京: [116.46, 39.92],
徐州: [117.2, 34.26],
衡水: [115.72, 37.72],
包头: [110, 40.58],
绵阳: [104.73, 31.48],
乌鲁木齐: [87.68, 43.77],
枣庄: [117.57, 34.86],
杭州: [120.19, 30.26],
淄博: [118.05, 36.78],
鞍山: [122.85, 41.12],
溧阳: [119.48, 31.43],
库尔勒: [86.06, 41.68],
安阳: [114.35, 36.1],
开封: [114.35, 34.79],
济南: [117, 36.65],
德阳: [104.37, 31.13],
温州: [120.65, 28.01],
九江: [115.97, 29.71],
邯郸: [114.47, 36.6],
临安: [119.72, 30.23],
兰州: [103.73, 36.03],
沧州: [116.83, 38.33],
临沂: [118.35, 35.05],
南充: [106.110698, 30.837793],
天津: [117.2, 39.13],
富阳: [119.95, 30.07],
泰安: [117.13, 36.18],
诸暨: [120.23, 29.71],
郑州: [113.65, 34.76],
哈尔滨: [126.63, 45.75],
聊城: [115.97, 36.45],
芜湖: [118.38, 31.33],
唐山: [118.02, 39.63],
平顶山: [113.29, 33.75],
邢台: [114.48, 37.05],
德州: [116.29, 37.45],
济宁: [116.59, 35.38],
荆州: [112.239741, 30.335165],
宜昌: [111.3, 30.7],
义乌: [120.06, 29.32],
丽水: [119.92, 28.45],
洛阳: [112.44, 34.7],
秦皇岛: [119.57, 39.95],
株洲: [113.16, 27.83],
石家庄: [114.48, 38.03],
莱芜: [117.67, 36.19],
常德: [111.69, 29.05],
保定: [115.48, 38.85],
湘潭: [112.91, 27.87],
金华: [119.64, 29.12],
岳阳: [113.09, 29.37],
长沙: [113, 28.21],
衢州: [118.88, 28.97],
廊坊: [116.7, 39.53],
菏泽: [115.480656, 35.23375],
合肥: [117.27, 31.86],
武汉: [114.31, 30.52],
大庆: [125.03, 46.58],
}
let convertData = function (data) {
let res = []
for (let i = 0; i < data.length; i++) {
let geoCoord = geoCoordMap[data[i].name]
if (geoCoord) {
res.push({
name: data[i].name,
value: geoCoord.concat(data[i].value),
})
}
}
return res
}
let option = {
scale: 0.1,
backgroundColor: 'transparent',
title: {
text: '全国省市产品数据',
subtext: '数据来自淘宝',
// sublink: 'http://www.pm25.in',
left: 'center',
textStyle: {
color: '#fff',
},
},
// 这个属性用于实现光圈效果
geo: {
show: true,
map: 'china',
zoom: 1.25,
label: {
normal: {
show: true,
textStyle: {
// 鼠标移入省份字体颜色变化
color: 'white',
// 鼠标移入省份字体大小变化
fontSize: '10px',
},
},
},
},
roam: true,
itemStyle: {
normal: {
areacolor: 'skyblue',
borderColor: '#fff',
},
emphasis: {
areaColor: '#2B91B7',
},
},
series: [
{
name: '销量',
type: 'effectScatter',
coordinateSystem: 'geo',
data: convertData(data),
symbolSize(val) {
return val[2] / 10
},
showEffectOn: 'render',
rippleEffect: {
brushType: 'stroke',
},
hoverAnimation: true,
label: {
formatter: '{b}',
position: 'right',
show: true,
},
itemStyle: {
color: '#ddb926',
},
emphasis: {
label: {
show: true,
},
},
},
],
}
if (option) {
myChart.setOption(option)
}
},
drawRightTop() {
let myChart = this.$echarts.init(this.$refs.fourthMain)
let option = {
xAxis: {
type: 'category',
data: this.realData.LineRowData,
axisLabel: {
interval: 0, // 强制显示所有标签
textStyle: {
color: '#C0C0C0',
},
},
name: '元',
},
yAxis: {
type: 'value',
name: '个',
axisLabel: {
interval: 0, // 强制显示所有标签
textStyle: {
color: '#C0C0C0',
},
},
},
title: {
text: '商品价格占比',
left: 'center',
textStyle: {
color: '#C0C0C0',
},
},
tooltip: {
trigger: 'item',
},
legend: {
data: ['占比情况'],
textStyle: {
color: '#C0C0C0',
},
top: '8%',
right: '10%',
},
series: [
{
name: '占比情况',
data: this.realData.LineColData,
type: 'line',
},
],
}
if (option) {
myChart.setOption(option)
}
},
changeData(x) {
let st = x[0]
for (let i = 0; i < x.length - 1; i++) {
x[i] = x[i + 1]
}
x[x.length - 1] = st
},
updataBarChart() {
if (this.isHovered == true) {
this.changeData(this.realData.cityList)
this.changeData(this.realData.volumnList)
let myChart = this.$echarts.init(this.$refs.firstMain)
myChart.setOption({
xAxis: {
data: this.realData.cityList,
},
series: [
{
data: this.realData.volumnList,
},
],
})
} else {
clearInterval(this.timer)
}
},
startDataupdataInterval() {
const interval = 2000
clearInterval(this.timer)
setInterval(this.updataBarChart, interval)
},
startAction() {
this.isHovered = false
},
cancelAction() {
this.isHovered = true
},
},
async mounted() {
// 生命周期
// 异步请求,使用await
const res = await this.$http.get('myApp/screenData/')
// 接收数据
this.$set(this.realData, 'cityList', res.data.cityList)
this.$set(this.realData, 'volumnList', res.data.volumnList)
this.$set(this.realData, 'pieList', res.data.pieList)
this.$set(this.realData, 'mapData', res.data.mapData)
this.$set(this.realData, 'LineRowData', res.data.LineRowData)
this.$set(this.realData, 'LineColData', res.data.LineColData)
this.$set(this.config, 'data', res.data.circlieList)
// console.log(res.data.circlieList)
},
async updated() {
this.drawLeftTop()
this.drawLeftBottom()
this.drawCenterMap()
this.startDataupdataInterval()
this.drawRightTop()
},
}
</script>

<style scoped>
/*设置整体*/
.layout {
height: 100vh;
width: 100%;
background: url('../assets/imgs/bg03.jpg');
}

.header {
display: flex;
align-items: center;
justify-content: center;
}

.header div {
width: 50%;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}

.header div span {
color: steelblue;
font-weight: bold;
font-size: 23px;
padding: 10px;
}

.content {
width: 100%;
height: 100%;
display: flex;
flex-direction: row;
}

.content .left {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 350px;
margin-left: 30px;
}

.content .left div {
flex-grow: 1;
height: 300px;
width: 380px;
padding: 5px;
}

.content .right {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 350px;
}

.content .right div {
flex-grow: 1;
height: 300px;
width: 350px;
padding: 5px;
}

.content .center {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 80%;
width: 600px;
}
</style>