Objective: To collect data from Plc to database and draw real-time dynamic curve with Echart.
1 ideas
-Django performs tasks periodically, pushing data to Echart.
-the front-end reads the back-end data periodically and displays it on the Echart.
The first way of thinking seems to be out of line, mainly consider the second approach.
The second way to think first is to use JavaScript to read the database directly, and periodically update the Echart curve.
Later to understand that JS is just the front-end language, no access to the database, so finally turned to Ajax.
The biggest advantage of AJAX is that you can exchange data with the server and update portions of the Web page without reloading the entire page.
This is officially the function I need.
2, Task decomposition
-Echart Dynamic curve shows how to achieve (official routines)
-How Ajax is used (Runoob Ajax tutorial)
-Django Background Data preparation
3. Implementation
Ajax.html
<! DOCTYPE html>
Django Background Program
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 (), RA Ndom.random ()] # dfx[' c '] = [Random.random (), Random.random (), Random.random ()] # dfx[' a '] = Pd.to_datetime (DF X.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 option = De.eplot (dfn,1) str_op
tion = 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 option = De.eplot (dfn,1) str_op
tion = json.dumps (option) context = {"Mycontext": str_option} #context = {"Mycontext": {' a ': [1,2], ' B ': [3,4]}} return render (Request, ' myapp/ajax.html ', context)
The front end obtains a string by accessing the MyText function, which is converted to Echart object by Json.parse ().
Finally, using the JS timer function setinterval (func1,1000) Timing function, read the data regularly and update the Echart chart.