对量具进行图表分析,了解量具的使用情况. 建立文件 analysisclass.py
# -*- coding: utf-8 -*- import os import sqlite3 import tkinter.ttk from tkinter import * import datetime from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg # 创建画布需要的库 from matplotlib.figure import Figure # 导入绘图需要的模块 import matplotlib import re from tkinter.messagebox import * import tkinter.font import baseclass import framebaseclass def get_fieldvalue_from_db(table_name,field): #字段值 conn=sqlite3.connect("database.db") c=conn.cursor() sql="SELECT %s FROM '%s' "%( field, table_name) c.execute(sql) li=c.fetchall() conn.commit() conn.close() return li def count_repeat(value): dict={} for i in value: condi=dict.setdefault(i,0) if condi: dict[i ]=dict[i ]+1 else: dict[i]=1 return dict class analysis(framebaseclass.baseFrame): def __init__(self,master,user,framename): self.root=master self.user=user self.framename=framename super( ).__init__(self.root,self.user,self.framename) self.showchart() def showchart(self): cv =Canvas(self.root, background='white') cv.place(relx=0.01,rely=0.05,relwidth=0.97,relheight=0.8) f=Figure(figsize=(4,4),dpi=70, tight_layout=True, facecolor="WhiteSmoke") matplotlib.rcParams['font.sans-serif']=['SimHei'] matplotlib.rcParams['axes.unicode_minus']=False value=get_fieldvalue_from_db("instruments_info","Description描述")#图一 data=count_repeat(value) #print(data) instrument_kind=[] times=[] for i in data.items(): CN = ''.join(re.findall(r'[\u4e00-\u9fa5]+', i[0][0])) instrument_kind.append(CN) times.append(i[1]) a=f.add_subplot(2,1,1,title="柱状图分析_量具数量",xlabel="量具种类",ylabel="数量") y=times x= instrument_kind a.bar(x=x, height=y,alpha=0.5) for x1, yy in zip(x, y): a.text(x1, yy + 1, str(yy), ha='center', va='top', fontsize=10, rotation=0) value_status=get_fieldvalue_from_db("instruments_info","Status状态")#图二 data_status=count_repeat(value_status) status_kind=[] status_times=[] for i in data_status.items(): status_kind.append(i[0][0]) status_times.append(i[1]) try: a=f.add_subplot(2,2,3,title="饼图_量具状态") y=status_times x= status_kind explode = (0.1,0,0,0) a.pie(y,explode=explode,labels=x,autopct='%1.1f%%',shadow=False,startangle=150) except Exception as e: print(e) a=f.add_subplot(2,2,4,title="柱状图分析_量具状态",xlabel="状态种类",ylabel="数量") y=status_times x= status_kind a.bar(x=x, height=y,alpha=0.5) for x1, yy in zip(x, y): a.text(x1, yy + 1, str(yy), ha='center', va='top', fontsize=10, rotation=0) canvas=FigureCanvasTkAgg(f,master=cv) # 创建画布控件 canvas.draw() canvas.get_tk_widget().pack(side=tkinter.TOP,fill=tkinter.BOTH,expand=1) # 显示画布控件 if __name__ == '__main__': root = Tk() analysis(root,"usertest","frame") root.mainloop()