{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"contentcontainer med left\" style=\"margin-left: -50px;\">\n",
    "    <dl class=\"dl-horizontal\">\n",
    "      <dt>Title</dt> <dd> BoundsY stream example</dd>\n",
    "      <dt>Description</dt> <dd>A linked streams example demonstrating how to use BoundsY streams.</dd>\n",
    "      <dt>Backends</dt> <dd> Bokeh</dd>\n",
    "      <dt>Tags</dt> <dd> streams, linked, interactive</dd>\n",
    "    </dl>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "from holoviews import streams\n",
    "hv.extension('bokeh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%opts Curve[tools=['ybox_select']]\n",
    "\n",
    "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",
    "\n",
    "curve * hv.DynamicMap(make_area, streams=[bounds_stream]) + hv.DynamicMap(make_items, streams=[bounds_stream])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><img src=\"http://assets.holoviews.org/gifs/examples/streams/bokeh/boundsy_selection.gif\"></center>"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
