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
|
import sys
from PySide6 import QtCharts, QtCore
from PySide6.QtGui import QPixmap, QPainter, Qt
from PySide6.QtSql import QSqlQuery
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtCharts import QChart, QChartView, QPieSeries, QLineSeries, QBarCategoryAxis, QValueAxis
from uis.ui_main import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.graph_lbl.setScaledContents(True)
self.conn = Connexion()
self.req2_btn.clicked.connect(self.nb_chiffre)
# Date du jour par défaut pour DateEnd
self.dateEnd.setDate(QtCore.QDate.currentDate())
def nb_chiffre(self):
query = QSqlQuery(f"""
SELECT DATE_FORMAT(d.date, '%m/%y') as 'Date',
CASE WHEN dispatch ='andre' THEN count(c.id) else 0 END as "andre", dvi
CASE WHEN dispatch ='paul' THEN count(c.id) else 0 END as "paul", dmp
CASE WHEN dispatch ='marc' THEN count(c.id) else 0 END as "marc" coprod
FROM consumes c
RIGHT JOIN datas d on c.datas_id = d.id
WHERE d.date between '{self.dateBegin.date().toString(format=Qt.ISODateWithMs)}' and
'{self.dateEnd.date().toString(format=Qt.ISODateWithMs)}'
GROUP BY DATE_FORMAT(d.date, '%y/%m')
""")
rec = query.record()
print(rec.count())
self.series_date = []
self.series_andre = QLineSeries()
self.series_andre.setName("andre")
self.series_marc = QLineSeries()
self.series_marc.setName("marc")
self.series_paul = QLineSeries()
self.series_paul.setName("paul")
#Ajout des valeurs dans les series
i=0
while query.next():
self.series_date.append(query.value(rec.indexOf("Date")))
self.series_andre.append(QtCore.QPointF(i, query.value(1)))
self.series_paul.append(QtCore.QPointF(i, query.value(2)))
self.series_marc.append(QtCore.QPointF(i, query.value(3)))
i+= 1
self.chart = QChart()
self.chart.setTheme(QChart.ChartThemeDark)
self.chart.addSeries(self.series_andre)
self.chart.addSeries(self.series_paul)
self.chart.addSeries(self.series_marc)
# Valeurs des abscisses
self.axis_x = QBarCategoryAxis()
self.axis_x.append(self.series_date)
self.axis_x.setLabelsAngle(45)
self.axis_x.setGridLineVisible(False)
self.chart.setAxisX(self.axis_x, self.series_andre)
self.chart.setAxisX(self.axis_x, self.series_paul)
self.chart.setAxisX(self.axis_x, self.series_marc)
# Valeurs des ordonnées
self.axis_y = QValueAxis()
self.chart.setAxisY(self.axis_y, self.series_andre)
self.chart.setAxisY(self.axis_y, self.series_paul)
self.chart.setAxisY(self.axis_y, self.series_marc)
self.chart.legend().setVisible(True)
# Construction de la chartView
self.graph = Show_graph(self.chart)
self.graph.show()
class Show_graph(QMainWindow):
def __init__(self, chart):
super().__init__()
self.resize(1000, 800)
self.chart = chart
self._chart_view = QChartView(self.chart)
self._chart_view.setRenderHint(QPainter.Antialiasing)
self._chart_view.resize(1000, 800)
self.vb = QVBoxLayout()
self.saveBtn = QPushButton('Enregistrer')
self.vb.addWidget(self._chart_view)
self.vb.addWidget(self.saveBtn)
self.fram = QFrame()
self.fram.setLayout(self.vb)
self.setCentralWidget(self.fram)
class Connexion(QWidget):
def __init__(self):
super().__init__()
self.db = self.connexion()
def connexion(self):
db = QSqlDatabase.addDatabase("QMYSQL")
db.setHostName("localhost")
db.setDatabaseName("market")
db.setUserName("root")
db.setPassword("")
if not db.open():
print(db.lastError().text())
sys.exit()
return db
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = MainWindow()
ui.show()
sys.exit(app.exec()) |
Partager