o
    jX*                     @  st   d Z ddlmZ ddlZddlmZ ddlmZ G dd deZ	G dd	 d	e	Z
G d
d de	ZG dd deZdS )z"Chart builder and related objects.    )annotationsN)contextmanager)Workbookc                      s@   e Zd ZdZ fddZedd Zedd Zdd	 Z	  Z
S )
_BaseWorkbookWriterz:Base class for workbook writers, providing shared members.c                   s   t t|   || _d S )N)superr   __init___chart_data)self
chart_data	__class__ O/home/air/Certificate/back/venv/lib/python3.10/site-packages/pptx/chart/xlsx.pyr      s   
z_BaseWorkbookWriter.__init__c                 C  sP   t  }| |\}}| || W d   | S 1 sw   Y  | S )z+bytes for Excel file containing chart_data.N)ioBytesIO_open_worksheet_populate_worksheetgetvaluer	   	xlsx_fileworkbook	worksheetr   r   r   	xlsx_blob   s   
z_BaseWorkbookWriter.xlsx_blobc                 c  s.    t |ddi}| }||fV  |  dS )z
        Enable XlsxWriter Worksheet object to be opened, operated on, and
        then automatically closed within a `with` statement. A filename or
        stream object (such as an `io.BytesIO` instance) is expected as
        *xlsx_file*.
        	in_memoryTN)r   add_worksheetcloser   r   r   r   r      s
   
z#_BaseWorkbookWriter._open_worksheetc                 C  s   t d)zy
        Must be overridden by each subclass to provide the particulars of
        writing the spreadsheet data.
        z!must be provided by each subclass)NotImplementedErrorr	   r   r   r   r   r   r   '   s   z'_BaseWorkbookWriter._populate_worksheet)__name__
__module____qualname____doc__r   propertyr   r   r   r   __classcell__r   r   r   r   r      s    

