
    ag                        d dl Zd dlZd dlZd dlZd dlmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZ ddlmZ ddlmZ dd	lmZmZmZmZ  e eej>                         eej@                        z
   eejB                        z
   ed
dg      z         e eej@                         edg      z
        ejB                  dZ"g de"d   z   e"d   z   e"d<   g de"d<    e ee"d   e"d   z   e"d   z               e"d<    e       D  cg c]  } | jG                  d      r|  c} Z$ejJ                  Z&g dZ'dg ee      z   Z(g dZ)g dZ*dZ+dZ,d Z-d8dZ. G d de      Z/ G d  d!e/      Z0 G d" d#e/      Z1 G d$ d%e/      Z2 G d& d'e/      Z3 G d( d)e/      Z4 G d* d+e/      Z5 G d, d-e/      Z6 G d. d/ejn                        Z8 G d0 d1e      Z9 G d2 d3e9      Z: G d4 d5e9      Z; G d6 d7e9      Z<yc c} w )9    N)datetime_types	dt_to_int	is_number	max_range)tile_sources)
list_cmaps)Viewer   )HoloViewsConverter)hvPlot)is_geodataframe	is_xarrayinstantiate_crs_strimport_geoviewspointspathsdataset)	dataframegriddedgeom)	bivariateheatmaphexbinlabelsvectorfieldr   r   r   r   2d)histkde
boxwhiskerviolinr   barbarhstatsr   all_r_r)borders	coastlinelandlakesoceanriversstatesgrid)crs
crs_kwargs
projectionprojection_kwargsglobal_extentprojectfeaturesfeature_scale)Ncountminmaxmeansumanyi    c                 .    t        j                  | fi |S )as  Explore your data and design your plot via an interactive user interface.

    This function returns an interactive Panel component that enable you to quickly change the
    settings of your plot via widgets.

    Reference: https://hvplot.holoviz.org/getting_started/explorer.html

    Parameters
    ----------
    data : pandas.DataFrame | xarray.DataArray | xarray.Dataset
        Data structure to explore.
    kwargs : optional
        Arguments that `data.hvplot()` would also accept like `kind='bar'`.

    Returns
    -------
    hvplotExporer
        Panel component to explore the data and design your plot.

    Example
    -------

    >>> import hvplot.pandas
    >>> import pandas as pd
    >>> df = pd.DataFrame({"x": [1, 2, 3], "y": [1, 4, 9]})
    >>> hvplot.explorer(df)

    You can also specify initial values

    >>> hvplot.explorer(df, kind='bar', x='x')
    )hvPlotExplorer	from_data)datakwargss     )lib/python3.12/site-packages/hvplot/ui.pyexplorerrC   7   s    @ ##D3F33    c                 B   |xs i }| j                   D ]X  }|dk(  r	||vri ||<   t        | j                   |   t         j                  t         j                  f      rd||   d<   d||   d<   Z d|t        d}|r||d<   t        j                  | j                   fi |}|S )	NnameT	throttledstretch_widthsizing_modeF)	show_namewidgetswidth
parameters)param
isinstanceNumberRangeCONTROLS_WIDTHpnParam)instwidgets_kwargsrM   pnamerA   panes         rB   _create_param_panerY   Z   s    #)rN F?&$&N5!djj'%,,)DE15N5!+./>um,  !F
 )|88DJJ)&)DKrD   c                   d     e Zd Z ej                  ed      Zi ZdZ fdZ	d Z
ed        Z xZS )Controls)class_
precedenceTc                 2    || _         t        |   di | y )N )_datasuper__init__)selfdfparams	__class__s      rB   rc   zControls.__init__x   s    
"6"rD   c                 0    t        | | j                        S )N)rV   )rY   _widgets_kwargsrd   s    rB   	__panel__zControls.__panel__|   s    !$t7K7KLLrD   c                     | j                   j                         j                         D ci c]  \  }}|dvr
||dk7  r|| c}}S c c}}w )N)rF   rC    )rN   valuesitems)rd   kvs      rB   rA   zControls.kwargs   sZ     

))+113
31,,17 qD3
 	
 
s   A)__name__
__module____qualname__rN   ClassSelectorr	   rC   ri   _Controls__abstractrc   rk   propertyrA   __classcell__rg   s   @rB   r[   r[   q   s@    "u""&R@HOJ#M 
 
rD   r[   c                       e Zd Z ej                  dd      Z ej                  ddg d      Z ej                  d	      Z	 ej                  d	      Z ej                  ed   d
e      Z ej                  d	      Z ej                  dd      ZdddiiZ fdZed        Z ej*                  dddd      d        Z xZS )ColormappingzColorbar Limits (clim)z0
        Upper and lower limits of the colorbar.labeldoczColorbar Normalization (cnorm)linear)r   logeq_hist)r}   defaultobjectsNr   Colormap)r   r}   r   TFz4
        Whether the data are symmetric around zero.r   r~   climplaceholderz
