Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add range control to plot #223

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

benjeffery
Copy link
Member

I've spent too much time trying to get bidirectional binding working here without any luck. So for now the scrolling on the plot doesn't update the control. Annoyingly I can get it to work in a toy example:

import panel as pn
import holoviews as hv
import hvplot.pandas
import numpy as np
import pandas as pd

pn.extension()
hv.extension('bokeh')

x = np.linspace(0, 100, 1000)
y = np.sin(x/10) + np.random.randn(len(x))*0.1
df = pd.DataFrame({'x': x, 'y': y})

x_range = pn.widgets.TextInput(value='0:100', name='X Range (start:stop)')

def parse_range(range_str):
    try:
        start, end = map(float, range_str.split(':'))
        return (start, end)
    except (ValueError, TypeError):
        return (0, 100)  # Default range if parsing fails

def get_plot(range_str):
    x_range = parse_range(range_str)
    return df.hvplot.line(x='x', y='y').opts(xlim=x_range, framewise=True)

dmap = hv.DynamicMap(pn.bind(get_plot, x_range))

range_stream = hv.streams.RangeX(source=dmap)

def update_widget(event):
    if event.new is not None:
        new_start, new_end = event.new
        new_value = f"{new_start}:{new_end}"
        if new_value != x_range.value:
            x_range.value = new_value

range_stream.param.watch(update_widget, 'x_range')

pn.Column(x_range, dmap).servable()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant