在QChart QCandlestickSeries中的axisX中显示重复(非唯一)类别标签

2024-10-01 17:34:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为QChart-QCandlestickSeries对象设置axisX的类别标签;但是,它只接受唯一的值。我想将axisX的类别标签设置为每个月的第一个字母。我也完全了解图表的轴类别(QBarCategoryAxis)必须是有效的QString,并且不能重复(https://doc.qt.io/qt-5/qbarcategoryaxis.html#setCategories)。是否仍有方法将图表显示为所需的输出

下面是python程序的参考

import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtChart import *

class Window(QMainWindow):

    def __init__(self):
        super().__init__()

        self.data = (("J",15.28,15.54,15.22,15.26),("F",15.3,15.56,15.3,15.42),("M",15.7,15.98,15.4,15.4),("A",15.58,15.6,15.24,15.34),("M",15.3,15.34,15.06,15.1),("J",15.3,15.48,14.72,14.78),("J",14.82,15,14.6,14.62),("A",14.76,15.1,14.76,15),("S",15.1,15.58,15.1,15.4),("O",15.5,15.66,15.48,15.66),("N",15.66,16.14,15.58,15.58),("D",15.58,15.96,15.52,15.88))
        self.chart = QChart()
        self.series = QCandlestickSeries()

        color = QColor()
        color.setRgb(229,57, 53)
        self.series.setDecreasingColor(color)

        color.setRgb(76,175, 80)
        self.series.setIncreasingColor(color)

        self.series.setBodyOutlineVisible(False)

        self.date_labels = []
        self.resize(640, 480)

        for month, o, h, l, c in self.data:
            self.series.append(QCandlestickSet(o, h, l, c))
            self.date_labels.append(str(month))

        self.chart.addSeries(self.series)
        self.chart.createDefaultAxes()
        self.chart.legend().hide()
        self.chart.axisX(self.series).setCategories(self.date_labels) #It only accepts unique category labels though

        self.chartview = QChartView(self.chart)
        self.chartview.setChart(self.chart)
        self.chartview.setRubberBand(QChartView.HorizontalRubberBand); #Right click to zoom out. To specify the zooming area, create a rubber band around the data points you wish to zoom. 

        central_widget = QtWidgets.QWidget()        
        lay = QtWidgets.QVBoxLayout(central_widget)
        lay.addWidget(self.chartview)

        self.setCentralWidget(central_widget)

        self.show()


app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())

程序的输出:

期望输出:


Tags: fromimportselfdatadatelabelssyschart

热门问题