QChart 添加轴不显示,当悬停信息不正确时?
QChart add axis not show and when hovered info not work correct?
我正在用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()
1。添加自定义轴:
如果您 运行 您的代码在 console/CMD 中,您将获得以下命令:
Series not in the chart. Please addSeries to chart first.
Series not in the chart. Please addSeries to chart first.
这清楚地表明您必须先将系列添加到 QChart,然后再添加轴:
self.chart.addSeries(series) # first add the series
series.attachAxis(axis_x)
series.attachAxis(axis_y)
2。显示对应点的QLabel:
问题是因为线太细了,鼠标进进出出,每时每刻都在发射悬停,让我们看不到它。
等待对我的评论的答复以提出解决方案。
我正在用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()
1。添加自定义轴:
如果您 运行 您的代码在 console/CMD 中,您将获得以下命令:
Series not in the chart. Please addSeries to chart first.
Series not in the chart. Please addSeries to chart first.
这清楚地表明您必须先将系列添加到 QChart,然后再添加轴:
self.chart.addSeries(series) # first add the series
series.attachAxis(axis_x)
series.attachAxis(axis_y)
2。显示对应点的QLabel:
问题是因为线太细了,鼠标进进出出,每时每刻都在发射悬停,让我们看不到它。
等待对我的评论的答复以提出解决方案。