亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

django+echart數(shù)據(jù)動(dòng)態(tài)顯示的例子

 更新時(shí)間:2019年08月12日 12:01:23   作者:inch2006  
今天小編就為大家分享一篇django+echart數(shù)據(jù)動(dòng)態(tài)顯示的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

目標(biāo):從plc采集數(shù)據(jù)到數(shù)據(jù)庫(kù),利用echart繪制實(shí)時(shí)動(dòng)態(tài)曲線。

1 思路

- django定時(shí)執(zhí)行任務(wù),將數(shù)據(jù)推送到echart。

- 前端定時(shí)讀取后端數(shù)據(jù),并顯示到echart上。

第一種思路貌似走不通,主要考慮第二種方式。

第二種方式首先想到的是利用javascript直接讀取數(shù)據(jù)庫(kù),并定時(shí)更新echart曲線。

后來(lái)了解js只是前端語(yǔ)言,沒(méi)有訪問(wèn)數(shù)據(jù)庫(kù)的能力,因此最后轉(zhuǎn)向ajax。

AJAX 最大的優(yōu)點(diǎn)是在不重新加載整個(gè)頁(yè)面的情況下,可以與服務(wù)器交換數(shù)據(jù)并更新部分網(wǎng)頁(yè)內(nèi)容。

這個(gè)正是我需要的功能。

2、任務(wù)分解

- echart動(dòng)態(tài)曲線顯示如何實(shí)現(xiàn)(官方有例程)

- ajax如何使用(runoob ajax教程)

- django后臺(tái)數(shù)據(jù)準(zhǔn)備

3、執(zhí)行

ajax.html

<!DOCTYPE html>
<html>
 
<head>
 
	{% load static %}
	<script src="{% static 'myapp/json2.js'%}"></script>
	<script src="{% static 'myapp/echarts.js'%}"></script>
	<script src="{% static 'myapp/matplotlib.js'%}"></script>
 
	<meta charset="utf-8">
 
</head>
 
<body>
 
	<div id="main" style="background-color:#eceaea; width: 800px; height:600px;"></div>
 
	<div id="myDiv">
		<h2>使用 AJAX 修改該文本內(nèi)容</h2></div>
	<button type="button" οnclick="loadXMLDoc()">修改內(nèi)容</button>
 
 
 
	<script>
		var json = {{myContext | safe}}
		var jstr = JSON.stringify(json)
		var option = JSON.parse(jstr)
		// console.log(option)
 
		<!-- 為ECharts準(zhǔn)備一個(gè)具備大?。▽捀撸┑腄om -->
		// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
		var myChart = echarts.init(document.getElementById('main'), 'matplotlib');
 
		// 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表。
		myChart.setOption(option);
 
 
		function loadXMLDoc() {
			var xmlhttp;
			if (window.XMLHttpRequest) {
				// IE7+, Firefox, Chrome, Opera, Safari 瀏覽器執(zhí)行代碼
				xmlhttp = new XMLHttpRequest();
			} else {
				// IE6, IE5 瀏覽器執(zhí)行代碼
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					//document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
					//content = "{{ myContext }}";
					//console.log(content)
 
					//var json = xmlhttp.responseText;
					//var jstr = JSON.stringify(json)
					option = JSON.parse(xmlhttp.responseText)
 
					// 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表。
					myChart.setOption(option);
					//console.log(option);
 
				}
			}
			xmlhttp.open("GET", "/myapp/mytext", true);
			xmlhttp.send();
		}
 
 
		setInterval(loadXMLDoc, 500);
	</script>
 
</body>
 
</html>

django后臺(tái)程序

def mytext(request):
  #df = pd.read_csv(r'E:\mywebsite\ui\myapp\xx.csv')
 
  import random
 
  # dfx = pd.DataFrame()
  # dfx['a'] = ['2017-08-08','2017-08-09','2017-08-10']
  # dfx['b'] = [random.random(),random.random(),random.random()]
  # dfx['c'] = [random.random(),random.random(),random.random()]
  #
  # dfx['a'] = pd.to_datetime(dfx.a)
  #
  # dfx = dfx.set_index('a')
 
 
  import sqlite3
  conn = sqlite3.connect(r"E:\01_Lab\L02_Ads\practise\ads_sample\multi_freq_data\multi_freq_data\bin\x86\Debug\db_all.db")
  df = pd.read_sql('select * from buffer',conn)
  df = df.set_index(pd.to_datetime(df.TimeStamp))
  dfn = pd.DataFrame()
  dfn['ws'] = df.grWindSpeed.astype(float)
 
  dfn = dfn.tail(500)
 
  option = de.eplot(dfn,1)
  str_option = json.dumps(option)
  context = {"myContext": str_option}
 
  #return render(request,'myapp/a.html',context)
  return HttpResponse(str_option)
 
 
def test_ajax(request):
 
  import sqlite3
  conn = sqlite3.connect(r"E:\01_Lab\L02_Ads\practise\ads_sample\multi_freq_data\multi_freq_data\bin\x86\Debug\db_all.db")
  df = pd.read_sql('select * from buffer',conn)
  df = df.set_index(pd.to_datetime(df.TimeStamp))
  dfn = pd.DataFrame()
  dfn['ws'] = df.grWindSpeed.astype(float)
 
  dfn = dfn.tail(500)
 
  option = de.eplot(dfn,1)
  str_option = json.dumps(option)
  context = {"myContext": str_option}
 
 
  #context = {"myContext": {'a':[1,2],'b':[3,4]}}
  return render(request, 'myapp/ajax.html', context)

前端通過(guò)訪問(wèn)mytext函數(shù)獲取到一個(gè)字符串,通過(guò)json.parse()轉(zhuǎn)為echart對(duì)象。

最后,利用js定時(shí)功能setInterval(func1,1000)定時(shí)功能,定時(shí)讀取數(shù)據(jù)并更新echart圖表。

以上這篇django+echart數(shù)據(jù)動(dòng)態(tài)顯示的例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論