Advertisement
Higem

Untitled

Apr 4th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. from bokeh.io import show
  2. from bokeh.layouts import column, row
  3. from bokeh.models import DataTable, TableColumn, Button, CustomJS, ColumnDataSource
  4. from bokeh.plotting import figure
  5. from bokeh.io import output_notebook
  6.  
  7. output_notebook()
  8.  
  9. # new x_ranges (1d and 2d) with their values
  10. range1d = ["c", "d"]
  11. values1d = [100, 150];
  12. range2d_p1 = ["e", "e", "f", "f", "g", "g"];
  13. range2d_p2 = ["0", "1", "0", "1", "0", "1"];
  14. values2d = [50, 30, 40, 80, 120, 110];
  15.  
  16. # default fig
  17. fig = figure(x_range=["a", "b"],
  18. plot_height=350,
  19. plot_width=490,
  20. tools="box_edit",
  21. toolbar_location="right")
  22.  
  23. source_plot = ColumnDataSource(data={"Column": ["a", "b"], "Result": [0,0]})
  24. bar = fig.vbar(x='Column', top='Result', width=0.9, source=source_plot,
  25. fill_color='#97F0AA', line_color="black", name="main_vbar")
  26.  
  27. # buttons
  28. xrange_1d = Button(label="xrange_1d", width=100)
  29. xrange_2d = Button(label="xrange_2d", width=100)
  30.  
  31. # callbacks
  32. xrange_1d.js_on_click(CustomJS(args=dict(fig=fig, source_plot=source_plot,
  33. range1d=range1d, values1d=values1d),
  34. code="""
  35. console.log(fig.x_range.factors);
  36. fig.x_range.factors = range1d;
  37. source_plot.data["Column"] = range1d;
  38. source_plot.data["Result"] = values1d;
  39. source_plot.change.emit();
  40. """))
  41.  
  42. xrange_2d.js_on_click(CustomJS(args=dict(fig=fig, source_plot=source_plot,
  43. range2d_p1=range2d_p1, range2d_p2=range2d_p2,
  44. values2d=values2d),
  45. code="""
  46. console.log(fig.x_range.factors);
  47. var factorsZipped = range2d_p1.map(function(e, i) {
  48. return [e, range2d_p2[i]];
  49. });
  50. console.log(factorsZipped);
  51. fig.x_range.factors = factorsZipped;
  52. source_plot.data["Column"] = factorsZipped;
  53. source_plot.data["Result"] = values2d;
  54.  
  55. source_plot.change.emit();
  56. """))
  57.  
  58.  
  59. show(column(row(xrange_1d, xrange_2d), fig))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement