大家好,我是安果!
使用 Django 進(jìn)行 Web 開發(fā)時,經(jīng)常有需要展示圖表的需求,以此來豐富網(wǎng)頁的數(shù)據(jù)展示
常見方案包含:Highcharts、Matplotlib、Echarts、Pyecharts,其中后 2 種方案使用頻率更高
本篇文章將聊聊 Django 結(jié)合 Echarts、Pyecharts 實(shí)現(xiàn)圖表可視化的具體流程
Echarts 是百度開源的一個非常優(yōu)秀的可視化框架,它可以展示非常復(fù)雜的圖表類型
以展示簡單的柱狀圖為例,講講 Django 集成 Echarts 的流程
首先,在某個 App 的 views.py 編寫視圖函數(shù)
當(dāng)請求方法為 POST 時,定義柱狀圖中的數(shù)據(jù)值,然后使用 JsonResponse 返回數(shù)據(jù)
from django.http import JsonResponse from django.shortcuts import render def index_view(request): if request.method == "POST": # 柱狀圖的數(shù)據(jù) datas = [5, 20, 36, 10, 10, 20] # 返回數(shù)據(jù) return JsonResponse({'bar_datas': datas}) else: return render(request, 'index.html', )
在模板文件中,導(dǎo)入 Echarts 的依賴
PS:可以使用本地 JS 文件或 CDN 加速服務(wù)
{#導(dǎo)入js和echarts依賴#} script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js">/script> script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.0.2/echarts.common.js">/script>
然后,重寫 window.onload 函數(shù),發(fā)送一個 Ajax 請求給后端,利用 Echarts 將返回結(jié)果展示到圖表中去
script> // 柱狀圖 function show_bar(data) { //控件 var bar_widget = echarts.init(document.getElementById('bar_div')); //設(shè)置option option = { title: { text: '簡單的柱狀圖' }, tooltip: {}, legend: { data: ['銷量'] }, xAxis: { type: 'category', data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"] }, yAxis: { type: 'value' }, series: [{ data: data, type: 'bar' }] }; bar_widget.setOption(option) } //顯示即加載調(diào)用 window.onload = function () { //發(fā)送post請求,地址為index(Jquery) $.ajax({ url: "/", type: "POST", data: {}, success: function (data) { // 柱狀圖 show_bar(data['bar_datas']); //后端返回的結(jié)果 console.log(data) } }) } /script>
最后,編寫路由 URL,運(yùn)行項(xiàng)目
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('',include('index.urls')), path('admin/', admin.site.urls), ]
發(fā)現(xiàn),首頁展示了一個簡單的柱狀圖
更多復(fù)雜的圖表展示可以參考官方
https://echarts.apache.org/examples/zh/index.html
Pyecharts 是一款使用 Python 對 Echarts 進(jìn)行再次封裝后的開源框架
相比 Echarts,Django 集成 Pyecharts 更快捷、方便
Django 集成 Pyecharts 只需要 4 步
# 安裝依賴 pip(3) install pyecharts
將虛擬環(huán)境中 pyecharts 的模板文件拷貝到項(xiàng)目的模板文件夾下
比如本機(jī)路徑如下:
/Users/xingag/Envs/xh_log/lib/python3.7/site-packages/pyecharts/render/templates/
在視圖文件中,使用 pyecharts 庫內(nèi)置的類 Bar 創(chuàng)建一個柱狀圖
# Create your views here. from django.http import HttpResponse from jinja2 import Environment, FileSystemLoader from pyecharts.globals import CurrentConfig CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("./index/templates")) from pyecharts import options as opts from pyecharts.charts import Bar # http://127.0.0.1:8000/demo/ def index(request): 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="我是副標(biāo)題")) ) return HttpResponse(c.render_embed())
運(yùn)行項(xiàng)目,生成的柱狀圖如下:
這只是最簡單的使用實(shí)例,更多復(fù)雜的圖表及前后端分離、更新的例子
可以參考官網(wǎng):
https://pyecharts.org/#/zh-cn/web_django?id=django-前后端分離
文中介紹了 Django 快速集成 Echarts 和 Pyecharts 的基本步驟
實(shí)際項(xiàng)目中,一些復(fù)雜的圖表、前后端分離數(shù)據(jù)更新可以參考官網(wǎng)去拓展
源碼:https://github.com/xingag/python_web
以上就是Django展示可視化圖表的多種方式的詳細(xì)內(nèi)容,更多關(guān)于Django 可視化圖表的資料請關(guān)注腳本之家其它相關(guān)文章!
標(biāo)簽:中山 湖州 呼和浩特 江蘇 股票 畢節(jié) 衡水 駐馬店
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Django展示可視化圖表的多種方式》,本文關(guān)鍵詞 Django,展示,可視化,圖表,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。