MCPcopy Create free account
hub / github.com/PyQt5/PyQt / ChartView

Class ChartView

QtChart/ToolTip2.py:130–212  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

128
129
130class ChartView(QChartView):
131
132 def __init__(self, *args, **kwargs):
133 super(ChartView, self).__init__(*args, **kwargs)
134 self.resize(800, 600)
135 self.setRenderHint(QPainter.Antialiasing) # 抗锯齿
136 self.initChart()
137
138 self.toolTipWidget = GraphicsProxyWidget(self._chart)
139
140 # line
141 self.lineItem = QGraphicsLineItem(self._chart)
142 self.lineItem.setZValue(998)
143 self.lineItem.hide()
144
145 # 一些固定计算,减少mouseMoveEvent中的计算量
146 # 获取x和y轴的最小最大值
147 axisX, axisY = self._chart.axisX(), self._chart.axisY()
148 self.min_x, self.max_x = axisX.min(), axisX.max()
149 self.min_y, self.max_y = axisY.min(), axisY.max()
150 # 坐标系中左上角顶点
151 self.point_top = self._chart.mapToPosition(
152 QPointF(self.min_x, self.max_y))
153 # 坐标原点坐标
154 self.point_bottom = self._chart.mapToPosition(
155 QPointF(self.min_x, self.min_y))
156 self.step_x = (self.max_x - self.min_x) / (axisX.tickCount() - 1)
157
158 # self.step_y = (self.max_y - self.min_y) / (axisY.tickCount() - 1)
159
160 def mouseMoveEvent(self, event):
161 super(ChartView, self).mouseMoveEvent(event)
162 # 把鼠标位置所在点转换为对应的xy值
163 x = self._chart.mapToValue(event.pos()).x()
164 y = self._chart.mapToValue(event.pos()).y()
165 index = round((x - self.min_x) / self.step_x)
166 # print(x, pos_x, index, index * self.step_x + self.min_x)
167 # 得到在坐标系中的所有series的类型和点
168 points = [(serie, serie.at(index))
169 for serie in self._chart.series() if
170 self.min_x <= x <= self.max_x and self.min_y <= y <= self.max_y]
171 if points:
172 # 跟随鼠标的黑线条
173 self.lineItem.setLine(event.pos().x(), self.point_top.y(),
174 event.pos().x(), self.point_bottom.y())
175 self.lineItem.show()
176 self.toolTipWidget.show("", points, event.pos() + QPoint(20, 20))
177 else:
178 self.toolTipWidget.hide()
179 self.lineItem.hide()
180
181 def onSeriesHoverd(self, point, state):
182 if state:
183 try:
184 name = self.sender().name()
185 except:
186 name = ""
187 QToolTip.showText(QCursor.pos(), "%s\nx: %s\ny: %s" %

Callers 1

ToolTip2.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected