B
    Y%                 @   s   d Z ddlmZ ddl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mZ dd	lmZmZ dd
lmZmZmZ ddlmZ ddlmZmZ ddlmZ dddZG dd deZG dd deZ dS )zThis is the Bokeh charts interface. It gives you a high level API to build
complex plot is a simple way.

This is the Line class which lets you build your Line charts just
passing the arguments to the Chart class and calling the proper functions.
    )absolute_import)BoolStringList)ColumnDataSource)	iteritems)chain   )	XYBuildercreate_and_build)	LineGlyph
PointGlyph)DashAttr	ColorAttr
MarkerAttr)NumericalColumnsAssigner)StackDodge)add_tooltips_columnsNc             K   s   ||d< ||d< t t| f|S )a   Create a line chart using :class:`LineBuilder <bkcharts.builders.line_builder.LineBuilder>` to
    render the glyphs.

    The line chart is typically is used with column oriented data, where each column
    contains comparable measurements and the column names are treated as a categorical
    variable for differentiating the measurement values. One of the columns can be used as
    an index for either the x or y axis.

    .. note::
        Only the x or y axis can display multiple variables, while the other is used
        as an index.

    Args:
        data (list(list), numpy.ndarray, pandas.DataFrame, list(pd.Series)): a 2d data
            source with columns of data for each line.
        x (str or list(str), optional): specifies variable(s) to use for x axis
        y (str or list(str), optional): specifies variable(s) to use for y axis

    In addition to the parameters specific to this chart,
    :ref:`userguide_charts_defaults` are also accepted as keyword parameters.

    .. note::
        This chart type differs on input types as compared to other charts,
        due to the way that line charts typically are plotting labeled series. For
        example, a column for AAPL stock prices over time. Another way this could be
        plotted is to have a DataFrame with a column of `stock_label` and columns of
        `price`, which is the stacked format. Both should be supported, but the former
        is the expected one. Internally, the latter format is being derived.

    Returns:
        :class:`Chart`: includes glyph renderers that generate the lines

    Examples:

    .. bokeh-plot::
        :source-position: above

        import numpy as np
        from bkcharts import Line, output_file, show

        # (dict, OrderedDict, lists, arrays and DataFrames are valid inputs)
        xyvalues = np.array([[2, 3, 7, 5, 26], [12, 33, 47, 15, 126], [22, 43, 10, 25, 26]])

        line = Line(xyvalues, title="line", legend="top_left", ylabel='Languages')

        output_file('line.html')
        show(line)

    xy)r   LineBuilder)datar   r   Zkws r   =lib/python3.7/site-packages/bkcharts/builders/line_builder.pyLine&   s    3r   c               @   s   e Zd ZdZeeddZeddZe	 e
 e dZddgZeZeZed	d
 Zedd Zedd Zdd Zdd Zdd Zdd ZdddZdd Zdd ZdS ) r   ap  This is the Line class and it is in charge of plotting
    Line charts in an easy and intuitive way.
    Essentially, we provide a way to ingest the data, make the proper
    calculations and push the references into a source object.
    We additionally make calculations for the ranges.
    And finally add the needed lines taking the references from the source.
    z-Names that represent the items being plotted.)helpF)default)colorZdashZmarkerr   r   c             C   s4   t | jjtr| jjS t | jjtr,| jjS d S d S )N)
isinstancer   	selectionlistr   )selfr   r   r   measurest   s
    zLineBuilder.measuresc             C   s   t | jjtpt | jjtS )N)r   r   r    r!   r   )r"   r   r   r   measure_input}   s    zLineBuilder.measure_inputc                s    fddt  j D S )Nc                s   i | ]}  ||qS r   )attr_measurement).0k)r"   r   r   
