{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ddf9c59-ed50-4c19-a7f7-5f0ea3ce0933",
   "metadata": {},
   "outputs": [],
   "source": [
    "import holoviews as hv\n",
    "import panel as pn\n",
    "import pandas as pd\n",
    "\n",
    "pn.extension('vizzu', 'tabulator', design='material', template='material')\n",
    "import hvplot.pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e88022a-73cb-4f7e-9f2f-e1f055f176ae",
   "metadata": {},
   "source": [
    "## Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6a43f64-ea27-41eb-9685-816abbe308ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "windturbines = pn.state.as_cached(\n",
    "    'windturbines',\n",
    "    pd.read_parquet,\n",
    "    path='https://datasets.holoviz.org/windturbines/v1/windturbines.parq'\n",
    ")\n",
    "\n",
    "windturbines.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70724dc4-fcf0-4411-8943-4dc5dbb8b3c6",
   "metadata": {},
   "source": [
    "## Plot data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99185c2a-b628-43ce-93ba-4010346b8f5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def data(df, groupby, quant):\n",
    "    if quant == 'Count':\n",
    "        return df.value_counts(groupby).to_frame(name='Count').sort_index().reset_index().iloc[:50]\n",
    "    else:\n",
    "        return df.groupby(groupby)[quant].sum().reset_index().iloc[:50]\n",
    "\n",
    "def config(chart_type, groupby, quant):\n",
    "    if chart_type == 'Bubble Chart':\n",
    "        return {\n",
    "            \"channels\": {\n",
    "                \"x\": None,\n",
    "                \"y\": None,\n",
    "                \"color\": groupby,\n",
    "                \"label\": groupby,\n",
    "                \"size\": quant\n",
    "            },\n",
    "            'geometry': 'circle'\n",
    "        }\n",
    "    else:\n",
    "        return {\n",
    "            \"channels\": {\n",
    "                \"x\": groupby,\n",
    "                \"y\": quant,\n",
    "                \"color\": None,\n",
    "                \"label\": None,\n",
    "                \"size\": None\n",
    "            },\n",
    "            'geometry': 'rectangle'\n",
    "        }\n",
    "    \n",
    "ls = hv.link_selections.instance()\n",
    "\n",
    "geo = ls(windturbines.hvplot.points(\n",
    "    'easting', 'northing', xaxis=None, yaxis=None, rasterize=True, xlim=(-15000000, -5000000),\n",
    "    tiles='CartoLight', height=500, responsive=True, dynspread=True, cnorm='log', cmap='plasma'\n",
    "))\n",
    "    \n",
    "groupby = pn.widgets.RadioButtonGroup(options={'State': 't_state', 'Year': 'p_year', 'Manufacturer': 't_manu'}, align='center')\n",
    "chart_type = pn.widgets.RadioButtonGroup(options=['Bar Chart', 'Bubble Chart'], align='center')\n",
    "quant = pn.widgets.RadioButtonGroup(options={'Count': 'Count', 'Capacity': 'p_cap'}, align='center')\n",
    "lsdata = ls.selection_param(windturbines)\n",
    "\n",
    "vizzu = pn.pane.Vizzu(\n",
    "    pn.bind(data, lsdata, groupby, quant),\n",
    "    config=pn.bind(config, chart_type, groupby, quant),\n",
    "    column_types={'p_year': 'dimension'},\n",
    "    style={\n",
    "        \"plot\": {\n",
    "            \"xAxis\": {\n",
    "                \"label\": {\n",
    "                    \"angle\": \"-45deg\"\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "    },\n",
    "    sizing_mode='stretch_both'\n",
    ")\n",
    "\n",
    "def format_df(df):\n",
    "    df = df[['t_state', 't_county', 'p_name', 'p_year', 't_manu', 't_cap']]\n",
    "    return df.rename(\n",
    "        columns={col: col.split('_')[1].title() for col in df.columns}\n",
    "    )\n",
    "\n",
    "\n",
    "table = pn.widgets.Tabulator(\n",
    "    pn.bind(format_df, lsdata), page_size=8, pagination='remote',\n",
    "    show_index=False,\n",
    ")\n",
    "\n",
    "pn.Column(\n",
    "    pn.Row(quant, \"# by\", groupby, \"# as a\", chart_type).servable(area='header'),\n",
    "    pn.Column(\n",
    "        pn.Row(geo, table),\n",
    "        vizzu, min_height=1000,\n",
    "        sizing_mode='stretch_both'\n",
    "    ).servable(title='Windturbines')\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
