|
| 1 | +import pyqtgraph as pg |
| 2 | +from pyqtgraph import debug as debug |
| 3 | + |
| 4 | + |
| 5 | +class AdaptedAxisItem(pg.AxisItem): |
| 6 | + def __init__(self, orientation, parent=None,): |
| 7 | + pg.AxisItem.__init__(self, orientation, parent=parent) |
| 8 | + |
| 9 | + def drawPicture(self, p, axisSpec, tickSpecs, textSpecs): |
| 10 | + profiler = debug.Profiler() |
| 11 | + |
| 12 | + p.setRenderHint(p.Antialiasing, False) |
| 13 | + p.setRenderHint(p.TextAntialiasing, True) |
| 14 | + |
| 15 | + # draw long line along axis |
| 16 | + pen, p1, p2 = axisSpec |
| 17 | + p.setPen(pen) |
| 18 | + p.drawLine(p1, p2) |
| 19 | + p.translate(0.5, 0) # resolves some damn pixel ambiguity |
| 20 | + |
| 21 | + # draw ticks |
| 22 | + for pen, p1, p2 in tickSpecs: |
| 23 | + p.setPen(pen) |
| 24 | + p.drawLine(p1, p2) |
| 25 | + profiler('draw ticks') |
| 26 | + |
| 27 | + # Draw all text |
| 28 | + if self.style['tickFont'] is not None: |
| 29 | + p.setFont(self.style['tickFont']) |
| 30 | + p.setPen(self.textPen()) |
| 31 | + for rect, flags, text in textSpecs: |
| 32 | + p.drawText(rect, int(flags), text) |
| 33 | + |
| 34 | + profiler('draw text') |
| 35 | + |
| 36 | + |
| 37 | +def replace_axis(plot_item, orientation='left', pos=(2, 0)): |
| 38 | + |
| 39 | + new_axis = AdaptedAxisItem(orientation, parent=plot_item) |
| 40 | + oldAxis = plot_item.axes[orientation]['item'] |
| 41 | + plot_item.layout.removeItem(oldAxis) |
| 42 | + oldAxis.unlinkFromView() |
| 43 | + # |
| 44 | + new_axis.linkToView(plot_item.vb) |
| 45 | + plot_item.axes[orientation] = {'item': new_axis, 'pos': pos} |
| 46 | + plot_item.layout.addItem(new_axis, *pos) |
| 47 | + new_axis.setZValue(-1000) |
| 48 | + new_axis.setFlag(new_axis.ItemNegativeZStacksBehindParent) |
0 commit comments