結構:
/www
|
|-- /static
|....|-- jquery-3.1.1.js
|....|-- echarts.js(echarts3是單文件!!)
|
|-- /templates
|....|-- index.html
|
|-- app.py
|
|-- create_db.pyjavascript
# create_db.py # 只運行一次!!! import sqlite3 # 鏈接 conn = sqlite3.connect('mydb.db') c = conn.cursor() # 建立表 c.execute('''DROP TABLE IF EXISTS weather''') c.execute('''CREATE TABLE weather (month text, evaporation text, precipitation text)''') # 數據 # 格式:月份,蒸發量,降水量 purchases = [('1月', 2, 2.6), ('2月', 4.9, 5.9), ('3月', 7, 9), ('4月', 23.2, 26.4), ('5月', 25.6, 28.7), ('6月', 76.7, 70.7), ('7月', 135.6, 175.6), ('8月', 162.2, 182.2), ('9月', 32.6, 48.7), ('10月', 20, 18.8), ('11月', 6.4, 6), ('12月', 3.3, 2.3) ] # 插入數據 c.executemany('INSERT INTO weather VALUES (?,?,?)', purchases) # 提交!!! conn.commit() # 查詢方式一 for row in c.execute('SELECT * FROM weather'): print(row) # 查詢方式二 c.execute('SELECT * FROM weather') print(c.fetchall()) # 查詢方式二_2 res = c.execute('SELECT * FROM weather') print(res.fetchall()) # 關閉 conn.close()
一次性總體加載全部數據html
由以下函數實現:java
@app.route("/weather", methods=["GET"]) def weather(): if request.method == "GET": res = query_db("SELECT * FROM weather") return jsonify(month = [x[0] for x in res], evaporation = [x[1] for x in res], precipitation = [x[2] for x in res])
此函數用於處理ajax,返回json格式。形如:python
{ month: ['1月','2月',...], evaporation: [3.1, 4, 4.6, ...], precipitation: [...] }
完整app.py文件:jquery
# app.py import sqlite3 from flask import Flask, request, render_template, jsonify app = Flask(__name__) def get_db(): db = sqlite3.connect('mydb.db') db.row_factory = sqlite3.Row return db def query_db(query, args=(), one=False): db = get_db() cur = db.execute(query, args) db.commit() rv = cur.fetchall() db.close() return (rv[0] if rv else None) if one else rv @app.route("/", methods=["GET"]) def index(): return render_template("index.html") @app.route("/weather", methods=["POST"]) def weather(): if request.method == "POST": res = query_db("SELECT * FROM weather") return jsonify(month = [x[0] for x in res], evaporation = [x[1] for x in res], precipitation = [x[2] for x in res]) if __name__ == "__main__": app.run(debug=True)
ECharts3 開始再也不強制使用 AMD 的方式按需引入,代碼裏也再也不內置 AMD 加載器。所以引入方式簡單了不少,只須要像普通的 JavaScript 庫同樣用 script 標籤引入。ajax
ECharts3 中實現異步數據的更新很是簡單,在圖表初始化後無論任什麼時候候只要經過 jQuery 等工具異步獲取數據後經過 setOption 填入數據和配置項就行。sql
ECharts3 中在更新數據的時候須要經過name屬性對應到相應的系列,上面示例中若是name不存在也能夠根據系列的順序正常更新,可是更多時候推薦更新數據的時候加上系列的name數據。json
index.html文件以下:flask
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>ECharts3 Ajax</title> <script src="{{ url_for('static', filename='jquery-3.1.1.js') }}"></script> <script src="{{ url_for('static', filename='echarts.js') }}"></script> </head> <body> <!--爲ECharts準備一個具有大小(寬高)的Dom--> <div id="main" style="height:500px;border:1px solid #ccc;padding:10px;"></div> <script type="text/javascript"> var myChart = echarts.init(document.getElementById('main')); // 顯示標題,圖例和空的座標軸 myChart.setOption({ title: { text: '異步數據加載示例' }, tooltip: {}, legend: { data:['蒸發量','降水量'] }, xAxis: { data: [] }, yAxis: {}, series: [{ name: '蒸發量', type: 'bar', data: [] },{ name: '降水量', type: 'line', data: [] }] }); myChart.showLoading(); // 顯示加載動畫 // 異步加載數據 $.get('/weather').done(function (data) { myChart.hideLoading(); // 隱藏加載動畫 // 填入數據 myChart.setOption({ xAxis: { data: data.month }, series: [{ name: '蒸發量', // 根據名字對應到相應的系列 data: data.evaporation.map(parseFloat) // 轉化爲數字(注意map) },{ name: '降水量', data: data.precipitation.map(parseFloat) }] }); }); </script> </body> </html>