Pyecharts Visualization

The paper is short and long, and the story is still young, my story is still about you

Recently, in the process of developing web applications, visual display functions were needed, so I found a Python-related visualization module. Here is a simple record of the usage of the pyecharts module. It is recommended mainly because of its powerful features, more choice of visualization features, and easier to use.

pyecharts Introduction

First need to understand the operating mechanism of the pyecharts module, pyecharts is echarts python-api, and echarts is Baidu open source visualization framework. Echarts is used to manipulate js files, so the emergence of pyecharts is actually to make the python language better echarts. Simply put, pyecharts will help us generate js files.

Installing pyecharts

1
pip install pyecharts

Or Github download source installation: https://github.com/pyecharts/pyecharts

1
2
3
4
$ git clone https://github.com/pyecharts/pyecharts.git
$ cd pyecharts
$ pip install -r requirements.txt
$ python setup.py install

Simple use of pyecharts

Create a test.py file and write:

1
2
3
4
5
6
7
8
9
From pyecharts import Bar # histogram
attr = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
v1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
v2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
bar = Bar("Bar chart", "precipitation and evaporation one year")
bar.add("precipitation", attr, v1, mark_line=["average"], mark_point=["max", "min"])
bar.add("evaporation", attr, v2, mark_line=["average"], mark_point=["max", "min"])
Bar.render() # Generate an html file

Running test.py will generate a render.html file in the current directory, which is the web page containing the histogram. Looking at this html file, you will find that it generates a lot of js code.

Note: In addition to the histogram, pyecharts supports other visual displays, please refer to the official documentation: http://pyecharts.org/#/zh-cn/charts

pyecharts + Django

The previous introduction is to use pyecharts to generate an html page with a visual chart. How do you use it in a web framework such as Django or Flask? How do you generate chart code in the view layer and pass it to the template layer rendering display? Here only how to use pyecharts in Django, other web frameworks are the same, you can study it yourself.

view view layer

Write in the view.py file of the Django project:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from django.http import HttpResponse
from pyecharts import Pie
REMOTE_HOST = "https://pyecharts.github.io/assets/js"
def Pie_():
Attr = ["shirt", "sweater", "chiffon shirt", "trousers", "high heels", "socks"]
v1 = [11, 12, 13, 10, 10, 10]
Pie = Pie("pie chart example")
pie.add("", attr, v1, is_label_show=True)
return pie
def index(request):
# Visual display page
foot = Pie_ ()
Myechart=pie.render_embed() #pie chart
Host=REMOTE_HOST # js file source address
Script_list=pie.get_js_dependencies() # Get the name of the dependent js file (only get the js needed by the current view)
return render(request,"index.html",{"myechart":myechart,"host":host,"script_list":script_list})

Description: REMOTE_HOST can replace the cost address, that is, go to the https://github.com/pyecharts/assets clone project, then copy the js directory in the project to the static/js directory of the Django project, and then change the REMOTE_HOST in the code. for:

1
2
3
REMOTE_HOST = "https://pyecharts.github.io/assets/js"
Change to:
REMOTE_HOST = "../../static/js/js"

Django Routing

Write the contents of the urls.py file in the Django project:

1
url(r'^$',views.index, name="index"),

Template layer

Create an index.html file in the templates directory of the Django project and write:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Proudly presented by PycCharts</title>
{% for jsfile_name in script_list %}
<script src="{{ host }}/{{ jsfile_name }}.js"></script> # Load the js file
{% endfor %}
</head>
<body>
{{ myechart|safe }} # Display the visualization chart, pay attention to add safe, which means to parse the html content passed in the view layer
</body>
</html>

Running django

1
python manage.py runserver

Open the browser: http://127.0.0.1:8000

References

Official documentation: http://pyecharts.org/#/zh-cn/
Github project address: https://github.com/pyecharts/pyecharts

本文标题:Pyecharts Visualization

文章作者:nmask

发布时间:2018年04月09日 - 18:04

最后更新:2019年07月11日 - 18:07

原始链接:https://thief.one/2018/04/09/1/en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: