QCustomPlot2-python数据选择,框选和点选
2022-12-13 本文已影响0人
lk_erzanml
import sys
import math
import PyQt5
import time
from PyQt5.QtCore import Qt,QMargins,QDateTime,QTimer
from PyQt5.QtGui import QPen, QBrush, QColor,QFont,QMouseEvent
from PyQt5.QtWidgets import QApplication, QMainWindow
from QCustomPlot2 import * #先导入PyQt5,不然报错
app = QApplication(sys.argv)
window = QMainWindow()
window.resize(800, 600)
customPlot = QCustomPlot() #相当于pyqt5的一个控件
window.setCentralWidget(customPlot)
customPlot.legend.setVisible(True)
customPlot.legend.setFont(QFont("Helvetica", 9))
lineNames =[ "lsNone" , "lsLine" , "lsStepLeft" , "lsStepRight" , "lsStepCenter" , "lsImpulse"]
for i in range(QCPGraph.lsNone,QCPGraph.lsImpulse):
customPlot.addGraph()
customPlot.graph().setName(lineNames[i-QCPGraph.lsNone])
customPlot.graph().setLineStyle(i)
customPlot.graph().setScatterStyle(QCPScatterStyle(QCPScatterStyle.ssCircle, 5)) #设置图表散点图的样式
x,y=[],[]
for j in range(15):
x.append(j/15.0 * 5*3.14 + 0.01)
y.append(7*math.sin(x[j])/x[j] - (i-QCPGraph.lsNone)*5 + (QCPGraph.lsImpulse)*5 + 2)
customPlot.graph().setData(x, y)
customPlot.graph().rescaleAxes(True)
customPlot.yAxis.scaleRange(1.1, customPlot.yAxis.range().center())
customPlot.xAxis.scaleRange(1.1, customPlot.xAxis.range().center())
customPlot.xAxis.setTicks(True)
customPlot.yAxis.setTicks(True)
customPlot.xAxis.setTickLabels(True)
customPlot.yAxis.setTickLabels(True)
customPlot.axisRect().setupFullAxesBox()
customPlot.setInteractions(
QCP.iSelectAxes | QCP.iSelectLegend | QCP.iSelectPlottables | QCP.iMultiSelect) #轴、图例、图表可以被选择,并且是多选的方式
customPlot.setSelectionRectMode(QCP.srmSelect) #鼠标框选
customPlot.setMultiSelectModifier(Qt.ControlModifier) #使用ctrl键来多选
customPlot.xAxis.setSelectableParts(
QCPAxis.spAxis | QCPAxis.spAxisLabel | QCPAxis.spTickLabels) #轴的三个部分都可以被选择
customPlot.yAxis.setSelectableParts(QCPAxis.spAxis | QCPAxis.spAxisLabel | QCPAxis.spTickLabels)
customPlot.xAxis.setLabel("xAxis")
customPlot.yAxis.setLabel("yAxis")
customPlot.legend.setSelectableParts(QCPLegend.spItems) #图例本身不能被选择,只有里面的项可以被选择
customPlot.legend.setSelectedIconBorderPen(QPen(Qt.NoPen)) #设置图例里的项被选择时不显示Icon的边框
for i in range(customPlot.graphCount()):
graph = customPlot.graph(i)
graph.setSelectable(QCP.stDataRange) #可以点选
# 连接QCustomPlot的信号,selectionChangedByUser表明是由鼠标点击进行的选择
# 这里主要就是同步图表和图例的显示
def slot1():
for i in range(customPlot.graphCount()):
graph = customPlot.graph(i)
item = customPlot.legend.itemWithPlottable(graph)
if item.selected() and not graph.selected():
graph.setSelection(QCPDataSelection(graph.data().dataRange()))
elif graph.selected():
item.setSelected(True)
customPlot.selectionChangedByUser.connect(slot1)
# customPlot.setSelectionRectMode(QCP.srmNone) #点击点打印点的坐标,这个要打开
def slot2(plottable,dataIndex,e):
graphId = lineNames.index(plottable.name())
ghd = customPlot.graph(graphId).data().at(dataIndex)
print(ghd.get().key,ghd.get().value)
customPlot.plottableClick.connect(slot2)
customPlot.replot()
window.show()
sys.exit(app.exec_())