<s> from collections import OrderedDict <EOL> try : <EOL> from cdecimal import Decimal <EOL> except ImportError : <EOL> from decimal import Decimal <EOL> import sys <EOL> from babel . numbers import format_decimal <EOL> import six <EOL> from agate . aggregations import Min , Max <EOL> from agate . data_types import Number <EOL> from agate . exceptions import DataTypeError <EOL> from agate import utils <EOL> def print_bars ( self , label_column_name = '<STR_LIT>' , value_column_name = '<STR_LIT>' , domain = None , width = <NUM_LIT> , output = sys . stdout , printable = False ) : <EOL> """<STR_LIT>""" <EOL> y_label = label_column_name <EOL> label_column = self . _columns [ label_column_name ] <EOL> x_label = value_column_name <EOL> value_column = self . _columns [ value_column_name ] <EOL> if not isinstance ( value_column . data_type , Number ) : <EOL> raise DataTypeError ( '<STR_LIT>' ) <EOL> output = output <EOL> width = width <EOL> decimal_places = utils . max_precision ( value_column ) <EOL> value_formatter = utils . make_number_formatter ( decimal_places ) <EOL> formatted_labels = [ ] <EOL> for label in label_column : <EOL> formatted_labels . append ( six . text_type ( label ) ) <EOL> formatted_values = [ ] <EOL> for value in value_column : <EOL> formatted_values . append ( format_decimal ( <EOL> value , <EOL> format = value_formatter , <EOL> locale = utils . LC_NUMERIC <EOL> ) ) <EOL> max_label_width = max ( max ( [ len ( l ) for l in formatted_labels ] ) , len ( y_label ) ) <EOL> max_value_width = max ( max ( [ len ( v ) for v in formatted_values ] ) , len ( x_label ) ) <EOL> plot_width = width - ( max_label_width + max_value_width + <NUM_LIT:2> ) <EOL> min_value = Min ( value_column_name ) . run ( self ) <EOL> max_value = Max ( value_column_name ) . run ( self ) <EOL> if domain : <EOL> x_min = Decimal ( domain [ <NUM_LIT:0> ] ) <EOL> x_max = Decimal ( domain [ <NUM_LIT:1> ] ) <EOL> if min_value < x_min or max_value > x_max : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> x_min , x_max = utils . round_limits ( min_value , max_value ) <EOL> if x_min >= <NUM_LIT:0> : <EOL> x_min = Decimal ( '<STR_LIT:0>' ) <EOL> plot_negative_width = <NUM_LIT:0> <EOL> zero_line = <NUM_LIT:0> <EOL> plot_positive_width = plot_width - <NUM_LIT:1> <EOL> elif x_max <= <NUM_LIT:0> : <EOL> x_max = Decimal ( '<STR_LIT:0>' ) <EOL> plot_negative_width = plot_width - <NUM_LIT:1> <EOL> zero_line = plot_width - <NUM_LIT:1> <EOL> plot_positive_width = <NUM_LIT:0> <EOL> else : <EOL> spread = x_max - x_min <EOL> negative_portion = ( x_min . copy_abs ( ) / spread ) <EOL> plot_negative_width = int ( ( ( plot_width - <NUM_LIT:1> ) * negative_portion ) . to_integral_value ( ) ) <EOL> zero_line = plot_negative_width <EOL> plot_positive_width = plot_width - ( plot_negative_width + <NUM_LIT:1> ) <EOL> def project ( value ) : <EOL> if value >= <NUM_LIT:0> : <EOL> return plot_negative_width + int ( ( plot_positive_width * ( value / x_max ) ) . to_integral_value ( ) ) <EOL> else : <EOL> return plot_negative_width - int ( ( plot_negative_width * ( value / x_min ) ) . to_integral_value ( ) ) <EOL> ticks = OrderedDict ( ) <EOL> ticks [ <NUM_LIT:0> ] = x_min <EOL> ticks [ plot_width - <NUM_LIT:1> ] = x_max <EOL> tick_fractions = [ Decimal ( '<STR_LIT>' ) , Decimal ( '<STR_LIT>' ) , Decimal ( '<STR_LIT>' ) ] <EOL> if x_min >= <NUM_LIT:0> : <EOL> for fraction in tick_fractions : <EOL> value = x_max * fraction <EOL> ticks [ project ( value ) ] = value <EOL> elif x_max <= <NUM_LIT:0> : <EOL> for fraction in tick_fractions : <EOL> value = x_min * fraction <EOL> ticks [ project ( value ) ] = value <EOL> else : <EOL> ticks [ zero_line ] = Decimal ( '<STR_LIT:0>' ) <EOL> value = x_min * Decimal ( '<STR_LIT>' ) <EOL> ticks [ project ( value ) ] = value <EOL> value = x_max * Decimal ( '<STR_LIT>' ) <EOL> ticks [ project ( value ) ] = value <EOL> decimal_places = utils . max_precision ( ticks . values ( ) ) <EOL> tick_formatter = utils . make_number_formatter ( decimal_places ) <EOL> ticks_formatted = OrderedDict ( ) <EOL> for k , v in ticks . items ( ) : <EOL> ticks_formatted [ k ] = format_decimal ( <EOL> v , <EOL> format = tick_formatter , <EOL> locale = utils . LC_NUMERIC <EOL> ) <EOL> def write ( line ) : <EOL> output . write ( line + '<STR_LIT:\n>' ) <EOL> top_line = u'<STR_LIT>' % ( y_label . ljust ( max_label_width ) , x_label . rjust ( max_value_width ) ) <EOL> write ( top_line ) <EOL> if printable : <EOL> bar_mark = utils . PRINTABLE_BAR_MARK <EOL> zero_mark = utils . PRINTABLE_ZERO_MARK <EOL> else : <EOL> bar_mark = utils . BAR_MARK <EOL> zero_mark = utils . ZERO_MARK <EOL> for i , label in enumerate ( formatted_labels ) : <EOL> value = value_column [ i ] <EOL> if value == <NUM_LIT:0> : <EOL> bar_width = <NUM_LIT:0> <EOL> elif value > <NUM_LIT:0> : <EOL> bar_width = project ( value ) - plot_negative_width <EOL> elif value < <NUM_LIT:0> : <EOL> bar_width = plot_negative_width - project ( value ) <EOL> label_text = label . ljust ( max_label_width ) <EOL> value_text = formatted_values [ i ] . rjust ( max_value_width ) <EOL> bar = bar_mark * bar_width <EOL> if value >= <NUM_LIT:0> : <EOL> gap = ( u'<STR_LIT:U+0020>' * plot_negative_width ) <EOL> if x_min <= <NUM_LIT:0> : <EOL> bar = gap + zero_mark + bar <EOL> else : <EOL> bar = bar + gap + zero_mark <EOL> else : <EOL> bar = u'<STR_LIT:U+0020>' * ( plot_negative_width - bar_width ) + bar <EOL> if x_max > value : <EOL> bar = bar + zero_mark <EOL> bar = bar . ljust ( plot_width ) <EOL> write ( '<STR_LIT>' % ( label_text , value_text , bar ) ) <EOL> axis = utils . HORIZONTAL_LINE * plot_width <EOL> tick_text = u'<STR_LIT:U+0020>' * width <EOL> for i , ( tick , label ) in enumerate ( ticks_formatted . items ( ) ) : <EOL> if tick == <NUM_LIT:0> : <EOL> offset = <NUM_LIT:0> <EOL> elif tick == plot_width - <NUM_LIT:1> : <EOL> offset = - ( len ( label ) - <NUM_LIT:1> ) <EOL> else : <EOL> offset = int ( - ( len ( label ) / <NUM_LIT:2> ) ) <EOL> pos = ( width - plot_width ) + tick + offset <EOL> if tick != <NUM_LIT:0> and tick != plot_width - <NUM_LIT:1> : <EOL> if tick_text [ pos - <NUM_LIT:1> : pos + len ( label ) + <NUM_LIT:1> ] != '<STR_LIT:U+0020>' * ( len ( label ) + <NUM_LIT:2> ) : <EOL> continue <EOL> tick_text = tick_text [ : pos ] + label + tick_text [ pos + len ( label ) : ] <EOL> axis = axis [ : tick ] + utils . TICK_MARK + axis [ tick + <NUM_LIT:1> : ] <EOL> write ( axis . rjust ( width ) ) <EOL> write ( tick_text ) </s>
