QChart add axis not show and when hovered info not work correct?QChart添加轴不显示,悬停信息不正确?

2024-09-23 02:32:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我是用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()

image


Tags: posselfptinitvaluedefshowchart
1条回答
网友
1楼 · 发布于 2024-09-23 02:32:46

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:

问题是,由于这条线太细了,鼠标进出使得悬停的声音在每一刻都被发射出来,使它看起来像是被我们的眼睛隐藏了。你知道吗

等待对我的评论的答复以提出解决方案。

相关问题 更多 >