B
    eJd                 @   s   d dl mZ 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 )    )absolute_import)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
   N/var/www/html/venv/lib/python3.7/site-packages/plotly/figure_factory/_gantt.py_get_corner_points   s    r   c             C   s   t rt| t jjjrx*tD ]"}|| krtdd	tqW t
| j}g }x>t|D ]2}i }x| D ]}| j| | ||< qfW || qXW |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   s.    




r"   FTc                s|  |dkrg }|	dkrg }	|
dkr$g }
x`t t| D ]P}t| | d | | d | | d d}d| | krx| | d |d< || q2W t t  |rd}nd	}g g d
d|d}g g dg tddddddd}x6t t|D ]&}|| d }|r||	kr|	| qW |r|	  d}xvt t|D ]d}|| d }|| d= |}|rX|	|}|| || d< || || d< |t|krd}|| || 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r` | d || d   | d || d  || d= n$ | d d  | d d |d7 }q(W |}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sizeopacityF)r)   r*   r+   textmarkerr#   
showlegendr   r   	fillcolorlegendgroupr)   r*   r   r   r5   r1   r4   closest)showgridticktexttickvalsr   	autorangezeroline   1wdaybackward)countlabelstepstepmode1mmonth   6mYTDyeartodate1yall)rF   )buttonsdate)r;   r?   rangeselectortype)titler6   heightwidthshapes	hovermodeyaxis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
   )r\   r]   )marker_data_dictr
   r   r_      s    )datalayout)r   r   r   r   reverser   copydeepcopystrr   r   sortedgoFigure)r!   colorsrU   	bar_width
showgrid_x
showgrid_yrV   rW   tasks
task_namesra   group_tasksshow_hover_fillshow_colorbarr   taskr-   scatter_data_templatemarker_data_templatetnZcolor_indexgroupIDcolor_idxsysr6   rb   figr
   )r`   r^   r   ganttF   s    








r|   c       &         s0	  |
dkrg }
|dkrg }|dkr$g }d}x`t t| D ]P}t| | d | | d | | d d}d| | kr|| | d |d< |
| q6W t 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 }x6t t|
D ]&}| | | |kr|| | |  qW |  t| d | trt|dk rHtdx<t t|
D ],}|
| d	 }|rv||krV|| qVW |r|	  xt t|
D ]}|
| d	 }|
| d	= |}|r|
|}|| |
| d< || |
| d< t|tj}|d }|d }| | | d }t|||}t|tj}||
| d< |
| d }|krnt||< || 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rt| |< | | d  d!< | | d<  | d |
| d   | d |
| d   | d |  | d | d|
| kr& | d" |
| d   | d" |
| d  |
| d= n$ | d" d  | d" d qW |d#krt  d }  |  d  td|d gd|d ggd#d$dd% t| d | trJg }x:t t|
D ]*}| | | |kr|| | |  qW |  t|t|k rtd&i }!d}"x8|D ]0}#|"t|d kr<d}"||" |!|#< |"d7 }"q"W x<t t|
D ],}|
| d	 }|r||krd|| qdW |r|	  xzt 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rPt| |< | | d  d!< | | d<  | d |
| d   | d |
| d   | d |  | d | d|
| kr | d" |
| d   | d" |
| d  |
| d= n$ | d" d  | d" d qW |d#krJd#}xD ]} ||  d'< q4W 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 )Az=
    Refer to FigureFactory.create_gantt() for docstring
    NFr   r	   r   )r   r   r#   r$   r%   r#   r&   r'   r(   r/   )r)   r*   r+   r,   r6   r-   r8   r.   r0   r   )r1   r2   r3   )r)   r*   r+   r4   r5   r#   r6   r8      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@r7   r8   r)   r9   r*   r   r   r5   r1   r4   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.r6   r:   )r;   r<   r=   r   r>   r?   r@   rA   rB   rC   )rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   )rF   )rQ   rR   )r;   r?   rS   rT   )rU   r6   rV   rW   rX   rY   rZ   r[   c                s   g | ]} | qS r
   r
   )r\   r]   )r^   r
   r   r_   M  s    z$gantt_colorscale.<locals>.<listcomp>c                s   g | ]} | qS r
   r
   )r\   r]   )r`   r
   r   r_   N  s    )ra   rb   )r   r   r   r   sortr   r   r   r   rc   r   clrsZcolor_parserZunlabel_rgbZfind_intermediate_colorZ	label_rgbrd   re   rf   r   r   keysupdaterg   rh   ri   )&r!   rj   rU   	index_colrr   rk   rl   rm   rV   rW   rn   ro   ra   rp   rq   r6   r   rs   r-   rt   ru   
index_valsrowrv   rw   ZlowcolorZ	highcolorZintermedZintermed_colorrx   ry   rz   r]   Zindex_vals_dictZc_indexr    rb   r{   r
   )r`   r^   r   gantt_colorscale  s   




















