Created
September 23, 2016 16:02
-
-
Save schaunwheeler/cc389a756c8fc8dfb16304522fdf7f71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pandas import DataFrame | |
from bokeh.models.axes import CategoricalAxis | |
from bokeh.models import Plot, ColumnDataSource | |
from bokeh.models.ranges import FactorRange, Range1d | |
from bokeh.models.glyphs import Circle | |
from bokeh.models.widgets.markups import PreText | |
from bokeh.layouts import Column | |
from bokeh.models.widgets.inputs import Select | |
from bokeh.plotting import curdoc | |
from bokeh.models.callbacks import CustomJS | |
window_resize_code = ''' | |
window.dispatchEvent(new Event('resize')); | |
''' | |
class TestResize(object): | |
def __init__(self): | |
self.plot_source = ColumnDataSource() | |
self.plot = Plot() | |
self.number_elements = Select(options=['5', '10', '15', '20'], value='20') | |
self.number_elements.on_change('value', self.resize_plot) | |
self.monitor_box = PreText(width=500, height=75) | |
self.update_plot() | |
self.make_plot() | |
self.update_monitor() | |
self.layout = Column() | |
self.layout.children = [self.number_elements, self.monitor_box, self.plot] | |
def make_plot(self): | |
plot = Plot( | |
x_range=FactorRange(factors=[str(x) for x in range(int(self.number_elements.value) + 1)]), | |
y_range=FactorRange(factors=[str(x) for x in range(int(self.number_elements.value) + 1)]), | |
min_border_bottom=10, | |
min_border_top=10, | |
min_border_right=10, | |
min_border_left=10, | |
width=int(self.number_elements.value) * 25, | |
height=int(self.number_elements.value) * 25, | |
outline_line_alpha=0.0, | |
toolbar_location=None, | |
logo=None) | |
plot.extra_y_ranges = {'resizer_axis': Range1d(start=0, callback=CustomJS(code=window_resize_code))} | |
circle = Circle(x='x_position', y='y_position', radius=0.5) | |
plot.add_glyph(self.plot_source, circle) | |
plot.add_layout(CategoricalAxis(), 'left') | |
plot.add_layout(CategoricalAxis(), 'above') | |
self.plot = plot | |
def update_plot(self): | |
df = DataFrame() | |
df['x_position'] = [x for x in range(int(self.number_elements.value) + 1)] | |
df['y_position'] = [x for x in range(int(self.number_elements.value) + 1)] | |
self.plot_source.data = ColumnDataSource(df).data | |
self.plot_source.column_names = df.columns.tolist() | |
if self.plot.width: | |
self.plot.width = int(self.number_elements.value) * 25 | |
self.plot.height = int(self.number_elements.value) * 25 | |
self.plot.x_range.factors = [str(x) for x in range(int(self.number_elements.value) + 1)] | |
self.plot.y_range.factors = [str(x) for x in range(int(self.number_elements.value) + 1)] | |
self.plot.extra_y_ranges['resizer_axis'].start += 1 | |
def resize_plot(self, attr, old, new): | |
self.update_plot() | |
self.update_monitor() | |
def update_monitor(self): | |
self.monitor_box.text = '' | |
self.monitor_box.text += "self.plot.extra_y_ranges['resizer_axis'].start == " + str(self.plot.extra_y_ranges['resizer_axis'].start) + '\n' | |
self.monitor_box.text += 'self.plot.width == ' + str(self.plot.width) + '\n' | |
self.monitor_box.text += "self.plot.ref['id']: " + self.plot.ref['id'] + '\n' | |
app = TestResize() | |
curdoc().add_root(app.layout) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment