Insert QChartView to ui

后端 未结 1 1928
闹比i
闹比i 2021-01-14 18:38

I am trying to put plot candlestick and 5-days average line on a same qtchart but give two x axis plot code into a UI loader

import sys
from PyQt5.QtWidgets          


        
1条回答
  •  轮回少年
    2021-01-14 19:10

    It is not necessary to promote a class, although it is a valid option, so that it can be displayed in a window generated based on a .ui since you can use empty QWidget (without promoting it) as a container, then place a layout, and within the layout the QChartView.

    The image shows an empty QWidget whose name is "chart_container" where the QChartView will be placed:

    
    
     MainWindow
     
      
       
        0
        0
        800
        600
       
      
      
       MainWindow
      
      
       
        
         
          210
          280
          88
          33
         
        
        
         PushButton
        
       
       
        
         
          340
          80
          88
          33
         
        
        
         Test Trade
        
       
       
        
         
          29
          29
          291
          231
         
        
       
      
      
       
        
         0
         0
         800
         24
        
       
      
      
     
     
     
    
    

    The code is then implemented by adding the QChartView to the container through a layout:

    import sys
    
    from PyQt5 import QtCore, QtWidgets, uic, QtChart
    
    # load both ui file
    uifile_1 = "UI/main.ui"
    form_1, base_1 = uic.loadUiType(uifile_1)
    
    
    class Example(base_1, form_1):
        def __init__(self):
            super(base_1, self).__init__()
            self.setupUi(self)
    
            data = (
                (1, 7380, 7520, 7380, 7510, 7324),
                (2, 7520, 7580, 7410, 7440, 7372),
                (3, 7440, 7650, 7310, 7520, 7434),
                (4, 7450, 7640, 7450, 7550, 7480),
                (5, 7510, 7590, 7460, 7490, 7502),
                (6, 7500, 7590, 7480, 7560, 7512),
                (7, 7560, 7830, 7540, 7800, 7584),
            )
    
            series = QtChart.QCandlestickSeries()
            series.setDecreasingColor(QtCore.Qt.red)
            series.setIncreasingColor(QtCore.Qt.green)
    
            ma5 = QtChart.QLineSeries()  # 5-days average data line
            tm = []  # stores str type data
    
            # in a loop,  series and ma5 append corresponding data
            for num, o, h, l, c, m in data:
                series.append(QtChart.QCandlestickSet(o, h, l, c))
                ma5.append(QtCore.QPointF(num, m))
                tm.append(str(num))
    
            chart = QtChart.QChart()
            chart.addSeries(series)  # candle
            chart.addSeries(ma5)  # ma5 line
    
            chart.setAnimationOptions(QtChart.QChart.SeriesAnimations)
            chart.createDefaultAxes()
            chart.legend().hide()
    
            chart.axisX(series).setCategories(tm)
            chart.axisX(ma5).setVisible(False)
    
            chartview = QtChart.QChartView(chart)
    
            self.chart_container.setContentsMargins(0, 0, 0, 0)
            lay = QtWidgets.QHBoxLayout(self.chart_container)
            lay.setContentsMargins(0, 0, 0, 0)
            lay.addWidget(chartview)
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        ex = Example()
        ex.show()
        sys.exit(app.exec_())

    0 讨论(0)
提交回复
热议问题