網(wǎng)站首頁 編程語言 正文
在開發(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、旭日圖,多維數(shù)據(jù)可視化的平行坐標(biāo),還有用于 BI 的漏斗圖,儀表盤,并且支持圖與圖之間的混搭(官網(wǎng)照抄,有興趣的小伙伴可以去官網(wǎng)發(fā)現(xiàn)更多echarts的運(yùn)用)。下面直接上代碼:
一、后端
1. models模塊
from django.db import models
# 一個(gè)簡單的統(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>{b}: {c}(bsd5o550550j%)'
},
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的方式動態(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的格式。
原文鏈接:https://blog.csdn.net/weixin_45508740/article/details/102483068
相關(guān)推薦
- 2023-05-16 Golang函數(shù)這些神操作你知道哪些_Golang
- 2022-04-09 python多線程互斥鎖與死鎖問題詳解_python
- 2022-08-25 高級消息隊(duì)列協(xié)議AMQP簡介_其它綜合
- 2022-09-03 Python?pandas?DataFrame基礎(chǔ)運(yùn)算及空值填充詳解_python
- 2022-09-06 python實(shí)現(xiàn)plt?x軸坐標(biāo)按1刻度顯示_python
- 2022-07-22 C/C++冒泡排序
- 2023-01-23 Python列表對象中元素的刪除操作方法_python
- 2022-06-12 Python數(shù)據(jù)傳輸黏包問題_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支