import schedule
from pymysql import Connection
import pyecharts.options as opts
from pyecharts.charts import Gauge,Bar,Page,Line,Liquid
from pyecharts.components import Table
conn = Connection(
host="localhost",
port=3306,
user="root",
password="123456",
autocommit=True
)
# 获取游标对象
cursor = conn.cursor()
# 选择数据库
conn.select_db("bs")
def run():
sql = f"select UpdateTime from heweather order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result = reversed(result)
data = ""
for i in result:
UpdateTime = i.__str__()
UpdateTime = UpdateTime[2:17]
data+=UpdateTime
UpdateTime = data.replace("+",",")
UpdateTime = UpdateTime.strip(",")
print(UpdateTime)
UpdateTimelist = UpdateTime.split(',')
sql = f"select Weather from heweather where Num = (select max(Num) from heweather)"
cursor.execute(sql)
result=cursor.fetchall()
Weather = result.__str__()
Weather = Weather.split("'")
print(Weather[1])
sql = f"select WindDir from heweather where Num = (select max(Num) from heweather)"
cursor.execute(sql)
result=cursor.fetchall()
WindDir = result.__str__()
WindDir = WindDir.split("'")
print(WindDir[1])
sql = f"select WindScale from heweather order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result = reversed(result) # 反转
windscale = ""
for i in result:
WindScale = i.__str__()
WindScale = WindScale[1:3]
WindScale = WindScale.split(")")
WindScale = WindScale[0]
windscale += WindScale
windscale = windscale.strip(",")
print(windscale)
windscale = windscale.split(',')
sql = f"select Precip from heweather order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result=reversed(result)#反转
precip=""
for i in result:
Precip = i.__str__()
Precip = Precip[1:5]
Precip = Precip.split(")")
Precip =Precip[0]
precip += Precip
precip = precip.strip(",")
print(precip)
precip = precip.split(',')
sql = f"select Pressure from heweather order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result=reversed(result)#反转
pressure=""
for i in result:
Pressure = i.__str__()
Pressure = Pressure[1:7]
Pressure = Pressure.split(")")
Pressure =Pressure[0]
pressure += Pressure
pressure = pressure.strip(",")
print(pressure)
pressure = pressure.split(',')
sql = f"select Tem from receive order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result=reversed(result)#反转
tem=""
for i in result:
Tem = i.__str__()
Tem = Tem[1:5]
Tem = Tem.split(")")
Tem =Tem[0]
tem += Tem
tem = tem.strip(",")
print(tem)
temlist = tem.split(',')
sql = f"select Hum from receive order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result=reversed(result)#反转
hum=""
for i in result:
Hum = i.__str__()
Hum = Hum[1:5]
Hum = Hum.split(")")
Hum =Hum[0]
hum += Hum
hum = hum.strip(",")
print(hum)
humlist = hum.split(',')
sql = f"select UV from receive order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result=reversed(result)#反转
uv=""
for i in result:
UV = i.__str__()
UV = UV[1:5]
UV = UV.split(")")
UV =UV[0]
uv += UV
uv = uv.strip(",")
print(uv)
uv = uv.split(',')
sql = f"select PM from receive order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result=reversed(result)#反转
pm=""
for i in result:
PM = i.__str__()
PM = PM[1:8]
PM = PM.split(")")
PM =PM[0]
pm += PM
pm = pm.strip(",")
print(pm)
pm = pm.split(',')
x_data = [UpdateTimelist[0], UpdateTimelist[1], UpdateTimelist[2], UpdateTimelist[3], UpdateTimelist[4],
UpdateTimelist[5], UpdateTimelist[6], UpdateTimelist[7], UpdateTimelist[8], UpdateTimelist[9],
UpdateTimelist[10], UpdateTimelist[11]
]
y_data1 = [windscale[0], windscale[1], windscale[2], windscale[3], windscale[4],
windscale[5], windscale[6], windscale[7], windscale[8], windscale[9],
windscale[10], windscale[11]
]
y_data2 = [precip[0],precip[1],precip[2],precip[3],precip[4],
precip[5],precip[6],precip[7],precip[8],precip[9],
precip[10],precip[11]
]
y_data3=[pressure[0],pressure[1],pressure[2],pressure[3],pressure[4],
pressure[5],pressure[6],pressure[7],pressure[8],pressure[9],
pressure[10],pressure[11]
]
y_data4=[temlist[0],temlist[1],temlist[2],temlist[3],temlist[4],
temlist[5],temlist[6],temlist[7],temlist[8],temlist[9],
temlist[10],temlist[11]
]
y_data5=[humlist[0],humlist[1],humlist[2],humlist[3],humlist[4],
humlist[5],humlist[6],humlist[7],humlist[8],humlist[9],
humlist[10],humlist[11]
]
y_data6=[uv[0],uv[1],uv[2],uv[3],uv[4],
uv[5],uv[6],uv[7],uv[8],uv[9],
uv[10],uv[11]
]
y_data7=[pm[0],pm[1],pm[2],pm[3],pm[4],
pm[5],pm[6],pm[7],pm[8],pm[9],
pm[10],pm[11]
]
attr = [x_data[11]]
v1 = [y_data1[11]]
v2 = [y_data2[11]]
v3 = [y_data3[11]]
v4 = [y_data6[11]]
v5 = [y_data7[11]]
def table_base() -> Table:
table = Table()
table.chart_id="0b661b17c86444b4b197426083dd2285"
headers = ["获取项","数据"]
rows = [
["UpdateTime",UpdateTimelist[11]],
["Weather",Weather[1]],
["WindDir",WindDir[1]],
["WindScale",windscale[11]]
]
table.add(headers, rows)
return table
def bar_datazoom_slider() -> Bar:
bar = Bar()
bar.chart_id="f9a9d6f1ad0748beb1cac2488fdf378a"
bar.add_xaxis(attr)
bar.add_yaxis("雨量", v2)
bar.add_yaxis("气压", v3)
bar.add_yaxis("UV", v4)
bar.add_yaxis("PM2.5", v5)
bar.set_global_opts(
title_opts=opts.TitleOpts(title="基于物联网的户外空气监测系统"),
)
return bar
def line_markpoint() -> Line:
line = Line()
line.chart_id="ae0c0ade615a4a79a86fa899ebed7544"
line.set_global_opts(
# tooltip_opts=opts.TooltipOpts(is_show=True,trigger="axis"),
legend_opts=opts.LegendOpts(is_show=True),
toolbox_opts=opts.ToolboxOpts(is_show=True,pos_left = "96%"),
xaxis_opts=opts.AxisOpts(type_="category", axislabel_opts={"rotate": 30}),
yaxis_opts=opts.AxisOpts(
type_="value",
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
line.add_xaxis(xaxis_data=x_data)
line.add_yaxis(
series_name="风力等级折线",
y_axis=y_data1,
symbol="emptyCircle",
color='orange',
is_symbol_show=True,
label_opts=opts.LabelOpts(is_show=False),
)
line.add_yaxis(
series_name="雨量折线",
y_axis=y_data2,
symbol="emptyCircle",
color='red',
is_symbol_show=True,
label_opts=opts.LabelOpts(is_show=False),
)
line.add_yaxis(
series_name="气压折线",
y_axis=y_data3,