{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### **Title**: BoundsY stream example\n",
    "\n",
    "**Description**: A linked streams example demonstrating how to use BoundsY streams.\n",
    "\n",
    "**Dependencies**: Bokeh\n",
    "\n",
    "**Backends**: [Bokeh](./BoundsY.ipynb), [Plotly](../plotly/BoundsY.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "from holoviews import opts, streams\n",
    "\n",
    "hv.extension('bokeh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xs = np.linspace(0, 1, 200)\n",
    "ys = xs*(1-xs)\n",
    "curve = hv.Curve((xs,ys))\n",
    "\n",
    "bounds_stream = streams.BoundsY(source=curve,boundsy=(0,0))\n",
    "\n",
    "def make_area(boundsy):\n",
    "    return hv.Area((xs, np.minimum(ys, boundsy[0]), np.minimum(ys, boundsy[1])), vdims=['min','max'])\n",
    "\n",
    "\n",
    "def make_items(boundsy):\n",
    "    times = [\"{0:.2f}\".format(x) for x in sorted(np.roots([-1,1,-boundsy[0]])) + sorted(np.roots([-1,1,-boundsy[1]]))]\n",
    "    return hv.ItemTable(sorted(zip(['1_entry', '2_exit', '1_exit', '2_entry'], times)))\n",
    "\n",
    "area_dmap = hv.DynamicMap(make_area, streams=[bounds_stream])\n",
    "table_dmap = hv.DynamicMap(make_items, streams=[bounds_stream])\n",
    "\n",
    "(curve * area_dmap + table_dmap).opts(\n",
    "    opts.Layout(merge_tools=False),\n",
    "    opts.Area(tools=['ybox_select'], active_tools=['ybox_select'])\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><img src=\"https://assets.holoviews.org/gifs/examples/streams/bokeh/boundsy_selection.gif\"></center>"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