(min, max)c                     t        |   |fi | d|vr6| j                  j                  j                  j                  dd      | _        y y )N	symmetricF)rb   rc   rC   
_converter
_plot_optsgetr   rd   r@   rf   rg   s      rB   rc   zColormapping.__init__   sG    ((f$!]]55@@DD[RWXDN %rD   c                     | j                   j                  t        j                  v ry| j                  d uxr | j                  | j
                  v S NT)rC   kind_hvConverter_colorbar_typescolorra   rj   s    rB   colormappedzColormapping.colormapped   s>    ==!=!==zz%B$**

*BBrD   r   zexplorer.kindr   watchc                    | j                   r)| j                  t        t        j	                               vry | j
                  j                  t        j                  v r| j                  rdnd}d| _
        n_| j                  | j                  v rF| j                  | j                     j                  j                  }|dv rd}n| j                  rd}nd}ny t        |   | _        y )N	divergingr   TOSUcategorical)r   cmaplistDEFAULT_CMAPSrn   rC   r   r   r   r   colorbarr   ra   dtype)rd   keyr   s      rB   _update_coloroptszColormapping._update_coloropts   s    499D9M9M9O4P#P==!=!==!%+XC DMZZ4::%::djj)//44Du}#!!#&	rD   )rr   rs   rt   rN   rQ   r   SelectorcnormStringr   Booleanr   r   CMAPSr   rescale_discrete_levelsr   ri   rc   rw   r   dependsr   rx   ry   s   @rB   r{   r{      s    5;;&3D ENN.,E ELL&Eu}}T*H5>>-"9UZ[D+emmD97I |<=OY
 C C
 U]]7O[E' F'rD   r{   c                   2    e Zd Z ej                  d      Zy)Styler
   r   N)rr   rs   rt   rN   	Magnitudealphar`   rD   rB   r   r      s    EOOA&ErD   r   c                       e Zd Z ej                  dej                        Z ej                  d      Z	 ej                  d      Z
 ej                  dd      Z ej                  dd      Z ej                  d      Z ej                  d      Z ej                          Z ej                          Z ej                  d      Z	 ej                  d      Z
 fd	Z ej(                  d
dd      d        Zed        Z xZS )Axesbottom_rightr   r   Fr   N)r   N)r   boundsTc                 F    t        |   |fi | | j                          y N)rb   rc   _update_rangesr   s      rB   rc   zAxes.__init__   s!    ((rD   zexplorer.xlimzexplorer.ylimr   c                    | j                   j                         }|ot        |d         rat        |d         rS|d   |d   k7  rH| j                  |      }d| j                  j                  _        || j                  j                  _        nd| j                  j                  _        | j                   j                         }|ot        |d         rat        |d         rS|d   |d   k7  rH| j                  |      }d| j                  j                  _        || j                  j                  _        y d| j                  j                  _        y )Nr   r
   r\   )rC   xlimr   _convert_to_intrN   r^   r   ylim)rd   r   r   s      rB   r   zAxes._update_ranges   s   }}!!#	$q' 2ya7IdSTgY]^_Y`N`''-D)*DJJOO&%)DJJOO")+DJJOO&}}!!#	$q' 2ya7IdSTgY]^_Y`N`''-D)*DJJOO&%)DJJOO")+DJJOO&rD   c                     t        | d   t              r1t        | d   t              rt        | d   d      t        | d   d      f} | S )z
        Converts datetime to int to avoid the error in https://github.com/holoviz/hvplot/issues/964.

        This function is a workaround and should be removed when a better solution is found.

        r   r
   ms)rO   r   r   )vals    rB   r   zAxes._convert_to_int   sD     c!fn-*SV^2TSVT*Ic!fd,CDC
rD   )rr   rs   rt   rN   r   r   _legend_positionslegendr   logxlogyIntegerheightrL   
responsiveshared_axesrQ   r   r   rc   r   r   staticmethodr   rx   ry   s   @rB   r   r      s    U^^NL<Z<Z[F5=='D5=='DU]]4	:FEMM$y9Et,J%---K5;;=D5;;=D5=='D5=='D U]]?O4@, A,   rD   r   c                       e Zd Z ej                  d      Z ej                  d      Z ej                  d      Z ej                  dd      Z ej                  dd	
      Z
 ej                  dddd      Zy)LabelszTitle for the plot)r~   zAxis labels for the x-axis.zAxis labels for the y-axis.zColorbar Label (clabel)z&
        Axis labels for the colorbar.r|   r
   z
        Scales the size of all fonts by the same amount, e.g. fontscale=1.5
        enlarges all fonts (title, xticks, labels etc.) by 50%.r   r   )r   h  zX Tick Labels Rotation (rot)z\
        Rotates the axis ticks along the x-axis by the specified
        number of degrees.)r   r   r}   r~   N)rr   rs   rt   rN   r   titlexlabelylabelclabelrP   	fontscaler   rotr`   rD   rB   r   r     s    ELL12EU\\;<FU\\;<FU\\')F CI %--,	CrD   r   c                        e Zd Z ej                  ded      Z ej                  dd      Z ej                  dd      Z
 ej                  i d      Z ej                  dd	      Z ej                  i d
      Z ej                  dd      Z ej                  dd      Z ej"                  ded      Z ej                  dg dd      Zddej,                  j.                  iiZ fdZ ej4                  dd      d        Z xZS )
