ZoomAndScrollDemo Example
import sys, os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../shared")
from DevMachines import __pyside2__, __pyside6__
from DevMachines.QtitanBase import Qtitan
from DevMachines.QtitanChart import (Chart, ChartSeries2D, ChartLineSeries2D, ChartSplineAreaSeries2D, ChartBarSeries2D, ChartTitle)
if __pyside2__:
from PySide2 import QtCore
from PySide2.QtCore import Qt
from PySide2.QtGui import QBrush, QFont, QCursor
from PySide2.QtWidgets import (QApplication, QComboBox)
if __pyside6__:
from PySide6 import QtCore
from PySide6.QtCore import Qt
from PySide6.QtGui import QBrush, QFont, QCursor
from PySide6.QtWidgets import (QApplication, QComboBox)
from DemoChartWindow import DemoChartWindow
class MainWindow(DemoChartWindow):
Line = 0
Area = 1
Column = 2
def __init__(self):
DemoChartWindow.__init__(self, "Zoom Scroll Charts")
self.setChart(None)
self.createSeriesParametrs()
self.seriesChanged(self.seriesSwitcher.currentIndex())
self.updateValueParameters()
def createSeriesParametrs(self):
seriesTypeGroup = self.createGroupParameters(self.tr("Series"))
localLayout = seriesTypeGroup.layout()
self.seriesSwitcher = QComboBox(seriesTypeGroup)
self.seriesSwitcher.addItem("Line", MainWindow.Line)
self.seriesSwitcher.addItem("Area", MainWindow.Area)
self.seriesSwitcher.addItem("Column", MainWindow.Column)
self.seriesSwitcher.setCurrentIndex(0)
self.connect(self.seriesSwitcher, QtCore.SIGNAL("currentIndexChanged(int)"), self, QtCore.SLOT("seriesChanged(int)"))
localLayout.addRow(self.seriesSwitcher)
optionsGroup = self.createGroupParameters(self.tr("Chart Options:"))
localOptionsLayout = optionsGroup.layout()
self.dataPointsCountSwitcher = QComboBox(optionsGroup)
self.dataPointsCountSwitcher.addItem("500", 500)
self.dataPointsCountSwitcher.addItem("1000", 1000)
self.dataPointsCountSwitcher.addItem("1500", 1500)
self.dataPointsCountSwitcher.addItem("2000", 2000)
self.dataPointsCountSwitcher.setCurrentIndex(0)
self.connect(self.dataPointsCountSwitcher, QtCore.SIGNAL("currentIndexChanged(int)"), self, QtCore.SLOT("pointsCountChanged(int)"))
localOptionsLayout.addRow("Data Points Count:", self.dataPointsCountSwitcher)
subTitle = ChartTitle()
self.chart.appendTitle(subTitle)
subTitle.setText(self.tr("To zoom chart use mouse wheel. If you want to pan, use the left mouse button."))
subTitle.setDockPosition(ChartTitle.BottomDock)
subTitle.setAlignment(Qt.AlignLeft)
subTitle.setFont(QFont("Tahoma", 8))
subTitle.setTextColor(Qt.gray)
def updateValueParameters(self):
DemoChartWindow.updateValueParameters(self)
def seriesChanged(self, index):
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
# self.chart.setCursor(QCursor(Qt.WaitCursor))
self.chart.clearSeries()
self.chart.clearViews()
self.chart.setBackgroundBrush(QBrush())
series = None
if index == MainWindow.Line:
series = ChartLineSeries2D()
elif index == MainWindow.Area:
series = ChartSplineAreaSeries2D()
else:
series = ChartBarSeries2D()
self.chart.appendSeries(series)
series.label().setVisible(False)
view2D = self.chart.views()[0]
series.setXScaleType(ChartSeries2D.ScaleNumerical)
view2D.axisX().setViewAutoRange(False)
view2D.axisX().setFixedViewRange(0, 100)
if series.inherits("Qtitan::ChartPointSeries"):
series.setMarkerSize(5)
series.setMarkerVisible(False)
view2D.setZoomEnabled(True)
view2D.setScrollBarEnabled(True)
self.pointsCountChanged(self.dataPointsCountSwitcher.currentIndex())
self.updateValueParameters()
# self.chart.unsetCursor()
QApplication.restoreOverrideCursor()
def pointsCountChanged(self, index):
if len(self.chart.series()) == 0:
return
series = self.chart.series()[0]
if series.inherits("Qtitan::ChartSeries2D"):
series.clear()
v = self.dataPointsCountSwitcher.itemData(index)
batchCount = int(v)
# Fill series data
maxVal = 0.0
val = 0.0
for pointIndex in range(0, batchCount):
if val > 250:
delta = self.getRandomValue(-250, 250)
else:
delta = self.getRandomValue(0, 500)
val = val + delta
series.addXY(pointIndex, val)
maxVal = max(maxVal, val)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())