
Yc        	   @` s   d  Z  d d l 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 d d
 l m Z d d l m Z d d l m Z d d d d e e e d d  Z 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 HeatMap class which lets you build your HeatMap charts just passing
the arguments to the Chart class and calling the proper functions.
i    (   t   absolute_importt   print_functiont   division(   t   Floatt   String(   t   Blues6i   (   t	   XYBuildert   create_and_build(   t   Bins(   t	   Dimension(   t	   Aggregate(   t	   ColorAttr(   t   BinGlyph(   t   build_agg_tooltipt   countc	         K` s~   | |	 d <| |	 d <| |	 d <| |	 d <t  t |  d | d | |	 }
 | rz t d | d | d	 |  } |
 j | g  n  |
 S(
   s   Represent 3 dimensions in a HeatMap chart using x, y, and values.

    Uses the :class:`~bkcharts.builders.heatmap_builder.HeatMapBuilder`
    to render the geometry from values.

    A HeatMap is a 3 Dimensional chart that crosses two dimensions, then aggregates
    values  that correspond to the intersection of the horizontal and vertical
    dimensions. The value that falls at the intersection is then mapped to a
    color in a palette by default. All values that map to the positions on the chart are
    binned by the number of discrete colors in the palette.

    Args:
        data (:ref:`userguide_charts_data_types`): the data source for the chart
        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
        values (str, optional): the values to use for producing the histogram using
            table-like input data
        stat (str, optional): the aggregation to use. Defaults to count. If provided
            `None`, then no aggregation will be attempted. This is useful for cases
            when the values have already been aggregated.
        hover_tool (bool, optional): whether to show the hover tool. Defaults to `True`
        hover_text (str, optional): a string to place beside the value in the hover
            tooltip. Defaults to `None`. When `None`, a hover_text will be derived from
            the aggregation and the values column.

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

    Returns:
        a new :class:`Chart <bkcharts.Chart>`

    Examples:

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

        from bkcharts import HeatMap, output_file, show

        # (dict, OrderedDict, lists, arrays and DataFrames are valid inputs)
        data = {'fruit': ['apples']*3 + ['bananas']*3 + ['pears']*3,
                'fruit_count': [4, 5, 8, 1, 2, 4, 6, 5, 4],
                'sample': [1, 2, 3]*3}

        hm = HeatMap(data, x='fruit', y='sample', values='fruit_count',
                     title='Fruits', stat=None)

        output_file('heatmap.html')
        show(hm)

    t   xt   yt   valuest   statt   xgridt   ygridt
   hover_textt   aggregated_colt   agg_text(   R   t   HeatMapBuilderR   t   add_tooltips(   t   dataR   R   R   R   R   R   t
   hover_toolR   t   kwt   chartt   tooltip(    (    s@   lib/python2.7/site-packages/bkcharts/builders/heatmap_builder.pyt   HeatMap$   s    4



	R   c        
   B` s   e  Z d  Z e d  Z d d d g Z d d g d d d g g Z i e d e d e	 d e d e
  d 6Z e d	 d
 d d  Z e d	 d
 d d  Z e d	 d d d  Z e d	 d d d  Z d   Z d   Z d   Z RS(   s9  Assists in producing glyphs required to represent values by a glyph attribute.

    Primary use case is to display the 3rd dimension of a value by binning and
    aggregating as needed and assigning the results to color. This color is represented
    on a glyph that is positioned by the x and y dimensions.

    R   R   R   t   bint   palettet   sortt	   ascendingt   colort   defaultg      ?t   helpsM   
        A derived property that is used to size the glyph in width.
        sN   
        A derived property that is used to size the glyph in height.
        gffffff?s  
        Multiplied by the bin height and width to shrink or grow the relative size
        of the glyphs. The closer to 0 this becomes, the amount of space between the
        glyphs will increase. When above 1.0, the glyphs will begin to overlap.
        t   sums   
        The stat to be applied to the values that fall into each x and y intersection.
        When stat is set to `None`, then no aggregation will occur.
        c         C` s   |  j  d  k r$ d |  _  d |  _ n  x |  j j D]~ } t | t  r1 | j |  j j	 k r d |  j
 d t  k r | j |  _ q q d |  j
 d t  k r | j |  _ q q1 q1 Wd  S(   NR$   R#   t	   bin_widtht   include_defaultst
   bin_height(   t   legend_sort_fieldt   Nonet   legend_sort_directiont   _datat
   operationst
   isinstanceR   t   centers_columnR   t	   selectiont   properties_with_valuest   FalseR(   R*   (   t   selft   op(    (    s@   lib/python2.7/site-packages/bkcharts/builders/heatmap_builder.pyt   setup   s    	c         C` s   |  j  d k	 rr t d |  j j |  j j g d |  j j d |  j   } | j |  j j  |  j _ | j	 |  j _ n  |  j
 d j d |  j j d |  j j  |  j
 d j |  j  d S(   s,   Perform aggregation and binning as requried.t
   dimensionst   columnsR   R$   R   N(   R   R,   R
   R   R2   R   R   t   applyR.   t
   agg_columnt
   attributesR7   t   sourcet   add_bin_labels(   R5   t   agg(    (    s@   lib/python2.7/site-packages/bkcharts/builders/heatmap_builder.pyt   process_data   s    c         c` s   x |  j  j |  j   D] } |  j j d } t d | j |  j j  d | j |  j j  d | j |  d |  j	 |  j
 d |  j |  j
 d | d d	 | d d
 | j  } |  j | |  x | j D] } | Vq Wq Wd S(   s.   Generate a set fo bins for each group of data.t   _valuesR   R   R   t   widtht   heightt
   line_colorR$   t
   fill_colort   labelN(   R.   t   groupbyR<   R   R2   R   t
   get_valuesR   R   R(   t   spacing_ratioR*   RF   t	   add_glypht	   renderers(   R5   t   groupt   binned_valuest
   comp_glypht   renderer(    (    s@   lib/python2.7/site-packages/bkcharts/builders/heatmap_builder.pyt   yield_renderers   s    

(   t   __name__t
   __module__t   __doc__R	   R   R8   t   req_dimensionsR   t   TrueR   R4   t   default_attributesR   R(   R*   RI   R   R   R7   R@   RP   (    (    (    s@   lib/python2.7/site-packages/bkcharts/builders/heatmap_builder.pyR   f   s$   							N(   RS   t
   __future__R    R   R   t   bokeh.core.propertiesR   R   t   bokeh.palettesR   t   builderR   R   t   statsR   t
   propertiesR	   R/   R
   R<   R   t   glyphsR   t   utilsR   R,   R4   RU   R   R   (    (    (    s@   lib/python2.7/site-packages/bkcharts/builders/heatmap_builder.pyt   <module>   s   A