#NEWLINE# def normalize_series_by_unit( series ) : #NEWLINE# #INDENT# "Transform series' values into a more human readable form:\n    1) Determine the data point with the maximum value\n    2) Decide the unit appropriate for this value (normalize it)\n    3) Convert other values to this new unit, if necessary\n    " #NEWLINE# if ( not series ) : #NEWLINE# #INDENT# return series #NEWLINE# #UNINDENT# source_unit = target_unit = series [ 0 ] [ 'unit' ] #NEWLINE# if ( not units . is_supported ( source_unit ) ) : #NEWLINE# #INDENT# return series #NEWLINE# #UNINDENT# maximum = max ( [ d [ 'y' ] for point in series for d in point [ 'data' ] ] ) #NEWLINE# unit = units . normalize ( maximum , source_unit ) [ 1 ] #NEWLINE# if units . is_larger ( unit , target_unit ) : #NEWLINE# #INDENT# target_unit = unit #NEWLINE# for ( i , point ) in enumerate ( series [ : ] ) : #NEWLINE# #INDENT# if ( point [ 'unit' ] != target_unit ) : #NEWLINE# #INDENT# series [ i ] [ 'unit' ] = target_unit #NEWLINE# for ( j , d ) in enumerate ( point [ 'data' ] [ : ] ) : #NEWLINE# #INDENT# series [ i ] [ 'data' ] [ j ] [ 'y' ] = units . convert ( d [ 'y' ] , source_unit , target_unit , fmt = True ) [ 0 ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return series