20 KiB
20 KiB
In [ ]:
import pymysql
import pyecharts
from datetime import datetime
db = pymysql.connect("localhost","songyi","yylzs","mydata" )
cursor = db.cursor()
item_id =1
per_id = 1
list_date = []
dict_tar = {}
dict_det = {}
dict_date = {}
## 获取项目信息
sql = "select a.id,a.name,a.unit from sports_target as a,sports_item_target as b where b.item_id =%s and a.id =b.target_id"
cursor.execute(sql, (item_id))
results = cursor.fetchall()
for result in results:
dict_tar[result[0]] = [result[1],result[2]]
## 获取运动记录信息
sql = "select id,re_date from sports_record where item_id =%s and person_id =%s"
cursor.execute(sql, (item_id,per_id))
results = cursor.fetchall()
for result in results:
list_date.append(result[0])
dict_date[result[0]] = result[1].strftime("%Y-%m-%d")
s_date = ''
sql = "select rec_id_id,target_id_id,value from sports_detail where rec_id_id in {}".format(tuple(list_date))
cursor.execute(sql)
results = cursor.fetchall()
for s in list_date:
dict_det[dict_date[s]] = {}
for result in results:
s = result[0]
dict_det[dict_date[s]][result[1]] = result[2]
#print(dict_det)
In [ ]:
#柱状、曲线组合
from pyecharts.globals import CurrentConfig, NotebookType
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
from pyecharts import options as opts
from pyecharts.charts import Bar,Line
import pyecharts.options as opts
x_data = []
y_data1 = []
y_data2 = []
y_data3 = []
for key,value in dict_det.items():
x_data.append(key)
y_data1.append(value[8])
y_data2.append(value[9])
y_data3.append(value[2])
bar = (
Bar()
.add_xaxis(x_data)
.add_yaxis("平均心率", y_data1,label_opts=opts.LabelOpts(is_show=False),gap="0%")
.add_yaxis("最大心率", y_data2,label_opts=opts.LabelOpts(is_show=False),gap="0%")
.extend_axis(
yaxis=opts.AxisOpts(
name="运动时间",
type_="value",
min_=min(y_data3),
max_=max(y_data3),
interval=int((max(y_data3)-min(y_data3))/4),
axislabel_opts=opts.LabelOpts(formatter="{value} 分"),
)
)
.set_global_opts(
tooltip_opts=opts.TooltipOpts(
is_show=True, trigger="axis", axis_pointer_type="cross"
),
xaxis_opts=opts.AxisOpts(
type_="category",
axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"),
),
yaxis_opts=opts.AxisOpts(
name="心率",
type_="value",
min_=100,
max_=180,
interval=20,
axislabel_opts=opts.LabelOpts(formatter="{value} 次/分钟"),
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="运动心率", subtitle="户外运动"),)
)
line = (
Line()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="运动时间",
yaxis_index=1,
y_axis=y_data3,
label_opts=opts.LabelOpts(is_show=False),
)
)
bar.overlap(line).load_javascript()In [ ]:
bar.overlap(line).render_notebook()In [ ]:
In [ ]:
#Grid - Grid_vertical多图组合
from pyecharts.globals import CurrentConfig, NotebookType
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid
import pyecharts.options as opts
x_data = []
y_data1 = []
y_data2 = []
y_data3 = []
y_data4 = []
for key,value in dict_det.items():
x_data.append(key)
y_data1.append(value[8])
y_data2.append(value[9])
y_data3.append(value[6])
y_data4.append(value[5])
bar1 = (
Bar()
.add_xaxis(x_data)
.add_yaxis("平均心率", y_data1,label_opts=opts.LabelOpts(is_show=False),gap="0%")
.add_yaxis("最大心率", y_data2,label_opts=opts.LabelOpts(is_show=False),gap="0%")
.set_global_opts(
tooltip_opts=opts.TooltipOpts(
is_show=True, trigger="axis", axis_pointer_type="cross"
),
xaxis_opts=opts.AxisOpts(
type_="category",
axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"),
),
yaxis_opts=opts.AxisOpts(
name="心率",
type_="value",
min_=100,
max_=180,
interval=20,
axislabel_opts=opts.LabelOpts(formatter="{value} 次/分钟"),
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="运动心率", subtitle="户外运动"),)
)
bar2 = (
Bar()
.add_xaxis(x_data)
.add_yaxis("平均步幅", y_data3,label_opts=opts.LabelOpts(is_show=False),gap="0%")
.add_yaxis("平均步频", y_data4,label_opts=opts.LabelOpts(is_show=False),gap="0%")
.set_global_opts(
tooltip_opts=opts.TooltipOpts(
is_show=True, trigger="axis", axis_pointer_type="cross"
),
xaxis_opts=opts.AxisOpts(
type_="category",
axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"),
),
yaxis_opts=opts.AxisOpts(
name="心率",
type_="value",
min_=40,
max_=180,
interval=20,
axislabel_opts=opts.LabelOpts(formatter="{value} "),
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="运动步频", pos_top="48%"),
legend_opts=opts.LegendOpts(pos_top="48%"),
)
)
grid = (
Grid()
.add(bar1, grid_opts=opts.GridOpts(pos_bottom="60%"))
.add(bar2, grid_opts=opts.GridOpts(pos_top="60%"))
)
grid.load_javascript()
In [ ]:
grid.render_notebook()In [45]:
from pyecharts import options as opts
from pyecharts.globals import CurrentConfig, NotebookType
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType
from pyecharts.faker import Faker
c = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE))
# 等价于 Bar(init_opts=opts.InitOpts(theme=ThemeType.WHITE))
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.add_yaxis("商家C", Faker.values())
.add_yaxis("商家D", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts("Theme-default"))
)
c.load_javascript()Out [45]:
<pyecharts.render.display.Javascript at 0x7fc0c5da81f0>
In [46]:
c.render_notebook()Out [46]:
In [ ]: