U
    SZfч                     @   s   d dl mZ d dlZd dlmZmZ d dlmZ d dl	m
Z
 d dlmZ edZdddgZd	d
 Zdd ZdddZdddZdddZdddZdS )    )NumberN)
exceptionsoptional_imports)utilsZpandasTaskStartFinishc                 C   s   | ||| g||||gfS )z
    Returns the corner points of a scatter rectangle

    :param x0: x-start
    :param y0: y-lower
    :param x1: x-end
    :param y1: y-upper
    :return: ([x], [y]), tuple of lists containing the x and y values
     )x0y0x1y1r	   r	   @/tmp/pip-unpacked-wheel-5ksk5baj/plotly/figure_factory/_gantt.py_get_corner_points   s    r   c                 C   s   t rt| t jjjrtD ]"}|| krtdd	tqt
| j}g }t|D ].}i }| D ]}| j| | ||< q^|| qR|S t| tstdt
| dkrtdt| d tstd| S )z2
    Validates the inputted dataframe or list
    zBThe columns in your dataframe must include the following keys: {0}z, z<You must input either a dataframe or a list of dictionaries.r   z<Your list is empty. It must contain at least one dictionary.z)Your list must only include dictionaries.)pd
isinstancecoreframeZ	DataFrameREQUIRED_GANTT_KEYSr   PlotlyErrorformatjoinlenindexrangeZilocappendlistdict)dfkeyZnum_of_rowschartr   Z	task_dictr	   r	   r   validate_gantt   s6    


r!   FTc                    sn  |dkrg }|	dkrg }	|
dkr$g }
t t| D ]P}t| | d | | d | | d d}d| | krv| | d |d< || q0t t  |rd}nd	}g g d
d|d}g g dg tddddddd}t t|D ]&}|| d }|r||	kr|	| q|r|	  d}t t|D ]d}|| d }|| d= |}|rL|	|}|| || d< || || d< |t|kr~d}|| || d< || d }|krt||< || d< t|| d< || d< t| d dkr | d | d d  | d d t	|| d || d || d || d \}}| d  |7  < | d  |7  < | krt| |< | | d d< | | d<  | d || d   | d || d   | d |  | d | d|| krT | d || d   | d || d  || d= n$ | d d  | d d |d7 }q|}t||||g d t||	t
t t|	dt|	d gddd!t|dtt
td"d#d$d%d&tdd'd(d%d&td)d*d(d%d&tdd+d,d-d&tdd.d,d%d&td/d0gd1d2d3d4}fd5d6tD }
|
 fd7d6t D 7 }
tj|
|d8}|S )9z/
    Refer to create_gantt() for docstring
    Nr   r   r   r
   r   nameDescriptiondescriptionr#   skipnonetoself)xymodefill	hoverinfomarkers    r   colorsizeZopacityFr)   r*   r+   textmarkerr#   
showlegendr   r   	fillcolorlegendgroupr)   r*   r
   r   r6   r2   r5   closestshowgridZticktextZtickvalsr   Z	autorangezeroline   1wdaybackwardcountlabelstepZstepmode1mmonth   6mYTDyeartodate1yallrF   Zbuttonsdater=   r>   Zrangeselectortypetitler7   heightwidthZshapesZ	hovermodeZyaxisZxaxisc                    s   g | ]} | qS r	   r	   .0kscatter_data_dictr	   r   
