Attempt to make `.default_view()` time-index ready

As in make the call to `Flume.slice_from_time()` to try and convert any
time index values from the view range to array-indices; all untested
atm.

Also drop some old/unused/moved methods:
- `._set_xlimits()`
- `.bars_range()`
- `.curve_width_pxs()`

and fix some `flow` -> `viz` var naming.
pre_viz_calls
Tyler Goodlet 2022-11-29 10:56:17 -05:00
parent 301bfa2463
commit fc993146b6
1 changed files with 58 additions and 60 deletions

View File

@ -60,7 +60,7 @@ from ._style import (
hcolor, hcolor,
CHART_MARGINS, CHART_MARGINS,
_xaxis_at, _xaxis_at,
_min_points_to_show, # _min_points_to_show,
) )
from ..data.feed import ( from ..data.feed import (
Feed, Feed,
@ -951,41 +951,10 @@ class ChartPlotWidget(pg.PlotWidget):
def focus(self) -> None: def focus(self) -> None:
self.view.setFocus() self.view.setFocus()
def _set_xlimits(
self,
xfirst: int,
xlast: int
) -> None:
"""Set view limits (what's shown in the main chart "pane")
based on max/min x/y coords.
"""
self.setLimits(
xMin=xfirst,
xMax=xlast,
minXRange=_min_points_to_show,
)
def view_range(self) -> tuple[int, int]: def view_range(self) -> tuple[int, int]:
vr = self.viewRect() vr = self.viewRect()
return int(vr.left()), int(vr.right()) return int(vr.left()), int(vr.right())
def bars_range(self) -> tuple[int, int, int, int]:
'''
Return a range tuple for the bars present in view.
'''
main_flow = self._vizs[self.name]
ifirst, l, lbar, rbar, r, ilast = main_flow.datums_range()
return l, lbar, rbar, r
def curve_width_pxs(
self,
) -> float:
_, lbar, rbar, _ = self.bars_range()
return self.view.mapViewToDevice(
QLineF(lbar, 0, rbar, 0)
).length()
def pre_l1_xs(self) -> tuple[float, float]: def pre_l1_xs(self) -> tuple[float, float]:
''' '''
Return the view x-coord for the value just before Return the view x-coord for the value just before
@ -1038,37 +1007,65 @@ class ChartPlotWidget(pg.PlotWidget):
Set the view box to the "default" startup view of the scene. Set the view box to the "default" startup view of the scene.
''' '''
flow = self._vizs.get(self.name) viz = self.get_viz(self.name)
if not flow: if not viz:
log.warning(f'`Viz` for {self.name} not loaded yet?') log.warning(f'`Viz` for {self.name} not loaded yet?')
return return
arr = flow.shm.array renderer = viz._src_r
index = arr['index'] if renderer is None:
# times = arr['time'] return
# these will be epoch time floats (
xfirst, xlast = index[0], index[-1] start,
l, lbar, rbar, r = self.bars_range() l,
datum_start,
datum_stop,
r,
stop,
) = viz.datums_range()
view = self.view array = viz.shm.array
index_field = renderer.fmtr.index_field
if index_field == 'time':
vr = viz.plot.viewRect()
(
abs_slc,
read_slc,
mask,
) = viz.flume.slice_from_time(
array,
start_t=vr.left(),
stop_t=vr.right(),
)
iv_arr = array[mask]
index = iv_arr['index']
# these must be array-index-ints (hence the slice from time
# above).
x_start, x_stop = index[0], index[-1]
l, lbar, rbar, r = viz.bars_range()
view: ChartView = viz.plot.vb
if ( if (
rbar < 0 rbar < 0
or l < xfirst or l < x_start
or l < 0 or l < 0
or (rbar - lbar) < 6 or (rbar - lbar) < 6
): ):
# TODO: set fixed bars count on screen that approx includes as # TODO: set fixed bars count on screen that approx includes as
# many bars as possible before a downsample line is shown. # many bars as possible before a downsample line is shown.
begin = xlast - bars_from_y begin = x_stop - bars_from_y
view.setXRange( view.setXRange(
min=begin, min=begin,
max=xlast, max=x_stop,
padding=0, padding=0,
) )
# re-get range # re-get range
l, lbar, rbar, r = self.bars_range() l, lbar, rbar, r = viz.bars_range()
# we get the L1 spread label "length" in view coords # we get the L1 spread label "length" in view coords
# terms now that we've scaled either by user control # terms now that we've scaled either by user control
@ -1076,21 +1073,21 @@ class ChartPlotWidget(pg.PlotWidget):
# above. # above.
if not y_offset: if not y_offset:
marker_pos, l1_len = self.pre_l1_xs() marker_pos, l1_len = self.pre_l1_xs()
end = xlast + l1_len + 1 end = x_stop + l1_len + 1
else: else:
end = xlast + y_offset + 1 end = x_stop + y_offset + 1
begin = end - (r - l) begin = end - (r - l)
# for debugging # for debugging
# print( print(
# # f'bars range: {brange}\n' # f'bars range: {brange}\n'
# f'xlast: {xlast}\n' f'x_stop: {x_stop}\n'
# f'marker pos: {marker_pos}\n' f'marker pos: {marker_pos}\n'
# f'l1 len: {l1_len}\n' f'l1 len: {l1_len}\n'
# f'begin: {begin}\n' f'begin: {begin}\n'
# f'end: {end}\n' f'end: {end}\n'
# ) )
# remove any custom user yrange setttings # remove any custom user yrange setttings
if self._static_yrange == 'axis': if self._static_yrange == 'axis':
@ -1254,17 +1251,17 @@ class ChartPlotWidget(pg.PlotWidget):
**graphics_kwargs, **graphics_kwargs,
) )
flow = self._vizs[data_key] = Viz( viz = self._vizs[data_key] = Viz(
data_key, data_key,
pi, pi,
shm, shm,
flume, flume,
is_ohlc=is_ohlc, is_ohlc=is_ohlc,
# register curve graphics with this flow # register curve graphics with this viz
graphics=graphics, graphics=graphics,
) )
assert isinstance(flow.shm, ShmArray) assert isinstance(viz.shm, ShmArray)
# TODO: this probably needs its own method? # TODO: this probably needs its own method?
if overlay: if overlay:
@ -1321,7 +1318,7 @@ class ChartPlotWidget(pg.PlotWidget):
# understand. # understand.
pi.addItem(graphics) pi.addItem(graphics)
return flow return viz
def draw_ohlc( def draw_ohlc(
self, self,
@ -1446,6 +1443,7 @@ class ChartPlotWidget(pg.PlotWidget):
r, r,
last, last,
) = bars_range or viz.datums_range() ) = bars_range or viz.datums_range()
profiler(f'{self.name} got bars range') profiler(f'{self.name} got bars range')
key = round(lbar), round(rbar) key = round(lbar), round(rbar)
@ -1470,4 +1468,4 @@ class ChartPlotWidget(pg.PlotWidget):
self, self,
key: str, key: str,
) -> Viz: ) -> Viz:
return self._vizs[key] return self._vizs.get(key)