赞
踩
Python2版本参考 :https://05x-docs.pyecharts.org/#/zh-cn/django
Python3版本参考:https://pyecharts.org/#/zh-cn/web_django
一、python 2中调用pyecharts版本操作如下
1,在views中操作
- # -*- coding:utf-8 -*-
- from ..models import xxxx
- from django.http import JsonResponse,HttpResponse
- from pyecharts import Bar
- from django.template import loader
- from django.db.models import Count
- REMOTE_HOST = "https://pyecharts.github.io/assets/js"
-
- def AreaUser(req):
- """查询各区域注册率"""""
- department=list(xxxx.objects.all().values_list('depart',flat=True).annotate(num=Count('id')))
- staffuser=StaffTB.objects.filter(create_time__gte='2019-01-01',create_time__lte='2019-06-30')
- x = department
- y = [] #注册
- y1 = [] #未注册
- template = loader.get_template('pyecharts.html') #该html 是创建和App文件同级的templates下的
- for zcbm in department:
- zc_num=staffuser.filter(openid__isnull=False,depart=zcbm).count()
- y.append(zc_num)
- wzc_num=staffuser.filter(openid__isnull=True,depart=zcbm).count()
- y1.append(wzc_num)
- bar = barline(x,y,y1)
- context = dict(
- myechart=bar.render_embed(),
- host=REMOTE_HOST,
- script_list=bar.get_js_dependencies()
- )
- return HttpResponse(template.render(context, req))
-
- def barline(x,y,y1):
- bar=Bar(width=1200, height=600) #宽高也可以不定义
- bar.add('已注册',x,y,is_more_utils=True,xaxis_rotate=60,is_datazoom_show=True)
- bar.add('未注册',x,y1,mark_line =['min','max'],xaxis_rotate=60,is_datazoom_show=True)
- #xaxis_rotate=60 x轴倾斜
- return bar
2,在html中操作
- <!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>
- {% endfor %}
- </head>
-
- <body>
- {{ myechart|safe }}
- </body>
-
- </html>
3,结果如下
二、python 3.6 版本调用
1,先将github 上的 html模板拷贝到项目中templates目录下 地址:https://github.com/pyecharts/pyecharts/tree/master/pyecharts/render/templates
2,修改views
- # -*- coding:utf-8 -*-
- from jinja2 import Environment, FileSystemLoader
- from pyecharts.globals import CurrentConfig
- from django.http import HttpResponse
-
- CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("./templates"))
- from pyecharts import options as opts
- from pyecharts.charts import Bar
-
- # Create your views here.
-
- def TestData(req):
- """数据测试"""""
- c = (
- Bar()
- .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
- .add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
- .add_yaxis("商家B", [15, 25, 16, 55, 48, 8])
- .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
- )
- return HttpResponse(c.render_embed())
3,显示效果如下
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。