<dictcomp>   s    z+LineBuilder.stack_flags.<locals>.<dictcomp>)r!   
attributeskeys)r"   r   )r"   r   stack_flags   s    zLineBuilder.stack_flagsc                s$    fddt |D }tt|S )Nc                sB   g | ]:\}}|s j | j jkr j | jd k	r j | jqS )N)r)   columnsr#   )r&   attrstack)r"   r   r   
<listcomp>   s   
z+LineBuilder.get_id_cols.<locals>.<listcomp>)r   r!   r   from_iterable)r"   r+   id_colsr   )r"   r   get_id_cols   s    
zLineBuilder.get_id_colsc             C   s   | j r| j}| |}tdd t| D r8d|d< | j|d t| jj	}x4t
|D ](\}}|| rZ| j| }|jd|d qZW | jjdkr| jjdk	rd	| j_n | jjdk	r| jjdkrd	| j_dS )
zKHandle input options that require transforming data and/or user selections.c             s   s   | ]}|d kV  qdS )FNr   )r&   r-   r   r   r   	<genexpr>   s    z$LineBuilder.setup.<locals>.<genexpr>Tr   )idsseries)r,   r   Nindex)r$   r+   r2   allr!   values_stack_measuresr   _datadfr   r)   setupr   r    r   )r"   r+   r1   source	attr_nameZ
stack_flagZdefault_attrr   r   r   r<      s    


zLineBuilder.setupc             C   s,   | j | j}|dk	o*|| jjkp*|| jjkS )z;Detect if the attribute has been given measurement columns.N)r)   r,   r   r    r   )r"   r>   Zcolsr   r   r   r%      s    zLineBuilder.attr_measurementc             C   s,   | j j|   }dd |D }|| _d S )Nc             S   s   g | ]}t |qS r   )str)r&   itemr   r   r   r/      s    z*LineBuilder.set_series.<locals>.<listcomp>)r:   r;   Zdrop_duplicatestolistseries_names)r"   Zcol_namer5   r   r   r   
set_series   s    zLineBuilder.set_seriesr5   c             C   s   t | jjtr.d}| jjdk	rL|| jj nd}| jjdk	rL|| jj t|dkr\d}t| |}| jj	|j||d |
| j | d dS )zStack data and keep the ids columns.

        Args:
            ids (list(str)): the column names that describe the measures

        r   Nr   r   )r#   r4   var_namer5   )r   r   r    r!   r   appendlengetattrr:   Zstack_measuresset_datarC   )r"   r4   rD   ZdimZdim_propr   r   r   r9      s    
zLineBuilder._stack_measuresc                s      } fdd|D S )Nc                s&   i | ]}| j  krt ||qS r   )glyph
propertiesrG   )r&   r-   )r"   r   r   r(      s    z0LineBuilder.get_builder_attr.<locals>.<dictcomp>)rJ   )r"   attrsr   )r"   r   get_builder_attr   s    zLineBuilder.get_builder_attrc             #   s      }t j } fdd|D }x jjf  jD ]~} ||}||  jf |j	|
 jj|
 jjd|} || x(|jD ]} jrt| j|}|V  qW q:W  jrt  j t  j d S )Nc                s   g | ]}| j  kr|qS r   )rI   rJ   )r&   r-   )r"   r   r   r/      s    z/LineBuilder.yield_renderers.<locals>.<listcomp>)labelr   r   )rL   r!   r)   r*   r:   groupbyZget_group_kwargsupdaterI   rM   Z
get_valuesr   r    r   Z	add_glyphZ	renderersZtooltipsr   r.   r   ZapplyZcomp_glyphsr   )r"   Z
build_attrrK   groupZgroup_kwargsrI   Zrendererr   )r"   r   yield_renderers   s$    

zLineBuilder.yield_renderersN)r5   )__name__
__module____qualname____doc__r   r   rB   r   r.   r   r   r   Zdefault_attributesZ
dimensionsr   Zcolumn_selectorr   rI   propertyr#   r$   r+   r2   r<   r%   rC   r9   rL   rQ   r   r   r   r   r   ^   s&   

		
r   c               @   s   e Zd ZeZdS )PointSeriesBuilderN)rR   rS   rT   r   rI   r   r   r   r   rW      s   rW   )NNN)!rU   Z
__future__r   Zbokeh.core.propertiesr   r   r   Zbokeh.models.sourcesr   Zsixr   	itertoolsr   Zbuilderr
   r   Zglyphsr   r   r)   r   r   r   Zdata_sourcer   Z
operationsr   r   Zutilsr   r   r   rW   r   r   r   r   <module>   s   
8 !