r   c                 s  |
dkrg }
|dkrg }|dkr$g }d}x`t t| D ]P}t| | d | | d | | d d}d| | kr|| | d |d< |
| q6W t t  |rd	}nd
}g g dd|dd}g g dg tddddddd}g }x6t t|
D ]&}| | | |kr|| | |  qW |  x"|D ]}||krtdqW x<t t|
D ],}|
| d	 }|rj||krJ|| qJW |r|  xTt t|
D ]B}|
| d	 }|
| d	= |}|r||}|| |
| d< || |
| d< || | |  |
| d< |
| d }|kr$t	
||< || d< || d< 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 qW |d!krd!}x"|D ]}t|||  d	< qW 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d0d.d'd(td1d2gd3d4d5d6}fd7d8tD }| fd9d8t D 7 }tj||d:}|S );z=
    Refer to FigureFactory.create_gantt() for docstring
    NFr   r	   r   )r   r   r#   r$   r%   r#   r&   r'   r(   r/   )r)   r*   r+   r,   r-   r8   r.   r0   r   )r1   r2   r3   )r)   r*   r+   r4   r5   r#   r6   zdIf you are using colors as a dictionary, all of its keys must be all the values in the index column.r   r   r7   r8   r)   r9   r*   r   r   r5   r1   r4   Tr:   )r;   r<   r=   r   r>   r?   r@   rA   rB   rC   )rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   )rF   )rQ   rR   )r;   r?   rS   rT   )rU   r6   rV   rW   rX   rY   rZ   r[   c                s   g | ]} | qS r
   r
   )r\   r]   )r^   r
   r   r_     s    zgantt_dict.<locals>.<listcomp>c                s   g | ]} | qS r
   r
   )r\   r]   )r`   r
   r   r_     s    )ra   rb   )r   r   r   r   r   r   r   rc   r   rd   re   r   rf   r   rg   rh   ri   ) r!   rj   rU   r   rr   rk   rl   rm   rV   rW   rn   ro   ra   rp   rq   r6   r   rs   r-   rt   ru   r   r   r    rv   rw   rx   ry   rz   Zindex_valuerb   r{   r
   )r`   r^   r   
gantt_dictW  s    












r   Gantt Chart皙?X  c             C   s"  t | }|rL||d kr"tdg }x|D ]}|||  q,W t| t|trdt	|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.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)rn   ro   ra   rp   rq   rr   )rn   ro   ra   rp   rq   )r"   r   r   r   r   Zvalidate_indexr   r   r   Zvalidate_colors_dictZvalidate_colorsrc   r|   r   r   )r   rj   r   rr   Zreverse_colorsrU   rk   rl   rm   rV   rW   rn   ro   ra   rp   rq   r!   Z
index_list
dictionaryr{   r
   r
   r   create_gantt#  s     




r   )NNNFTT)NNNFT)NNNFT)NNFFr   r   FFr   NNNNFT)
__future__r   numbersr   rd   Zplotlyr   r   Zplotly.colorsrj   r   Zplotly.figure_factoryr   Zplotly.graph_objectsZgraph_objectsrh   Z
get_moduler   r   r   r"   r|   r   r   r   r
   r
   r
   r   <module>   sZ   

0     
 9    
  T    
 @              