Flask 框架:运用Echarts绘制图形( 二 )

后端代码如下,路由曾则只保留一个index映射
from flask import Flask,render_template,requestimport jsonapp = Flask(import_name=__name__,static_url_path='/python',# 配置静态文件的访问url前缀static_folder='static',# 配置静态文件的文件夹template_folder='templates') # 配置模板文件的文件夹def Count_Flag_And_Type(file):list = []flag = {}with open(file) as f:contexts = f.readlines()for line in contexts:addr = line.split()[0].replace("(","").replace(")","")if addr != "::1":list.append(addr)# 去重并将其转为字典list_num = set(list)for item in list_num:num = list.count(item)# 如果地址只有一次则忽略if num > 1:flag[item] = numreturn flag@app.route('/', methods=["GET"])def index():Types = {'Linux': 23, 'studies': 57, 'Windows': 87, 'compatible': 44, 'web': 32, 'X11': 78}# Types = Count_Flag_And_Type("d://access_log")return render_template("index.html",data = https://www.huyubaike.com/biancheng/json.dumps(Types))if __name__ =='__main__':app.run(host="127.0.0.1", port=80, debug=False)柱状图绘制效果如下:

Flask 框架:运用Echarts绘制图形

文章插图
实现绘制折线图: 统计指定的时间段内的访问流量数据.
前端index.html代码如下
<html> <head><meta charset="UTF-8"><title>LyShark</title><script src="http://shimg.jingyanzongjie.com/230725/031T53O7-0.jpg"></script><script src="http://shimg.jingyanzongjie.com/230725/031T533F-1.jpg"></script> </head> <body><div class="panel panel-primary" style="width: 100%;height: 30%; float: left"><div class="panel-heading"><h3 class="panel-title">LyShark 网站流量统计</h3></div><div class="panel-body"><div id="main" style="width:100%; height: 400px"></div></div></div> </body> <script type="text/javascript" charset="UTF-8">var kv = new Array();var keys = new Array();var values = new Array();kv = {{ data | safe }};for(var logkey in kv){keys.push(logkey);values.push(kv[logkey]);}var display = function() {var main = echarts.init(document.getElementById("main"));var option = {xAxis: {type: 'category',boundaryGap: false,data: keys},yAxis: {type: 'value'},series: [{data: values,type: 'line',areaStyle: {},}]};main.setOption(option,true);};display(); </script></html>后端代码如下,路由曾则只保留一个index映射
from flask import Flask,render_template,requestimport jsonapp = Flask(import_name=__name__,static_url_path='/python',# 配置静态文件的访问url前缀static_folder='static',# 配置静态文件的文件夹template_folder='templates') # 配置模板文件的文件夹def Count_Time_And_Flow(file):times = {}# key 保存当前时间信息flow = {}# value 当前时间流量总和Count= 0# 针对IP地址的计数器with open(file) as f:contexts = f.readlines()for line in contexts:if line.split()[9] != "-" and line.split()[9] != '"-"':size = line.split()[9]temp = line.split()[3]ip_attr = temp.split(":")[1] + ":" + temp.split(":")[2]Count = int(size) + Countif ip_attr in times.keys():flow[ip_attr] = flow[ip_attr] + int(size)else:times[ip_attr] = 1flow[ip_attr] = int(size)return flow@app.route('/', methods=["GET"])def index():OutFlow = {'03:30': 12, '03:48': 25, '04:15': 47, '04:28': 89, '04:42': 66, '04:51': 54}# OutFlow = Count_Time_And_Flow("d://access_log")return render_template("index.html",data = https://www.huyubaike.com/biancheng/json.dumps(OutFlow))if __name__ =='__main__':app.run(host="127.0.0.1", port=80, debug=False)折现图绘制效果如下:
Flask 框架:运用Echarts绘制图形

文章插图
如上是三种常用图形的绘制方式 , 其他图形同理可以参考如上方代码中的写法 , 我们可以将这三个图形合并在一起 , 主要是前端对其进行排版即可 。

经验总结扩展阅读