ECharts(Enterprise Charts)是一个基于 JavaScript 的开源可视化库,由百度开发和维护。它支持各种图表类型,包括折线图、柱状图、饼图、散点图等,以及支持地理坐标系、时间轴、数据筛选等丰富的交互特性。ECharts的设计注重性能和扩展性,适用于在 Web 页面中创建各种交互式和动态的数据可视化。
该开发库目前发展非常不错,且支持各类图形的绘制可定制程度高,Echarts绘图库同样可以与Flask
结合,前台使用echart
绘图库进行图形的生成与展示,后台则是Flask通过render_template
方法返回一串JSON数据集,前台收到后将其应用到绘图库上,实现动态展示功能。
饼状图(Pie Chart)是一种常见的数据可视化图表类型,用于展示数据集中各部分之间的比例关系。它以圆形的方式将整体分割为多个扇形,每个扇形的面积(或角度)表示相应数据部分在整体中的占比。饼状图通常用于展示数据的相对份额,以便直观地比较各部分的大小。
模拟统计Web容器的日志数据,通过饼状图将访问状态统计出来。
前端部分/templates/index.html
代码如下:
<html>
<head>
<meta charset="UTF-8">
<title>LySharktitle>
<script src="https://www.lyshark.com/javascript/jquery/3.5.1/jquery.min.js">script>
<script src="https://www.lyshark.com/javascript/echarts/5.0.0/echarts.min.js">script>
head>
<body>
<div class="panel panel-primary" style="width: 40%;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: 300px">div>
div>
div>
body>
<script type="text/javascript" charset="UTF-8">
var kv = new Array();
kv = {{ data | safe }}
var test = new Array();
for(var logkey in kv){
test.push( {value:kv[logkey], name:logkey} )
}
var display = function(){
var main = echarts.init(document.getElementById("main"));
var option = {
legend: {
orient: 'vertical',
left: 'left',
},
series: [
{
type: 'pie',
radius: '70%',
center: ['50%', '50%'],
detail: {formatter:'{value}'},
data: test
}
]
};
main.setOption(option,true);
};
display();
script>
html>
后端代码如下通过模拟render_template
返回一些数据。
from flask import Flask,render_template,request
import json
app = Flask(import_name=__name__,
static_url_path='/python', # 配置静态文件的访问url前缀
static_folder='static', # 配置静态文件的文件夹
template_folder='templates') # 配置模板文件的文件夹
def Count_Flag_And_Flow(file):
list = []
flag = {}
with open(file) as f:
contexts = f.readlines()
for line in contexts:
it = line.split()[8]
list.append(it)
list_num = set(list)
for item in list_num:
num = list.count(item)
flag[item] = num
return flag
@app.route('/', methods=["GET"])
def index():
Address = {'226': 4, '404': 12, '200': 159, '400': 25, '102': 117, '302': 1625}
# Address = Count_Flag_And_Flow("d://access_log")
return render_template("index.html",data = json.dumps(Address))
if __name__ == '__main__':
app.run(host="127.0.0.1", port=80, debug=False)
运行后访问自定义域名,输出如下效果的饼状图:
柱状图(Bar Chart)是一种用矩形(柱形)表示数据的图表类型,用于展示不同类别的数据之间的数量或数值关系。每个矩形的高度(或长度)代表相应数据的数值,而矩形的宽度通常是相同的,表示各类别之间的独立性。
统计访问了本站的所有ID地址并将地址数大于2的全部显示出来.
前端index.html
代码如下
<html>
<head>
<meta charset="UTF-8">
<title>LySharktitle>
<script src="https://www.lyshark.com/javascript/jquery/3.5.1/jquery.min.js">script>
<script src="https://www.lyshark.com/javascript/echarts/5.0.0/echarts.min.js">script>
head>
<body>
<div class="panel panel-primary" style="width: 58%;height: 30%; float: left">
<div class="panel-heading">
<h3 class="panel-title">LyShark 网站设备类型统计h3>
div>
<div class="panel-body">
<div id="main1" style="width:100%; height: 300px">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 main1 = echarts.init(document.getElementById("main1"));
var option = {
xAxis: {
type: 'category',
data: keys
},
yAxis: {
type: 'value'
},
series: [{
data: values,
type: 'bar'
}]
};
main1.setOption(option,true);
};
display();
script>
html>
后端代码如下,路由曾则只保留一个index映射
from flask import Flask,render_template,request
import json
app = 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] = num
return 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 = json.dumps(Types))
if __name__ == '__main__':
app.run(host="127.0.0.1", port=80, debug=False)
柱状图绘制效果如下:
折线图(Line Chart)是一种用折线连接各数据点的图表类型,用于显示数据随着连续或有序类别的变化而变化的趋势。折线图的横轴通常表示类别或连续的数据点,纵轴表示相应的数值。
统计指定的时间段内的访问流量数据.
前端index.html
代码如下
<html>
<head>
<meta charset="UTF-8">
<title>LySharktitle>
<script src="https://www.lyshark.com/javascript/jquery/3.5.1/jquery.min.js">script>
<script src="https://www.lyshark.com/javascript/echarts/5.0.0/echarts.min.js">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,request
import json
app = 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) + Count
if ip_attr in times.keys():
flow[ip_attr] = flow[ip_attr] + int(size)
else:
times[ip_attr] = 1
flow[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 = json.dumps(OutFlow))
if __name__ == '__main__':
app.run(host="127.0.0.1", port=80, debug=False)
折现图绘制效果如下:
如上是三种常用图形的绘制方式,其他图形同理可以参考如上方代码中的写法,我们可以将这三个图形合并在一起,主要是前端对其进行排版即可。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://www.lyshark.com/javascript/bootstrap/3.3.7/css/bootstrap.min.css">
<script type="text/javascript" src="https://www.lyshark.com/javascript/jquery/3.5.1/jquery.min.js">script>
<script type="text/javascript" src="https://www.lyshark.com/javascript/echarts/5.0.0/echarts.min.js">script>
head>
<body>
<div class="panel panel-primary" style="width: 40%;height: 30%;float: left">
<div class="panel-heading">
<h3 class="panel-title">饼状图绘制h3>
div>
<div class="panel-body">
<div id="PieChart" style="width:100%; height: 300px">div>
div>
div>
<div class="panel panel-primary" style="width: 58%;height: 30%; float: right">
<div class="panel-heading">
<h3 class="panel-title">柱状图绘制h3>
div>
<div class="panel-body">
<div id="HistogramChart" style="width:100%; height: 300px">div>
div>
div>
<div class="panel panel-primary" style="width: 100%;height: 40%; float: left">
<div class="panel-heading">
<h3 class="panel-title">折线图绘制h3>
div>
<div class="panel-body">
<div id="Linechart" style="width:100%; height: 460px">div>
div>
div>
<script type="text/javascript" charset="UTF-8">
var kv = new Array();
kv = {{ Address | safe }}
var test = new Array();
for(var logkey in kv){
test.push( {value:kv[logkey], name:logkey} )
}
var display = function(){
var echo = echarts.init(document.getElementById("PieChart"));
var option = {
legend: {
orient: 'vertical',
left: 'left',
},
series: [
{
type: 'pie',
radius: '70%',
center: ['50%', '50%'],
detail: {formatter:'{value}'},
data: test
}
]
};
echo.setOption(option,true);
};
display();
script>
<script type="text/javascript" charset="UTF-8">
var kv = new Array();
var keys = new Array();
var values = new Array();
kv = {{ Types | safe }}
for(var logkey in kv){
keys.push(logkey);
values.push(kv[logkey]);
}
var display = function() {
var echo = echarts.init(document.getElementById("HistogramChart"));
var option = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'category',
data: keys
},
yAxis: {
type: 'value'
},
series: [{
data: values,
type: 'bar'
}]
};
echo.setOption(option,true);
};
display();
script>
<script type="text/javascript" charset="UTF-8">
// 函数主要用于将传入的字典分解成key,value格式并返回
var get_key_value = function(kv)
{
var keys = new Array();
var values = new Array();
for(var logkey in kv)
{
keys.push(logkey);
values.push(kv[logkey]);
}
return [keys,values];
}
// 输出1分钟负载
var kv = new Array();
kv = {{ x | safe }};
var x = get_key_value(kv);
// 输出5分钟负载
var kv = new Array();
kv = {{ y | safe }};
var y = get_key_value(kv);
// 输出15分钟负载
var kv = new Array();
kv = {{ z | safe }};
var z = get_key_value(kv);
// 显示利用率
var display = function() {
var echo = echarts.init(document.getElementById("Linechart"));
var option = {
title: {
left: 'left',
text: 'CPU 利用表',
},
// 调节大小
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
// tooltip 鼠标放上去之后会自动出现坐标
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#6a7985'
}
}
},
legend: {
data: ['1分钟负载', '5分钟负载', '15分钟负载']
},
xAxis: {
type: 'category',
// data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
data: x[0]
},
yAxis: {
type: 'value'
},
series:
[
{
name: "1分钟负载",
stack: "总量",
//data: [10, 25, 99, 87, 54, 66, 2],
data: x[1],
type: 'line'
},
{
name: "5分钟负载",
stack: "总量",
//data: [89, 57, 85, 44, 25, 4, 54],
data: y[1],
type: 'line'
},
{
name: "15分钟负载",
stack: "总量",
//data: [1, 43, 2, 12, 5, 4, 7],
data: z[1],
type: 'line'
}
]
};
echo.setOption(option,true);
};
display();
script>
body>
后端代码如下,其中的参数可以从数据库内提取也可以从文件中读入。
from flask import Flask,render_template,request
import json
app = Flask(import_name=__name__,
static_url_path='/python', # 配置静态文件的访问url前缀
static_folder='static', # 配置静态文件的文件夹
template_folder='templates') # 配置模板文件的文件夹
@app.route('/', methods=["GET"])
def index():
Address = {'226': 4, '404': 12, '200': 159, '400': 25, '102': 117, '302': 1625}
Types = {'Linux': 23, 'studies': 57, 'Windows': 87, 'compatible': 44, 'web': 32, 'X11': 78}
x = {'03:30': 12, '03:48': 25, '04:15': 47, '04:28': 89, '04:42': 66, '04:51': 54}
y = {'05:22': 55, '07:48': 29, '07:15': 98, '08:54': 11, '08:41': 61, '06:51': 5}
z = {'07:30': 1, '09:48': 5, '06:15': 24, '08:28': 59, '2:42': 11, '08:51': 22}
return render_template("index.html",Address = json.dumps(Address), Types= json.dumps(Types), x = json.dumps(x), y = json.dumps(y), z = json.dumps(z))
if __name__ == '__main__':
app.run(host="127.0.0.1", port=80, debug=False)
输出效果如下: