Django展示可視化圖表的多種方式
大家好,我是安果!
使用 Django 進行 Web 開發時,經常有需要展示圖表的需求,以此來豐富網頁的數據展示
常見方案包含:Highcharts、Matplotlib、Echarts、Pyecharts,其中后 2 種方案使用頻率更高
本篇文章將聊聊 Django 結合 Echarts、Pyecharts 實現圖表可視化的具體流程
2. EchartsEcharts 是百度開源的一個非常優秀的可視化框架,它可以展示非常復雜的圖表類型
以展示簡單的柱狀圖為例,講講 Django 集成 Echarts 的流程
首先,在某個 App 的 views.py 編寫視圖函數
當請求方法為 POST 時,定義柱狀圖中的數據值,然后使用 JsonResponse 返回數據
from django.http import JsonResponsefrom django.shortcuts import renderdef index_view(request): if request.method == 'POST':# 柱狀圖的數據datas = [5, 20, 36, 10, 10, 20]# 返回數據return JsonResponse({’bar_datas’: datas}) else:return render(request, ’index.html’, )
在模板文件中,導入 Echarts 的依賴
PS:可以使用本地 JS 文件或 CDN 加速服務
{#導入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 函數,發送一個 Ajax 請求給后端,利用 Echarts 將返回結果展示到圖表中去
<script> // 柱狀圖 function show_bar(data) {//控件var bar_widget = echarts.init(document.getElementById(’bar_div’));//設置optionoption = { title: {text: ’簡單的柱狀圖’ }, tooltip: {}, legend: {data: [’銷量’] }, xAxis: {type: ’category’,data: ['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子'] }, yAxis: {type: ’value’ }, series: [{data: data,type: ’bar’ }]};bar_widget.setOption(option) } //顯示即加載調用 window.onload = function () {//發送post請求,地址為index(Jquery)$.ajax({ url: '/', type: 'POST', data: {}, success: function (data) {// 柱狀圖show_bar(data[’bar_datas’]);//后端返回的結果console.log(data) }}) }</script>
最后,編寫路由 URL,運行項目
from django.contrib import adminfrom django.urls import path, includeurlpatterns = [ path(’’,include(’index.urls’)), path(’admin/’, admin.site.urls),]
發現,首頁展示了一個簡單的柱狀圖
更多復雜的圖表展示可以參考官方
https://echarts.apache.org/examples/zh/index.html
3. PyechartsPyecharts 是一款使用 Python 對 Echarts 進行再次封裝后的開源框架
相比 Echarts,Django 集成 Pyecharts 更快捷、方便
Django 集成 Pyecharts 只需要 4 步
3-1 安裝依賴# 安裝依賴pip(3) install pyecharts3-2 拷貝 pyecharts 的模板文件到項目下
將虛擬環境中 pyecharts 的模板文件拷貝到項目的模板文件夾下
比如本機路徑如下:
/Users/xingag/Envs/xh_log/lib/python3.7/site-packages/pyecharts/render/templates/
在視圖文件中,使用 pyecharts 庫內置的類 Bar 創建一個柱狀圖
# Create your views here.from django.http import HttpResponsefrom jinja2 import Environment, FileSystemLoaderfrom pyecharts.globals import CurrentConfigCurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader('./index/templates'))from pyecharts import options as optsfrom 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='我是副標題')) ) return HttpResponse(c.render_embed())3-4 運行項目
運行項目,生成的柱狀圖如下:
這只是最簡單的使用實例,更多復雜的圖表及前后端分離、更新的例子
可以參考官網:
https://pyecharts.org/#/zh-cn/web_django?id=django-前后端分離
4. 最后文中介紹了 Django 快速集成 Echarts 和 Pyecharts 的基本步驟
實際項目中,一些復雜的圖表、前后端分離數據更新可以參考官網去拓展
源碼:https://github.com/xingag/python_web
以上就是Django展示可視化圖表的多種方式的詳細內容,更多關于Django 可視化圖表的資料請關注好吧啦網其它相關文章!
相關文章:
