B
    Y                  @   s   d Z ddlmZ ddl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mZmZmZmZmZmZmZ d	d
lmZmZ G dd deZG dd deZdS )z

    )absolute_importN)	iteritems)HasProps)GlyphRenderer)ColumnDataSource)AnyColorDictEitherFloatInstanceListString   )ColumnColumnLabelc                   sT  e Zd ZdZeeeeedddZee	e
e	eddZeeddZeeeZeeeZeeddZed	d
dZed	dZedddZe
ddZe
ddZe
ddZe
ddZe
ddZe
ddZ fddZdd Zdd Z e!dd Z"e!dd Z#dd Z$dd Z%dd  Z&d!d" Z'd#d$ Z(d%d& Z)d'd( Z*d)d* Z+d+d, Z,e-d-d. Z.  Z/S )/CompositeGlyphaY  Represents a subset of data.

    A collection of hetero or homogeneous glyph
    renderers which represent a subset of data. The
    purpose of the composite glyph is to abstract
    away the details of constructing glyphs, based on
    the details of a subset of data, from the grouping
    operations that a generalized builders must implement.

    In general, the Builder operates at the full column
    oriented data source level, segmenting and assigning
    attributes from a large selection, while the composite glyphs
    will typically be passed an array-like structures with
    one or more singular attributes to apply.

    Another way to explain the concept is that the Builder
    operates as the groupby, as in pandas, while the
    CompositeGlyph operates as the function used in the apply.

    What is the responsibility of the Composite Glyph?
        - Produce GlyphRenderers
        - Apply any aggregations
        - Tag the GlyphRenderers with the group label
        - Apply transforms due to chart operations
            - Note: Operations require implementation of special methods
    NonezIdentifies the subset of data.)defaulthelpaU  
        Array-like values, which are used as the input to the composite glyph.

        Most composite glyphs add their own representation of one or more values-like
        columns/arrays that they receive as inputs. These are compiled together for
        generating `source`, `data`, and `df` by the individual composite glyphs.
        )r   zThe data source used for the contained
        glyph renderers. Simple glyphs part of the composite glyph might not use the
        column data source.z\A list of chart operations that can be applied to
        manipulate their visual depiction.ZgrayzsA high level color. Some glyphs will
        implement more specific color attributes for parts or specific glyphs.)r   Zblackz5A default outline color for contained
        glyphs.g?g      ?g        c                sJ   | d}t |s"t |r,|g|d< tt| jf | |   d S )Nvalues)getr   Zis_validr   superr   __init__setup)self
propertiesZvals)	__class__ .lib/python3.7/site-packages/bkcharts/models.pyr   O   s
    

