σ
ΞYc           @@  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 d  Z d e f d     YZ  d e  f d     YZ! d S(   sχ   This 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.
i    (   t   absolute_import(   t   Boolt   Stringt   List(   t   ColumnDataSource(   t	   iteritems(   t   chaini   (   t	   XYBuildert   create_and_build(   t	   LineGlypht
   PointGlyph(   t   DashAttrt	   ColorAttrt
   MarkerAttr(   t   NumericalColumnsAssigner(   t   Stackt   Dodge(   t   add_tooltips_columnsc         K@  s$   | | d <| | d <t  t |  |  S(   s   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)

    t   xt   y(   R   t   LineBuilder(   t   dataR   R   t   kws(    (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyt   Line&   s    3

R   c           B@  sΪ   e  Z d  Z e e d d Z e d e  Z i e	   d 6e
   d 6e   d 6Z d d g Z e Z e Z e d	    Z e d
    Z e d    Z d   Z d   Z d   Z d   Z d d  Z d   Z d   Z RS(   sp  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.
    t   helps-   Names that represent the items being plotted.t   defaultt   colort   dasht   markerR   R   c         C@  sF   t  |  j j t  r |  j j St  |  j j t  r> |  j j Sd  Sd  S(   N(   t
   isinstanceR   t	   selectiont   listR   t   None(   t   self(    (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyt   measurest   s
    

c         C@  s(   t  |  j j t  p' t  |  j j t  S(   N(   R   R   R   R   R   (   R!   (    (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyt   measure_input}   s    c         @  s#     f d   t    j j    D S(   Nc         @  s"   i  |  ] }   j  |  |  q S(    (   t   attr_measurement(   t   .0t   k(   R!   (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pys
   <dictcomp>   s   	 (   R   t
   attributest   keys(   R!   (    (   R!   s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyt   stack_flags   s    c         C@  sx   g  t  |  D]R \ } } | r |  j | j |  j k r |  j | j d  k	 r |  j | j ^ q } t t j |   S(   N(   R   R'   t   columnsR"   R    R   R   t   from_iterable(   R!   R)   t   attrt   stackt   id_cols(    (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyt   get_id_cols   s
    ,c         C@  s,  |  j  rΒ |  j } |  j |  } t d   t | j    D  rP t | d <n  |  j d |  t |  j	 j
  } xM t |  D]< \ } } | | r |  j | } | j d d d |  q q Wn  |  j j d k rυ |  j j d k	 rυ d |  j _ n3 |  j j d k	 r(|  j j d k r(d |  j _ n  d S(	   sK   Handle input options that require transforming data and/or user selections.c         s@  s   |  ] } | t  k Vq d  S(   N(   t   False(   R%   R,   (    (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pys	   <genexpr>   s    R   t   idsR*   t   seriesR   t   indexN(   R#   R)   R/   t   allR   t   valuest   Truet   _stack_measuresR   t   _datat   dfR   R'   t   setupR   R   R    R   (   R!   R)   R.   t   sourcet	   attr_namet
   stack_flagt   default_attr(    (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyR:      s    		"
 $$c         C@  s>   |  j  | j } | d k	 o= | |  j j k p= | |  j j k S(   s;   Detect if the attribute has been given measurement columns.N(   R'   R*   R    R   R   R   (   R!   R<   t   cols(    (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyR$   ­   s    c         C@  sH   |  j  j | j   j   } g  | D] } t |  ^ q# } | |  _ d  S(   N(   R8   R9   t   drop_duplicatest   tolistt   strt   series_names(   R!   t   col_nameR2   t   item(    (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyt
   set_series³   s    R2   c         C@  sα   t  |  j j t  rF d } |  j j d k	 rt | j |  j j  qt n. d } |  j j d k	 rt | j |  j j  n  t |  d k r d } n  t |  |  } |  j	 j
 d | j d | d |  | j |  j	  |  j d  d S(	   s   Stack data and keep the ids columns.

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

        R   R   i    R"   R1   t   var_nameR2   N(   R   R   R   R   R   R    t   appendt   lent   getattrR8   t   stack_measurest   set_dataRF   (   R!   R1   RG   t   dimt   dim_prop(    (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyR7   Έ   s    	c         @  s      j    }   f d   | D S(   Nc         @  s7   i  |  ]- } |   j  j   k r t   |  |  q S(    (   t   glypht
   propertiesRJ   (   R%   R,   (   R!   (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pys
   <dictcomp>Ψ   s   	 (   RP   (   R!   t   attrs(    (   R!   s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyt   get_builder_attrΦ   s    c      	   c@  sP  |  j    } t |  j j    } g  | D]! } | |  j j   k r( | ^ q( } xΘ |  j j |  j   D]± } |  j | |  } | j	 |  |  j d | j
 d | j |  j j  d | j |  j j  |  } |  j | |  x7 | j D], } |  j rt | |  j |  } n  | Vqζ Wqe W|  j r9t   j |  j  n  t   j |  j  d  S(   Nt   labelR   R   (   RR   R   R'   R(   RO   RP   R8   t   groupbyt   get_group_kwargst   updateRS   t
   get_valuesR   R   R   t	   add_glypht	   rendererst   tooltipsR   R-   R   t   applyt   comp_glyphsR   (   R!   t
   build_attrRQ   R,   t   groupt   group_kwargsRO   t   renderer(    (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyt   yield_renderersΫ   s$    .			(   t   __name__t
   __module__t   __doc__R   R   RC   R   R0   R-   R   R   R   t   default_attributest
   dimensionsR   t   column_selectorR	   RO   t   propertyR"   R#   R)   R/   R:   R$   RF   R7   RR   Ra   (    (    (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyR   ^   s&   
							t   PointSeriesBuilderc           B@  s   e  Z e Z RS(    (   Rb   Rc   R
   RO   (    (    (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyRi   ώ   s   N("   Rd   t
   __future__R    t   bokeh.core.propertiesR   R   R   t   bokeh.models.sourcesR   t   sixR   t	   itertoolsR   t   builderR   R   t   glyphsR	   R
   R'   R   R   R   t   data_sourceR   t
   operationsR   R   t   utilsR   R    R   R   Ri   (    (    (    s=   lib/python2.7/site-packages/bkcharts/builders/line_builder.pyt   <module>   s   8 