Results 1 to 1 of 1

Thread: How not to display QDateTimeAxis data on weekends and holidays(PyQt5)

  1. #1
    Join Date
    Aug 2019
    Posts
    15
    Thanks
    7
    Qt products
    Platforms
    Windows

    Default How not to display QDateTimeAxis data on weekends and holidays(PyQt5)

    I managed to sort out the axes. But I am not satisfied that QDateTimeAxis creates data on the x-axis, where there is no data on the y-axis.
    In C# and matplotlib, I was able to solve this problem by resetting the axis labels to string format.
    Here is an example of how this is done in matplotlib https://matplotlib.org/3.2.1/gallery...x-formatter-py

    I don't know how to do this in pyqt5(Qt), and I can't find any examples.
    Data " file.txt" are located here https://drive.google.com/file/d/1nzf...3CxUuyMSN/view

    Below I attach a screenshot of what these empty values look like
    123.jpg

    Qt Code:
    1. from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
    2. from PyQt5.QtCore import *
    3. from PyQt5.QtChart import *
    4. import math
    5. import numpy as np
    6. import pandas as pd
    7.  
    8. df = pd.read_csv('file.txt',
    9. index_col='DATE',
    10. parse_dates=True,
    11. infer_datetime_format=True)
    12.  
    13. date = df.iloc[:, 0].index.date
    14. o = df.iloc[:, 0].values
    15. h = df.iloc[:, 1].values
    16. l = df.iloc[:, 2].values
    17. z = df.iloc[:, 3].values
    18. x = len(z)
    19. x_ = x - 1
    20.  
    21. qt = [None] * x
    22.  
    23. for i in range(0, x):
    24. qt[i] = QDateTime(date[i]).toMSecsSinceEpoch()
    25.  
    26. class MainWindow(QtWidgets.QMainWindow):
    27. def __init__(self, parent=None):
    28. super().__init__(parent)
    29.  
    30. self.step = 30
    31. self._chart_view = QtChart.QChartView()
    32. self.scrollbar = QtWidgets.QScrollBar(
    33. QtCore.Qt.Horizontal,
    34. sliderMoved=self.onAxisSliderMoved,
    35. pageStep=self.step,
    36. )
    37.  
    38. self.scrollbar.setRange(0, x_)
    39.  
    40. central_widget = QtWidgets.QWidget()
    41. self.setCentralWidget(central_widget)
    42.  
    43. lay = QtWidgets.QVBoxLayout(central_widget)
    44. for w in (self._chart_view, self.scrollbar):
    45. lay.addWidget(w)
    46.  
    47. self._chart = QtChart.QChart()
    48. self._candlestick_serie = QtChart.QCandlestickSeries()
    49.  
    50. for i in range(0, len(z)):
    51. o_ = o[i]
    52. h_ = h[i]
    53. l_ = l[i]
    54. c_ = z[i]
    55. tm = float(qt[i])
    56. self._candlestick_serie.append(QtChart.QCandlestickSet(o_, h_, l_, c_, tm))
    57.  
    58.  
    59. min_x, max_x = 0, x_
    60.  
    61. self._chart.addSeries(self._candlestick_serie)
    62. self._chart.legend().hide()
    63.  
    64. self._chart_view.setChart(self._chart)
    65. self.lims = np.array([min_x, max_x])
    66.  
    67. axisX = QDateTimeAxis()
    68. axisX.setTickCount(5)
    69. axisX.setFormat("yyyy-MM-dd")
    70. self._chart.addAxis(axisX, Qt.AlignBottom)
    71. self._candlestick_serie.attachAxis(axisX)
    72.  
    73. axisY = QValueAxis()
    74. self._chart.addAxis(axisY, Qt.AlignLeft)
    75. self._candlestick_serie.attachAxis(axisY)
    76.  
    77.  
    78. self.onAxisSliderMoved(self.scrollbar.value())
    79. self.adjust_axes(1, 31)
    80.  
    81. def adjust_axes(self, value_min, value_max):
    82. if value_min > 0 and value_max > 0 and value_max <= x_ and value_max > value_min:
    83. self._chart.axisX(self._candlestick_serie).setRange(QDateTime(date[value_min]), QDateTime(date[value_max]))
    84.  
    85.  
    86. @QtCore.pyqtSlot(int)
    87. def onAxisSliderMoved(self, value):
    88. value2 = value + self.step
    89. value1 = value
    90. if value2 >= x_:
    91. value2 = x_
    92. value1 = value2 - self.step
    93. self.adjust_axes(math.floor(value1), math.ceil(value2))
    94.  
    95.  
    96. if __name__ == "__main__":
    97. import sys
    98.  
    99. app = QtWidgets.QApplication(sys.argv)
    100. w = MainWindow()
    101. w.show()
    102. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    Last edited by quant; 16th June 2020 at 11:42.

Similar Threads

  1. QDateTimeAxis
    By bandito in forum Qt Programming
    Replies: 0
    Last Post: 9th October 2016, 14:47
  2. Display Data from C++ to QML
    By Binary01 in forum Qt Quick
    Replies: 7
    Last Post: 19th April 2016, 18:37
  3. How to change weekends red color in QCalendarWidget?
    By Dimon in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2011, 11:50
  4. Display DIB data
    By nightroad in forum Qt Programming
    Replies: 2
    Last Post: 29th December 2010, 22:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.