
    g.                     Z    d Z ddlZddlZddlmZ ddlZddlmZ ddlmZ  G d de	      Z
y)	z
Matplotlib Exporter
===================
This submodule contains tools for crawling a matplotlib figure and exporting
relevant pieces to a renderer.
    N   )utils)
transforms)FigureCanvasAggc                   l    e Zd ZdZddZd Ze	 dd       Zd Zd Z	d Z
dd	Zdd
ZddZ	 ddZd Zy)Exportera  Matplotlib Exporter

    Parameters
    ----------
    renderer : Renderer object
        The renderer object called by the exporter to create a figure
        visualization.  See mplexporter.Renderer for information on the
        methods which should be defined within the renderer.
    close_mpl : bool
        If True (default), close the matplotlib figure as it is rendered. This
        is useful for when the exporter is used within the notebook, or with
        an interactive matplotlib backend.
    c                      || _         || _        y N)	close_mplrenderer)selfr   r   s      e/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/plotly/matplotlylib/mplexporter/exporter.py__init__zExporter.__init__   s    "     c                     |j                   t        |      }|j                  t        j                         d|j
                         | j                  rddlm} |j                  |       | j                  |       y)z
        Run the exporter on the given figure

        Parmeters
        ---------
        fig : matplotlib.Figure instance
            The figure to export
        Npng)formatdpir   )canvasr   savefigioBytesIOr   r   matplotlib.pyplotpyplotclose	crawl_fig)r   figr   plts       r   runzExporter.run#   sU     ::$S)FBJJLCGG<>>+IIcNsr   Nc                    t        | t        j                        rt        j                  d       ||| |z
  j                  |      }|} d}|jd|j                  fd|j                  fd|j                  j                  fdt        j                         ffD ]  \  }}| j                  |      s|| |z
  } } n |)|r|| j                  |      | fS || j                  |      fS |r|| fS |S )a?  Process the transform and convert data to figure or data coordinates

        Parameters
        ----------
        transform : matplotlib Transform object
            The transform applied to the data
        ax : matplotlib Axes object (optional)
            The axes the data is associated with
        data : ndarray (optional)
            The array of data to be transformed.
        return_trans : bool (optional)
            If true, return the final transform of the data
        force_trans : matplotlib.transform instance (optional)
            If supplied, first force the data to this transform

        Returns
        -------
        code : string
            Code is either "data", "axes", "figure", or "display", indicating
            the type of coordinates output.
        transform : matplotlib transform
            the transform used to map input data to output data.
            Returned only if return_trans is True
        new_data : ndarray
            Data transformed to match the given coordinate code.
            Returned only if data is specified
        zMBlended transforms not yet supported. Zoom behavior may not work as expected.displaydataaxesfigure)
isinstancer   BlendedGenericTransformwarningswarn	transform	transData	transAxesr$   transFigureIdentityTransformcontains_branch)r)   axr"   return_transforce_transcodectranss           r   process_transformzExporter.process_transform7   s   > i!C!CDMM:
 "!K/::4@#I>&&299001J88:;	 5 ,,U3'()e*;)D Y006	AAY00666Y&r   c                     | j                   j                  |t        j                  |            5  |j                  D ]  }| j                  |        	 ddd       y# 1 sw Y   yxY w)z%Crawl the figure and process all axes)r   propsN)r   draw_figurer   get_figure_propertiesr#   crawl_ax)r   r   r/   s      r   r   zExporter.crawl_figx   sY    ]]&&3e6Q6QRU6V&W 	"hh "b!"	" 	" 	"s   #AA'c                 n   | j                   j                  |t        j                  |            5  |j                  D ]  }| j                  ||        |j                  D ]  }| j                  ||        t        |j                  j                  |j                  j                  |j                  gg d      D ]B  \  }}t        |d      s|j                         s$| j                  |||j                  |       D |j                   D ]9  }t#        |t$        j&                  j(                        s(| j                  ||       ; |j*                  D ]  }| j-                  ||        |j.                  D ]  }| j1                  ||        |j2                  D ]  }| j5                  ||        |j7                         }	|	St        j8                  ||	      }
| j                   j;                  |	|
      5  |
d   r| j=                  ||	       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)z.Crawl the axes and process all elements within)r/   r7   )xlabelylabeltitleget_text)r1   	text_typeN)legendr7   visible)r   	draw_axesr   get_axes_propertieslines	draw_linetexts	draw_textzipxaxislabelyaxisr>   hasattrr?   r+   artistsr%   
matplotlibtextTextpatches
draw_patchcollectionsdraw_collectionimages
draw_image
get_legendget_legend_propertiesdraw_legendcrawl_legend)r   r/   linerP   ttpartistpatch
collectionimagerA   r7   s              r   r:   zExporter.crawl_ax~   s   ]]$$%2K2KB2O$P 	6 )r4() )r4() :- V	c 4,NN2tQTNUV ** /fjoo&:&:;NN2v./  +E*+ nn 5
$$R45 +E*+ ]]_F!33B?]]..fE.J 6Y'))"f563	6 	626 63	6 	6s8   BH+H+AH+4CH+6HH+H(	$H++H4c                 0   t        t        j                  |j                  d            }|j	                  |j
                         |D ]  }|j                  d|j                         z          t        |t        j                  j                        r"|j                  |j                         dz
         	 t        |t        j                  j                        r| j                  |||j                         nt        |t        j                  j                         r2|j#                         dk7  r| j%                  |||j                         nt        |t        j&                  j(                        r| j+                  |||j                         n[t        |t        j,                  j.                        r| j1                  |||j                         nt3        j4                  d|z          y	# t6        $ r t3        j4                  d|z         Y w xY w)
