| 141 | (self._chart.axisX().tickCount() - 1) |
| 142 | |
| 143 | def mouseMoveEvent(self, event): |
| 144 | super(ChartView, self).mouseMoveEvent(event) |
| 145 | pos = event.pos() |
| 146 | # 把鼠标位置所在点转换为对应的xy值 |
| 147 | x = self._chart.mapToValue(pos).x() |
| 148 | y = self._chart.mapToValue(pos).y() |
| 149 | index = round((x - self.min_x) / self.step_x) |
| 150 | # 得到在坐标系中的所有正常显示的series的类型和点 |
| 151 | points = [(serie, serie.at(index)) |
| 152 | for serie in self._chart.series() |
| 153 | if self.min_x <= x <= self.max_x and |
| 154 | self.min_y <= y <= self.max_y] |
| 155 | if points: |
| 156 | pos_x = self._chart.mapToPosition( |
| 157 | QPointF(index * self.step_x + self.min_x, self.min_y)) |
| 158 | self.lineItem.setLine(pos_x.x(), self.point_top.y(), |
| 159 | pos_x.x(), self.point_bottom.y()) |
| 160 | self.lineItem.show() |
| 161 | try: |
| 162 | title = self.category[index] |
| 163 | except: |
| 164 | title = "" |
| 165 | t_width = self.toolTipWidget.width() |
| 166 | t_height = self.toolTipWidget.height() |
| 167 | # 如果鼠标位置离右侧的距离小于tip宽度 |
| 168 | x = pos.x() - t_width if self.width() - \ |
| 169 | pos.x() - 20 < t_width else pos.x() |
| 170 | # 如果鼠标位置离底部的高度小于tip高度 |
| 171 | y = pos.y() - t_height if self.height() - \ |
| 172 | pos.y() - 20 < t_height else pos.y() |
| 173 | self.toolTipWidget.show( |
| 174 | title, points, QPoint(x, y)) |
| 175 | else: |
| 176 | self.toolTipWidget.hide() |
| 177 | self.lineItem.hide() |
| 178 | |
| 179 | def handleMarkerClicked(self): |
| 180 | marker = self.sender() # 信号发送者 |