当前位置:   article > 正文

django 使用 可视化包-Pyechart

django pyechart 不顯示圖像

Echarts 是百度开源的一个数据可视化 JS 库,主要用于数据可视化。pyecharts 是一个用于生成 Echarts 图表的类库。实际上就是 Echarts 与 Python 的对接。

 

本次使用django2.0 和 pyecharts0.3.3

 

1. setttins.py 添加自己创建的app名称,和静态文件的路径

  1. INSTALLED_APPS = [
  2. 'django.contrib.admin',
  3. 'django.contrib.auth',
  4. 'django.contrib.contenttypes',
  5. 'django.contrib.sessions',
  6. 'django.contrib.messages',
  7. 'django.contrib.staticfiles',
  8. 'my_report',
  9. ]
  10. DATABASES = {
  11. 'default': {
  12. 'ENGINE': 'django.db.backends.oracle',
  13. 'NAME': 'devdb',
  14. 'USER': 'hysh',
  15. 'PASSWORD': 'hysh',
  16. 'HOST': '192.168.191.3',
  17. 'PORT': '1521',
  18. }
  19. }
  20. STATIC_URL = '/static/'
  21. STATICFILES_DIRS = (
  22. os.path.join(BASE_DIR, 'static'),
  23. )

    

2.urls.py

  1. from django.contrib import admin
  2. from django.urls import path
  3. from month_report import views
  4. urlpatterns = [
  5. path('admin/', admin.site.urls),
  6. path('guo/', views.guo_report, name='index'),
  7. ]

  

3.views.py

  1. from django.shortcuts import render
  2. from django.db import connection
  3. from django.http import HttpResponse
  4. from django.template import loader
  5. from pyecharts import Bar, Geo
  6. def exc_sql(sql):
  7. cursor = connection.cursor()
  8. cursor.execute(sql)
  9. result = cursor.fetchall()
  10. return result
  11. def guo_report(request):
  12. # select province, difi_re_num from REPORT_REG
  13. ret = """select city, difi_re_num from REPORT_REG a, province_to_city b where a.province=b.province"""
  14. data_list = exc_sql(ret)
  15. attr = [i[0] for i in data_list]
  16. value = [i[1] for i in data_list]
  17. geo = Geo("全国各地用户注册图", width=1200, height=600)
  18. geo.add("各省注册量", attr, value, type="effectScatter", border_color="#ffffff", symbol_size=2,
  19. is_label_show=True, label_text_color="#00FF00", label_pos="inside", symbol_color="yellow",
  20. geo_normal_color="#006edd", geo_emphasis_color="#0000ff")
  21. data = {'data': geo.render_embed()}
  22. return render(request, 'guo_report.html', data)

  

4.guo_report.html

  1. <!-- myfirstvis/templates/myfirstvis/pyecharts.html -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <title>MY-ECharts</title>
  7. <script src="http://oog4yfyu0.bkt.clouddn.com/echarts.min.js"></script>
  8. <script src="http://oog4yfyu0.bkt.clouddn.com/echarts-gl.js"></script>
  9. <script type="text/javascript " src="http://echarts.baidu.com/gallery/vendors/echarts/map/js/china.js"></script>
  10. <script type="text/javascript " src="http://echarts.baidu.com/gallery/vendors/echarts/map/js/world.js"></script>
  11. <script type="text/javascript " src="http://oog4yfyu0.bkt.clouddn.com/wordcloud.js"></script>
  12. </head>
  13. <body>
  14. {{data|safe}}
  15. </body>
  16. </html>

  

效果图:

 

可参考:https://zhuanlan.zhihu.com/p/28424938

转载于:https://www.cnblogs.com/wumingxiaoyao/p/8508060.html

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

闽ICP备14008679号