r   c                   @  s`   e Zd ZdZedd Zdd Zdd Zedd	 Z	d
d Z
dd Zdd Zdd Zdd ZdS )CategoryWorkbookWriterz
    Determines Excel worksheet layout and can write an Excel workbook from
    a CategoryChartData object. Serves as the authority for Excel worksheet
    ranges.
    c                 C  sF   | j j}|jdkrtdttd|j d }|jd }d||f S )z|
        The Excel worksheet reference to the categories for this chart (not
        including the column heading).
        r   z!chart data contains no categoriesA   zSheet1!$A$2:$%s$%d)r   
categoriesdepth
ValueErrorchrord
leaf_count)r	   r'   	right_col
bottom_rowr   r   r   categories_ref6   s   

z%CategoryWorkbookWriter.categories_refc                 C  s   d|  | S )z
        Return the Excel worksheet reference to the cell containing the name
        for *series*. This also serves as the column heading for the series
        values.
        zSheet1!$%s$1)_series_col_letterr	   seriesr   r   r   series_name_refC   s   z&CategoryWorkbookWriter.series_name_refc                 C  s$   dj di | |t|d dS )zy
        The Excel worksheet reference to the values for this series (not
        including the column heading).
        z1Sheet1!${col_letter}$2:${col_letter}${bottom_row}r&   )
col_letterr.   Nr   )formatr0   lenr1   r   r   r   
values_refK   s
   
z!CategoryWorkbookWriter.values_refc                 C  sd   | dk s| dkrt dd}| r0| d }|dkrd}ttd| d }|| }| d d } | s|S )zReturn str Excel column reference like 'BQ' for *column_number*.

        *column_number* is an int in the range 1-16384 inclusive, where
        1 maps to column 'A'.
        r&   i @  z&column_number must be in range 1-16384    r   r%   )r)   r*   r+   )column_numbercol_ref	remainderr4   r   r   r   _column_referenceW   s   z(CategoryWorkbookWriter._column_referencec                 C  s   |  || | || dS )a  
        Write the chart data contents to *worksheet* in category chart
        layout. Write categories starting in the first column starting in
        the second row, and proceeding one column per category level (for
        charts having multi-level categories). Write series as columns
        starting in the next following column, placing the series title in
        the first cell.
        N)_write_categories_write_seriesr   r   r   r   r   w   s   	z*CategoryWorkbookWriter._populate_worksheetc                 C  s   d|j j |j }| |S )zj
        The letter of the Excel worksheet column in which the data for a
        series appears.
        r&   )r'   r(   indexr=   )r	   r2   r:   r   r   r   r0      s   
z)CategoryWorkbookWriter._series_col_letterc           	      C  sR   | j j}|d|ji}|j}t|jD ]\}}|| d }| |||| qdS )aF  
        Write the categories column(s) to *worksheet*. Categories start in
        the first column starting in the second row, and proceeding one
        column per category level (for charts having multi-level categories).
        A date category is formatted as a date. All others are formatted
        `General`.
        
num_formatr&   N)r   r'   
add_formatnumber_formatr(   	enumeratelevels_write_cat_column)	r	   r   r   r'   rA   r(   idxlevelcolr   r   r   r>      s   z(CategoryWorkbookWriter._write_categoriesc                 C  s8   | ||d |D ]\}}|d }||||| q	dS )z
        Write a category column defined by *level* to *worksheet* at offset
        *col* and formatted with *num_format*.
        
   r&   N)
set_columnwrite)r	   r   rI   rH   rA   offnamerowr   r   r   rF      s
   z(CategoryWorkbookWriter._write_cat_columnc                 C  s\   | j jj}t| j D ]!\}}|d|ji}|| }|d||j |d||j	| q
dS )z
        Write the series column(s) to *worksheet*. Series start in the column
        following the last categories column, placing the series title in the
        first cell.
        rA   r   r&   N)
r   r'   r(   rD   rB   rC   rL   rN   write_columnvalues)r	   r   r   
col_offsetrG   r2   rA   
series_colr   r   r   r?      s   
z$CategoryWorkbookWriter._write_seriesN)r   r   r    r!   r"   r/   r3   r7   staticmethodr=   r   r0   r>   rF   r?   r   r   r   r   r$   /   s    


r$   c                   @  s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )XyWorkbookWriterz
    Determines Excel worksheet layout and can write an Excel workbook from XY
    chart data. Serves as the authority for Excel worksheet ranges.
    c                 C  s   |  |d }d| S )z
        Return the Excel worksheet reference to the cell containing the name
        for *series*. This also serves as the column heading for the series
        Y values.
        r&   zSheet1!$B$%d)series_table_row_offset)r	   r2   rO   r   r   r   r3      s   z XyWorkbookWriter.series_name_refc                 C  s   |j d }|j}|| S )zq
        Return the number of rows preceding the data table for *series* in
        the Excel worksheet.
           )r@   data_point_offset)r	   r2   title_and_spacer_rowsdata_point_rowsr   r   r   rV      s   
z(XyWorkbookWriter.series_table_row_offsetc                 C  *   |  |d }|t| d }d||f S )zx
        The Excel worksheet reference to the X values for this chart (not
        including the column label).
        rW   r&   zSheet1!$A$%d:$A$%drV   r6   r	   r2   top_rowr.   r   r   r   x_values_ref      zXyWorkbookWriter.x_values_refc                 C  r[   )zx
        The Excel worksheet reference to the Y values for this chart (not
        including the column label).
        rW   r&   zSheet1!$B$%d:$B$%dr\   r]   r   r   r   y_values_ref   r`   zXyWorkbookWriter.y_values_refc                 C  sx   | d| jji}| jD ]-}| d|ji}| |}||d d|j| ||d|j ||d d|j| qdS )a#  
        Write chart data contents to *worksheet* in the standard XY chart
        layout. Write the data for each series to a separate two-column
        table, X values in column A and Y values in column B. Place the
        series label in the first (heading) cell of the column.
        rA   r&   r   N)	rB   r   rC   rV   rP   x_valuesrL   rN   y_valuesr	   r   r   chart_num_formatr2   series_num_formatoffsetr   r   r   r      s   

z$XyWorkbookWriter._populate_worksheetN)	r   r   r    r!   r3   rV   r_   ra   r   r   r   r   r   rU      s    				rU   c                   @  s    e Zd ZdZdd Zdd ZdS )BubbleWorkbookWriterz^
    Service object that knows how to write an Excel workbook from bubble
    chart data.
    c                 C  r[   )z
        The Excel worksheet reference to the range containing the bubble
        sizes for *series* (not including the column heading cell).
        rW   r&   zSheet1!$C$%d:$C$%dr\   r]   r   r   r   bubble_sizes_ref   r`   z%BubbleWorkbookWriter.bubble_sizes_refc                 C  s   | d| jji}| jD ]?}| d|ji}| |}||d d|j| ||d|j ||d d|j| ||dd ||d d|j	| qdS )aN  
        Write chart data contents to *worksheet* in the bubble chart layout.
        Write the data for each series to a separate three-column table with
        X values in column A, Y values in column B, and bubble sizes in
        column C. Place the series label in the first (heading) cell of the
        values column.
        rA   r&   r   rW   SizeN)
rB   r   rC   rV   rP   rb   rL   rN   rc   bubble_sizesrd   r   r   r   r      s   

z(BubbleWorkbookWriter._populate_worksheetN)r   r   r    r!   ri   r   r   r   r   r   rh      s    	rh   )r!   
__future__r   r   
contextlibr   
xlsxwriterr   objectr   r$   rU   rh   r   r   r   r   <module>   s    $ <