| 32 | |
| 33 | |
| 34 | class ChartView(QChartView): |
| 35 | |
| 36 | def __init__(self, file, parent=None): |
| 37 | super(ChartView, self).__init__(parent) |
| 38 | self._chart = QChart() |
| 39 | self._chart.setAcceptHoverEvents(True) |
| 40 | self.setChart(self._chart) |
| 41 | self.initUi(file) |
| 42 | |
| 43 | def initUi(self, file): |
| 44 | if isinstance(file, dict): |
| 45 | return self.__analysis(file) |
| 46 | if isinstance(file, str): |
| 47 | if not os.path.isfile(file): |
| 48 | return self.__analysis(json.loads(file)) |
| 49 | with open(file, "rb") as fp: |
| 50 | data = fp.read() |
| 51 | encoding = chardet.detect(data) or {} |
| 52 | data = data.decode(encoding.get("encoding") or "utf-8") |
| 53 | self.__analysis(json.loads(data)) |
| 54 | |
| 55 | # def onSeriesHoverd(self, point, state): |
| 56 | # print(point, state) |
| 57 | |
| 58 | def mouseMoveEvent(self, event): |
| 59 | super(ChartView, self).mouseMoveEvent(event) |
| 60 | # 获取x和y轴的最小最大值 |
| 61 | axisX, axisY = self._chart.axisX(), self._chart.axisY() |
| 62 | min_x, max_x = axisX.min(), axisX.max() |
| 63 | min_y, max_y = axisY.min(), axisY.max() |
| 64 | # 把鼠标位置所在点转换为对应的xy值 |
| 65 | x = self._chart.mapToValue(event.pos()).x() |
| 66 | y = self._chart.mapToValue(event.pos()).y() |
| 67 | index = round(x) # 四舍五入 |
| 68 | print(x, y, index) |
| 69 | # 得到在坐标系中的所有series的类型和点 |
| 70 | points = [(s.type(), s.at(index)) |
| 71 | for s in self._chart.series() if min_x <= x <= max_x and min_y <= y <= max_y] |
| 72 | print(points) |
| 73 | |
| 74 | def __getColor(self, color=None, default=Qt.white): |
| 75 | ''' |
| 76 | :param color: int|str|[r,g,b]|[r,g,b,a] |
| 77 | ''' |
| 78 | if not color: |
| 79 | return QColor(default) |
| 80 | if isinstance(color, QBrush): |
| 81 | return color |
| 82 | # 比如[r,g,b]或[r,g,b,a] |
| 83 | if isinstance(color, list) and 3 <= len(color) <= 4: |
| 84 | return QColor(*color) |
| 85 | else: |
| 86 | return QColor(color) |
| 87 | |
| 88 | def __getPen(self, pen=None, default=QPen( |
| 89 | Qt.white, 1, Qt.SolidLine, |
| 90 | Qt.SquareCap, Qt.BevelJoin)): |
| 91 | ''' |