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

Class ChartView

QtChart/LineStack.py:104–279  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

102
103
104class ChartView(QChartView):
105
106 def __init__(self, *args, **kwargs):
107 super(ChartView, self).__init__(*args, **kwargs)
108 self.resize(800, 600)
109 self.setRenderHint(QPainter.Antialiasing) # 抗锯齿
110 # 自定义x轴label
111 self.category = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
112 self.initChart()
113
114 # 提示widget
115 self.toolTipWidget = GraphicsProxyWidget(self._chart)
116
117 # line
118 self.lineItem = QGraphicsLineItem(self._chart)
119 pen = QPen(Qt.gray)
120 pen.setWidth(1)
121 self.lineItem.setPen(pen)
122 self.lineItem.setZValue(998)
123 self.lineItem.hide()
124
125 # 一些固定计算,减少mouseMoveEvent中的计算量
126 # 获取x和y轴的最小最大值
127 axisX, axisY = self._chart.axisX(), self._chart.axisY()
128 self.min_x, self.max_x = axisX.min(), axisX.max()
129 self.min_y, self.max_y = axisY.min(), axisY.max()
130
131 def resizeEvent(self, event):
132 super(ChartView, self).resizeEvent(event)
133 # 当窗口大小改变时需要重新计算
134 # 坐标系中左上角顶点
135 self.point_top = self._chart.mapToPosition(
136 QPointF(self.min_x, self.max_y))
137 # 坐标原点坐标
138 self.point_bottom = self._chart.mapToPosition(
139 QPointF(self.min_x, self.min_y))
140 self.step_x = (self.max_x - self.min_x) / \
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:

Callers 1

LineStack.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected