コード例 #1
0
import random

# example qtcharts/examples/zoomlinechart tranlated in python
if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)

    # [1]
    series = QLineSeries()
    for i in range(500):
        p = QPointF(i, 100 * math.sin(math.pi/50 * i) + random.randint(0, 20))
        series.append(p)
    # [1]

    chart = Chart()
    chart.addSeries(series)
    chart.setTitle("Zoom in/out example")
    chart.setAnimationOptions(QChart.SeriesAnimations)
    chart.legend().hide()
    chart.createDefaultAxes()

    chartView = ChartView(chart)
    chartView.setRenderHint(QPainter.Antialiasing)

    window = QMainWindow()

    window.setCentralWidget(chartView)
    window.resize(400, 300)
    window.grabGesture(Qt.PanGesture)
    window.grabGesture(Qt.PinchGesture)
    window.show()
コード例 #2
0
class Navigation(QtCore.QObject):
    clicked = QtCore.pyqtSignal(QtCore.QModelIndex)

    def __init__(self, chartData, navData, parent=None):
        super(Navigation, self).__init__(parent)
        print('navdata', navData)
        self.toolbar = QtWidgets.QToolBar()
        self.toolbar.actionTriggered.connect(self.on_actionTriggered)
        self.model = QtGui.QStandardItemModel(self)
        dict_to_model(self.model.invisibleRootItem(), navData)
        it = self.model.item(0, 0)
        ix = self.model.indexFromItem(it)
        root_action = self.toolbar.addAction(it.text())
        root_action.setData(QtCore.QPersistentModelIndex(ix))
        self.listview = QtWidgets.QListView()
        self.listview.setEditTriggers(
            QtWidgets.QAbstractItemView.NoEditTriggers)
        self.listview.clicked.connect(self.on_clicked)
        self.listview.setModel(self.model)
        self.listview.setRootIndex(ix)

        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")

        self.listview.setFixedWidth(350)

        self.gridLayout.addWidget(self.listview, 0, 0, 2, 1)

        self.frame = QtWidgets.QFrame()
        self.chart = Chart('map_file', chartData, self)

        self.ly = QtWidgets.QVBoxLayout()
        self.frame.setLayout(self.ly)

        self.gridLayout1 = QtWidgets.QGridLayout()
        self.gridLayout1.addWidget(self.frame)

        self.gridLayout2 = QtWidgets.QGridLayout()
        self.gridLayout2.addWidget(self.chart.chartview)  #calling chart view

        self.gridLayout.addLayout(self.gridLayout2, 0, 2, 0, 1)
        self.gridLayout.addLayout(self.gridLayout1, 0, 2, 0, 1)

        self.horizontalGroupBox = QGroupBox()
        self.horizontalGroupBox.setLayout(self.gridLayout)

    #make the listed items clickable
    @QtCore.pyqtSlot(QtCore.QModelIndex)
    def on_clicked(self, index):
        if not self.model.hasChildren(index):
            self.clicked.emit(index)
            return
        action = self.toolbar.addAction(index.data())
        action.setData(QtCore.QPersistentModelIndex(index))
        self.listview.setRootIndex(index)
        self.chart.addSeries(index.data())

    #make the breadcrumbs clickable in order to go back and forth
    #no children for object files, hence it is not listed in the nav bar
    @QtCore.pyqtSlot(QtWidgets.QAction)
    def on_actionTriggered(self, action):
        ix = action.data()
        self.chart.addSeries(ix.data())
        model = ix.model()
        self.listview.setRootIndex(QtCore.QModelIndex(ix))
        self.toolbar.clear()
        ixs = []
        while ix.isValid():
            ixs.append(ix)
            ix = ix.parent()
        for ix in reversed(ixs):
            action = self.toolbar.addAction(ix.data())
            action.setData(ix)