本篇内容介绍了“Django2.2+pyecharts怎么实现柱形图和世界地图”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
首先创建项目项目、应用、在settings中注册
diango-admin startproject pyecharts_django_demo
cd pyecharts_django_demo
python manage.py startapp demo
按照官网说的在demo文件夹下新建templates文件夹,复制模板文件到这个目录下 配置路由
### pyecharts_django_demo目录下的url文件
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('demo/',include('demo.urls',namespace='demo')),
]
### demo目录下的urls
from django.urls import path
from . import views
app_name = 'demo'
urlpatterns = [
path('',views.index,name='index'),
]
视图函数
from django.shortcuts import render
from jinja2 import Environment, FileSystemLoader
from pyecharts.globals import CurrentConfig
from django.http import HttpResponse
CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("./demo/templates"))
from pyecharts import options as opts
from pyecharts.charts import Bar, Map
from pyecharts.faker import Faker
# Create your views here.
def index(request):
bar_plot = (
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="我是副标题"))
)
bar_plot = bar_plot.render_embed()
world_map = (
Map()
.add("", [list(z) for z in zip(Faker.country, Faker.values())], "world",is_map_symbol_show=False,is_roam=False)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-世界地图"),
visualmap_opts=opts.VisualMapOpts(max_=200))
)
world_map = world_map.render_embed()
context = {'bar_plot':bar_plot,'world_map':world_map}
return render(request,'demo/index.html',context=context)
配置模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<title>pyecharts加Django的小例子</title>
<!-- CSS only -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h2 >你好吗?</h2>
{{ bar_plot | safe }}
</div>
<br>
<div class="container">
{{ world_map | safe }}
</div>
</body>
</html>
启动服务器
python manang.py runserver
“Django2.2+pyecharts怎么实现柱形图和世界地图”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。