Article From:https://www.cnblogs.com/Python6359/p/9123995.html

Step 0: Using a new virtualenv environment

It is recommended to use the 1.11.4 version of Django

$ virtualenv --no-site-packages pyecharts-env
$ source pyecharts-env/bin/activate
$ pip install django==1.11.4
$ pip install pyecharts

Step 1: Build a new Django project

$ django-admin startproject myechartsite

Create an application

$ python manage.py startapp myfirstvis
$ ls
db.sqlite3      manage.py       myechartsite    myfirstvis

At the same timemyechartsite/settings.py Registered applications

# myechartsite/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myfirstvis'  # <---
]

We first edit urls.py.. The function of this file in Django is to link the HTTP requirement and the backstage service function in the preceding paragraph. In Step3, we reintroduce the back end service function

1 # myfirstvis/urls.py
2 from django.conf.urls import url
3 
4 from . import views
5 
6 urlpatterns = [
7     url(r'^$', views.index, name='index'),
8 ]

At the same timemyechartsite/urls.py New’myfirstvis.urls’

1 myechartsite/urls.py
2 from django.conf.urls import include, url
3 from django.contrib import admin
4 
5 urlpatterns = [
6     url(r'^admin/', admin.site.urls),
7     url(r'myfirstvis/', include('myfirstvis.urls'))  # <---
8 ]

Step 2: Processing view functional parts

Save the following codemyfirstvis/views.py In the middle.

 

 1 from __future__ import unicode_literals
 2 import math
 3 
 4 from django.http import HttpResponse
 5 from django.template import loader
 6 from pyecharts import Line3D
 7 
 8 
 9 REMOTE_HOST = "https://pyecharts.github.io/assets/js"
10 
11 
12 def index(request):
13     template = loader.get_template('myfirstvis/pyecharts.html')
14     l3d = line3d()
15     context = dict(
16         myechart=l3d.render_embed(),
17         host=REMOTE_HOST,
18         script_list=l3d.get_js_dependencies()
19     )
20     return HttpResponse(template.render(context, request))
21 
22 
23 def line3d():
24     _data = []
25     for t in range(0, 25000):
26         _t = t / 1000
27         x = (1 + 0.25 * math.cos(75 * _t)) * math.cos(_t)
28         y = (1 + 0.25 * math.cos(75 * _t)) * math.sin(_t)
29         z = _t + 2.0 * math.sin(75 * _t)
30         _data.append([x, y, z])
31     range_color = [
32         '#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf',
33         '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
34     line3d = Line3D("3D line plot demo", width=1200, height=600)
35     line3d.add("", _data, is_visualmap=True,
36                visual_range_color=range_color, visual_range=[0, 30],
37                is_grid3D_rotate=True, grid3D_rotate_speed=180)
38     return line3d

 

cript_list It is the echarts JS library that Page () class needs to rely on rendering web pages, and the number of libraries depends on the type of graphics to render.

host It’s the address of the echarts JS library. The default address is https://pyecharts.github.io/assets/js, of course, if you want, you can change the address and clone https://gith first.Ub.com/pyecharts/assetsjs The folder is mounted on your own server.

Step 3: Provide your own template for the project

Windows system


In the myfirstvis directory, create a new templates/myfirstvis subdirectory.

myfirstvis Catalog

 

─ myfirstvis
    ├── admin.py
    ├── apps.py
    ├── __init__.py
    ├── migrations
    │   ├── __init__.py
    ├── models.py
    ├── templates
    │   └── myfirstvis
    │       └── pyecharts.html
    ├── tests.py
    ├── urls.py
    └── views.py

 

Save the following HTML template code to pyecharts.html, ensure that the absolute path of the pyecharts.html file is<project root>/myfirstvis/templates/myfirstvis

 1 <!-- myfirstvis/templates/pyecharts.html -->
 2 <!DOCTYPE html>
 3 <html>
 4 
 5 <head>
 6     <meta charset="utf-8">
 7     <title>Proudly presented by PycCharts</title>
 8     {% for jsfile_name in script_list %}
 9         <script src="{{ host }}/{{ jsfile_name }}.js"></script>
10     {% endfor %}
11 </head>
12 
13 <body>
14   {{ myechart|safe }}
15 </body>
16 
17 </html>

Step 4: Running project

 

 

$ cd myechartsite
$ python manage.py runserver

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

August 08, 2017 - 05:48:38
Django version 1.11.4, using settings 'myechartsite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

 

If you visit http://localhost:8000/myfirstvis/, you can see the cool 3D map.

 

 

 

summary

See, just a few simple steps can be used to create visual charts using pyecharts. Django official tutorial needs seven steps. Here we have three steps.

 

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *