ó
ÇÅ>Yc           @` sL  d  Z  d d l m Z m Z m Z d d l m Z d d l m Z m	 Z	 m
 Z
 m Z d d l m Z m Z d d l m Z d d l m Z 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 d d l m  Z  d e f d „  ƒ  YZ! e  e! ƒ d d d d d d d d e# e$ d d „ ƒ Z% 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 Bar class which lets you build your Bar charts just passing
the arguments to the Chart class and calling the proper functions.
It also add a new chained stacked method.
i    (   t   absolute_importt   print_functiont   division(   t   Aggregation(   t   Floatt   Enumt   Boolt   Override(   t   FactorRanget   Range1d(   t   ColumnDataSourcei   (   t   Buildert   create_and_build(   t   BarGlyph(   t	   Dimension(   t	   ColorAttrt   CatAttr(   t   Stackt   Dodge(   t   stats(   t   helpt
   BarBuilderc           B` s  e  Z d  Z e d ƒ Z d g Z i e ƒ  d 6e ƒ  d 6e d d ƒ d 6e ƒ  d 6e ƒ  d 6Z e	 e
 d d	 ƒZ e d
 ƒ Z e d ƒ Z e d d ƒ Z e d d ƒ Z e Z e d e g ƒ Z d d g Z e e ƒ Z e e ƒ Z e Z e Z d „  Z d „  Z d „  Z d „  Z d „  Z  RS(   se  This is the Bar builder and it is in charge of plotting
    Bar chart (grouped and stacked) in an easy and intuitive way.

    Essentially, it utilizes a standardized way to ingest the data,
    make the proper calculations and generate renderers. The renderers
    reference the transformed data, which represent the groups of data
    that were derived from the inputs. We additionally make calculations
    for the ranges.

    The x_range is categorical, and is made either from the label argument
    or from the `pandas.DataFrame.index`. The y_range can be supplied as the
    parameter continuous_range, or will be calculated as a linear range
    (Range1d) based on the supplied values.

    The bar builder is and can be further used as a base class for other
    builders that might also be performing some aggregation across
    derived groups of data.

    t   valuest   labelt   colort   defaultt   whitet
   line_colort   stackt   groupt   sumg      ð?g        gš™™™™™é?c         C` s  |  j  d j d  k r |  j  d j d  k	 rP |  j  d j d |  j  d j ƒ n  |  j  d j d  k	 r |  j  d j d |  j  d j ƒ q n  |  j  d j d  k	 r¯ t |  _ n  |  j  d j d  k	 rÑ t |  _ n  |  j j j	 d k r!t
 |  j ƒ d k r!d |  _ |  j  d j |  j j ƒ n  |  j ƒ  |  j d  k r“|  j  d j d  k	 rt d	 j |  j  d j ƒ j ƒ  ƒ j ƒ  |  _ q“|  j j |  _ n  |  j d  k r|  j sÜd
 |  j j ƒ  t |  j j ƒ j ƒ  f |  _ qd
 |  j j ƒ  d	 j |  j  d j ƒ j ƒ  f |  _ n  d  S(   NR   R   t   columnsR   t   objecti    t   countR   s   , s   %s( %s )(   t
   attributesR   t   Nonet   setupt   Truet   _perform_stackt   _perform_groupR   t   dtypet   namet   lent   attribute_columnst   aggt   set_columnst	   selectiont   _apply_inferred_indext   xlabelt   strt   joint   titlet   ylabelt
   label_only(   t   self(    (    s<   lib/python2.7/site-packages/bkcharts/builders/bar_builder.pyR$   Z   s0    $'*	
.	.c         C` s“   |  j  d j d k r |  j j d k	 r |  j  d j d k	 rG d } n d } | |  j d <|  j  d j d t |  j j ƒ d | ƒ d |  _	 n  d S(	   sF   Configure chart when labels are provided as index instead of as kwarg.R   R   t   unityt   indext   dataR   t    N(
   R"   R   R#   R   R.   t   _dataR$   R
   t   dfR0   (   R6   t   special_column(    (    s<   lib/python2.7/site-packages/bkcharts/builders/bar_builder.pyR/   ƒ   s    	"c         C` sô   |  j  d j } | d
 k r% d } n  g  } x0 | D]( } |  j | ƒ } | j t | ƒ ƒ q2 Wt d | ƒ |  _ t d |  j	 |  j
 d ƒ } |  j	 d k  r­ |  j	 | } n d } |  j
 d k rÒ |  j
 | } n d } t d | d	 | ƒ |  _ d
 S(   s]   Push the Bar data into the ColumnDataSource and calculate
        the proper ranges.
        R   R:   t   factorsgš™™™™™¹?i   i    g        t   startt   endN(   R"   t   itemsR#   t
   _get_labelt   appendR1   R   t   x_ranget   abst
   min_heightt
   max_heightR	   t   y_range(   R6   t   x_itemst   x_labelst   itemt   y_shiftR?   R@   (    (    s<   lib/python2.7/site-packages/bkcharts/builders/bar_builder.pyt
   set_ranges•   s     	c         ` s=   ˆ  j  t k	 r5 ˆ  j d t ƒ } ‡  f d †  | Dƒ Si  Sd  S(   Nt
   with_basesc         ` s"   i  |  ] } t  ˆ  | ƒ | “ q S(    (   t   getattr(   t   .0t   attr(   R6   (    s<   lib/python2.7/site-packages/bkcharts/builders/bar_builder.pys
   <dictcomp>¸   s   	 (   t	   __class__R   t
   propertiest   False(   R6   t   attrs(    (   R6   s<   lib/python2.7/site-packages/bkcharts/builders/bar_builder.pyt   get_extra_argsµ   s    c         c` s4  |  j  ƒ  } |  j ƒ  } xZ|  j j |  j   D]C} |  j | | ƒ } | j ƒ  } | j | ƒ |  j j	 ƒ  j
 t d g ƒ ƒ } x" d d g D] } | | | | <q Wx0 t | ƒ D]" } | | k r² | j | ƒ q² q² W|  j d | j d |  j | d ƒ d | j |  j j j d t |  j ƒ  d |  j d |  j d	 |  j | d
 ƒ d |  j | d ƒ |  } |  j | | ƒ q. W|  j r”t ƒ  j |  j ƒ n  |  j r³t ƒ  j |  j ƒ n  t g  |  j D] }	 |	 j ^ qÀƒ |  _ t  g  |  j D] }	 |	 j! ^ qèƒ |  _" x* |  j D] }	 x |	 j# D] }
 |
 VqWqWd S(   s{   Use the rect glyphs to display the bars.

        Takes reference points from data loaded at the ColumnDataSource.
        R   R   R   t   x_labelR   R,   t   widtht
   fill_alphat   stack_labelR   t   dodge_labelR   N($   RV   t   collect_attr_kwargsR;   t   groupbyR"   t   get_group_kwargst   copyt   updatet   glyphRS   t
   differencet   sett   popR   RB   R9   R   R.   R   R,   t	   bar_widthRY   t	   add_glyphR&   R   t   applyt   comp_glyphsR'   R   t   maxt   y_maxRG   t   mint   y_minRF   t	   renderers(   R6   t   kwargsRU   R   t   glyph_kwargst   group_kwargst   propst   kt   bgt   renderert   sub_renderer(    (    s<   lib/python2.7/site-packages/bkcharts/builders/bar_builder.pyt   yield_renderers¼   s>    !					(((!   t   __name__t
   __module__t   __doc__R   R   t
   dimensionsR   R   t   default_attributesR   R   R,   R   RG   RF   Re   RY   R   Ra   R   t   comp_glyph_typest   label_attributesR   RT   R5   t   values_onlyR&   R'   R$   R/   RM   RV   Rv   (    (    (    s<   lib/python2.7/site-packages/bkcharts/builders/bar_builder.pyR   (   s2   	

	)		 	R   t   categoricalt   linearc         K` s-  | r% t  | t ƒ r% t d ƒ ‚ n  | d k	 rq | d k rq t | d <| d k s_ | d k rq d } | } qq n  | } | | d <| | d <| | d <| | d	 <| | d
 <| | d <| | d <| | d <|	 | d <|
 | d <| | d <t t |  |  } t | j j	 ƒ d k r)| r)t
 | j d _ n  | S(   s¶	   Create a Bar chart using :class:`BarBuilder <bkcharts.builders.bar_builder.BarBuilder>`
    render the geometry from values, cat and stacked.

    Args:
        data (:ref:`userguide_charts_data_types`): the data
            source for the chart.
        label (list(str) or str, optional): list of string representing the categories.
            (Defaults to None)
        values (str, optional): iterable 2d representing the data series
            values matrix.
        color (str or list(str) or `~bkcharts._attributes.ColorAttr`): string color,
            string column name, list of string columns or a custom `ColorAttr`,
            which replaces the default `ColorAttr` for the builder.
        stack (list(str) or str, optional): columns to use for stacking.
            (Defaults to False, so grouping is assumed)
        group (list(str) or str, optional): columns to use for grouping.
        agg (str): how to aggregate the `values`. (Defaults to 'sum', or only label is
            provided, then performs a `count`)
        continuous_range(Range1d, optional): Custom continuous_range to be
            used. (Defaults to None)

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

    Returns:
        :class:`Chart`: includes glyph renderers that generate bars

    Examples:

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

            from bkcharts import Bar, output_file, show
            from bokeh.layouts import row

            # best support is with data in a format that is table-like
            data = {
                'sample': ['1st', '2nd', '1st', '2nd', '1st', '2nd'],
                'interpreter': ['python', 'python', 'pypy', 'pypy', 'jython', 'jython'],
                'timing': [-2, 5, 12, 40, 22, 30]
            }

            # x-axis labels pulled from the interpreter column, stacking labels from sample column
            bar = Bar(data, values='timing', label='interpreter', stack='sample', agg='mean',
                      title="Python Interpreter Sampling", legend='top_right', plot_width=400)

            # table-like data results in reconfiguration of the chart with no data manipulation
            bar2 = Bar(data, values='timing', label=['interpreter', 'sample'],
                       agg='mean', title="Python Interpreters", plot_width=400)

            output_file("stacked_bar.html")
            show(row(bar, bar2))

    sC   continuous_range must be an instance of bokeh.models.ranges.Range1dR5   R   t   meanR!   R   R   R   R   R   R,   t   xscalet   yscalet   xgridt   ygridRH   i   i    N(   t
   isinstanceR	   t
   ValueErrorR#   R%   R   R   R*   RD   R>   RT   t   belowt   visible(   R9   R   R   R   R   R   R,   R‚   Rƒ   R„   R…   t   continuous_ranget   kwRH   t   chart(    (    s<   lib/python2.7/site-packages/bkcharts/builders/bar_builder.pyt   Barï   s0    :











N(&   Ry   t
   __future__R    R   R   t   bokeh.core.enumsR   t   bokeh.core.propertiesR   R   R   R   t   bokeh.modelsR   R	   t   bokeh.models.sourcesR
   t   builderR   R   t   glyphsR   RS   R   R"   R   R   t
   operationsR   R   R   t   utilsR   R   R#   RT   R%   R   (    (    (    s<   lib/python2.7/site-packages/bkcharts/builders/bar_builder.pyt   <module>   s"   "Ç	