zCompositeGlyph.__init__c             C   s*   dd |   D | _| jdk	r&|   dS )z=Build renderers and data source and set sources on renderers.c             S   s   g | ]}|qS r   r   ).0rendererr   r   r   
<listcomp>X   s    z(CompositeGlyph.setup.<locals>.<listcomp>N)build_renderers	renderersrefresh)r   r   r   r   r   V   s    
zCompositeGlyph.setupc             C   s`   | j dk	r\|  }|dk	r\t|tr,t|}t|tsH|dk	rHtdn| || _|   dS )zrUpdate the GlyphRenderers.

        .. note:
            this method would be called after data is added.
        Nz2build_source must return dict or ColumnDataSource.)	r$   build_source
isinstancedictr   	TypeErroradd_chart_indexsource_set_sources)r   datar+   r   r   r   r%   \   s    


zCompositeGlyph.refreshc             C   s   | j d k	r| j jS i S d S )N)r+   r-   )r   r   r   r   r-   q   s    
zCompositeGlyph.datac             C   s   | j rt| j S t S d S )N)r-   pdZ	DataFrame)r   r   r   r   dfx   s    zCompositeGlyph.dfc             C   s   t |tr|}|j}nd}d|krtt| dkrtt| d }| jg| |d< t | jtrx$t	| jD ]\}}|g| ||< qrW |dk	r||_|S |S dS )aF  Add identifier of the data group as a column for each row.

        Args:
            data (dict or `ColumnDataSource`): can be the type of data used internally
                to ColumnDataSource, or a ColumnDataSource.

        Returns:
            dict or `ColumnDataSource`: returns the same type of data provided
        NZchart_indexr   )
r'   r   r-   lenlistkeysr   labelr(   r   )r   r-   r+   Zn_rowscolvalr   r   r   r*      s    
zCompositeGlyph.add_chart_indexc             c   s   t  V  d S )N)r   )r   r   r   r   r#      s    zCompositeGlyph.build_renderersc             C   s   i }| j d k	rd| j i}|S )Nr   )r   )r   r-   r   r   r   r&      s    

zCompositeGlyph.build_sourcec             C   s   x| j D ]}| j|_qW dS )zStore reference to source in each GlyphRenderer.

        .. note::
            if the glyphs that are part of the composite glyph differ, you may have to
            override this method and handle the sources manually.
        N)r$   r+   Zdata_source)r   r!   r   r   r   r,      s    zCompositeGlyph._set_sourcesc             C   s   dS )zBA special method the `stack` function applies to composite glyphs.Nr   )r   glyphsr   r   r   	__stack__   s    zCompositeGlyph.__stack__c             C   s   dS )zCA special method the `jitter` function applies to composite glyphs.Nr   )r   r6   r   r   r   
__jitter__   s    zCompositeGlyph.__jitter__c             C   s   dS )zBA special method the `dodge` function applies to composite glyphs.Nr   )r   r6   r   r   r   	__dodge__   s    zCompositeGlyph.__dodge__c             C   s   dS )zDA special method the `overlay` function applies to composite glyphs.Nr   )r   r6   r   r   r   __overlay__   s    zCompositeGlyph.__overlay__c             C   s   d S )Nr   )r   r   r   r   apply_operations   s    zCompositeGlyph.apply_operationsc             C   s0   i }x&t | jD ]\}}|jdd||< qW |S )NT)Z	withbases)r   r6   Zclass_properties)clsZpropsnameZglyphr   r   r   glyph_properties   s    zCompositeGlyph.glyph_properties)0__name__
__module____qualname____doc__r
   r   r	   r   r3   r   r   r   r   r   r+   r   r   r$   r6   Z
operationsr   ZcolorZ
fill_colorZ
line_colorZ
fill_alphaZ
line_alphaZleft_bufferZright_bufferZ
top_bufferZbottom_bufferr   r   r%   propertyr-   r/   r*   r#   r&   r,   r7   r8   r9   r:   r;   classmethodr>   __classcell__r   r   )r   r   r      sJ   







#
r   c               @   sX   e Zd ZdZeeeddZeddZ	eddZ
eeeeddZdd Zdd
dZd	S )CollisionModifiera5  Models an special type of operation that alters how glyphs interact.

    Used to handle the manipulation of glyphs for operations, such as stacking. The
    list of `CompositeGlyph`s can either be input into the `CollisionModifier` as
    keyword args, or added individually with the `add_glyph` method.
    zAA list of composite glyphs,
        to apply the modification to.)r   z#The name of the collision modifier.zzThe name of the method that will be utilized on
        the composite glyphs. This method must exist on all `comp_glyphs`.zcSome collision modifiers
        might require column labels to apply the operation in relation to.c             C   s   | j | d S )N)comp_glyphsappend)r   Z
comp_glyphr   r   r   	add_glyph   s    zCollisionModifier.add_glyphNc             C   sP   t | jdkr|| _t | jdkr<t| jd | j| j ntd| jj d S )Nr   z6%s must be applied to available renderers, none found.)r0   rG   getattrmethod_nameAttributeErrorr   r?   )r   r$   r   r   r   apply   s    zCollisionModifier.apply)N)r?   r@   rA   rB   r   r   r   rG   r   r=   rK   r
   r   columnsrI   rM   r   r   r   r   rF      s   

rF   )rB   Z
__future__r   Zpandasr.   Zsixr   Zbokeh.core.has_propsr   Zbokeh.models.renderersr   Zbokeh.models.sourcesr   Zbokeh.core.propertiesr   r   r	   r
   r   r   r   r   r   r   r   r   rF   r   r   r   r   <module>   s   ( D