o
    e\4                     @  s   d dl mZ d dlZd dlmZmZ d dlmZ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Zd d
lmZmZ d dlmZmZ d dlmZ erpd dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$m%Z% G dd deZ&dS )    )annotationsN)TYPE_CHECKINGAny)
export_png
export_svgshow)get_screenshot_as_png)gridplot)Label)
Category10)figure)FillTypeLineType)filled_to_bokehlines_to_bokeh)Renderer)GridPlot)Palette)	ArrayLike)
FillReturn
LineReturnc                   @  s   e Zd ZU ded< ded< ded< ded< 	 					
		dUdVddZdWddZdXdd Z	!	"	#dYdZd*d+Z	!	,	-	.	!d[d\d5d6Z	!	"	7		d]d^d=d>Z		!	,d_d`dAdBZ
dadbdEdFZdcdHdIZdddJdKZdedfdMdNZ	!	O	P	dgdhdSdTZd.S )iBokehRendererzlist[figure]_figuresr   _layoutr   _palettebool	_want_svg   	   r   TFnrowsintncolsfigsizetuple[float, float]
show_framewant_svgreturnNonec                 C  s   || _ td | _dtj|td }|| }g | _| j rdnd}t|D ]}	t|d}
d|
j	_
d|
j_
| j|
 |sAd |
_d|
j_
q#t| j|d |d | |d	 | d
| _d S )N
   d   )ZdtypesvgZcanvas)Zoutput_backendFr   r   )r"   Ztoolbar_locationwidthheight)r   r   r   npasarrayr!   r   ranger   ZxgridvisibleZygridappendZoutline_line_coloraxisr	   r   )selfr    r"   r#   r%   r&   
total_sizeZnfiguresbackend_fig r9   MD:\Projects\ConvertPro\env\Lib\site-packages\contourpy/util/bokeh_renderer.py__init__0   s&   

zBokehRenderer.__init__colorstrc                 C  s4   t |tr|d dkrt|dd  }| j| }|S )Nr   Cr   )
isinstancer=   r!   r   )r4   r<   indexr9   r9   r:   _convert_colorM   s   
zBokehRenderer._convert_coloraxfigure | intr   c                 C  s   t |tr
| j| }|S )N)r?   r!   r   )r4   rB   r9   r9   r:   _get_figureS   s   

zBokehRenderer._get_figurer   C0ffffff?filledr   	fill_typer   alphafloatc           	      C  sN   |  |}| |}t||\}}t|dkr%|j|g|g||dd dS dS )a  Plot filled contours on a single plot.

        Args:
            filled (sequence of arrays): Filled contour data as returned by
                :func:`~contourpy.ContourGenerator.filled`.
            fill_type (FillType): Type of ``filled`` data, as returned by
                :attr:`~contourpy.ContourGenerator.fill_type`.
            ax (int or Bokeh Figure, optional): Which plot to use, default ``0``.
            color (str, optional): Color to plot with. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``Category10`` palette. Default ``"C0"``.
            alpha (float, optional): Opacity to plot with, default ``0.7``.
        r   )xsysr<   Z
fill_alpha
line_widthN)rD   rA   r   lenZmulti_polygons)	r4   rG   rH   rB   r<   rI   r8   rK   rL   r9   r9   r:   rG   X   s   

zBokehRenderer.filledblack皙?Nxr   ypoint_color
str | Nonequad_as_tri_alphac              
   C  s  |  |}| ||\}}dd |D dd |jD  }	dd |D dd |jD  }
t||d}|j|	|
fi | |dkr0d|ddddf |d	dddf  |ddd	df  |d	dd	df    }d|ddddf |d	dddf  |ddd	df  |d	dd	df    }|jd
d tj|ddddf  ||d	dd	df  fd	dD dd tj|ddddf  ||d	dd	df  fd	dD fi | |jdd tj|ddd	df  ||d	dddf  fd	dD dd tj|ddd	df  ||d	dddf  fd	dD fi | |durF|j| | |d|dd dS dS )a  Plot quad grid lines on a single plot.

        Args:
            x (array-like of shape (ny, nx) or (nx,)): The x-coordinates of the grid points.
            y (array-like of shape (ny, nx) or (ny,)): The y-coordinates of the grid points.
            ax (int or Bokeh Figure, optional): Which plot to use, default ``0``.
            color (str, optional): Color to plot grid lines, default ``"black"``.
            alpha (float, optional): Opacity to plot lines with, default ``0.1``.
            point_color (str, optional): Color to plot grid points or ``None`` if grid points
                should not be plotted, default ``None``.
            quad_as_tri_alpha (float, optional): Opacity to plot ``quad_as_tri`` grid, default
                ``0``.

        Colors may be a string color or the letter ``"C"`` followed by an integer in the range
        ``"C0"`` to ``"C9"`` to use a color from the ``Category10`` palette.

        Warning:
            ``quad_as_tri_alpha > 0`` plots all quads as though they are unmasked.
        c                 S     g | ]}|qS r9   r9   .0rowr9   r9   r:   
<listcomp>       z&BokehRenderer.grid.<locals>.<listcomp>c                 S  rV   r9   r9   rW   r9   r9   r:   rZ      r[   )
line_colorrI   r   g      ?Nr   c                 S  rV   r9   r9   rW   r9   r9   r:   rZ      r[   )r3   c                 S  rV   r9   r9   rW   r9   r9   r:   rZ      r[   c                 S  rV   r9   r9   rW   r9   r9   r:   rZ      r[   c                 S  rV   r9   r9   rW   r9   r9   r:   rZ      r[      )rQ   rR   
fill_colorr\   rI   size)	rD   _grid_as_2dTdict
multi_lineZravelr.   stackcircle)r4   rQ   rR   rB   r<   rI   rS   rU   r8   rK   rL   kwargsZxmidZymidr9   r9   r:   grids   s4   

XXDDDD

zBokehRenderer.grid      ?linesr   	line_typer   	linewidthc           
      C  sJ   |  |}| |}t||\}}	t|dkr#|j||	|||d dS dS )a  Plot contour lines on a single plot.

        Args:
            lines (sequence of arrays): Contour line data as returned by
                :func:`~contourpy.ContourGenerator.lines`.
            line_type (LineType): Type of ``lines`` data, as returned by
                :attr:`~contourpy.ContourGenerator.line_type`.
            ax (int or Bokeh Figure, optional): Which plot to use, default ``0``.
            color (str, optional): Color to plot lines. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``Category10`` palette. Default ``"C0"``.
            alpha (float, optional): Opacity to plot lines with, default ``1.0``.
            linewidth (float, optional): Width of lines, default ``1``.

        Note:
            Assumes all lines are open line strips not closed line loops.
        r   )r\   Z
line_alpharM   N)rD   rA   r   rN   rd   )
r4   rj   rk   rB   r<   rI   rl   r8   rK   rL   r9   r9   r:   rj      s   

zBokehRenderer.linesz'ArrayLike | np.ma.MaskedArray[Any, Any]c                 C  s^   t j|}|t jju rdS | |}| |}| ||\}}|j|| || |dd dS )a  Plot masked out grid points as circles on a single plot.

        Args:
            x (array-like of shape (ny, nx) or (nx,)): The x-coordinates of the grid points.
            y (array-like of shape (ny, nx) or (ny,)): The y-coordinates of the grid points.
            z (masked array of shape (ny, nx): z-values.
            ax (int or Bokeh Figure, optional): Which plot to use, default ``0``.
            color (str, optional): Circle color, default ``"black"``.
        Nr)   )r_   r`   )r.   maZgetmaskZnomaskrD   rA   ra   rf   )r4   rQ   rR   rm   rB   r<   maskr8   r9   r9   r:   rp      s   

zBokehRenderer.maskfilenametransparentc                 C  sF   |r| j D ]}d|_d|_q| jrt| j|d dS t| j|d dS )aO  Save plots to SVG or PNG file.

        Args:
            filename (str): Filename to save to.
            transparent (bool, optional): Whether background should be transparent, default
                ``False``.

        Warning:
            To output to SVG file, ``want_svg=True`` must have been passed to the constructor.
        N)rq   )r   Zbackground_fill_colorZborder_fill_colorr   r   r   r   )r4   rq   rr   r8   r9   r9   r:   save   s   
zBokehRenderer.save
io.BytesIOc                 C  s"   t | j}t }||d |S )zhSave plots to an ``io.BytesIO`` buffer.

        Return:
            BytesIO: PNG image buffer.
        Zpng)r   r   ioBytesIOrs   )r4   imagebufferr9   r9   r:   save_to_buffer   s   
zBokehRenderer.save_to_bufferc                 C  s   t | j dS )z:Show plots in web browser, in usual Bokeh manner.
        N)r   r   )r4   r9   r9   r:   r      s   zBokehRenderer.showtitlec                 C  s6   |  |}||_d|j_|dur| ||j_dS dS )a  Set the title of a single plot.

        Args:
            title (str): Title text.
            ax (int or Bokeh Figure, optional): Which plot to set the title of, default ``0``.
            color (str, optional): Color to set title. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``Category10`` palette. Default ``None`` which is ``black``.
        centerN)rD   rz   alignrA   
