B
    Y%              
   @   s   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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 Donut builder which lets you build your Donut plots just passing
the arguments to the Chart class and calling the proper functions.
    )absolute_import)StringInstanceFloatColorEitherList)	HoverTool)AnnularWedgeText)Range1d)GlyphRenderer)ColumnDataSource   )create_and_buildBuilder)build_wedge_sourcebuild_wedge_text_sourcebuild_agg_tooltipderive_aggregation)	ColorAttrCatAttr)	DimensionindexNT  Fc             K   s   ||d< ||d< ||d< |	|d< |
|d< ||d< ||d< |dk	rH||d	< t t| f|}d
|jd _d
|jd _t|||d\}}|rt|||d}|t|gd |S )a	   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)

    labelvaluescolorxgridygridplot_height
plot_widthNaggFr   )Zdim_colsagg_colr"   )
hover_textZaggregated_colZagg_text)Ztooltips)	r   DonutBuilderleftZvisibleZbelowr   r   Z	add_toolsr	   )datar   r   r   r"   Z
hover_toolr$   r    r!   r   r   kwZchartZtooltip r)   >lib/python3.7/site-packages/bkcharts/builders/donut_builder.pyDonut(   s$    Cr+   c               @   s   e Zd ZdZe e e dZ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
d Zdd Zdd Zdd ZdS )r%   zProduces 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   stackr   sum)defaultg      ?g        Z10ptZWhitec                s  | j d jd kr2| j d j| jj| jjjd d | jjd kr*tdd | jjj	j
D st| jjj	j
 | jjjdd | j d j| jj d  fdd| jjjD }n6| j d jd d	krd
d | jjjD }n| j d j}|d | j_| jj| jj jjdkr*| j d j| jj|d d d| _| j d jd kr`| j d j| jj| j d jd d | j d jd kr| j d j| jj| j d jd d d S )Nr   r   )r'   columnsc             S   s   g | ]}|d kqS )Nr)   ).0namer)   r)   r*   
<listcomp>   s    z&DonutBuilder.setup.<locals>.<listcomp>T)Zinplacec                s   g | ]}| d g kr|qS )r   r)   )r0   col)
label_colsr)   r*   r2      s    r   c             S   s   g | ]}|d kr|qS ))r   r)   )r0   r3   r)   r)   r*   r2      s    objectcountr   r,   )
attributesr/   setup_datasourcedfr   	selectionallr   nameslistZreset_indexZdtyper1   r"   )selfZcolsr)   )r4   r*   r8      s0    zDonutBuilder.setupc             C   s:   t | jjd d | j }t| || _t| || _d S )Nlevelg?)max
chart_datar'   level_widthr   Zx_rangeZy_range)r@   rngr)   r)   r*   
set_ranges   s    zDonutBuilder.set_rangesc             C   sz   t | jj| jd j| jj| j| j| j	d}d|d< x.| jj
f | jD ]}|d |j|d df< qDW t|| _t|| _d S )Nr   )Zcat_colsr#   r"   rD   level_spacing r   r,   )r   r9   r;   r7   r/   r   r<   r"   rD   rG   groupbyZlocr   rC   r   	text_data)r@   Z
polar_datagroupr)   r)   r*   process_data   s    


zDonutBuilder.process_datac             c   sX   t dddddddd| jd	}t| j|d	V  td
ddddd| jd}t| j|d	V  d S )Nr   ZinnersZoutersstartendr   g?)	xyZinner_radiusZouter_radiusZstart_angleZ	end_angleZ
fill_colorZ
fill_alpha
line_color)Zdata_sourceZglyphrO   rP   textZ
text_anglecenterZmiddle)rO   rP   rR   ZangleZ
text_alignZtext_baselinetext_font_size)r
   rQ   r   rC   r   rT   rJ   )r@   ZawZtxtr)   r)   r*   yield_renderers   s    


zDonutBuilder.yield_renderersN)__name__
__module____qualname____doc__r   r   Zdefault_attributesZ
dimensionsr   r   r   r"   r   r   rC   rJ   r   rD   r   r   rG   rT   r   rQ   r8   rF   rL   rU   r)   r)   r)   r*   r%      s"   




/r%   )
r   NNNTNr   r   FF)$rY   Z
__future__r   Zbokeh.core.propertiesr   r   r   r   r   r   Zbokeh.modelsr	   Zbokeh.models.glyphsr
   r   Zbokeh.models.rangesr   Zbokeh.models.renderersr   Zbokeh.models.sourcesr   Zbuilderr   r   Zutilsr   r   r   r   r7   r   r   Z
propertiesr   r+   r%   r)   r)   r)   r*   <module>   s      
Z