Skip to content

Commit

Permalink
Merge pull request #715 from bitcraze/rik/plotting
Browse files Browse the repository at this point in the history
Implement x axis manipulation in plotting tab
  • Loading branch information
gemenerik authored Jun 20, 2024
2 parents 02271f0 + fa7df56 commit 254f997
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 53 deletions.
86 changes: 39 additions & 47 deletions src/cfclient/ui/widgets/plotter.ui
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,51 @@
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0">
<widget class="QRadioButton" name="_enable_range_x">
<property name="text">
<string>Range (s)</string>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="_range_x_min">
<widget class="QDoubleSpinBox" name="_range_x_min">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>0</string>
<property name="minimum">
<double>0</double>
</property>
<property name="maximum">
<double>86400</double>
</property>
<property name="singleStep">
<double>1</double>
</property>
<property name="value">
<double>0</double>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="label_2">
<property name="text">
<string>-</string>
<item row="0" column="4">
<widget class="QDoubleSpinBox" name="_range_x_max">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimum">
<double>0</double>
</property>
<property name="maximum">
<double>86400</double>
</property>
<property name="singleStep">
<double>1</double>
</property>
<property name="value">
<double>60</double>
</property>
</widget>
</item>
Expand All @@ -70,7 +101,7 @@
<string/>
</property>
<property name="maximum">
<number>2000</number>
<number>3000</number>
</property>
<property name="singleStep">
<number>100</number>
Expand All @@ -80,47 +111,8 @@
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QLineEdit" name="_range_x_max">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>1000</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QRadioButton" name="_enable_range_x">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Range</string>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QRadioButton" name="_enable_manual_x">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Manual</string>
</property>
<property name="autoExclusive">
<bool>false</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QRadioButton" name="_enable_seconds_x">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Seconds</string>
</property>
Expand Down
60 changes: 54 additions & 6 deletions src/cfclient/ui/widgets/plotwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,17 @@ def __init__(self, parent=None, fps=100, title="", *args):
self._enable_samples_x.setChecked(True)
self._last_ts = None
self._dtime = None
self._first_ts = None

self._x_range = (
float(self._range_x_min.text()), float(self._range_x_max.text()))
self._nbr_samples = int(self._nbr_of_samples_x.text())

self._nbr_of_samples_x.valueChanged.connect(self._nbr_samples_changed)
self._nbr_seconds = int(self._nbr_of_seconds_x.text())
self._nbr_of_seconds_x.valueChanged.connect(self._nbr_of_seconds_changed)
self._range_x_min.valueChanged.connect(self._x_range_changed)
self._range_x_max.valueChanged.connect(self._x_range_changed)

self._range_y_min.valueChanged.connect(self._y_range_changed)
self._range_y_max.valueChanged.connect(self._y_range_changed)

Expand All @@ -175,8 +180,8 @@ def __init__(self, parent=None, fps=100, title="", *args):
self._x_btn_group.addButton(self._enable_range_x)
self._x_btn_group.addButton(self._enable_samples_x)
self._x_btn_group.addButton(self._enable_seconds_x)
self._x_btn_group.addButton(self._enable_manual_x)
self._x_btn_group.setExclusive(True)
self._x_btn_group.buttonClicked.connect(self._x_mode_change)

self._draw_graph = True
self._auto_redraw.stateChanged.connect(self._auto_redraw_change)
Expand All @@ -188,6 +193,20 @@ def _auto_redraw_change(self, state):
else:
self._draw_graph = True

def _x_mode_change(self, box):
"""Callback when user changes the X-axis mode"""
self._nbr_of_samples_x.setEnabled(False)
self._nbr_of_seconds_x.setEnabled(False)
self._range_x_min.setEnabled(False)
self._range_x_max.setEnabled(False)
if box == self._enable_range_x:
self._range_x_min.setEnabled(True)
self._range_x_max.setEnabled(True)
elif box == self._enable_samples_x:
self._nbr_of_samples_x.setEnabled(True)
elif box == self._enable_seconds_x:
self._nbr_of_seconds_x.setEnabled(True)

def _y_mode_change(self, box):
"""Callback when user changes the Y-axis mode"""
if box == self._enable_range_y:
Expand Down Expand Up @@ -228,6 +247,14 @@ def _nbr_samples_changed(self, val):
"""Callback when user changes the number of samples to be shown"""
self._nbr_samples = val

def _nbr_of_seconds_changed(self, val):
"""Callback when user changes the number of seconds to be shown"""
self._nbr_seconds = val

def _x_range_changed(self, val):
self._range_x_min.setMaximum(self._range_x_max.value()-1)
self._range_x_max.setMinimum(self._range_x_min.value()+1)

def set_title(self, title):
"""
Set the title of the plot.
Expand All @@ -254,18 +281,33 @@ def add_data(self, data, ts):
pairs
ts - timestamp of the data in ms
"""
if not self._last_ts:
self._last_ts = ts
elif not self._last_ts:

if self._first_ts is None:
self._first_ts = ts

if self._last_ts is None:
self._dtime = 1e12
else:
self._dtime = ts - self._last_ts
self._last_ts = ts
self._last_ts = ts

x_min_limit = 0
x_max_limit = 0
# We are adding new datasets, calculate what we should show.
if self._enable_samples_x.isChecked():
x_min_limit = max(0, self._last_item - self._nbr_samples)
x_max_limit = max(self._last_item, self._nbr_samples)
self._range_x_min.setValue(int(self._first_ts + x_min_limit * self._dtime)/1000.)
self._range_x_max.setValue(int(self._first_ts + x_max_limit * self._dtime)/1000.)
elif self._enable_seconds_x.isChecked():
x_min_limit = max(0, int(((self._last_ts - self._first_ts) - self._nbr_seconds * 1000.) / self._dtime))
x_max_limit = max(0, self._last_item)
print(self._first_ts, x_min_limit, x_max_limit, self._dtime, self._nbr_seconds)
self._range_x_min.setValue((self._first_ts + x_min_limit * self._dtime)/1000.)
self._range_x_max.setValue((self._first_ts + x_min_limit * self._dtime)/1000. + self._nbr_seconds)
elif self._enable_range_x.isChecked():
x_min_limit = max(0, int((self._range_x_min.value() * 1000. - self._first_ts) / self._dtime))
x_max_limit = max(0, int((self._range_x_max.value() * 1000. - self._first_ts) / self._dtime))

for name in self._items:
self._items[name].add_point(data[name], ts)
Expand All @@ -277,6 +319,11 @@ def add_data(self, data, ts):
if (self._enable_samples_x.isChecked() and self._dtime and
self._last_item < self._nbr_samples):
self._x_max = self._x_min + self._nbr_samples * self._dtime
elif (self._enable_seconds_x.isChecked() and self._dtime and
self._last_item < int(self._nbr_seconds * 1000. / self._dtime)):
self._x_max = self._x_min + self._nbr_seconds * 1000.
elif (self._enable_range_x.isChecked() and self._dtime) and self._last_item < x_max_limit:
self._x_max = self._x_min + (x_max_limit - x_min_limit) * self._dtime

self._last_item = self._last_item + 1
self._plot_widget.getViewBox().setRange(
Expand All @@ -292,6 +339,7 @@ def removeAllDatasets(self):
self._items = {}
self._last_item = 0
self._last_ts = None
self._first_ts = None
self._dtime = None
self._plot_widget.clear()

Expand Down

0 comments on commit 254f997

Please sign in to comment.