zE
        Recursively look through objects in legend children
        T)skipContainersg    .Ar   r1   None)force_pathtransz Legend element %s not impementedN)listr   iter_all_children_legend_boxappendlegendPatch
set_zorder
get_zorderr%   rO   rR   FancyBboxPatchPatchrS   r+   rP   rQ   r?   rH   rE   Line2DrF   rT   
CollectionrU   r'   r(   NotImplementedError)r   r/   rA   legendElementschilds        r   r[   zExporter.crawl_legend   s    ##F$6$6tL
 	f001# 	JES5#3#3#556 %!3!3!B!BC  !1!1!3a!78JeZ%7%7%=%=>OOB2<<OHz';';<~~'61r5bllKz'7'7'>'>?NN2u",,NGz'='='H'HI((UBLL(QMM"Du"LM)	J* ' J@5HIJs   5D7G00!HHc                    | j                  |j                         ||j                         |      \  }}t        j                  |      }|d   
|d   dk(  rd}t        j
                  |      }|d   dv s|d   d	   j                  d	k(  rd}|j                         }|s|r"| j                  j                  ||||||
       yy)z5Process a matplotlib line and call renderer.draw_linerd   	dasharrayN	drawstyledefaultmarker)re   noneN
markerpathr   )r"   coordinates	linestylemarkerstylerK   mplobj)
r5   get_transform
get_xydatar   get_line_styleget_marker_stylesize	get_labelr   draw_marked_line)	r   r/   r\   r1   r|   r"   r}   r~   rK   s	            r   rF   zExporter.draw_line   s     22 "doo&7[ 3 
T ((.	[!)i.D	.QI,,T2!%;;<(+00A5K )MM**'#' +  $r   c                    |j                         }|ro|j                         }|j                         }| j                  ||||      \  }}t	        j
                  |      }	| j                  j                  |||||	|       yy)z<Process a matplotlib text object and call renderer.draw_textrd   )rP   positionr|   r@   styler   N)r?   r   get_positionr5   r   get_text_styler   rH   )
r   r/   rP   r1   r@   contentr)   r   coordsr   s
             r   rH   zExporter.draw_text   s    --/**,I((*H#552x[  6  FH ((.EMM##!"# $  r   c                 *   t        j                  |j                               \  }}|j                         }| j	                  ||||      \  }}t        j
                  ||j                               }| j                  j                  |||||       y)z=Process a matplotlib patch object and call renderer.draw_pathrd   )fill)r"   r|   	pathcodesr   r   N)	r   SVG_pathget_pathr   r5   get_path_styleget_fillr   	draw_path)	r   r/   r_   r1   verticesr   r)   r|   r}   s	            r   rS   zExporter.draw_patch   s    #nnU^^-=>)'')	 $ 6 6r8 !7 !
X ((U^^5EF	# 	  	
r   c           
         |j                         \  }}}}| j                  ||||      \  }	}| j                  |||      }
|D cg c]  }t        j                  |       }}|D cg c]"  }| j                  |||d   |      d   |d   f$ }}|j	                         }	 |D cg c]  }|j                          }}|j                         |j                         |j                         |j                  |j                         d}ddd}t        |d      r||j                            nd}| j                  j                  ||
|||	|||	       y
c c}w c c}w c c}w # t        $ r Y w xY w)zAProcess a matplotlib collection and call renderer.draw_collectionrd   r   r   )	linewidth	facecolor	edgecoloralphazorderbeforeafter)r"   screenget_offset_position)pathspath_coordinatespath_transformsoffsetsoffset_coordinatesoffset_orderstylesr   N)_prepare_pointsr5   r   r   get_transforms
get_matrixAttributeErrorget_linewidthsget_facecolorsget_edgecolors_alpharm   rM   r   r   draw_path_collection)r   r/   r`   rf   force_offsettransr)   transOffsetr   r   offset_coordspath_coordspathprocessed_pathsr   tr   offset_dictr   s                     r   rU   zExporter.draw_collection   s    4>3M3M3O0K%!%!7!7W2C "8 "
w ,,Y,X<ABD5>>$/BB (
  &&r47 '  Q	
 
 %335	 8GG!q||~GOG $224#224#224&& ++-
  (7; z#89 
6689 	 	**!(+,% 	+ 		
K C
 H 		s0   E('E"E" &E=E" E" "	E.-E.c                     | j                   j                  t        j                  |      |j	                         d|j                         |j                         d|       y)z>Process a matplotlib image object and call renderer.draw_imager"   )r   r   )imdataextentr|   r   r   N)r   rW   r   image_to_base64
get_extent	get_alpharm   )r   r/   ra   s      r   rW   zExporter.draw_image4  sQ      ((/##%!OO-9I9I9KL 	! 	
r   )T)NNFNr
   )NN)__name__
__module____qualname____doc__r   r   staticmethodr5   r   r:   r[   rF   rH   rS   rU   rW    r   r   r   r      s[    !( GK> >@"6>J@2&
" GK9
v
r   r   )r   r'   r    r   rO   r   matplotlib.backends.backend_aggr   objectr   r   r   r   <module>r      s,     	   ! ;l
v l
r   