MCPcopy Index your code
hub / github.com/PyQt5/PyQt / initChart

Method initChart

QtChart/LineStack.py:231–279  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

229 series.setPen(pen)
230
231 def initChart(self):
232 self._chart = QChart(title="折线图堆叠")
233 self._chart.setAcceptHoverEvents(True)
234 # Series动画
235 self._chart.setAnimationOptions(QChart.SeriesAnimations)
236 dataTable = [
237 ["邮件营销", [120, 132, 101, 134, 90, 230, 210]],
238 ["联盟广告", [220, 182, 191, 234, 290, 330, 310]],
239 ["视频广告", [150, 232, 201, 154, 190, 330, 410]],
240 ["直接访问", [320, 332, 301, 334, 390, 330, 320]],
241 ["搜索引擎", [820, 932, 901, 934, 1290, 1330, 1320]]
242 ]
243 for series_name, data_list in dataTable:
244 series = QLineSeries(self._chart)
245 for j, v in enumerate(data_list):
246 series.append(j, v)
247 series.setName(series_name)
248 series.setPointsVisible(True) # 显示圆点
249 series.hovered.connect(self.handleSeriesHoverd) # 鼠标悬停
250 self._chart.addSeries(series)
251 self._chart.createDefaultAxes() # 创建默认的轴
252 axisX = self._chart.axisX() # x轴
253 axisX.setTickCount(7) # x轴设置7个刻度
254 axisX.setGridLineVisible(False) # 隐藏从x轴往上的线条
255 axisY = self._chart.axisY()
256 axisY.setTickCount(7) # y轴设置7个刻度
257 axisY.setRange(0, 1500) # 设置y轴范围
258 # 自定义x轴
259 axis_x = QCategoryAxis(
260 self._chart, labelsPosition=QCategoryAxis.AxisLabelsPositionOnValue)
261 axis_x.setTickCount(7)
262 axis_x.setGridLineVisible(False)
263 min_x = axisX.min()
264 max_x = axisX.max()
265 step = (max_x - min_x) / (7 - 1) # 7个tick
266 for i in range(0, 7):
267 axis_x.append(self.category[i], min_x + i * step)
268 self._chart.setAxisX(axis_x, self._chart.series()[-1])
269 # chart的图例
270 legend = self._chart.legend()
271 # 设置图例由Series来决定样式
272 legend.setMarkerShape(QLegend.MarkerShapeFromSeries)
273 # 遍历图例上的标记并绑定信号
274 for marker in legend.markers():
275 # 点击事件
276 marker.clicked.connect(self.handleMarkerClicked)
277 # 鼠标悬停事件
278 marker.hovered.connect(self.handleMarkerHovered)
279 self.setChart(self._chart)
280
281
282if __name__ == "__main__":

Callers 1

__init__Method · 0.95

Calls 1

setRangeMethod · 0.45

Tested by

no test coverage detected