echarts動(dòng)態(tài)獲取Django數(shù)據(jù)的實(shí)現(xiàn)示例
在開發(fā)過程中我們需要將我們的數(shù)據(jù)通過圖標(biāo)的形式展現(xiàn)出來,接下來我為大家介紹一個(gè)有趣的框架:Echarts。這是一個(gè)使用JavaScript實(shí)現(xiàn)的開源可視化庫,提供了常規(guī)的折線圖、柱狀圖、散點(diǎn)圖、餅圖、K線圖,用于統(tǒng)計(jì)的盒形圖,用于地理數(shù)據(jù)可視化的地圖、熱力圖、線圖,用于關(guān)系數(shù)據(jù)可視化的關(guān)系圖、treemap、旭日?qǐng)D,多維數(shù)據(jù)可視化的平行坐標(biāo),還有用于 BI 的漏斗圖,儀表盤,并且支持圖與圖之間的混搭(官網(wǎng)照抄,有興趣的小伙伴可以去官網(wǎng)發(fā)現(xiàn)更多echarts的運(yùn)用)。下面直接上代碼:
一、后端
1. models模塊
from django.db import models
# 一個(gè)簡(jiǎn)單的統(tǒng)計(jì)地區(qū)
class EventInfo(models.Model):
event_location = models.CharField(max_length=30)
class Meta:
db_table = 'app_event_info'
2. urls
from django.conf.urls import url
from app1 import views
urlpatterns = [
url(r'^home/', views.home), # 展示數(shù)據(jù)
url(r'^test/', views.test), # api,提供json
]
3. views
import json
from django.db.models import Count
from django.http import JsonResponse
from django.shortcuts import render
from app1.models import EventInfo
def home(request):
return render(request, 'echarts_pie.html') # 數(shù)據(jù)展示
def test(request):
info = EventInfo.objects.values_list('event_location').annotate(Count('id'))
# 這里使用了Model.object.values_list().annotate()的方法,計(jì)數(shù)'event_location',生成id_count,以list的形式展示出來,大家可以去網(wǎng)上研究一下annotate函數(shù)
# >>> print info
# [('上海', 6), ('北京', 5), ('天津', 4), ('太原', 4), ('南京', 3), ('蘇州', 4)]
jsondata = {
"name": [i[0] for i in info],
"count": [i[1] for i in info]
}
cities = []
for n, c in zip(jsondata['name'], jsondata['count']):
b = {}
b['name'] = n
b['count'] = c
cities.append(b)
test_dic = {}
test_dic['data'] = cities
# 將數(shù)據(jù)轉(zhuǎn)換成json格式,方便ajax調(diào)用
return JsonResponse(test_dic, safe=False)
二、前端
1. HTML
// 倒包,這是直接調(diào)用網(wǎng)上的包,不需要額外在靜態(tài)文件中下載
<script src="http://echarts.baidu.com/dist/echarts.min.js"></script>
<script type="text/javascript" charset="utf8" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
// 創(chuàng)建一個(gè)div,id為main,方便JavaScript使用
<div style="border:2px solid #a6e1ec;width:49%;height:450px;float:left" id="main"></div>
2. JavaScript
<script type="text/javascript">
// echartss的標(biāo)準(zhǔn)格式,屬性可以去官網(wǎng)查看
var myChart = echarts.init(document.getElementById('main'));
myChart.setOption({
//color: [ '#00FFFF', '#00FF00', '#FFFF00', '#FF8C00', '#FF0000', '#FE8463'], // 自定義echarts的顏色
title: { // 標(biāo)題
text: 'cityinfo',
subtext: 'just-test',
x: 'center'
},
tooltip: { // 提示框組件
trigger: 'item',
formatter: '{a}</br>: {c}(sr3imwa%)'
},
legend: { // 圖例組件
orient: 'vertical',
x: 'left',
data: []
},
toolbox: { // 工具欄
show: true,
feature: {
mark: {show: true},
dataView: {show: true, readOnly: false},
magicType: {
show: true,
type: ['pie', 'funnel'],
option: {
funnel: {
x: '25%',
width: '50%',
funnelAlign: 'center',
max: 1548
}
}
},
restore: {show: true},
saveAsImage: {show: true}
},
},
calculable: true,
series: [{ // 設(shè)置圖形種類,常用的有pie(餅狀圖),bar(柱狀體),line(折線圖)
name: 'city',
type: 'pie',
radius: '55%',
center: ['50%', '60%'],
itemStyle: {
normal: {
label: {show: true},
labelLine: {
show: true
},
color: function (value) { // 隨機(jī)生成顏色(官網(wǎng)的默認(rèn)顏色比較low,生成的也不怎么樣)
return "#" + ("00000" + ((Math.random() * 16777215 + 0.5) >> 0).toString(16)).slice(-6);
}
},
emphasis: {
label: {
show: true,
position: 'center',
textStyle: {
fontSize: '20',
fontWeight: 'bold'
}
}
}
},
data: []
}]
});
myChart.showLoading();
var names = [];
var brower = [];
$.ajax({ // ajax的方式動(dòng)態(tài)獲取后端代碼
type: 'get',
url: 'http://127.0.0.1:8000/test/test/',
dataType: 'json',
success: function (result) {
$.each(result.data, function (index, item) {
names.push(item.name);
brower.push({
value: item.count,
name: item.name
});
});
myChart.hideLoading();
myChart.setOption({
legend: {
data: names
},
series: [{
data: brower
}]
});
},
error: function (errormsg) {
alert('errormsg');
myChart.hideLoading();
}
});
</script>
三、頁面效果

四、總結(jié)
大家在開發(fā)過程中如果需要將數(shù)據(jù)展示出來可以嘗試著使用echarts,結(jié)合實(shí)際情況酌情使用餅狀圖、柱狀體、折線圖及其他,在使用的過程中注意官網(wǎng)中data的格式。
到此這篇關(guān)于echarts動(dòng)態(tài)獲取Django數(shù)據(jù)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)echarts動(dòng)態(tài)獲取Django數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mac中升級(jí)Python2.7到Python3.5步驟詳解
本篇文章主要介紹了Mac中升級(jí)Python2.7到Python3.5步驟詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
在PyCharm導(dǎo)航區(qū)中打開多個(gè)Project的關(guān)閉方法
今天小編就為大家分享一篇在PyCharm導(dǎo)航區(qū)中打開多個(gè)Project的關(guān)閉方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
python使用pytest接口自動(dòng)化測(cè)試的使用
這篇文章主要介紹了python使用pytest接口自動(dòng)化測(cè)試的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
解決import tensorflow as tf 出錯(cuò)的原因
這篇文章主要介紹了解決import tensorflow as tf 出錯(cuò)的原因,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Kears 使用:通過回調(diào)函數(shù)保存最佳準(zhǔn)確率下的模型操作
這篇文章主要介紹了Kears 使用:通過回調(diào)函數(shù)保存最佳準(zhǔn)確率下的模型操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python深度學(xué)習(xí)線性代數(shù)示例詳解
這篇文章主要為大家講解了Python深度學(xué)習(xí)中線性代數(shù)的示例詳解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
Python實(shí)現(xiàn)HTML轉(zhuǎn)Word的示例代碼
這篇文章主要為大家詳細(xì)介紹了使用Python實(shí)現(xiàn)HTML轉(zhuǎn)Word的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
詳解Python的多線程定時(shí)器threading.Timer
這篇文章主要為大家介紹了Python的多線程定時(shí)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01