text_color)r4   rz   rB   r<   r8   r9   r9   r:   rz     s   

zBokehRenderer.titlegreen.1ffmtquad_as_tric                 C  sT  |  |}| |}| ||\}}t|}|j\}	}
t|ddd}t|	D ]&}t|
D ]}|t	d|||f |||f |||f | d| q-q'|rt|	d D ]Q}t|
d D ]F}t
|||d ||d f }t
|||d ||d f }t
|||d ||d f }|t	d|||| d| q^qVdS dS )	a  Show ``z`` values on a single plot.

        Args:
            x (array-like of shape (ny, nx) or (nx,)): The x-coordinates of the grid points.
            y (array-like of shape (ny, nx) or (ny,)): The y-coordinates of the grid points.
            z (array-like of shape (ny, nx): z-values.
            ax (int or Bokeh Figure, optional): Which plot to use, default ``0``.
            color (str, optional): Color of added text. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``Category10`` palette. Default ``"green"``.
            fmt (str, optional): Format to display z-values, default ``".1f"``.
            quad_as_tri (bool, optional): Whether to show z-values at the ``quad_as_tri`` centres
                of quads.

        Warning:
            ``quad_as_tri=True`` shows z-values for all quads, even if masked.
        r{   middle)r}   Z
text_alignZtext_baseline)rQ   rR   textr      Nr9   )rD   rA   ra   r.   r/   shaperc   r0   Z
add_layoutr
   mean)r4   rQ   rR   rm   rB   r<   r   r   r8   nynxrg   jixxyyzzr9   r9   r:   z_values  s(   



<"""$zBokehRenderer.z_values)r   r   r   TF)r    r!   r"   r!   r#   r$   r%   r   r&   r   r'   r(   )r<   r=   r'   r=   )rB   rC   r'   r   )r   rE   rF   )rG   r   rH   r   rB   rC   r<   r=   rI   rJ   r'   r(   )r   rO   rP   Nr   )rQ   r   rR   r   rB   rC   r<   r=   rI   rJ   rS   rT   rU   rJ   r'   r(   )r   rE   ri   r   )rj   r   rk   r   rB   rC   r<   r=   rI   rJ   rl   rJ   r'   r(   )r   rO   )rQ   r   rR   r   rm   rn   rB   rC   r<   r=   r'   r(   )F)rq   r=   rr   r   r'   r(   )r'   rt   )r'   r(   )r   N)rz   r=   rB   rC   r<   rT   r'   r(   )r   r~   r   F)rQ   r   rR   r   rm   r   rB   rC   r<   r=   r   r=   r   r   r'   r(   )__name__
__module____qualname____annotations__r;   rA   rD   rG   rh   rj   rp   rs   ry   r   rz   r   r9   r9   r9   r:   r      sR   
 

	7%

r   )'
__future__r   ru   typingr   r   Zbokeh.ior   r   r   Zbokeh.io.exportr   Zbokeh.layoutsr	   Zbokeh.models.annotations.labelsr
   Zbokeh.palettesr   Zbokeh.plottingr   numpyr.   Z	contourpyr   r   Zcontourpy.util.bokeh_utilr   r   Zcontourpy.util.rendererr   Zbokeh.modelsr   r   Znumpy.typingr   Zcontourpy._contourpyr   r   r   r9   r9   r9   r:   <module>   s&    