GeographicNz
        Whether to overlay the plot on a tile source. Tiles sources
        can be selected by name or a tiles object or class can be passed,
        the default is 'Wikipedia'.r   r   r~   Fz
        Whether the plot should be treated as geographic (and assume
        PlateCarree, i.e. lat/lon coordinates). Require GeoViews.r   zu
        Coordinate reference system of the data specified as Cartopy
        CRS object, proj.4 string or EPSG code.z3
        Keyword arguments to pass to selected CRS.z2
        Projection to use for cartographic plots.z:
        Keyword arguments to pass to selected projection.zC
        Whether to expand the plot extent to span the whole globe.z
        Whether to project the data before plotting (adds initial
        overhead but avoids projecting data when plot is dynamically
        updated).z
        A list of features or a dictionary of features and the scale
        at which to render it. Available features include 'borders',
        'coastline', 'lakes', 'land', 'ocean', 'rivers' and 'states'.110m)r   50m10mz3
        The scale at which to render the features.geotypec                     t         dgz   }d }t        fd|D              r
t               }t        |   |fi  |s0|D ]  }d| j
                  |   _         d| j
                  d   _        y | j                          y )Nr   c              3   @   K   | ]  }j                  |        y wr   )r   ).0prf   s     rB   	<genexpr>z&Geographic.__init__.<locals>.<genexpr>n  s     1jvzz!}js   Tzgeo (require GeoViews))	GEO_KEYSr;   r   rb   rc   rN   constantr}   _update_crs_projection)rd   r@   rf   
geo_paramsgv_availabler   rg   s     `   rB   rc   zGeographic.__init__k  sx    '
1j11*,L(()-

1&   '?DJJu#'')rD   Tr   c                    t         j                  j                  |       5  t        | j                  xs | j
                        }t        D ]  }| | j                   |   _         || _        |s
	 d d d        y ddlm	} t        d t        j                  |      j                         D              }|j                  dd       |j                  dd       |j                  d       | j                  j                   dk(  rd| j                  _        n*| j                  j                   dk(  rd	| j                  _        || j                   j"                  _        || j                   j&                  _        i }| j&                  |d   |d
<   | j(                  d|d<   | j*                  dg|d<    | j                   j,                  di | d d d        y # 1 sw Y   y xY w)Nr   )CRSc              3   L   K   | ]  }|j                  d       s	|dk7  r|  yw)_r   N)
startswith)r   rp   s     rB   r   z4Geographic._update_crs_projection.<locals>.<genexpr>  s*      ?A||C(Q%Z ?s   "$GOOGLE_MERCATORPlateCarreescatterr   liner   r0   Tr2   r'   r4   r`   )rN   parameterizedbatch_call_watchersboolr   r3   r   r   cartopy.crsr   sortedconcrete_descendentskeysinsertremoverC   r   r.   r   r0   r2   r4   update)rd   enabledr   r   crs_listupdatess         rB   r   z!Geographic._update_crs_projectiony  s     44T:4883t||4G/6;

3(  DH ;: ( 33C8==? H
 OOA01OOA}-OOM*}}!!Y.%-"##v-%,"%-DJJNN",4DJJ!!)G&(0%!!)+/(}}$'2m
#DJJ((I ;::s   AG7EGG)rr   rs   rt   rN   ObjectSelector	GEO_TILEStilesr   r   r   r.   Dictr/   r0   r1   r2   r3   ListSelectorGEO_FEATURESr4   r5   rS   rK   Toggleri   rc   r   r   rx   ry   s   @rB   r   r   !  s[    E  'E %--EC %..3C 6J &%%5J #

= "EMMFM emmG "u!!IH )E((&6M vrzz'8'89:O* U]]5%%) &%)rD   r   c                      e Zd Z ej                  dd      Z ej                  dd      Z ej                  ded      Z	 ej                  dd      Z
 ej                  dd	      Z ej                  dd
      Z ej                  dd      d        Z ej                  dd      d        Z ej                  dddd      d        Zy)
OperationsFzm
        Whether to apply rasterization and shading using datashader
        library returning an RGB object.r   zk
        Whether to apply rasterization using the datashader library
        returning an aggregated Image.NzJ
        Aggregator to use when applying rasterize or datashade operation.r   z
        Allows plots generated with datashade=True or rasterize=True
        to increase the point size to make sparse regions more visible.zK
        Specifies the smallest allowed sampling interval along the x-axis.zK
        Specifies the smallest allowed sampling interval along the y-axis.	datashadeTr   c                 ,    | j                   rd| _        y y NF)r   	rasterizerj   s    rB   _toggle_rasterizezOperations._toggle_rasterize      >>"DN rD   r  c                 ,    | j                   rd| _        y y r  )r  r   rj   s    rB   _toggle_datashadezOperations._toggle_datashade  r  rD   )r   on_initc                    | j                   xs | j                  }| | j                  j                  _        | | j                  j
                  _        | | j                  j                  _        | | j                  j                  _        y r   )r  r   rN   	dynspreadr   
