我正在使用pyqt5绘制一个简单的图表,需要添加自定义轴,但是当我添加轴时,图表不显示它,当悬停信号发出时,我需要显示对应点,但它也没有显示,它需要点击显示。
class Demo(QChartView):
def __init__(self):
super().__init__()
self.chart = QChart()
self.setChart(self.chart)
self.setRenderHint(QPainter.Antialiasing)
axis_x = QValueAxis()
axis_x.setTickCount(10)
axis_x.setTitleText('x')
self.chart.addAxis(axis_x, Qt.AlignBottom)
axis_y= QValueAxis()
axis_y.setLinePenColor(Qt.red)
self.chart.addAxis(axis_y, Qt.AlignLeft)
series = QLineSeries()
series.setPointsVisible(True)
series.hovered.connect(self.show_tool_tip)
series << QPointF(1, 5) << QPointF(3.5, 18) << QPointF(4.8, 7.5) << QPointF(10, 2.5)
series.attachAxis(axis_x)
series.attachAxis(axis_y)
self.chart.addSeries(series)
self.value_label = QLabel(self)
def show_tool_tip(self, pt, state):
pos = self.chart.mapToPosition(pt)
if state:
self.value_label.move(int(pos.x()), int(pos.y()))
self.value_label.setText(f'{pt}')
self.value_label.show()
else:
self.value_label.hide()