
Yc           @@  s9  d  Z  d d l m Z d d l m Z m Z m Z 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 l m Z d d l m Z d	 d
 l m Z 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 d d e# d d d e$ e$ 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 Donut builder which lets you build your Donut plots just passing
the arguments to the Chart class and calling the proper functions.
i    (   t   absolute_import(   t   Stringt   Instancet   Floatt   Colort   Eithert   List(   t	   HoverTool(   t   AnnularWedget   Text(   t   Range1d(   t   GlyphRenderer(   t   ColumnDataSourcei   (   t   create_and_buildt   Builder(   t   build_wedge_sourcet   build_wedge_text_sourcet   build_agg_tooltipt   derive_aggregation(   t	   ColorAttrt   CatAttr(   t	   Dimensiont   indexi  c         K@  s   | | d <| | d <| | d <|	 | d <|
 | d <| | d <| | d <| d k	 r_ | | d <n  t t |  |  } t | j d	 _ t | j d	 _ t d
 | d | d |  \ } } | r t d | d | d |  } | j	 t
 d | g   n  | S(   s	   Create a Donut chart containing one or more layers from table-like data.

    Create a donut chart using :class:`DonutBuilder
    <bkcharts.builders.donut_builder.DonutBuilder>` to
    render the glyphs from input data and specification. The primary
    use case for the donut chart is to show relative amount each category, within a
    categorical array or multiple categorical arrays, makes up of the whole for some
    array of values.

    Args:
        data (:ref:`userguide_charts_data_types`): the data source for the chart
            label (str or list(str), optional): the categorical variable to use for
            creating separate boxes
        values (str, optional): the values to use for producing the boxplot using
            table-like input data
        color (str or list(str) or bkcharts._attributes.ColorAttr, optional): the
            categorical variable or color attribute specification to use for coloring
            the wedges
        agg (str, optional): how the values associated with a wedge should be
            aggregated
        hover_tool (bool, optional): whether to show the value of the
            wedge when hovering
        hover_text (str, optional): provide an alternative string to use to label the
            value shown with the hover tool
        **kw:

    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 the wedges the make up
        the donut(s)

    Examples:

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

        from bkcharts import Donut, show, output_file
        from bkcharts.utils import df_from_json
        from bokeh.sampledata.olympics2014 import data

        import pandas as pd

        # utilize utility to make it easy to get json/dict data converted to a dataframe
        df = df_from_json(data)

        # filter by countries with at least one medal and sort by total medals
        df = df[df['total'] > 8]
        df = df.sort_values(by="total", ascending=False)
        df = pd.melt(df, id_vars=['abbr'],
                     value_vars=['bronze', 'silver', 'gold'],
                     value_name='medal_count', var_name='medal')

        # original example
        d = Donut(df, label=['abbr', 'medal'], values='medal_count',
                  text_font_size='8pt', hover_text='medal_count')

        output_file("donut.html")

        show(d)

    t   labelt   valuest   colort   xgridt   ygridt   plot_heightt
   plot_widtht   aggi    t   dim_colst   agg_colt
   hover_textt   aggregated_colt   agg_textt   tooltipsN(   t   NoneR   t   DonutBuildert   Falset   leftt   visiblet   belowR   R   t	   add_toolsR   (   t   dataR   R   R   R   t
   hover_toolR!   R   R   R   R   t   kwt   chartt   tooltip(    (    s>   lib/python2.7/site-packages/bkcharts/builders/donut_builder.pyt   Donut(   s$    C






!	R&   c           B@  s   e  Z d  Z i e   d 6e   d 6e   d 6Z d g Z e d  Z e	 d d  Z
 e e  Z e e  Z e d d  Z e e e e  d d Z e	 d d	  Z e d d
  Z d   Z d   Z d   Z d   Z RS(   s   Produces layered donut for hierarchical groups of data.

    Handles derivation of chart settings from inputs and assignment of attributes
    to each group of data.

    R   R   t   stackR   t   defaultt   sumg      ?g        t   10ptt   Whitec         C@  sp  |  j  d j d  k rI |  j  d j d |  j j d |  j j j d  n  |  j j d  k rt	 g  |  j j j
 j D] } | d  k ^ qq  st |  j j j
 j  } |  j j j d t  |  j  d j d |  j j d |  g  |  j j j D] } | | d g k r | ^ q } n[ |  j  d j d d k r`g  |  j j j D] } | d k r?| ^ q?} n |  j  d j } | d |  j _ |  j j |  j j j j d k r|  j  d j d |  j j d | d  d |  _ qn  |  j  d	 j d  k r"|  j  d	 j d |  j j d |  j  d j d  n  |  j  d
 j d  k rl|  j  d
 j d |  j j d |  j  d j d  n  d  S(   NR   R,   t   columnsi    t   inplaceR   t   objectt   countR   R2   (   R   (   t
   attributesR7   R%   t   setupt   _datat   sourcet   dfR   t	   selectiont   allR   t   namest   listt   reset_indext   Truet   dtypet   nameR   (   t   selfRG   t
   label_colst   colt   cols(    (    s>   lib/python2.7/site-packages/bkcharts/builders/donut_builder.pyR<      s2    11"c         C@  sK   t  |  j j d  d |  j } t | |  |  _ t | |  |  _ d  S(   Nt   levelg?(   t   maxt
   chart_dataR,   t   level_widthR
   t   x_ranget   y_range(   RH   t   rng(    (    s>   lib/python2.7/site-packages/bkcharts/builders/donut_builder.pyt
   set_ranges   s    !c         C@  s   t  |  j j d |  j d j d |  j j d |  j d |  j d |  j	 } d | d <x8 |  j j
 |  j   D]! } | d | j | d	 d f <qi Wt |  |  _ t |  |  _ d  S(
   Nt   cat_colsR   R    R   RO   t   level_spacingt    R   R2   (   R   R=   R?   R;   R7   R   R@   R   RO   RU   t   groupbyt   locR   RN   R   t	   text_data(   RH   t
   polar_datat   group(    (    s>   lib/python2.7/site-packages/bkcharts/builders/donut_builder.pyt   process_data   s    		
c         c@  s   t  d d d d d d d d d d	 d
 d d d d d d |  j  	} t d |  j d |  Vt d d d d d d d d d d d d d |  j  } t d |  j d |  Vd  S(   Nt   xi    t   yt   inner_radiust   innerst   outer_radiust   outerst   start_anglet   startt	   end_anglet   endt
   fill_colorR   t
   fill_alphag?t
   line_colort   data_sourcet   glypht   textt   anglet
   text_anglet
   text_alignt   centert   text_baselinet   middlet   text_font_size(   R   Ri   R   RN   R	   Rs   RY   (   RH   t   awt   txt(    (    s>   lib/python2.7/site-packages/bkcharts/builders/donut_builder.pyt   yield_renderers   s    (   t   __name__t
   __module__t   __doc__R   R   t   default_attributest
   dimensionsR   R   R   R   R   R   RN   RY   R   RO   R   R   RU   Rs   R   Ri   R<   RS   R\   Rv   (    (    (    s>   lib/python2.7/site-packages/bkcharts/builders/donut_builder.pyR&      s"   
		/		N('   Ry   t
   __future__R    t   bokeh.core.propertiesR   R   R   R   R   R   t   bokeh.modelsR   t   bokeh.models.glyphsR   R	   t   bokeh.models.rangesR
   t   bokeh.models.renderersR   t   bokeh.models.sourcesR   t   builderR   R   t   utilsR   R   R   R   R;   R   R   t
   propertiesR   R%   RE   R'   R1   R&   (    (    (    s>   lib/python2.7/site-packages/bkcharts/builders/donut_builder.pyt   <module>   s   ."Z