<s> """<STR_LIT>""" <EOL> import warnings <EOL> from graphy . backends . google_chart_api import util <EOL> class BaseChartEncoder ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , chart ) : <EOL> self . extra_params = { } <EOL> self . url_base = '<STR_LIT>' <EOL> self . formatters = self . _GetFormatters ( ) <EOL> self . chart = chart <EOL> self . enhanced_encoding = False <EOL> self . escape_url = True <EOL> self . _width = <NUM_LIT:0> <EOL> self . _height = <NUM_LIT:0> <EOL> def Url ( self , width , height , use_html_entities = False ) : <EOL> """<STR_LIT>""" <EOL> self . _width = width <EOL> self . _height = height <EOL> params = self . _Params ( self . chart ) <EOL> return util . EncodeUrl ( self . url_base , params , self . escape_url , <EOL> use_html_entities ) <EOL> def Img ( self , width , height ) : <EOL> """<STR_LIT>""" <EOL> url = self . Url ( width , height , use_html_entities = True ) <EOL> tag = '<STR_LIT>' <EOL> return tag % ( url , width , height ) <EOL> def _GetType ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def _GetFormatters ( self ) : <EOL> """<STR_LIT>""" <EOL> formatters = [ self . _GetLegendParams , <EOL> self . _GetDataSeriesParams , <EOL> self . _GetColors , <EOL> self . _GetAxisParams , <EOL> self . _GetGridParams , <EOL> self . _GetType , <EOL> self . _GetExtraParams , <EOL> self . _GetSizeParams , <EOL> ] <EOL> return formatters <EOL> def _Params ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> chart = chart . GetFormattedChart ( ) <EOL> params = { } <EOL> def Add ( new_params ) : <EOL> params . update ( util . ShortenParameterNames ( new_params ) ) <EOL> for formatter in self . formatters : <EOL> Add ( formatter ( chart ) ) <EOL> for key in params : <EOL> params [ key ] = str ( params [ key ] ) <EOL> return params <EOL> def _GetSizeParams ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT:size>' : '<STR_LIT>' % ( int ( self . _width ) , int ( self . _height ) ) } <EOL> def _GetExtraParams ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> return self . extra_params <EOL> def _GetDataSeriesParams ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> y_min , y_max = chart . GetDependentAxis ( ) . min , chart . GetDependentAxis ( ) . max <EOL> series_data = [ ] <EOL> markers = [ ] <EOL> for i , series in enumerate ( chart . data ) : <EOL> data = series . data <EOL> if not data : <EOL> continue <EOL> series_data . append ( data ) <EOL> for x , marker in series . markers : <EOL> args = [ marker . shape , marker . color , i , x , marker . size ] <EOL> markers . append ( '<STR_LIT:U+002C>' . join ( str ( arg ) for arg in args ) ) <EOL> encoder = self . _GetDataEncoder ( chart ) <EOL> result = util . EncodeData ( chart , series_data , y_min , y_max , encoder ) <EOL> result . update ( util . JoinLists ( marker = markers ) ) <EOL> return result <EOL> def _GetColors ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> colors = [ ] <EOL> for series in chart . data : <EOL> if not series . data : <EOL> continue <EOL> colors . append ( series . style . color ) <EOL> return util . JoinLists ( color = colors ) <EOL> def _GetDataEncoder ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> if not self . enhanced_encoding : <EOL> return util . SimpleDataEncoder ( ) <EOL> return util . EnhancedDataEncoder ( ) <EOL> def _GetLegendParams ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> if chart . _show_legend : <EOL> return util . JoinLists ( data_series_label = chart . _legend_labels ) <EOL> return { } <EOL> def _GetAxisLabelsAndPositions ( self , axis , chart ) : <EOL> """<STR_LIT>""" <EOL> return axis . labels , axis . label_positions <EOL> def _GetAxisParams ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> axis_types = [ ] <EOL> axis_ranges = [ ] <EOL> axis_labels = [ ] <EOL> axis_label_positions = [ ] <EOL> axis_label_gridlines = [ ] <EOL> mark_length = max ( self . _width , self . _height ) <EOL> for i , axis_pair in enumerate ( a for a in chart . _GetAxes ( ) if a [ <NUM_LIT:1> ] . labels ) : <EOL> axis_type_code , axis = axis_pair <EOL> axis_types . append ( axis_type_code ) <EOL> if axis . min is not None or axis . max is not None : <EOL> assert axis . min is not None <EOL> assert axis . max is not None <EOL> axis_ranges . append ( '<STR_LIT>' % ( i , axis . min , axis . max ) ) <EOL> labels , positions = self . _GetAxisLabelsAndPositions ( axis , chart ) <EOL> if labels : <EOL> axis_labels . append ( '<STR_LIT>' % i ) <EOL> axis_labels . extend ( labels ) <EOL> if positions : <EOL> positions = [ i ] + list ( positions ) <EOL> axis_label_positions . append ( '<STR_LIT:U+002C>' . join ( str ( x ) for x in positions ) ) <EOL> if axis . label_gridlines : <EOL> axis_label_gridlines . append ( "<STR_LIT>" % ( i , - mark_length ) ) <EOL> return util . JoinLists ( axis_type = axis_types , <EOL> axis_range = axis_ranges , <EOL> axis_label = axis_labels , <EOL> axis_position = axis_label_positions , <EOL> axis_tick_marks = axis_label_gridlines , <EOL> ) <EOL> def _GetGridParams ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> x = <NUM_LIT:0> <EOL> y = <NUM_LIT:0> <EOL> if chart . bottom . grid_spacing : <EOL> assert ( chart . bottom . min is not None ) <EOL> assert ( chart . bottom . max is not None ) <EOL> total = float ( chart . bottom . max - chart . bottom . min ) <EOL> x = <NUM_LIT:100> * chart . bottom . grid_spacing / total <EOL> if chart . left . grid_spacing : <EOL> assert ( chart . left . min is not None ) <EOL> assert ( chart . left . max is not None ) <EOL> total = float ( chart . left . max - chart . left . min ) <EOL> y = <NUM_LIT:100> * chart . left . grid_spacing / total <EOL> if x or y : <EOL> return dict ( grid = '<STR_LIT>' % ( x , y ) ) <EOL> return { } <EOL> class LineChartEncoder ( BaseChartEncoder ) : <EOL> """<STR_LIT>""" <EOL> def _GetType ( self , chart ) : <EOL> return { '<STR_LIT>' : '<STR_LIT>' } <EOL> def _GetLineStyles ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> styles = [ ] <EOL> for series in chart . data : <EOL> style = series . style <EOL> if style : <EOL> styles . append ( '<STR_LIT>' % ( style . width , style . on , style . off ) ) <EOL> else : <EOL> assert ( not styles ) <EOL> return util . JoinLists ( line_style = styles ) <EOL> def _GetFormatters ( self ) : <EOL> out = super ( LineChartEncoder , self ) . _GetFormatters ( ) <EOL> out . insert ( - <NUM_LIT:2> , self . _GetLineStyles ) <EOL> return out <EOL> class SparklineEncoder ( LineChartEncoder ) : <EOL> """<STR_LIT>""" <EOL> def _GetType ( self , chart ) : <EOL> return { '<STR_LIT>' : '<STR_LIT>' } <EOL> class BarChartEncoder ( BaseChartEncoder ) : <EOL> """<STR_LIT>""" <EOL> __STYLE_DEPRECATION = ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> def __init__ ( self , chart , style = None ) : <EOL> """<STR_LIT>""" <EOL> super ( BarChartEncoder , self ) . __init__ ( chart ) <EOL> if style is not None : <EOL> warnings . warn ( self . __STYLE_DEPRECATION , DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> chart . style = style <EOL> def _GetType ( self , chart ) : <EOL> types = { ( True , False ) : '<STR_LIT>' , <EOL> ( True , True ) : '<STR_LIT>' , <EOL> ( False , False ) : '<STR_LIT>' , <EOL> ( False , True ) : '<STR_LIT>' } <EOL> return { '<STR_LIT>' : types [ ( chart . vertical , chart . stacked ) ] } <EOL> def _GetAxisLabelsAndPositions ( self , axis , chart ) : <EOL> """<STR_LIT>""" <EOL> if not chart . vertical and axis == chart . left : <EOL> return reversed ( axis . labels ) , reversed ( axis . label_positions ) <EOL> return axis . labels , axis . label_positions <EOL> def _GetFormatters ( self ) : <EOL> out = super ( BarChartEncoder , self ) . _GetFormatters ( ) <EOL> out . insert ( - <NUM_LIT:2> , self . _ZeroPoint ) <EOL> out . insert ( - <NUM_LIT:2> , self . _ApplyBarChartStyle ) <EOL> return out <EOL> def _ZeroPoint ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> min , max = chart . GetDependentAxis ( ) . min , chart . GetDependentAxis ( ) . max <EOL> out = { } <EOL> if min < <NUM_LIT:0> : <EOL> if max < <NUM_LIT:0> : <EOL> out [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> else : <EOL> out [ '<STR_LIT>' ] = - min / float ( max - min ) <EOL> return out <EOL> def _ApplyBarChartStyle ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> if chart . style is None or not chart . data : <EOL> return { } <EOL> ( bar_thickness , bar_gap , group_gap ) = ( chart . style . bar_thickness , <EOL> chart . style . bar_gap , <EOL> chart . style . group_gap ) <EOL> if bar_gap is None and group_gap is not None : <EOL> bar_gap = max ( <NUM_LIT:0> , group_gap / <NUM_LIT:2> ) <EOL> if not chart . style . use_fractional_gap_spacing : <EOL> bar_gap = int ( bar_gap ) <EOL> if group_gap is None and bar_gap is not None : <EOL> group_gap = max ( <NUM_LIT:0> , bar_gap * <NUM_LIT:2> ) <EOL> if bar_thickness is None : <EOL> if chart . style . use_fractional_gap_spacing : <EOL> bar_thickness = '<STR_LIT:r>' <EOL> else : <EOL> bar_thickness = '<STR_LIT:a>' <EOL> else : <EOL> if chart . style . use_fractional_gap_spacing : <EOL> if bar_gap : <EOL> bar_gap = int ( bar_thickness * bar_gap ) <EOL> if group_gap : <EOL> group_gap = int ( bar_thickness * group_gap ) <EOL> spec = [ bar_thickness ] <EOL> if bar_gap is not None : <EOL> spec . append ( bar_gap ) <EOL> if group_gap is not None and not chart . stacked : <EOL> spec . append ( group_gap ) <EOL> return util . JoinLists ( bar_size = spec ) <EOL> def __GetStyle ( self ) : <EOL> warnings . warn ( self . __STYLE_DEPRECATION , DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> return self . chart . style <EOL> def __SetStyle ( self , value ) : <EOL> warnings . warn ( self . __STYLE_DEPRECATION , DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> self . chart . style = value <EOL> style = property ( __GetStyle , __SetStyle , __STYLE_DEPRECATION ) <EOL> class PieChartEncoder ( BaseChartEncoder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , chart , is3d = False , angle = None ) : <EOL> """<STR_LIT>""" <EOL> super ( PieChartEncoder , self ) . __init__ ( chart ) <EOL> self . is3d = is3d <EOL> self . angle = None <EOL> def _GetFormatters ( self ) : <EOL> """<STR_LIT>""" <EOL> formatters = super ( PieChartEncoder , self ) . _GetFormatters ( ) <EOL> formatters . append ( self . _GetAngleParams ) <EOL> return formatters <EOL> def _GetType ( self , chart ) : <EOL> if len ( chart . data ) > <NUM_LIT:1> : <EOL> if self . is3d : <EOL> warnings . warn ( <EOL> '<STR_LIT>' , <EOL> RuntimeWarning , stacklevel = <NUM_LIT:2> ) <EOL> chart_type = '<STR_LIT>' <EOL> else : <EOL> if self . is3d : <EOL> chart_type = '<STR_LIT>' <EOL> else : <EOL> chart_type = '<STR_LIT:p>' <EOL> return { '<STR_LIT>' : chart_type } <EOL> def _GetDataSeriesParams ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> pie_points = [ ] <EOL> labels = [ ] <EOL> max_val = <NUM_LIT:1> <EOL> for pie in chart . data : <EOL> points = [ ] <EOL> for segment in pie : <EOL> if segment : <EOL> points . append ( segment . size ) <EOL> max_val = max ( max_val , segment . size ) <EOL> labels . append ( segment . label or '<STR_LIT>' ) <EOL> if points : <EOL> pie_points . append ( points ) <EOL> encoder = self . _GetDataEncoder ( chart ) <EOL> result = util . EncodeData ( chart , pie_points , <NUM_LIT:0> , max_val , encoder ) <EOL> result . update ( util . JoinLists ( label = labels ) ) <EOL> return result <EOL> def _GetColors ( self , chart ) : <EOL> if chart . _colors : <EOL> colors = chart . _colors <EOL> else : <EOL> colors = [ ] <EOL> for pie in chart . data : <EOL> for segment in pie : <EOL> if segment and segment . color : <EOL> colors . append ( segment . color ) <EOL> return util . JoinLists ( color = colors ) <EOL> def _GetAngleParams ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> if self . angle : <EOL> return { '<STR_LIT>' : str ( self . angle ) } <EOL> return { } </s>
