我使用图表的滚动条。添加第二个及后续图形时,条形图不会在新图形中滚动。
当删除新图表时,在尝试滚动时会出现错误。
我知道需要单独访问每个图表,但我不知道该怎么做?
我认为需要访问";自我_chart.axis";通过一个数组将所有的图。
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
import math
mas =[1.33, 1.15, 1.55, 1.65, 1.64, 1.91, 1.33, 2.3, 1.5, 1.35, 2.52, 1.77, 1.7, 1.87, 2.0, 1.55, 1.73, 2.1,
1.33, 1.15, 1.55, 1.92, 1.64, 1.91, 1.33, 1.71, 1.5, 1.35, 1.22, 1.77, 1.7, 1.87, 2.7, 1.55, 1.73, 2.1,
1.33, 1.15, 1.55, 1.92, 1.64, 1.91, 1.33, 1.71, 1.5, 1.35, 1.22, 1.77, 1.7, 1.87, 2.0, 1.55, 1.73, 2.1]
x = len(mas)
x_ = x - 1
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, start = 1, parent=None):
self.start = start
super().__init__(parent)
self.step = 30
self.scrollbar = QtWidgets.QScrollBar(
QtCore.Qt.Horizontal,
sliderMoved=self.onAxisSliderMoved,
pageStep=self.step,
)
self.scrollbar.setRange(0, x_)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
self.open = QtWidgets.QPushButton()
self.open.setText("Open")
self.open.clicked.connect(self.open_clicked)
self.close = QtWidgets.QPushButton()
self.close.setText("Close")
self.close.clicked.connect(self.close_clicked)
self.hbox = QtWidgets.QHBoxLayout()
self.hbox.addWidget(self.open)
self.hbox.addWidget(self.close)
self.splitter = QtWidgets.QSplitter(QtCore.Qt.Vertical)
self.lay = QtWidgets.QVBoxLayout(central_widget)
self.lay.insertLayout(0, self.hbox)
self.lay.addWidget(self.splitter, stretch=1)
self.lay.addWidget(self.scrollbar)
self.open_clicked()
def open_clicked(self):
self._chart_view = QtChart.QChartView()
self._chart = QtChart.QChart()
self._line_serie = QtChart.QLineSeries()
for i in range(0, len(mas)):
self._line_serie.append(QtCore.QPointF(i, mas[i]))
self._chart.addSeries(self._line_serie)
self._chart.createDefaultAxes()
self._chart_view.setChart(self._chart)
self.splitter.addWidget(self._chart_view)
def close_clicked(self):
count = self.splitter.count()
if count > 1:
w = self.splitter.widget(count - 1)
if w is not None:
w.deleteLater()
def adjust_axes(self, value_min, value_max):
if value_min >= 0 and value_max >= 0 and value_max <= x_ and value_max > value_min:
self._chart.axisX(self._line_serie).setRange(value_min, value_max)
@QtCore.pyqtSlot(int)
def onAxisSliderMoved(self, value):
value2 = value + self.step
value1 = value
if value2 >= x_:
value2 = x_
value1 = value2 - self.step
self.adjust_axes(math.floor(value1), math.ceil(value2))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow(start = 0)
w.show()
sys.exit(app.exec_())
问题与您之前的文章类似:如果使用同一个变量引用多个对象,那么该变量只引用最后一个对象,因此当删除该对象并想要删除返回的对象时,应用程序会崩溃。解决方案是使用拆分器对QChartView进行迭代,并使用该信息获得QChart和QLineSeries:
def open_clicked(self):
chart_view = QtChart.QChartView()
chart = QtChart.QChart()
line_serie = QtChart.QLineSeries()
for i, value in enumerate(mas):
line_serie.append(QtCore.QPointF(i, value))
chart.addSeries(line_serie)
chart.createDefaultAxes()
chart_view.setChart(chart)
self.splitter.addWidget(chart_view)
def adjust_axes(self, value_min, value_max):
for i in range(self.splitter.count()):
chart_view = self.splitter.widget(i)
if isinstance(chart_view, QtChart.QChartView):
chart = chart_view.chart()
for serie in chart.series():
chart.axisX(serie).setRange(value_min, value_max)