x_sampling
y_sampling
aggregator)rd   r   s     rB   _update_optionszOperations._update_options  sd    ..2DNN,3

%-4

&-4

&-4

&rD   )rr   rs   rt   rN   r   r   r  r   AGGREGATORSr  r
  rP   r  r  r   r  r  r  r`   rD   rB   r   r     s   ,I *I  MJ KI NJ NJ U]];d+# ,# U]];d+# ,# U]];4F5 G5rD   r   c                   @    e Zd Z ej                  dd      ZdddiiZy)AdvancedzHoloViews .opts()z
        Options applied via HoloViews .opts().
        Examples:
        - image: {"color_levels": 11}
        - line: {"line_dash": "dashed"}
        - scatter: {'size': 5, 'marker': '^'}r|   optsr   z{'size': 5, 'marker': '^'}N)rr   rs   rt   rN   r   r  ri   r`   rD   rB   r  r    s,    5::!1D /KLMOrD   r  c                   4    e Zd Z ej                  dd      Zy)	StatusBarTzI
        Whether to automatically update the plot when a param is changedr   N)rr   rs   rt   rN   r   live_updater`   rD   rB   r  r    s    %--LKrD   r  c                       e Zd Z ej                         Z ej                         Z ej                         Z ej                  g d      Z	 ej                  g       Z
 ej                  g       Z ej                  e      Z ej                  e      Z ej                  e      Z ej                  e      Z ej                  e      Z ej                  e      Z ej                  e      Z ej                  e      Z ej:                  dd      Zed        Z d	 Z! fd
Z"d Z#d Z$d Z%e&d        Z'e&d        Z(e&d        Z)ddZ*d Z+d Z,d Z-ddZ.d Z/d Z0d Z1 xZ2S )r>   Y)r   r}   r   )r]   r\   z#
        Code to generate the plot.)r^   r~   c                 p    t        |      rt        d      t        |      rt        } nt        }  | |fi |S )Nz'GeoDataFrame objects not yet supported.)r   	TypeErrorr   hvGridExplorerhvDataFrameExplorer)clsr@   rf   s      rB   r?   zhvPlotExplorer.from_data  s7    4 EFFt_ C%C4"6""rD   c                     | j                   S r   )_layoutrj   s    rB   rk   zhvPlotExplorer.__panel__$  s    ||rD   c                    |j                  d      |j                  d      }}d|v r-t        |d   t              r|j                  d      n|d   g|d<   |j	                         D ci c]'  }|t
        j                  v s||j                  |      ) }}t        |||fi |j                         D ci c]  \  }}|dvs|| c}}}|j	                         D ci c]#  }|| j                  vs||j                  |      % }	}t        | (  d i | || _        || _        | j                  D 
ci c]  }
|
t        |
    }}
t        | g ddg |di      | _        | j                  j#                  | j$                  d       | j                  j#                  | j&                  d       | j                  j#                  | j(                  d	       | j+                          t-        j.                  d
      | _        t        d i || _        t-        j4                  | j2                  dt,        j6                  d      | _        | j                  j;                         j=                         D cg c]B  }t        |t        j>                        r&tA        |jB                  tD              r|jB                  D }}i }|D ]B  }|	j	                         D ci c]#  }||j                  v s||	j                  |      % c}||<   D |	rtG        d|	       |j                         D ci c])  \  }}|jH                  jK                          ||fd| i|+ c}}| _&         | j                  jN                  d i | jL                   | j                  j#                  | jP                  t        | j                               | jL                  j=                         D ];  }|j                  j#                  | jP                  t        |j                               = | j2                  j                  j#                  | jP                  t        | j2                  j                               t,        jR                  jU                  ddd      | _+        t,        jR                  jY                  ddddt,        j6                        | _-        t,        jR                  j]                  ddd      | _/        t-        j`                  | jV                  | j8                  t,        jb                  je                         t-        j6                  | j0                  t-        j.                  d| jZ                  jb                  fd| j^                  f      d      dd      | _3        | j                  ji                  d       y c c}w c c}}w c c}w c c}
