-
Notifications
You must be signed in to change notification settings - Fork 2
/
gui_action.py
161 lines (149 loc) · 6.25 KB
/
gui_action.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# coding=utf-8
# pip --trusted-host pypi.python.org install numpy
# pip --trusted-host pypi.python.org install matplotlib
# pyuic4.bat -x -o gui.py gui.ui
# pyinstaller.exe --windowed gui_action.py
import gui
import sys
import os
import myutil
import ChemistSpider
from ChemistDatabase import ChemistDatabase
from ChemistServer import LOG_FILE
from PyQt4.QtGui import QApplication, QMainWindow
import matplotlib.dates as mdates
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)
class GuiAction(QMainWindow, gui.Ui_MainWindow):
def __init__(self, ):
super(self.__class__, self).__init__()
self.setupUi(self)
# Input Area
self.pushButtonOpen.clicked.connect(self.open_logging)
self.pushButtonClear.clicked.connect(self.clear_diagram)
self.comboBoxProduct.currentIndexChanged.connect(self.input_changed)
self.comboBoxProduct.setCurrentIndex(0)
self.categories = ["Search"]
for url in ChemistSpider.CATEGORIES:
self.categories.append("category " + url.split('/')[-1])
self.comboBoxProduct.addItems(self.categories)
self.list_len = len(self.categories)
# 加载logging文件
# def read_logging(self):
# with open(LOG_FILE) as f:
# for line in f:
# color = 'red' if "ERROR" in line else 'grey'
# val = "<font color={}>{}</font>".format(color, line)
# self.textBrowserLog.moveCursor(QTextCursor.End)
# self.textBrowserLog.append(val)
# self.textBrowserLog.moveCursor(QTextCursor.End)
# self.textBrowserLog.append("")
def open_logging(self):
os.startfile(LOG_FILE)
def clear_diagram(self):
for i in reversed(range(self.verticalLayoutDraw.count())):
self.verticalLayoutDraw.takeAt(i).widget().setParent(None)
def input_changed(self):
if self.comboBoxProduct.currentIndex() <= 0:
pass
elif self.comboBoxProduct.currentIndex() < self.list_len:
# get product, show diagram
name = myutil.trim_str(
str(self.comboBoxProduct.currentText().toUtf8()))
if "category " in name:
db = ChemistDatabase()
db.openDatabase()
totals = db.findProduct("category-total " + name.split()[-1])
counts = db.findProduct("category-count " + name.split()[-1])
db.closeDatabase()
self.creatDiagramCategory(totals, counts)
else:
db = ChemistDatabase()
db.openDatabase()
product = db.findProduct(name)
db.closeDatabase()
self.creatDiagramProduct(product)
else:
# search product
search = myutil.trim_str(
str(self.comboBoxProduct.currentText().toUtf8()))
if search == "":
self.comboBoxProduct.clear()
self.comboBoxProduct.addItems(self.categories)
self.list_len = len(self.categories)
else:
db = ChemistDatabase()
db.openDatabase()
names = db.searchName(search)
db.closeDatabase()
self.comboBoxProduct.clear()
self.comboBoxProduct.addItem("Select or Search")
self.comboBoxProduct.addItems(names)
self.list_len = len(names) + 1
def creatDiagramCategory(self, totals, counts):
# basic setting
fig = Figure()
axis = fig.add_subplot(111)
canvas = FigureCanvas(fig)
self.verticalLayoutDraw.addWidget(canvas)
toolbar = NavigationToolbar(canvas, self.widgetDraw)
self.verticalLayoutDraw.addWidget(toolbar)
# cook data
name = "category " + str(totals[1][0]).split()[-1]
x = totals[2]
t_sale = totals[3]
t_save = totals[4]
c_sale = counts[3]
c_save = counts[4]
price_ratio = [round(t_sale[i] / (t_sale[i] + t_save[i]), 2) for i in range(len(x))]
count_ratio = [round(c_save[i] / c_sale[i], 2) for i in range(len(x))]
# 设置x轴为日期
try:
date_range = (x[-1] - x[1]).days
except:
date_range = 0
axis.xaxis.set_major_locator(
mdates.DayLocator(interval=(1 + date_range // 12)))
axis.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d %a"))
axis.set_title(name)
y1, = axis.plot(x, count_ratio, 'xg-', label='line1')
y2, = axis.plot(x, price_ratio, '+r-', label='line2')
axis.legend([y1, y2], ['save/total count', 'sale/total $'], loc=1)
fig.autofmt_xdate()
def creatDiagramProduct(self, product):
# basic setting
fig = Figure()
axis = fig.add_subplot(111)
canvas = FigureCanvas(fig)
self.verticalLayoutDraw.addWidget(canvas)
toolbar = NavigationToolbar(canvas, self.widgetDraw)
self.verticalLayoutDraw.addWidget(toolbar)
# cook data
name = str(product[1][0])
# x = [datetime.datetime.strptime(d, '%Y-%m-%d').date() for d in product[2]]
x = product[2]
sale = product[3]
save = product[4]
total = [sale[i] + save[i] for i in range(len(x))]
# 设置x轴为日期
try:
date_range = (x[-1] - x[1]).days
except:
date_range = 0
axis.xaxis.set_major_locator(
mdates.DayLocator(interval=(1 + date_range // 12)))
axis.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d %a"))
axis.set_title(name)
y1, = axis.plot(x, total, '+k--', label='line1')
y2, = axis.plot(x, sale, 'or-', label='line2')
y3, = axis.plot(x, save, 'xg-', label='line3')
axis.legend([y1, y2, y3], ['total', 'sale', 'save'], loc=4)
fig.autofmt_xdate()
if __name__ == '__main__':
# myutil.logging_init()
app = QApplication(sys.argv)
gui_action = GuiAction()
gui_action.show()
sys.exit(app.exec_())