当前位置:   article > 正文

Django+pyecharts 使用python2版本时使用的pyecharts为0.5.0版本 用python3.6版本时使用的 pyecharts 为1.2.1版本_python 3.6 pyecharts

python 3.6 pyecharts

Python2版本参考 :https://05x-docs.pyecharts.org/#/zh-cn/django

Python3版本参考:https://pyecharts.org/#/zh-cn/web_django

一、python 2中调用pyecharts版本操作如下

1,在views中操作

  1. # -*- coding:utf-8 -*-
  2. from ..models import xxxx
  3. from django.http import JsonResponse,HttpResponse
  4. from pyecharts import Bar
  5. from django.template import loader
  6. from django.db.models import Count
  7. REMOTE_HOST = "https://pyecharts.github.io/assets/js"
  8. def AreaUser(req):
  9. """查询各区域注册率"""""
  10. department=list(xxxx.objects.all().values_list('depart',flat=True).annotate(num=Count('id')))
  11. staffuser=StaffTB.objects.filter(create_time__gte='2019-01-01',create_time__lte='2019-06-30')
  12. x = department
  13. y = [] #注册
  14. y1 = [] #未注册
  15. template = loader.get_template('pyecharts.html') #该html 是创建和App文件同级的templates下的
  16. for zcbm in department:
  17. zc_num=staffuser.filter(openid__isnull=False,depart=zcbm).count()
  18. y.append(zc_num)
  19. wzc_num=staffuser.filter(openid__isnull=True,depart=zcbm).count()
  20. y1.append(wzc_num)
  21. bar = barline(x,y,y1)
  22. context = dict(
  23. myechart=bar.render_embed(),
  24. host=REMOTE_HOST,
  25. script_list=bar.get_js_dependencies()
  26. )
  27. return HttpResponse(template.render(context, req))
  28. def barline(x,y,y1):
  29. bar=Bar(width=1200, height=600) #宽高也可以不定义
  30. bar.add('已注册',x,y,is_more_utils=True,xaxis_rotate=60,is_datazoom_show=True)
  31. bar.add('未注册',x,y1,mark_line =['min','max'],xaxis_rotate=60,is_datazoom_show=True)
  32. #xaxis_rotate=60 x轴倾斜
  33. return bar

2,在html中操作

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

3,结果如下

二、python 3.6 版本调用

 1,先将github 上的 html模板拷贝到项目中templates目录下 地址:https://github.com/pyecharts/pyecharts/tree/master/pyecharts/render/templates

2,修改views

  1. # -*- coding:utf-8 -*-
  2. from jinja2 import Environment, FileSystemLoader
  3. from pyecharts.globals import CurrentConfig
  4. from django.http import HttpResponse
  5. CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("./templates"))
  6. from pyecharts import options as opts
  7. from pyecharts.charts import Bar
  8. # Create your views here.
  9. def TestData(req):
  10. """数据测试"""""
  11. c = (
  12. Bar()
  13. .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
  14. .add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
  15. .add_yaxis("商家B", [15, 25, 16, 55, 48, 8])
  16. .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
  17. )
  18. return HttpResponse(c.render_embed())

3,显示效果如下

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/165015
推荐阅读
相关标签
  

闽ICP备14008679号