U
    SZf%3                     @   sz   d dl Z d dlmZmZ d dlmZ d dlmZ d dlZd dl	m
Z edZdd Zdddddddd	ddi fd
dZdS )    N)
exceptionsoptional_imports)utilsZpandasc                 C   s  t | }|dkr|nd}|dkr$dn|}|	s4d| }	|
s@d| }
tjj||d|	|
d}|d jtg ddd	d
ddg t|dkrdnddd |d | |dkrd}d}nd}d}|d D ]z}d|ksd|krd|d | d< d|d | d< ||kr
d|d | d< ||krd|d | d< ddg|d | d< q|dkrZddg|d |d  d< |shddg}|svddg}t|D ] }tt | j| d  D ]}t	|d |d t | j| d  d!}|dkrt
| j| d  d"|  gndg}|dkr dgnt
| j| d  d"|  g}tj||t|d"|  d#d |dkrDd$nd%|d&dd'|d d(|d d)
}|| qtt | j| d* D ]}t	|d |d t | j| d* d!}|dkrt
| j| d* d"|  gnd+g}|dkrd+gnt
| j| d* d"|  g}tj||t|d"|  d#d*|dkr8d$nd%|ddd'|d d(|d d)
}|| q|dkr| j| d, nd+g}|dkrd+gn| j| d, }tjf ||d,|dkrd$nd%d'|d d(|d d-|}|| | j| d. }d/| kr d0| j| d/ }nd1}d2|| }t||dkrJ|| n|d ||dkr`|
n|	|dkrpd3nd4|dkrd5ndd}|d d6  |f7  < q~|S )7Nh   g      ?F)Z
print_gridhorizontal_spacingvertical_spacingZlayout)ZshapeszBullet ChartiX  i  stackx   P   )l)titleheightwidthZ
showlegendZbarmodeannotationsmarginyaxisxaxisZshowgridZzerolineZ	tickwidthZshowticklabelsr   rangeg?g333333?1domainrgb(200, 200, 200)rgb(245, 245, 245)rgb(31, 119, 180)rgb(176, 196, 221)rangesrgb)colorxy   zx{}zy{})
r   r    markername	hoverinfoorientationr   baser   r   measuresg      ?markers)r   r    r#   r$   r   r   titles	subtitlesz<br>{} z	<b>{}</b>rowcolTr   )lenplotlyZsubplotsZmake_subplotsupdatedictr   ZilocclrsZn_colorssortedgoBarformatZ	add_traceZScatterr   Zannotation_dict_for_label)dfr(   r'   r   r*   r)   r%   range_colorsmeasure_colorsr   r   scatter_optionslayout_optionsZnum_of_lanesZnum_of_rowsZnum_of_colsfigZ
width_axisZlength_axiskeyr,   idxZinter_colorsr   r    barr   subtitlelabelZannot rB   A/tmp/pip-unpacked-wheel-5ksk5baj/plotly/figure_factory/_bullet.py_bullet   s   	

    
 



	rD   r   )r   r   )r   r   c                    s  t stdt| r4tdd | D sJtdnt| t jsJtddddd	d
