数据矿工的博客

您现在的位置是:首页 > Python > 正文

Python

用pyecharts制作仪表盘——多图表在同一页面显示

张文迪2018-11-08Python14696
一、图表接口pyecharts.engine 定义了若干个继承自 jinja2.Environment 的模板引擎类,每个类都有其适合的使用场景。pyechart提供了一

一、图表接口

pyecharts.engine 定义了若干个继承自 jinja2.Environment 的模板引擎类,每个类都有其适合的使用场景。


pyechart提供了一个接口Page,只需要调用方法add("图表名")即可:

from pyecharts import Page, Line, Bar

page = Page()
line = Line('Demo Line')
# ... Add data to line
page.add_chart(line, name='line')
bar = Bar('Demo kline')
# ... Add data to bar
page.add_chart(bar)


二、图表方法


对于图表的一些属性的修改,可以在python源码中,使用一下方法:

  • page_title

  • js_dependencies

  • render_embed()

  • get_js_dependencies()

  • _repr_html_()


三、完整示例


demo.py

from __future__ import unicode_literals

from pyecharts import Bar
from pyecharts.conf import PyEchartsConfigfrom pyecharts.engine 
import EchartsEnvironmentfrom pyecharts.utils 
import write_utf8_html_file

attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = Bar("柱状图数据堆叠示例")
bar.add("商家A", attr, v1, is_stack=True)
bar.add("商家B", attr, v2, is_stack=True)
config = PyEchartsConfig(echarts_template_dir='my_tpl',
                         jshost='https://cdn.bootcss.com/echarts/3.6.2')
env = EchartsEnvironment(pyecharts_config=config)
tpl = env.get_template('tpl_demo.html')
html = tpl.render(bar=bar)
write_utf8_html_file('my_tpl_demo2.html', html)


tpl_demo.html 模板


<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>自定义模板</title>
    {{ echarts_js_dependencies(bar) }}
</head>
<body>
    {{ echarts_container(bar) }}
    {{ echarts_js_content(bar) }}
</body>
</html>


仪表盘结果示例:数据运营仪表盘







发表评论

评论列表

  • daemon(2020-04-16 09:46:40)回复取消回复

    您在demo.py中import write_utf8_html_file,这里的write_utf8_html_file的作用是什么呢?函数是自己写的吗?

    • admin(2020-06-28 07:52:17)回复取消回复

      @daemon 不是自己写的,是为了将图表写入模板中,按照utf8的字符格式写

展开