{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import panel as pn\n",
    "\n",
    "pn.extension()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ``Trend`` is a value indicator providing a visual representation of a value along with an indicator of its recent trend. It supports streaming data to the plot component making it possible to provide performant live updates on the recent trends in some value.\n",
    "\n",
    "#### Parameters:\n",
    "\n",
    "For layout and styling related parameters see the [customization user guide](../../user_guide/Customization.ipynb).\n",
    "\n",
    "\n",
    "\n",
    "* **``data``** (`dict(str, np.ndarray)` or `pd.DataFrame`): The plot data as a dictionary of arrays or a pandas DataFrame.\n",
    "* **``layout``** (`str`, `default='column'`): The layout of the indicator, either 'column' or 'row'.\n",
    "* **``plot_x``** (`str`, `default='y'`): The column in the data corresponding to x-values of the plot.\n",
    "* **``plot_y``** (`str`, `default='x'`): The column in the data corresponding to y-values of the plot.\n",
    "* **``plot_color``** (`str`, `default='#428bca'`): The color to use in the plot.\n",
    "* **``plot_type``** (`str`, `default='bar'`): The plot type to render the plot data as, on of 'line', 'bar', 'step' or 'area'.\n",
    "* **``pos_color``** (`str`, `default='#5cb85c'`): The color used to indicate a positive change.\n",
    "* **``neg_color``** (`str`, `default='#d9534f'`): The color used to indicate a negative change.\n",
    "* **``title``** (str): The title or a short description of the indicator.\n",
    "* **``value``** (float or int or \"auto\", `default='auto'`): The primary value to be displayed.\n",
    "* **``value_change``** (float or int or \"auto\", `default='auto'`): The change in the value expressed as a fraction.\n",
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The simplest form of a `Trend` just requires providing some `data` with x- and y-values, either declared as a dictionary or a `pandas.DataFrame`. The `value` and `value_change` values will then be automatically computed from the data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {'x': np.arange(50), 'y': np.random.randn(50).cumsum()}\n",
    "\n",
    "trend = pn.indicators.Trend(\n",
    "    title='Price', data=data, width=200, height=200\n",
    ")\n",
    "trend"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Streaming\n",
    "\n",
    "The `Trend` indicator also provides a convenient method to stream new data, which supports a `rollover` argument to limit the amount of data displayed. We will register a periodic callback using `pn.state.add_periodic_callback` to update the plot:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def stream_data():\n",
    "    trend.stream({'x': [trend.data['x'][-1]+1], 'y': [trend.data['y'][-1]+np.random.randn()]}, rollover=50)\n",
    "\n",
    "pn.state.add_periodic_callback(stream_data, period=250, count=100);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot types\n",
    "\n",
    "In addition to the default `plot_type` the stream indicator also supports several other options:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pn.Row(*(trend.clone(plot_type=pt) for pt in trend.param.plot_type.objects))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Controls\n",
    "\n",
    "To get a feeling for the different parameters of the `Trend` indicator we will display a set of controls:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trend = trend.clone()\n",
    "\n",
    "pn.Row(trend.controls(), trend)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