g}t| r t jr~fdd| D ndgt	|  rfdd| D ndgt	|   r fdd| D ng gt	|  rfdd| D ng gt	|  rfdd| D ng gt	|  g|d}nt| t jrt jrD|  
 ndgt	|  rb|  
 ndgt	|   r|   
 ng gt	|  r|  
 ng gt	|  r|  
 ng gt	|  g|d}t j|}dD ]`}t|| D ]L\}}z*t|}|s|dkr"g || |< W n tk
r:   Y nX qq||fD ]>}|rLt	|dkrntdt| t|dd }qLdddddi}|i kr|| n2|d D ](}||d kr|d | |d |< qt| ||||	|
||}|S )a  
    **deprecated**, use instead the plotly.graph_objects trace
    :class:`plotly.graph_objects.Indicator`.

    :param (pd.DataFrame | list | tuple) data: either a list/tuple of
        dictionaries or a pandas DataFrame.
    :param (str) markers: the column name or dictionary key for the markers in
        each subplot.
    :param (str) measures: the column name or dictionary key for the measure
        bars in each subplot. This bar usually represents the quantitative
        measure of performance, usually a list of two values [a, b] and are
        the blue bars in the foreground of each subplot by default.
    :param (str) ranges: the column name or dictionary key for the qualitative
        ranges of performance, usually a 3-item list [bad, okay, good]. They
        correspond to the grey bars in the background of each chart.
    :param (str) subtitles: the column name or dictionary key for the subtitle
        of each subplot chart. The subplots are displayed right underneath
        each title.
    :param (str) titles: the column name or dictionary key for the main label
        of each subplot chart.
    :param (bool) orientation: if 'h', the bars are placed horizontally as
        rows. If 'v' the bars are placed vertically in the chart.
    :param (list) range_colors: a tuple of two colors between which all
        the rectangles for the range are drawn. These rectangles are meant to
        be qualitative indicators against which the marker and measure bars
        are compared.
        Default=('rgb(200, 200, 200)', 'rgb(245, 245, 245)')
    :param (list) measure_colors: a tuple of two colors which is used to color
        the thin quantitative bars in the bullet chart.
        Default=('rgb(31, 119, 180)', 'rgb(176, 196, 221)')
    :param (float) horizontal_spacing: see the 'horizontal_spacing' param in
        plotly.tools.make_subplots. Ranges between 0 and 1.
    :param (float) vertical_spacing: see the 'vertical_spacing' param in
        plotly.tools.make_subplots. Ranges between 0 and 1.
    :param (dict) scatter_options: describes attributes for the scatter trace
        in each subplot such as name and marker size. Call
        help(plotly.graph_objs.Scatter) for more information on valid params.
    :param layout_options: describes attributes for the layout of the figure
        such as title, height and width. Call help(plotly.graph_objs.Layout)
        for more information on valid params.

    Example 1: Use a Dictionary

    >>> import plotly.figure_factory as ff

    >>> data = [
    ...   {"label": "revenue", "sublabel": "us$, in thousands",
    ...    "range": [150, 225, 300], "performance": [220,270], "point": [250]},
    ...   {"label": "Profit", "sublabel": "%", "range": [20, 25, 30],
    ...    "performance": [21, 23], "point": [26]},
    ...   {"label": "Order Size", "sublabel":"US$, average","range": [350, 500, 600],
    ...    "performance": [100,320],"point": [550]},
    ...   {"label": "New Customers", "sublabel": "count", "range": [1400, 2000, 2500],
    ...    "performance": [1000, 1650],"point": [2100]},
    ...   {"label": "Satisfaction", "sublabel": "out of 5","range": [3.5, 4.25, 5],
    ...    "performance": [3.2, 4.7], "point": [4.4]}
    ... ]

    >>> fig = ff.create_bullet(
    ...     data, titles='label', subtitles='sublabel', markers='point',
    ...     measures='performance', ranges='range', orientation='h',
    ...     title='my simple bullet chart'
    ... )
    >>> fig.show()

    Example 2: Use a DataFrame with Custom Colors

    >>> import plotly.figure_factory as ff
    >>> import pandas as pd
    >>> data = pd.read_json('https://cdn.rawgit.com/plotly/datasets/master/BulletData.json')

    >>> fig = ff.create_bullet(
    ...     data, titles='title', markers='markers', measures='measures',
    ...     orientation='v', measure_colors=['rgb(14, 52, 75)', 'rgb(31, 141, 127)'],
    ...     scatter_options={'marker': {'symbol': 'circle'}}, width=700)
    >>> fig.show()
    z3'pandas' must be installed for this figure factory.c                 s   s   | ]}t |tV  qd S )N)
isinstancer1   ).0itemrB   rB   rC   	<genexpr>  s     z create_bullet.<locals>.<genexpr>zGEvery entry of the data argument list, tuple, etc must be a dictionary.z=You must input a pandas DataFrame, or a list of dictionaries.r)   r@   r(   r'   r   c                    s   g | ]}|  qS rB   rB   rF   d)r)   rB   rC   
<listcomp>'  s     z!create_bullet.<locals>.<listcomp>r+   c                    s   g | ]}|  qS rB   rB   rI   )r*   rB   rC   rK   (  s     c                    s   g | ]}|  qS rB   rB   rI   )r(   rB   rC   rK   )  s     c                    s   g | ]}|  qS rB   rB   rI   )r'   rB   rC   rK   *  s     c                    s   g | ]}|  qS rB   rB   rI   )r   rB   rC   rK   +  s     )index)r   r'   r(   Nr!   zKBoth 'range_colors' or 'measure_colors' must be a list of two valid colors.r   r   r"      zdiamond-tallzrgb(0, 0, 0))sizesymbolr   )pdImportErrorr   Zis_sequenceallr   ZPlotlyErrorrE   Z	DataFramer.   tolistZ	transpose	enumeratemathisnan	TypeErrorr2   Zvalidate_colorsZconvert_colors_to_same_typer0   rD   )datar(   r'   r   r*   r)   r%   r8   r9   r   r   r:   r;   Z	col_namesr7   Z
needed_keyr>   rZr_is_nanZcolors_listZdefault_scatterkr<   rB   )r(   r'   r   r*   r)   rC   create_bullet   s    ]
""""$



 

r[   )rU   r/   r   r   Zplotly.colorscolorsr2   Zplotly.figure_factoryr   Zplotly.graph_objsZ
graph_objsr4   Z
get_modulerP   rD   r[   rB   rB   rB   rC   <module>   s(   
 ,