2

i trying to use a date or a time in the X axe with a pyqtgraph

i try the solution in this question How can I use DateAxisItem of PyQtGraph? but nothing work for me

this is my table

CREATE TABLE IF NOT EXISTS transactions (
            id SERIAL PRIMARY KEY  UNIQUE NOT NULL,
            montant DECIMAL(100,2), 
            medecin VARCHAR,
            date_d DATE, 
            time_d TIME,
            users_id INTEGER, 
            FOREIGN KEY(users_id) REFERENCES users(id))

and this is the ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton_113">
    <property name="geometry">
     <rect>
      <x>550</x>
      <y>72</y>
      <width>121</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>Recherche</string>
    </property>
   </widget>
   <widget class="QDateEdit" name="dateEdit_19">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>70</y>
      <width>471</width>
      <height>41</height>
     </rect>
    </property>
    <property name="dateTime">
     <datetime>
      <hour>0</hour>
      <minute>0</minute>
      <second>0</second>
      <year>2020</year>
      <month>1</month>
      <day>1</day>
     </datetime>
    </property>
    <property name="calendarPopup">
     <bool>true</bool>
    </property>
   </widget>
   <widget class="PlotWidget" name="graphicsView_2">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>130</y>
      <width>691</width>
      <height>391</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>PlotWidget</class>
   <extends>QGraphicsView</extends>
   <header location="global">pyqtgraph</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

and this is the last thnig i try to do

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import pandas as pd
from pandas import *
from datetime import datetime 

import psycopg2

from admin import Ui_MainWindow as ui

class TimeAxisItem(pg.AxisItem):
    def tickStrings(self, values, scale, spacing):
        return [datetime.fromtimestamp(value) for value in values]

class MainApp(QMainWindow, ui):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.Handel_Buttons()

    def Handel_Buttons(self):
        self.pushButton_113.clicked.connect(self.draw_graph_all)

    def draw_graph_all(self): #pushButton_113
            self.connection = psycopg2.connect(user="postgres",
                                            password="password",
                                            host="localhost",
                                            database="database")
            self.cur = self.connection.cursor()


            self.cur.execute( '''SELECT date_d, SUM(montant) FROM transactions GROUP BY date_d ''')
            rows = self.cur.fetchall()

            date_axis = TimeAxisItem(orientation='bottom')
            self.graphicsView_2 = pg.PlotWidget(axisItems = {'bottom': date_axis})

            date = []
            montant = []

            for row in rows:
                date.append(row[0])
                montant.append(row[1])

            #[x.timestamp() for x in date]
            self.graphicsView_2.plot(x=pd.to_datetime(date), y=montant, pen=None, symbol='o')
            self.setCentralWidget(self.graphicsView_2)
            self.graphicsView_2.show()
            

Update

after a several attemps the best that i can get is the graph show me the Y axe but there is no X axe like the image

enter image description here

is there another way to fix this

houhou
  • 157
  • 1
  • 11

0 Answers0