w c c}w c c}w c c}}w )!Nxyy_multi)r   r!  r"  )r   r   r!  groupbybyr   )optionsgroups)rM   rV   r$  left)tabs_locationF)   8   r   r*  )rJ   default_layoutmarginz8__init__() got keyword(s) not supported by any control: rC   dangerrH   )
alert_typevisiblerI   stretch_both   )r)  r)  r)     bottom)rI   
min_heightr,  widget_locationwidget_layout)r)  r)  r   r2  )rI   r4  r,  PlotCode)rI   iX  )rI   r   r`   )5r   rO   r   popcopyr  rN   r   ro   rb   rc   ra   r   _groupsKINDSrY   	_controlsr   _toggle_controls_check_y	_check_by	_populaterS   Tabs_control_tabs	statusbarrT   Row
_statusbarr   rn   ru   
issubclassr]   r[   r  rF   lower_controllersr   _refreshrX   Alert_alert	HoloViews_hv_paneMarkdown
_code_paneColumnlayoutDividerr  trigger)rd   re   rf   r   r!  rp   statusbar_paramsrq   	converterextrasgroupr&  r   controlscontroller_paramsr  cparams
controllerrg   s                     rB   rc   zhvPlotExplorer.__init__'  s   zz#

31&=3=fSk43P

3W]^aWbVcF96<kkm\mqIOOG[Avzz!},m\ 1
*0,,.[.$!QAEZ<ZA.[
	 -3KKMQMqQdjj=P!VZZ]"MQ"6"
#37<<@<%%u%<@+:"f$EF

 	

..7

	2

.WW6:"6%56((NNeBFF>

 ZZ'')002
2!U001j86T HH2 	 

 C@F%`1QRVYV_V_Q_aA&6%`c" VW]V^_`` !2 7 7 9
 9W HHNNc"?t?w?? 9
 	

.D--.

TZZ(89++224J""4==$z7G7G2HI 5""4==$t~~7K7K2LMggmmO $ 
 ))& $&& * 
 ''**&3} + 
 yyKKOOIIFF"" !5!568QR* (
  	

6"S ][ R A 
 &a
sC   'V"?V"2V'?V'V-0V-1V2+AV7V<!V< .Wc                    | j                   j                  }t        | j                   dg       }|D cg c]	  }||vs| }}| j                  D ]y  }|dk(  r	| j                  |   }t	        |t        j
                        s3|dk(  r||_        n||_        |dk(  s|dk(  sRt        | |d      `t        | ||j                  d          { yc c}w )zO
        Populates the options of the controls based on the data type.
        indexesr   r   r!  Nr   )r   	variablesgetattrrN   rO   r   r   setattrrd   r_  r^  rq   variables_no_indexrW   r   s          rB   rA  zhvPlotExplorer._populatev  s     OO--	$//9b9)2GAaw6FaGZZE

5!A!U^^,C< )AI 2AI SLESLgdE46P6XD%16   Hs
   	CCc           	         d| j                   j                  v r| j                  n| j                  }t	        |t
              rt        |      dk(  r|d   }i }| j                  j                         j                         D ]x  }t	        |t              r|j                  s t	        |t              r|j                  j                  di       }Mt	        |t              s^|j                  |j                         z |j                  d      rd|vrgt!        j"                  t!        j$                  | j'                                     }|dk  rdnd	| j(                  _        | j(                  j*                  |d<   d
D ]:  }||vr|j-                  | di       }t/        |j-                  |      fi |||<   < |j-                  dd       }|j-                  dg       D 	ci c]  }	|	| c}	|d<   d|d<   | j0                  }
d}t        |
      t2        kD  r| j4                  t6        d   v s|j                  d      s|j                  d      sdt2         dt        |
       d}| j4                  dk(  r"dt2         d| }|
j9                  t2              }
nd| }|
j;                  t2              }
| j<                  j                  j                  |d       t        j>                  j                  jA                  |       d}|
| _        d| jB                  _"        	  tG        | j0                        d!| j4                  | jH                  || jJ                  | jL                  d|| _'        r | jN                  jP                  d!i | | jN                  | jR                  _*        t        | jR                  jV                        dkD  r"| jR                  jV                  D ]	  }d|_,         |sd| j<                  _-        d| jB                  _"        y c c}	w # t\        $ r4}| j<                  j                  j                  d | d       Y d }~Od }~ww xY w# d| jB                  _"        w xY w)"Nr"  r
   r   r  r   r.   r   r   r   )r.   r0   _kwargsr5   r4   i  r4  Fr#   r  r   zplotted z rows out of zZ rows to avoid performance issues; use rasterize=True or datashade=True to visualize more.r   zSelected the first z
 rows and zRandomly sampled and )nT)objectr/  r   r   r!  r$  r#  )r2  r)  r)  r)  z+**Rendering failed with following error**: r`   )/r=  rM   r"  r!  rO   r   lenrN   rn   r   r   r  rA   r   r[   r   npr8   absr   
geographicr.   r9  r   ra   MAX_ROWSr   r<  headsamplerL  mainwarningr  loading_hvPlotr   r$  r#  _hvplotr  rN  rg  
widget_boxr,  r/  	Exception)rd   r!  rA   rq   opts_kwargsxmaxr   r/   r5   featurere   
show_alertwarn_messagewes                  rB   _plotzhvPlotExplorer._plot  s   %)B)BBDLLa3q6Q;!A""$++-A!Z(!X&hhll626Ax(ahh' . ::eF"vvbffTYY[127;s{mHY# $ 3 3u,f$#ZZ3%w<
1&**S/PZPs	 -
 #JJ=MHN

S]_aHb!cHbW'="8Hb!cF:"|ZZ
r7XIIw'6::k+BfjjQ\F] 8*M#b' ;g h  yyF"!4XJjWWWX&!6|nEYYY*KK$$L$$GJJ$$\2J
#	).74::. YY$&&A$''4<<SYDL !!!0K0#'<<DMM  4==++,q011A,AH 2&+# $)DLL W "dL  	KK$$DQCHRV %  	
 $)DLL s1   
