34 from __future__
import division
38 from python_qt_binding.QtCore
import QEvent, QSize, QPointF, Qt, Signal, Slot, qWarning
39 from python_qt_binding.QtGui
import QColor, QPen, QBrush, QVector2D
42 from numpy
import arange, zeros, concatenate
47 mouseCoordinatesChanged = Signal(QPointF)
48 _num_value_saved = 1000
49 _num_values_ploted = 1000
51 limits_changed = Signal()
54 super(QwtDataPlot, self).
__init__(*args)
55 self.setCanvasBackground(Qt.white)
56 self.insertLegend(Qwt.QwtLegend(), Qwt.QwtPlot.BottomLegend)
75 marker_axis_y = Qwt.QwtPlotMarker()
76 marker_axis_y.setLabelAlignment(Qt.AlignRight | Qt.AlignTop)
77 marker_axis_y.setLineStyle(Qwt.QwtPlotMarker.HLine)
78 marker_axis_y.setYValue(0.0)
79 marker_axis_y.attach(self)
82 Qwt.QwtPlot.xBottom, Qwt.QwtPlot.yLeft, Qwt.QwtPicker.PolygonSelection,
83 Qwt.QwtPlotPicker.PolygonRubberBand, Qwt.QwtPicker.AlwaysOn, self.canvas()
85 self.
_picker.setRubberBandPen(QPen(Qt.blue))
86 self.
_picker.setTrackerPen(QPen(Qt.blue))
91 self.canvas().setMouseTracking(
True)
92 self.canvas().installEventFilter(self)
95 if event.type() == QEvent.MouseButtonRelease:
96 x = self.invTransform(Qwt.QwtPlot.xBottom, event.pos().x())
97 y = self.invTransform(Qwt.QwtPlot.yLeft, event.pos().y())
100 elif event.type() == QEvent.MouseMove:
101 x = self.invTransform(Qwt.QwtPlot.xBottom, event.pos().x())
102 y = self.invTransform(Qwt.QwtPlot.yLeft, event.pos().y())
103 coords = QPointF(x, y)
105 toolTip =
'origin x: %.5f, y: %.5f' % (
108 toolTip +=
'\ndelta x: %.5f, y: %.5f\nlength: %.5f' % (
109 delta.x(), delta.y(), QVector2D(delta).length())
111 toolTip =
'buttons\nleft: measure\nmiddle: move\nright: zoom x/y\nwheel: zoom y'
112 self.setToolTip(toolTip)
116 def log(self, level, message):
121 Qwt.QwtPlot.resizeEvent(self, event)
124 def add_curve(self, curve_id, curve_name, curve_color=QColor(Qt.blue), markers_on=
False):
125 curve_id = str(curve_id)
128 curve_object = Qwt.QwtPlotCurve(curve_name)
129 curve_object.attach(self)
130 curve_object.setPen(curve_color)
132 curve_object.setSymbol(
133 Qwt.QwtSymbol(Qwt.QwtSymbol.Ellipse, QBrush(curve_color), QPen(Qt.black), QSize(4, 4)))
134 self.
_curves[curve_id] = curve_object
137 curve_id = str(curve_id)
140 self.
_curves[curve_id].attach(
None)
145 curve.setData(data_x, data_y)
154 self.setAxisScale(Qwt.QwtPlot.yLeft,
157 self.setAxisScale(Qwt.QwtPlot.xBottom,
167 add delta_x to the length of the x axis
176 set the y axis height to max_value, about the current center
178 canvas_display_height = max_value
180 y_lower_limit = canvas_offset_y - (canvas_display_height / 2)
181 y_upper_limit = canvas_offset_y + (canvas_display_height / 2)
182 self.
_y_limits = [y_lower_limit, y_upper_limit]
187 move the canvas by delta_x and delta_y in SCREEN COORDINATES
201 canvas_x = event.x() - self.canvas().x()
202 canvas_y = event.y() - self.canvas().y()
203 if event.buttons() & Qt.MiddleButton:
207 elif event.buttons() & Qt.RightButton:
208 zoom_factor = max(-0.6, min(0.6, (self.
_last_canvas_y - canvas_y) / 20.0 / 2.0))
210 self.
move_canvas(0, zoom_factor * delta_y * 1.0225)
219 canvas_y = event.y() - self.canvas().y()
222 delta = event.angleDelta().y()
223 except AttributeError:
224 delta = event.delta()
225 zoom_factor = max(-0.6, min(0.6, (delta / 120) / 6.0))
226 delta_y = (self.canvas().height() / 2.0) - canvas_y
227 self.
move_canvas(0, zoom_factor * delta_y * 1.0225)
234 qWarning(
"QwtDataPlot.vline is not implemented yet")
237 self.setAxisScale(Qwt.QwtPlot.xBottom, limits[0], limits[1])
241 self.setAxisScale(Qwt.QwtPlot.yLeft, limits[0], limits[1])
251 if __name__ ==
'__main__':
252 from python_qt_binding.QtGui
import QApplication
254 app = QApplication(sys.argv)
256 plot.resize(700, 500)
258 plot.add_curve(0,
'(x/500)^2')
259 plot.add_curve(1,
'sin(x / 20) * 500')
260 for i
in range(plot._num_value_saved):
261 plot.update_value(0, (i / 500.0) * (i / 5.0))
262 plot.update_value(1, math.sin(i / 20.0) * 500)
264 sys.exit(app.exec_())