I use the scroll bar for the chart. When adding a second and subsequent graph, the bars do not scroll through the new graphs.
And when deleting new charts, an error occurs when trying to scroll.
I understand that need to have separate access to each chart, but I don't know how to do it?
I assume that need to have access to "self._chart.axis" to all graphs, via an array.
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_())
The problem is similar to your previous post: If you use the same variable to reference several objects then the variable only refers to the last object, therefore when deleting that object and wanting to delete the returned object the application crashes. The solution is to use the splitter to iterate over the QChartView, and using that information get the QChart and 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.