PCP 	Q*Q ;Q  QQ Qc                 "   | j                   j                  sy | j                          t        j                  j                  |       5  | j                         | _        d d d        d| j                   d| j                  _	        y # 1 sw Y   )xY w)Nz
```python
z
```)
rD  r  r~  rN   r   discard_events	plot_codecoderP  rg  )rd   eventss     rB   rJ  zhvPlotExplorer._refresh  sd    ~~))

  //5(DI 6%05!C 65s   BBc                      y)Nr@   r`   rj   s    rB   	_var_namezhvPlotExplorer._var_name  s    rD   c                 0    | j                   t        d   v ryy)Nr   TF)r   r<  rj   s    rB   	_single_yzhvPlotExplorer._single_y  s    99d#rD   c                     t        d      )Nz"Must be implemented by subclasses.)NotImplementedErrorrj   s    rB   r;  zhvPlotExplorer._groups  s    !"FGGrD   c                    d}|r|j                   dv rddg}d}n<|r|j                   t        d   v rg d}n |r|j                   dv rd | _        g d	}ng d
}|| j                  _        d| j                  fg}|r|d| j
                  fd| j                  fd| j                  fd| j                  fd| j                  fd| j                  fgz  }|r,|j                   dvr|j                  dd| j                  f       || j                  d d  y )NT)tabler   r   columnsFr   rh  )r   r   density)r   r"  r$  r#  )r   r   r"  r$  r#  Fieldsr   r   r   r   r   r  )arear   r   ohlcrgbstepr)  r{   )newr<  r   r=  rM   axesr   style
operationsrl  advancedr   colormappingrC  )rd   eventr/  rM   tabss        rB   r>  zhvPlotExplorer._toggle_controls  s   UYY"66 ),JGuyyE$K/<Juyy$>>DF=JBJ$.!4>>*+#4;;'$**%t/t/T]]+ D *XXA0A0ABC $1rD   c                 r    t        |j                        dkD  r| j                  r|j                  | _        y y y Nr
   )ri  r  r$  oldr!  rd   r  s     rB   r?  zhvPlotExplorer._check_y  s+    uyy>A$''YYDF #*rD   c                     |j                   rGd| j                  j                  v r.| j                  r!t	        | j                        dkD  rg | _        y y y y y )Nr"  r
   )r  r=  rM   r"  ri  r$  r  s     rB   r@  zhvPlotExplorer._check_by	  sN    IIT^^666DLL!A%DG &  7 rD   c                 6    | j                   j                         S )z&Return the plot as a HoloViews object.)rt  clonerj   s    rB   hvplotzhvPlotExplorer.hvplot  s    ||!!##rD   c                     | j                         }d|vrd|d<   d|d<   d}|r| j                  |      }|xs | j                   d| d}| j                  j                  }|r| j                  |      }|d| dz  }|S )	a  Return a string representation that can be easily copy-pasted
        in a notebook cell to create a plot from a call to the `.hvplot`
        accessor, and that includes all the customized settings of the explorer.

        >>> hvexplorer.plot_code(var_name='data')
        "data.hvplot(x='time', y='value')"

        Parameters
        ----------
        var_name: string
            Data variable name by which the returned string will start.
        r   r   r3  r5  rm   z	.hvplot(
z
)z.opts(
)settings_build_kwargs_stringr  r  r  )rd   var_namer  settings_argssnippetr  	opts_argss          rB   r  zhvPlotExplorer.plot_code  s     ==?8#!/HX&."# 55h?M/0
=/M}}!!11$7I)C00GrD   c                 `    d}|r)|j                         D ]  \  }}|d| d|dz  } |d d }|S )Nrm   z    =z,
r\   )ro   )rd   rA   argsrp   rq   s        rB   r  z#hvPlotExplorer._build_kwargs_string5  sG    1$qc1%s++ '9DrD   c                 F    t        j                  | j                  |fi | y)a5  Save the plot to file.

        Calls the `holoviews.save` utility, refer to its documentation
        for a full description of the available kwargs.

        Parameters
        ----------
        filename: string, pathlib.Path or IO object
            The path or BytesIO/StringIO object to save to
        N)_hvsavert  )rd   filenamerA   s      rB   r  zhvPlotExplorer.save=  s     	x262rD   c                 L   i }| j                   j                         D ]Q  }t        |j                        ddhz
  }|D ]0  }t	        ||      }||j                  |   j
                  k7  s,|||<   2 S | j                  j                  D ]5  }t	        | |      }|| j                  |   j
                  k7  s|dk(  s1|||<   7 d|v r|j                  d      |d<   |j                  dd       t        t        |j                                     D ci c]  \  }}||
 }}}|S c c}}w )a   Return a dictionary of the customized settings.

        This dictionary can be reused as an unpacked input to the explorer or
        a call to the `.hvplot` accessor.

        >>> hvplot.explorer(df, **settings)
        >>> df.hvplot(**settings)
        rF   rC   r   r"  r!  r  N)rI  rn   setrN   r`  r   r=  rM   r9  r   r   ro   )rd   r  r\  rf   r   valuerp   rq   s           rB   r  zhvPlotExplorer.settingsJ  s    ++224J))*fj-AAF
