graph.py 2.52 KB
import sys
from PyQt5.QtWidgets import *
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

from sqlalchemy import create_engine
from library.cf import *

class Graph(QDialog):
    def __init__(self,num=1):
        QDialog.__init__(self,None)
        self.setGeometry(200,200,1000,800)

        self.engine1 = create_engine("mysql+pymysql://" + db_id + ":" + db_pw + "@" +
                                        db_ip + ":" + db_port + "/simul1", encoding='utf-8')
        self.engine2 = create_engine("mysql+pymysql://" + db_id + ":" + db_pw + "@" +
                                        db_ip + ":" + db_port + "/simul2", encoding='utf-8')
        self.engine3 = create_engine("mysql+pymysql://" + db_id + ":" + db_pw + "@" +
                                        db_ip + ":" + db_port + "/simul3", encoding='utf-8')
        self.engine4 = create_engine("mysql+pymysql://" + db_id + ":" + db_pw + "@" +
                                        db_ip + ":" + db_port + "/simul4", encoding='utf-8')

        date,data=self.getAlgorithm(num)

        col=""
        if num==1:
            col="red"
        elif num==2:
            col="blue"
        elif num==3:
            col="green"
        elif num==4:
            col="purple"

        N = len(data)
        value = data
        ind = np.arange(N)
        width = 0.35

        self.fig = plt.Figure()
        ax = self.fig.add_subplot(111)
        ax.bar(ind, value, width,color=col)
        ax.set_xticks(ind + width / 20)
        ax.set_xticklabels(date)

        canvas = FigureCanvas(self.fig)
        canvas.draw()

        lay=QVBoxLayout()
        self.setLayout(lay)
        lay.addWidget(canvas)
        canvas.show()


    def getdata(self,engine):
        query="select date, sum_valuation_profit from jango_data order by date"
        result=engine.execute(query).fetchall()
        date=list()
        data=list()
        for i in range(len(result)):
            date.append(result[i][0])
            data.append(int(result[i][1]))
        return date,data

    def getAlgorithm(self,num):
        if num==1:
            engine=self.engine1
        elif num==2:
            engine=self.engine2
        elif num==3:
            engine=self.engine3
        elif num==4:
            engine=self.engine4
        else:
            sys.exit()
        date,data=self.getdata(engine)
        return date,data

if __name__=="__main__":
    app=QApplication(sys.argv)
    main_dialog=Graph()
    main_dialog.show()
    app.exec_()