<listcomp>   s     zgantt.<locals>.<listcomp>c                    s   g | ]} | qS r	   r	   rY   marker_data_dictr	   r   r^      s     datalayout)r   r   r   r   reverser   copydeepcopystrr   r   sortedgoFigure)r    colorsrV   	bar_width
showgrid_x
showgrid_yrW   rX   tasks
task_namesrb   group_tasksshow_hover_fillshow_colorbarr   taskr-   scatter_data_templatemarker_data_templatetnZcolor_indexgroupIDcolor_idxsysr7   rc   figr	   r`   r]   r   ganttD   s    


	







"r~   c           &         s	  |
dkrg }
|dkrg }|dkr$g }d}t t| D ]P}t| | d | | d | | d d}d| | krz| | d |d< |
| q4t t  |rd	}nd
}g g ddd|dd}g g dg tdddddddd}g }t t|
D ]&}| | | |kr|| | |  q|  t| d | trt|dk r@tdt t|
D ],}|
| d	 }|rl||krL|| qL|r|	  t t|
D ]}|
| d	 }|
| d	= |}|r|
|}|| |
| d< || |
| d< t|tj}|d }|d }| | | d }t|||}t|tj}||
| d< |
| d }|kr^t||< || d< t| | | | d	< || d< t|tj}t| d dkr| d | d d  | d d t|
| d |
| d |
| d |
| d \}}| d  |7  < | d  |7  < | krpt| |< | | d  d!< | | d<  | d |
| d   | d |
| d   | d |  | d | d|
| kr | d" |
| d   | d" |
| d  |
| d= n$ | d" d  | d" d q|d#krt  d }  |  d  td|d gd|d ggd#d$dd% t| d | tr"g }t t|
D ]*}| | | |kr|| | |  q|  t|t|k rtd&i }!d}"|D ]0}#|"t|d kr$d}"||" |!|#< |"d7 }"q
t t|
D ],}|
| d	 }|rh||krH|| qH|r|	  t t|
D ]h}|
| d	 }|
| d	= |}|r|
|}|| |
| d< || |
| d< |!| | |  |
| d< |
| d }|krt||< || d< || d< t| | | | d	< t|tj}t| d dkr| d | d d  | d d t|
| d |
| d |
| d |
| d \}}| d  |7  < | d  |7  < | kr.t| |< | | d  d!< | | d<  | d |
| d   | d |
| d   | d |  | d | d|
| krԈ | d" |
| d   | d" |
| d  |
| d= n$ | d" d  | d" d q|d#kr"d#}D ]} ||  d'< qt||||	g d(t||tt t|dt|d gddd)t|dtttd*d+d,d-d.tdd/d0d-d.td1d2d0d-d.tdd3d4d5d.tdd6d4d-d.td7d8gd9d:d;d<}$fd=d>tD }| fd?d>t D 7 }tj||$d@}%|%S )A=
    Refer to FigureFactory.create_gantt() for docstring
    NFr   r   r   r"   r$   r%   r#   r&   r'   r(   r/   )r)   r*   r+   r,   r7   r-   r9   r.   r0   r   r1   )r)   r*   r+   r5   r6   r#   r7   r9      zYou must use at least 2 colors in 'colors' if you are using a colorscale. However only the first two colors given will be used for the lower and upper bounds on the colormap.r   r   g      Y@r8   r9   r)   r:   r*   r
   r   r6   r2   r5   Td   )Z
colorscaleZ	showscaleZcmaxZcminztError. The number of colors in 'colors' must be no less than the number of unique index values in your group column.r7   r;   r<   r?   r@   rA   rB   rC   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rU   c                    s   g | ]} | qS r	   r	   rY   r\   r	   r   r^   K  s     z$gantt_colorscale.<locals>.<listcomp>c                    s   g | ]} | qS r	   r	   rY   r_   r	   r   r^   L  s     ra   )r   r   r   r   sortr   r   r   r   rd   r   clrsZcolor_parserZunlabel_rgbZfind_intermediate_colorZ	label_rgbre   rf   rg   r   r   keysupdaterh   ri   rj   )&r    rk   rV   	index_colrs   rl   rm   rn   rW   rX   ro   rp   rb   rq   rr   r7   r   rt   r-   ru   rv   
index_valsrowrw   rx   ZlowcolorZ	highcolorZintermedZintermed_colorry   rz   r{   r[   Zindex_vals_dictZc_indexr   rc   r|   r	   r}   r   gantt_colorscale   s   











	








"r   c                     s  |
dkrg }
|dkrg }|dkr$g }d}t t| D ]P}t| | d | | d | | d d}d| | krz| | d |d< |
| q4t t  |rd	}nd
}g g dd|dd}g g dg tddddddd}g }t t|
D ]&}| | | |kr|| | |  q|  |D ]}||krtdqt t|
D ],}|
| d	 }|r\||kr<|| q<|rx|  t t|
D ]B}|
| d	 }|
| d	= |}|r||}|| |
| d< || |
| d< || | |  |
| d< |
| d }|krt	
||< || d< || d< t| d dkrn| d | d d  | d d t|
| d |
| d |
| d |
| d \}}| d  |7  < | d  |7  < | krt	
| |< | | d d< | | d<  | d |
| d   | d |
| d   | d |  | d | d|
| kr | d  |
| d   | d  |
| d  |
| d= n$ | d  d  | d  d q|d!krd!}|D ]}t|||  d	< qt||||	g d"t||tt t|dt|d gddd#t|dtttd$d%d&d'd(tdd)d*d'd(td+d,d*d'd(tdd-d.d/d(tdd0d.d'd(td1d2gd3d4d5d6}fd7d8tD }| fd9d8t D 7 }tj||d:}|S );r   NFr   r   r   r"   r$   r%   r#   r&   r'   r(   r/   )r)   r*   r+   r,   r-   r9   r.   r0   r   r1   r4   zdIf you are using colors as a dictionary, all of its keys must be all the values in the index column.r   r   r8   r9   r)   r:   r*   r
   r   r6   r2   r5   Tr;   r<   r?   r@   rA   rB   rC   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rU   c                    s   g | ]} | qS r	   r	   rY   r\   r	   r   r^     s     zgantt_dict.<locals>.<listcomp>c                    s   g | ]} | qS r	   r	   rY   r_   r	   r   r^     s     ra   )r   r   r   r   r   r   r   rd   r   re   rf   r   rg   r   rh   ri   rj   ) r    rk   rV   r   rs   rl   rm   rn   rW   rX   ro   rp   rb   rq   rr   r7   r   rt   r-   ru   rv   r   r   r   rw   rx   ry   rz   r{   Zindex_valuerc   r|   r	   r}   r   
gantt_dictU  s
   














"r   Gantt Chart皙?X  c                 C   s  t | }|rH||d kr"tdg }|D ]}|||  q*t| t|tr`t	|d}nt
|d}|dkr||  |st|trtdt|||||||	|
ddd|||d}|S t|tst|||||||||	|
ddd||d}|S t|||||||||	|
ddd||d}|S dS )	a  
    **deprecated**, use instead
    :func:`plotly.express.timeline`.

    Returns figure for a gantt chart

    :param (array|list) df: input data for gantt chart. Must be either a
        a dataframe or a list. If dataframe, the columns must include
        'Task', 'Start' and 'Finish'. Other columns can be included and
        used for indexing. If a list, its elements must be dictionaries
        with the same required column headers: 'Task', 'Start' and
        'Finish'.
    :param (str|list|dict|tuple) colors: either a plotly scale name, an
        rgb or hex color, a color tuple or a list of colors. An rgb color
        is of the form 'rgb(x, y, z)' where x, y, z belong to the interval
        [0, 255] and a color tuple is a tuple of the form (a, b, c) where
        a, b and c belong to [0, 1]. If colors is a list, it must
        contain the valid color types aforementioned as its members.
        If a dictionary, all values of the indexing column must be keys in
        colors.
    :param (str|float) index_col: the column header (if df is a data
        frame) that will function as the indexing column. If df is a list,
        index_col must be one of the keys in all the items of df.
    :param (bool) show_colorbar: determines if colorbar will be visible.
        Only applies if values in the index column are numeric.
    :param (bool) show_hover_fill: enables/disables the hovertext for the
        filled area of the chart.
    :param (bool) reverse_colors: reverses the order of selected colors
    :param (str) title: the title of the chart
    :param (float) bar_width: the width of the horizontal bars in the plot
    :param (bool) showgrid_x: show/hide the x-axis grid
    :param (bool) showgrid_y: show/hide the y-axis grid
    :param (float) height: the height of the chart
    :param (float) width: the width of the chart

    Example 1: Simple Gantt Chart

    >>> from plotly.figure_factory import create_gantt

    >>> # Make data for chart
    >>> df = [dict(Task="Job A", Start='2009-01-01', Finish='2009-02-30'),
    ...       dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
    ...       dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')]

    >>> # Create a figure
    >>> fig = create_gantt(df)
    >>> fig.show()


    Example 2: Index by Column with Numerical Entries

    >>> from plotly.figure_factory import create_gantt

    >>> # Make data for chart
    >>> df = [dict(Task="Job A", Start='2009-01-01',
    ...            Finish='2009-02-30', Complete=10),
    ...       dict(Task="Job B", Start='2009-03-05',
    ...            Finish='2009-04-15', Complete=60),
    ...       dict(Task="Job C", Start='2009-02-20',
    ...            Finish='2009-05-30', Complete=95)]

    >>> # Create a figure with Plotly colorscale
    >>> fig = create_gantt(df, colors='Blues', index_col='Complete',
    ...                    show_colorbar=True, bar_width=0.5,
    ...                    showgrid_x=True, showgrid_y=True)
    >>> fig.show()


    Example 3: Index by Column with String Entries

    >>> from plotly.figure_factory import create_gantt

    >>> # Make data for chart
    >>> df = [dict(Task="Job A", Start='2009-01-01',
    ...            Finish='2009-02-30', Resource='Apple'),
    ...       dict(Task="Job B", Start='2009-03-05',
    ...            Finish='2009-04-15', Resource='Grape'),
    ...       dict(Task="Job C", Start='2009-02-20',
    ...            Finish='2009-05-30', Resource='Banana')]

    >>> # Create a figure with Plotly colorscale
    >>> fig = create_gantt(df, colors=['rgb(200, 50, 25)', (1, 0, 1), '#6c4774'],
    ...                    index_col='Resource', reverse_colors=True,
    ...                    show_colorbar=True)
    >>> fig.show()


    Example 4: Use a dictionary for colors

    >>> from plotly.figure_factory import create_gantt
    >>> # Make data for chart
    >>> df = [dict(Task="Job A", Start='2009-01-01',
    ...            Finish='2009-02-30', Resource='Apple'),
    ...       dict(Task="Job B", Start='2009-03-05',
    ...            Finish='2009-04-15', Resource='Grape'),
    ...       dict(Task="Job C", Start='2009-02-20',
    ...            Finish='2009-05-30', Resource='Banana')]

    >>> # Make a dictionary of colors
    >>> colors = {'Apple': 'rgb(255, 0, 0)',
    ...           'Grape': 'rgb(170, 14, 200)',
    ...           'Banana': (1, 1, 0.2)}

    >>> # Create a figure with Plotly colorscale
    >>> fig = create_gantt(df, colors=colors, index_col='Resource',
    ...                    show_colorbar=True)

    >>> fig.show()

    Example 5: Use a pandas dataframe

    >>> from plotly.figure_factory import create_gantt
    >>> import pandas as pd

    >>> # Make data as a dataframe
    >>> df = pd.DataFrame([['Run', '2010-01-01', '2011-02-02', 10],
    ...                    ['Fast', '2011-01-01', '2012-06-05', 55],
    ...                    ['Eat', '2012-01-05', '2013-07-05', 94]],
    ...                   columns=['Task', 'Start', 'Finish', 'Complete'])

    >>> # Create a figure with Plotly colorscale
    >>> fig = create_gantt(df, colors='Blues', index_col='Complete',
    ...                    show_colorbar=True, bar_width=0.5,
    ...                    showgrid_x=True, showgrid_y=True)
    >>> fig.show()
    r   zIn order to use an indexing column and assign colors to the values of the index, you must choose an actual column name in the dataframe or key if a list of dictionaries is being used.ZrgbTzError. You have set colors to a dictionary but have not picked an index. An index is required if you are assigning colors to particular values in a dictionary.N)ro   rp   rb   rq   rr   rs   )ro   rp   rb   rq   rr   )r!   r   r   r   r   Zvalidate_indexr   r   r   Zvalidate_colors_dictZvalidate_colorsrd   r~   r   r   )r   rk   r   rs   Zreverse_colorsrV   rl   rm   rn   rW   rX   ro   rp   rb   rq   rr   r    Z
index_list
dictionaryr|   r	   r	   r   create_gantt!  s     



r   )NNNFTT)NNNFT)NNNFT)NNFFr   r   FFr   NNNNFT)Znumbersr   re   Zplotlyr   r   Zplotly.colorsrk   r   Zplotly.figure_factoryr   Zplotly.graph_objectsZgraph_objectsri   Z
get_moduler   r   r   r!   r~   r   r   r   r	   r	   r	   r   <module>   s`   

0      
 G     
  c     
 O               