A.J,,Q/777"'HQK  5 **AD!$E

1---f# +  $LL3HSMVT"%+D1A,B%CD%CTQAqD%CD Es   D r   )3rr   rs   rt   rN   r   r   r   r!  r   r"  r$  r#  ru   r   r  r{   r  r   r   r   rl  r   r  r  rD  r   r  r  r  r   r  classmethodr?   rk   rc   rA  r~  rJ  rw   r  r  r;  r>  r?  r@  r  r  r  r  r  rx   ry   s   @rB   r>   r>     s   5>>DAA e  37G			B	'B e  ,G 5d+D&5&&l;L U  /F$$$J7J$$$J7J###95IEu-E"u""(3H5<<&D # #M#^7*E)ND    
 H H%:$63rD   r>   c                       e Zd Z ej                  ded         Zed        Zed        Z	ed        Z
ed        Z ej                  d      d	        Z ej                  d
      d        Zed        Zy)hvGeomExplorerNr$   r   c                      y)Ngdfr`   rj   s    rB   r  zhvGeomExplorer._var_nameh  s    rD   c                      yr   r`   rj   s    rB   r  zhvGeomExplorer._single_yl      rD   c                      y r   r`   rj   s    rB   _xzhvGeomExplorer._xp  r  rD   c                      y r   r`   rj   s    rB   _yzhvGeomExplorer._yt  r  rD   r   c                      y r   r`   rj   s    rB   r   zhvGeomExplorer.xlimx      rD   r!  c                      y r   r`   rj   s    rB   r   zhvGeomExplorer.ylim|  r  rD   c                 
    ddgS )Nr   r   r`   rj   s    rB   r;  zhvGeomExplorer._groups  s    ;''rD   )rr   rs   rt   rN   r   r<  r   rw   r  r  r  r  r   r   r   r;  r`   rD   rB   r  r  e  s    5>>$e=D        U]]3  U]]3  ( (rD   r  c                        e Zd Z ej                  ded         Z fdZed        Z	ed        Z
ed        Z ej                  d      d	        Z ej                  d
d      d        Zed        Zd Z xZS )r  imager$   r   c                    dd l }d}t        ||j                        rt        |j                        }t        |      dk(  r||d      }d|d    d}nt        |j                        t        |j                        z
  }|r5|D ]0  }t        j                  t        ||               |j                  |<   2 |j                  d      j                  dd      }d}d	|vrd
|d	<   || _        t        | <  |fi | y )Nr   rm   r
   z['z']variable.z0.to_array('variable').transpose(..., 'variable')r   r  )xarrayrO   Datasetr   	data_varsri  r  dimscoordsrj  arangeto_array	transpose_var_name_suffixrb   rc   )	rd   dsrf   xrvar_name_suffixr  missing_dimsdimrg   s	           rB   rc   zhvGridExplorer.__init__  s    b"**%R\\*I9~"	!%$&y|nB"7"277|c"))n<+)+3r#w<)@		#  ,[[,66sJG"T$F6N /&v&rD   c                 :    | j                   rd| j                    S y)Nr  da)r  rj   s    rB   r  zhvGridExplorer._var_name  s"      --.//rD   c                     | j                   1| j                  j                   xs | j                  j                  d   S | j                   S Nr   )r   r   r^  rj   s    rB   r  zhvGridExplorer._x  :    DHFFN!!?T__%<%<Q%?^X\X^X^^rD   c                     | j                   1| j                  j                   xs | j                  j                  d   S | j                   S r  )r!  r   r^  rj   s    rB   r  zhvGridExplorer._y  r  rD   r   c                     	 | j                   | j                     }|j                  j                  dv ry t        j                  |      t        j                  |      fS # t        $ r Y yw xY w)Nr   r
   r   )ra   r  rv  r   r   rj  nanminnanmaxrd   rn   s     rB   r   zhvGridExplorer.xlim  sa    	ZZ(F <<%		&!299V#455	  		s   A 	A*)A*r!  r"  c           	            j                   }t        |t              s|g} fd|D        }t        |D cg c],  }t	        j
                  |      t	        j                  |      f. c}      S c c}w )Nc              3   <   K   | ]  }j                   |     y wr   ra   r   r!  rd   s     rB   r   z&hvGridExplorer.ylim.<locals>.<genexpr>  s     +A$**Q-   )r  rO   r   r   rj  r  r  )rd   r!  rn   vss   `   rB   r   zhvGridExplorer.ylim  sY    GG!T"A++6J6R299R="))B-86JKKJs   1A-c                 
    g dS )N)r   r   r   r`   rj   s    rB   r;  zhvGridExplorer._groups  s    //rD   c                    | j                   j                  }t        | j                   dg       }|D cg c]	  }||vs| }}| j                  D ]  }|dk(  r	| j                  |   }t	        |t        j
                        s3|dv r||_        n||_        |dk(  r(t        | |d       t        | ||j                  d          s|dk(  r(t        | |d       t        | ||j                  d          |dk(  st        t        | |g             dk(  st        |j                        d	kD  st        | ||j                  d	d          y c c}w )
Nr^  r   )r   r!  r#  r$  r   r   r!  r
   r#     )	r   r_  r`  rN   rO   r   r   ra  ri  rb  s          rB   rA  zhvGridExplorer._populate  s(   OO--	$//9b9)2GAaw6FaGZZE

5!A!U^^,77 'AI 2AI C<GD%$>$FD%16c\gdE4&@&HD%16Y&GD%45:AII*D%127)   Hs
   	EE)rr   rs   rt   rN   r   r<  r   rc   rw   r  r  r  r   r   r   r;  rA  rx   ry   s   @rB   r  r    s    5>>'5<@D',   _ _ _ _ U]]36 6 U]]3	"L #L 0 08rD   r  c                      e Zd Z ej                         Z ej                  ded         Zed        Z	ed        Z
