淘先锋技术网

首页 1 2 3 4 5 6 7

(硬件传入数据部分可以查看我上篇内容:(39条消息) esp32获取云平台天气数据并上传mysql_前后端交互头秃7天患者的博客-CSDN博客

1.数据库配置

数据库bs

表名 hykqzljc

2.数据库查询最新一条数据

from pymysql import Connection

conn = Connection(
    host="localhost",
    port=3306,
    user="root",
    password="123456",
    autocommit=True
)
# 获取游标对象
cursor = conn.cursor()
# 选择数据库
conn.select_db("bs")

sql = f"select * from hykqzljc where Num = (select max(Num) from hykqzljc)"
cursor.execute(sql)
result = cursor.fetchall()
print(result)

 运行结果显示如下3.Pyecharts显示

参考:

自定义图表 - pyecharts - A Python Echarts Plotting Library built with love.

(39条消息) Pyecharts基本图:仪表盘_pyecharts仪表盘图_晶晶家的小可爱的博客-CSDN博客

通过对查询到的数据进行处理,展示出温度仪表盘

from pymysql import Connection
import pyecharts.options as opts
from pyecharts.charts import Gauge
from pyecharts.globals import CurrentConfig
CurrentConfig.ONLINE_HOST = ""#改成自己的cdn地址

conn = Connection(
    host="localhost",
    port=3306,
    user="root",
    password="123456",
    autocommit=True
)
# 获取游标对象
cursor = conn.cursor()
# 选择数据库
conn.select_db("bs")

sql = f"select Tem from hykqzljc where Num = (select max(Num) from hykqzljc)"
cursor.execute(sql)
result = cursor.fetchall()
Tem = result.__str__()
Tem = Tem[2:5]
Tem = Tem.split(",")
print(Tem[0])

(
    Gauge(init_opts=opts.InitOpts(width="1200px", height="600px"))
        .add("温度测量",
             data_pair=[["温度", Tem[0]]],
             min_=-10,
             max_=50,
             split_number=6,  # 仪表盘平均分割段数
             axisline_opts=opts.AxisLineOpts(
                 linestyle_opts=opts.LineStyleOpts(
                     color=[(0.167, "#67e0e3"), (0.833, "#37a2da"), (1, "#fd666d")], width=30
                 )
             ),
             )
        .set_global_opts(
        title_opts=opts.TitleOpts(title="温度仪表盘"),
        legend_opts=opts.LegendOpts(is_show=False),  # 是否显示说明
        tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{a} <br/>{b} : {c}℃"),  # 提示框设定
    )
        .render("温度.html")
)

显示效果可以通过生成的 “温度.html” 查看

点击浏览器标识 

建议:1)有同学可能会发现,页面加载慢的情况,可以通过更改cdn地址来解决,即加上以下代码

from pyecharts.globals import CurrentConfig
CurrentConfig.ONLINE_HOST = ""#改成自己的cdn地址

  2)仪表盘中可能会一直存在一个%,这并不是温度的单位,通过修改gauge.py文件,来去掉%。

ctrl+鼠标左键跳转 

去掉该位置的%

(参考:(39条消息) python-pyecharts 仪表盘,去除百分号_peychart做仪表盘中百分数没有百分号_flying_birds的博客-CSDN博客) 

4.程序定时执行

只实现以上功能或许会显得过于LOW,通过定时执行程序和刷新html页面实现动态效果。

(参考:绝了!Python定时爬取微博热搜+pyecharts动态图展示 - 腾讯云开发者社区-腾讯云 (tencent.com)

 自动刷新的程序代码如下

import schedule
from pymysql import Connection
import pyecharts.options as opts
from pyecharts.charts import Gauge
from pyecharts.globals import CurrentConfig
CurrentConfig.ONLINE_HOST = ""#改成自己的cdn地址

conn = Connection(
    host="localhost",
    port=3306,
    user="root",
    password="123456",
    autocommit=True
)
# 获取游标对象
cursor = conn.cursor()
# 选择数据库
conn.select_db("bs")

def run():
    sql = f"select Tem from hykqzljc where Num = (select max(Num) from hykqzljc)"
    cursor.execute(sql)
    result = cursor.fetchall()
    Tem = result.__str__()
    Tem = Tem[2:5]
    Tem = Tem.split(",")
    print(Tem[0])

    (
        Gauge(init_opts=opts.InitOpts(width="1200px", height="600px"))
            .add("温度测量",
                 data_pair=[["温度", Tem[0]]],
                 min_=-10,
                 max_=50,
                 split_number=6,  # 仪表盘平均分割段数
                 axisline_opts=opts.AxisLineOpts(
                     linestyle_opts=opts.LineStyleOpts(
                         color=[(0.167, "#67e0e3"), (0.833, "#37a2da"), (1, "#fd666d")], width=30
                     )
                 ),
                 )
            .set_global_opts(
            title_opts=opts.TitleOpts(title="温度仪表盘"),
            legend_opts=opts.LegendOpts(is_show=False),  # 是否显示说明
            tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{a} <br/>{b} : {c}℃"),  # 提示框设定
        )
            .render("温度.html")
    )

schedule.every(1).minutes.do(run)
while True:
    schedule.run_pending()  # run_pending:运行所有可以运行的任务

5.刷新html页面

使用插件

以上,便实现了“动态”的效果。