def update_anchors(self): points, labels = self.master.get_anchors() if points is None: return if self.anchor_items is None: self.anchor_items = [] for point, label in zip(points, labels): anchor = TextItem() anchor.setText(label) anchor.setColor(QColor(0, 0, 0)) anchor.setPos(*point) self.plot_widget.addItem(anchor) self.anchor_items.append(anchor) else: for anchor, point in zip(self.anchor_items, points): anchor.setPos(*point) self._update_anchors_scatter_item(points)
def _add_text_items(self): if self._points is None: return for point in self._points: ti = TextItem() ti.setText(point[2].name) ti.setColor(QColor(0, 0, 0)) ti.setPos(point[0], point[1]) self._text_items.append(ti) self.plot_widget.addItem(ti)
def _update_points_labels(self): if self.plotdata.points is None: return for point_label in self.plotdata.point_labels: self.graph.plot_widget.removeItem(point_label) self.plotdata.point_labels = [] sx, sy = self.graph.view_box.viewPixelSize() for row in self.plotdata.points: ti = TextItem() metrics = QFontMetrics(ti.textItem.font()) text_width = ((RANGE.width())/2. - np.abs(row[0])) / sx name = row[2].name ti.setText(name) ti.setTextWidth(text_width) ti.setColor(QColor(0, 0, 0)) br = ti.boundingRect() width = metrics.width(name) if metrics.width(name) < br.width() else br.width() width = sx * (width + 5) height = sy * br.height() ti.setPos(row[0] - (row[0] < 0) * width, row[1] + (row[1] > 0) * height) self.plotdata.point_labels.append(ti) self.graph.plot_widget.addItem(ti)
def update_anchors(self): points, labels = self.master.get_anchors() if points is None: return if self.anchor_items is not None: for anchor in self.anchor_items: self.plot_widget.removeItem(anchor) self.anchor_items = [] label_len = 1 for point, label in zip(points, labels): anchor = TextItem() anchor.textItem.setToolTip(f"<b>{label}</b>") if len(label) > MAX_LABEL_LEN: i = label.rfind(" ", 0, MAX_LABEL_LEN) if i != -1: first_row = label[:i] + "\n" second_row = label[i + 1:] if len(second_row) > MAX_LABEL_LEN: j = second_row.rfind(" ", 0, MAX_LABEL_LEN) if j != -1: second_row = second_row[:j + 1] + "..." else: second_row = second_row[:MAX_LABEL_LEN - 3] + "..." label = first_row + second_row else: label = label[:MAX_LABEL_LEN - 3] + "..." anchor.setText(label) anchor.setFont(self.parameter_setter.anchor_font) label_len = min(MAX_LABEL_LEN, len(label)) anchor.setColor(QColor(0, 0, 0)) x, y = point angle = np.rad2deg(np.arctan2(y, x)) anchor.setPos(x * 1.025, y * 1.025) if abs(angle) < 90: anchor.setAngle(angle) anchor.setAnchor((0, 0.5)) else: anchor.setAngle(angle + 180) anchor.setAnchor((1, 0.5)) anchor.textItem.setTextWidth( anchor.textItem.boundingRect().width()) option = anchor.textItem.document().defaultTextOption() option.setAlignment(Qt.AlignRight) anchor.textItem.document().setDefaultTextOption(option) self.plot_widget.addItem(anchor) self.anchor_items.append(anchor) self.padding = label_len * 0.0175 self._update_anchors_scatter_item(points)
def _update_points_labels(self): if self.plotdata.points is None: return for point_label in self.plotdata.point_labels: self.graph.plot_widget.removeItem(point_label) self.plotdata.point_labels = [] sx, sy = self.graph.view_box.viewPixelSize() for row in self.plotdata.points: ti = TextItem() metrics = QFontMetrics(ti.textItem.font()) text_width = ((RANGE.width()) / 2.0 - np.abs(row[0])) / sx name = row[2].name ti.setText(name) ti.setTextWidth(text_width) ti.setColor(QColor(0, 0, 0)) br = ti.boundingRect() width = ( metrics.width(name) if metrics.width(name) < br.width() else br.width() ) width = sx * (width + 5) height = sy * br.height() ti.setPos(row[0] - (row[0] < 0) * width, row[1] + (row[1] > 0) * height) self.plotdata.point_labels.append(ti) self.graph.plot_widget.addItem(ti)