ed        Zed        Zed        Z ej                  d	      d
        Z ej                  dd      d        Zy)r  r   r$   r   c                      y)Nre   r`   rj   s    rB   r  zhvDataFrameExplorer._var_name  r  rD   c                     | j                   dv ry| j                  | j                     }|j                  j                   dv S )N)r!   boxr    Fr   )r   ra   r   r   r  s     rB   xcatzhvDataFrameExplorer.xcat  s9    9900DFF#||  E))rD   c                     | j                   1| j                  j                   xs | j                  j                  d   S | j                   S r  )r   r   r_  rj   s    rB   r  zhvDataFrameExplorer._x  s:    FJffn!!AT__%>%>q%A`Z^Z`Z``rD   c                    d| j                   j                  v r| j                  r| j                  }nmd| j                   j                  vr| j                  r| j                  }n<| j                  j                  | j                  | j                  d | j                        }t        |t              rt        |      dk(  r|d   }|S )Nr"  r
   r   )r=  rM   r"  r!  r   _process_chart_yra   r  r  rO   r   ri  )rd   r!  s     rB   r  zhvDataFrameExplorer._y  s    111dllAdnn777DFFA00TWWdDNN[Aa3q6Q;!ArD   c                     dgS )Nr   r`   rj   s    rB   r;  zhvDataFrameExplorer._groups  s
    }rD   r   c                    | j                   dk(  r!| j                  j                  j                  }n	 | j                  | j                      }t        |d      r|j                         }|j                  j                  dv ry t        |      s t        j                  t        j                  fS t        j                  |      t        j                  |      fS # t        $ r Y yw xY w)Nindexr  compute_chunk_sizesr   )r  ra   r  rn   rv  hasattrr  r   r   ri  rj  nanr  r  r  s     rB   r   zhvDataFrameExplorer.xlim  s    77gZZ%%,,FDGG, 601//1F<<%VFFBFF##		&!299V#455  s   C 	C! C!r!  r"  c           	      v     j                   }t        |t              s|g} fd|D        D cg c]  }t        |      s| }}t        |      s t        j
                  t        j
                  fS t        |D cg c],  }t	        j                  |      t	        j                  |      f. c}      S c c}w c c}w )Nc              3   <   K   | ]  }j                   |     y wr   r  r  s     rB   r   z+hvDataFrameExplorer.ylim.<locals>.<genexpr>  s     9q!

1qr  )	r  rO   r   ri  rj  r  r   r  r  )rd   r!  ysrn   r  s   `    rB   r   zhvDataFrameExplorer.ylim  s    GG!T"A9q9E9SW"9E6{FFBFF##6J6R299R="))B-86JKK F Ks   B1B191B6N)rr   rs   rt   rN   r   zr<  r   rw   r  r  r  r  r;  r   r   r   r`   rD   rB   r  r    s    A5>>)U5\BD  * * a a 	 	   U]]36 6" U]]3	"L #LrD   r  )NN)=	holoviewsr  numpyrj  panelrS   rN   holoviews.core.utilr   r   r   r   holoviews.elementr   holoviews.plotting.utilr   panel.viewabler	   rV  r   r   plottingr   rs  utilr   r   r   r   r   r  _kind_mapping_gridded_types_geom_typesr<  endswithr   _default_cmapsr   r   r   r   r  rm  rR   rC   rY   r[   r{   r   r   r   r   r   r  Parameterizedr  r>   r  r  r  )cms   0rB   <module>r     s-       O O * . ! 9 ' R R
 L&&'
l))
*	+
l&&
'	( x!
"	# c,556i[9IIJ$$
	 SIFm d
 Sgc%,uY/??%-OPQe l>l"++f*=l>++]FVL))		 B 4F.
v 
.9'8 9'x'H '88 8vX <~) ~)B95 95xNx N## nV nb(^ (@V8^ V8r>L. >Lu 	?s   G(G(